-
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.
chore: adding tests + coverage for common (#83)
- Loading branch information
Showing
5 changed files
with
119 additions
and
5 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
50 changes: 50 additions & 0 deletions
50
packages/auth-common/src/components/__tests__/getToken.test.ts
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,50 @@ | ||
import { type HeadersLike, getToken } from ".."; | ||
|
||
describe("getToken", () => { | ||
const clientId = "testClient"; | ||
|
||
it("should return empty string if no token is found", () => { | ||
const headers: HeadersLike = {}; | ||
expect(getToken(headers, clientId)).toBe(""); | ||
}); | ||
|
||
it("should extract token from authorization header", () => { | ||
const token = "someToken"; | ||
const headers: HeadersLike = { | ||
authorization: `Bearer ${token}`, | ||
}; | ||
expect(getToken(headers, clientId)).toBe(token); | ||
}); | ||
|
||
it("should extract token from cookie", () => { | ||
const token = "anotherToken"; | ||
const headers: HeadersLike = { | ||
cookie: `auth.${clientId}=${token};`, | ||
}; | ||
expect(getToken(headers, clientId)).toBe(token); | ||
}); | ||
|
||
it("should prioritize token from cookie over authorization header", () => { | ||
const headerToken = "headerToken"; | ||
const cookieToken = "cookieToken"; | ||
const headers: HeadersLike = { | ||
authorization: `Bearer ${headerToken}`, | ||
cookie: `auth.${clientId}=${cookieToken};`, | ||
}; | ||
expect(getToken(headers, clientId)).toBe(cookieToken); | ||
}); | ||
|
||
it("should return empty string if authorization header is not in correct format", () => { | ||
const headers: HeadersLike = { | ||
authorization: "InvalidHeader", | ||
}; | ||
expect(getToken(headers, clientId)).toBe(""); | ||
}); | ||
|
||
it("should return empty string if cookie does not contain the token", () => { | ||
const headers: HeadersLike = { | ||
cookie: "someOtherCookie=value;", | ||
}; | ||
expect(getToken(headers, clientId)).toBe(""); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/auth-common/src/components/__tests__/verifyToken.test.ts
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,63 @@ | ||
import { decodeJwt, importSPKI, jwtVerify } from "jose"; | ||
import { decodeToken, verifyAndExtractToken } from ".."; | ||
import { JWT, JWT_PUBLIC_KEY } from "../constants"; | ||
vi.mock("jose"); | ||
|
||
describe("verifyAndExtractToken", () => { | ||
const token = "testToken"; | ||
|
||
it("should verify and extract token successfully", async () => { | ||
const mockPublicKey = {}; | ||
const mockJwtVerifyResult = { | ||
payload: "testPayload", | ||
protectedHeader: "testHeader", | ||
}; | ||
// @ts-expect-error | ||
(importSPKI as vi.Mock).mockResolvedValue(mockPublicKey); | ||
// @ts-expect-error | ||
(jwtVerify as vi.Mock).mockResolvedValue(mockJwtVerifyResult); | ||
|
||
const result = await verifyAndExtractToken(token); | ||
|
||
expect(importSPKI).toHaveBeenCalledWith(JWT_PUBLIC_KEY, JWT.ALG); | ||
expect(jwtVerify).toHaveBeenCalledWith(token, mockPublicKey, { | ||
issuer: JWT.ISSUER, | ||
}); | ||
expect(result).toEqual(mockJwtVerifyResult); | ||
}); | ||
|
||
it("should return undefined on error", async () => { | ||
// @ts-expect-error | ||
(importSPKI as vi.Mock).mockRejectedValue(new Error("test error")); | ||
|
||
const result = await verifyAndExtractToken(token); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("decodeToken", () => { | ||
const token = "testToken"; | ||
|
||
it("should decode token successfully", () => { | ||
const mockDecodeResult = { payload: "testPayload" }; | ||
// @ts-expect-error | ||
(decodeJwt as vi.Mock).mockReturnValue(mockDecodeResult); | ||
|
||
const result = decodeToken(token); | ||
|
||
expect(decodeJwt).toHaveBeenCalledWith(token); | ||
expect(result).toEqual(mockDecodeResult); | ||
}); | ||
|
||
it("should return undefined on error", () => { | ||
// @ts-expect-error | ||
(decodeJwt as vi.Mock).mockImplementation(() => { | ||
throw new Error("test error"); | ||
}); | ||
|
||
const result = decodeToken(token); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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
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