generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from Real-Dev-Squad/develop
Dev to main sync
- Loading branch information
Showing
4 changed files
with
67 additions
and
6 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
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
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,47 @@ | ||
import jwt from "@tsndr/cloudflare-worker-jwt"; | ||
import { verifyAuthToken } from "../../../src/utils/verifyAuthToken"; | ||
import { | ||
AUTHENTICATION_ERROR, | ||
INVALID_TOKEN_FORMAT, | ||
} from "../../../src/constants/responses"; | ||
|
||
describe("verifyAuthToken", () => { | ||
const authToken = "validToken"; | ||
const mockEnv = { RDS_SERVERLESS_PUBLIC_KEY: "publicKey" }; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should verify a valid token successfully", async () => { | ||
jwt.verify = jest.fn().mockResolvedValue(true); | ||
const authHeader = `Bearer ${authToken}`; | ||
await expect(verifyAuthToken(authHeader, mockEnv)).resolves.not.toThrow(); | ||
expect(jwt.verify).toHaveBeenCalledWith( | ||
authToken, | ||
mockEnv.RDS_SERVERLESS_PUBLIC_KEY, | ||
{ algorithm: "RS256" } | ||
); | ||
}); | ||
|
||
it("should throw an error for an invalid token", async () => { | ||
const authHeader = "Bearer invalidToken"; | ||
jwt.verify = jest.fn().mockResolvedValue(false); | ||
await expect(verifyAuthToken(authHeader, mockEnv)).rejects.toThrow( | ||
AUTHENTICATION_ERROR | ||
); | ||
}); | ||
it("should throw an error when Bearer is not passed", async () => { | ||
const authHeader = "Beaer invalidToken"; | ||
await expect(verifyAuthToken(authHeader, mockEnv)).rejects.toThrow( | ||
INVALID_TOKEN_FORMAT | ||
); | ||
}); | ||
|
||
it("should throw an error for a malformed auth header", async () => { | ||
const malformedHeader = "invalidformat"; | ||
await expect(verifyAuthToken(malformedHeader, mockEnv)).rejects.toThrow( | ||
INVALID_TOKEN_FORMAT | ||
); | ||
}); | ||
}); |