forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for middleware/isAuth.ts (PalisadoesFoundation#836)
* 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
1 parent
4943097
commit 371af74
Showing
1 changed file
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |