From 3f118aacebee22d0d484c86cb5573dfcbb0a4365 Mon Sep 17 00:00:00 2001 From: Kan-A-Pesh Date: Tue, 5 Nov 2024 12:31:54 +0100 Subject: [PATCH] fix: issues with JWT token on tests --- tests/e2e/auth.test.ts | 10 +++++++--- tests/e2e/granters.test.ts | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/e2e/auth.test.ts b/tests/e2e/auth.test.ts index 5a94557..5f50b9b 100644 --- a/tests/e2e/auth.test.ts +++ b/tests/e2e/auth.test.ts @@ -8,11 +8,11 @@ const app = createApp("e2e-auth"); describe("Test authentication", () => { const email = "test-auth@no-reply.local"; const token = AuthController.generateCreationToken(email, false); - const redirectUrl = globals.env.MAIL_REDIRECT_URL.replace("{token}", token); test("should send an email", async () => { // We use X-ADMIN-KEY to fetch back the redirect URL without expiration const res = await post(app, "/auth/send-mail", { email: email }, { "X-ADMIN-KEY": globals.env.ADMIN_TOKEN }); + const rawRedirectUrl = globals.env.MAIL_REDIRECT_URL.replace("{token}", ""); expect(res.body).toStrictEqual({ masterStatus: 201, @@ -21,10 +21,12 @@ describe("Test authentication", () => { { status: 201, success: true, - data: redirectUrl + data: expect.stringContaining(rawRedirectUrl) } ] }); + + expect(AuthController.validateCreationToken(res.body.response[0].data.replace(rawRedirectUrl, ""))).toBe(email); }); test("should get 'too many requests' error", async () => { @@ -91,10 +93,12 @@ describe("Test authentication", () => { { status: 200, success: true, - data: expect.stringContaining(authToken.substring(0, authToken.indexOf("."))) // Remove the signature + data: expect.any(String) } ] }); + + expect(AuthController.validateAuthToken(res.body.response[0].data)).toBe(email); }); test("should get 'invalid credentials' error", async () => { diff --git a/tests/e2e/granters.test.ts b/tests/e2e/granters.test.ts index 2caa7bc..d39979b 100644 --- a/tests/e2e/granters.test.ts +++ b/tests/e2e/granters.test.ts @@ -143,12 +143,16 @@ describe("Test granters", () => { status: 200, success: true, data: { - token: testGlobals.granterAuthToken, + token: expect.any(String), clubId: testGlobals.clubId } } ] }); + + expect(AuthController.validateGranterAuthToken(res.body.response[0].data.token)).toBe( + "test-granters@no-reply.local" + ); }); test("should authenticate", async () => {