Skip to content

Commit

Permalink
Add tests for middleware/isAuth.ts (PalisadoesFoundation#836)
Browse files Browse the repository at this point in the history
* adds tests for middleware/isAuth.ts

* add afterEach in isAuth.spec.ts

* increases code coverage for isAuth.ts

* replace resetAllMocks to restoreAllMocks

* replace hard-coded value with autogenerated value
  • Loading branch information
beingnoble03 authored Jan 2, 2023
1 parent 4943097 commit 371af74
Showing 1 changed file with 197 additions and 0 deletions.
197 changes: 197 additions & 0 deletions __tests__/middleware/isAuth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { Request } from "express";
import { isAuth } from "../../src/lib/middleware/isAuth";
import { beforeEach, afterEach, describe, expect, it, vi } from "vitest";
import jwt from "jsonwebtoken";
import { logger } from "../../src/lib/libraries/logger";

interface Test_Interface_AuthData {
isAuth: boolean;
expired: boolean | undefined;
userId: string | undefined;
}

let testAuthData: Test_Interface_AuthData;

describe("middleware -> isAuth", () => {
beforeEach(() => {
testAuthData = {
isAuth: false,
expired: undefined,
userId: undefined,
};
});

afterEach(() => {
vi.restoreAllMocks();
});

it("returns authData if headers.authorisation === undefined", () => {
const mockRequest = {
headers: {},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

expect(authData).toEqual(testAuthData);
});

it("returns authData if token === undefined", () => {
const testAuthorizationHeader = (Math.random() + 1).toString(36).substring(2, 5);

const mockRequest = {
headers: {
authorization: testAuthorizationHeader,
},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

expect(authData).toEqual(testAuthData);
});

it("returns authData if token === ''", async () => {
const testAuthorizationHeader = (Math.random() + 1)
.toString(36)
.substring(2, 5)
.concat(" ");

const mockRequest = {
headers: {
authorization: testAuthorizationHeader,
},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

expect(authData).toEqual(testAuthData);
});

it("returns authData if token is expired", () => {
const verifyMocked = vi
.spyOn(jwt, "verify")
.mockImplementationOnce((...args: any) => {
const err = {
name: "TokenExpiredError",
};

const callBackFn = args[2];
return callBackFn(err, {});
});

const testToken = (Math.random() + 1).toString(36).substring(2, 5);
const testAuthorizationHeader = (Math.random() + 1)
.toString(36)
.substring(2, 5)
.concat(" ", testToken);

const mockRequest = {
headers: {
authorization: testAuthorizationHeader,
},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

testAuthData.expired = true;

expect(verifyMocked).toHaveBeenCalledWith(
testToken,
process.env.ACCESS_TOKEN_SECRET as string,
expect.anything()
);
expect(authData).toEqual(testAuthData);
});

it("returns authData if decoded token is not set", () => {
const verifyMocked = vi
.spyOn(jwt, "verify")
.mockImplementationOnce((..._args: any) => {
return "";
});

const infoSpy = vi.spyOn(logger, "info");

const testToken = (Math.random() + 1).toString(36).substring(2, 5);
const testAuthorizationHeader = (Math.random() + 1)
.toString(36)
.substring(2, 5)
.concat(" ", testToken);

const mockRequest = {
headers: {
authorization: testAuthorizationHeader,
},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

expect(verifyMocked).toHaveBeenCalledWith(
testToken,
process.env.ACCESS_TOKEN_SECRET as string,
expect.anything()
);
expect(infoSpy).toBeCalledWith("decoded token is not present");
expect(authData).toEqual(testAuthData);
});

it("returns authData if jwt.verify throws error", () => {
vi.spyOn(jwt, "verify").mockImplementationOnce((..._args: any) => {
throw new Error();
});

const testToken = (Math.random() + 1).toString(36).substring(2, 5);
const testAuthorizationHeader = (Math.random() + 1)
.toString(36)
.substring(2, 5)
.concat(" ", testToken);

const mockRequest = {
headers: {
authorization: testAuthorizationHeader,
},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

testAuthData.expired = true;

expect(authData).toEqual(testAuthData);
});

it("returns authData if token is valid", () => {
const verifyMocked = vi
.spyOn(jwt, "verify")
.mockImplementationOnce((...args: any) => {
const decoded = {
userId: "ValidUserId",
};

const callBackFn = args[2];
return callBackFn(null, decoded);
});

const testToken = (Math.random() + 1).toString(36).substring(2, 5);
const testAuthorizationHeader = (Math.random() + 1)
.toString(36)
.substring(2, 5)
.concat(" ", testToken);

const mockRequest = {
headers: {
authorization: testAuthorizationHeader,
},
} as Request;

const authData: Test_Interface_AuthData = isAuth(mockRequest);

testAuthData.isAuth = true;
testAuthData.userId = "ValidUserId";

expect(verifyMocked).toHaveBeenCalledWith(
testToken,
process.env.ACCESS_TOKEN_SECRET as string,
expect.anything()
);
expect(authData).toEqual(testAuthData);
});
});

0 comments on commit 371af74

Please sign in to comment.