From d37c352a2dcbf53b9648c3dfbcd7f13523adcb86 Mon Sep 17 00:00:00 2001 From: LGin-BJSS Date: Wed, 20 Mar 2024 12:23:02 +0000 Subject: [PATCH] feat(cb2-10665): add unit tests to check email is correct in logs --- tests/unit/services/tokens.unitTest.ts | 62 ++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/unit/services/tokens.unitTest.ts b/tests/unit/services/tokens.unitTest.ts index 34d5233..bfd6bff 100644 --- a/tests/unit/services/tokens.unitTest.ts +++ b/tests/unit/services/tokens.unitTest.ts @@ -1,5 +1,6 @@ import { getValidJwt } from "../../../src/services/tokens"; import { JWT_MESSAGE } from "../../../src/models/enums"; +import { ILogEvent } from "../../../src/models/ILogEvent"; jest.mock("../../../src/services/signature-check", () => { return { checkSignature: jest.fn().mockResolvedValue(true) }; @@ -76,4 +77,65 @@ describe("getValidJwt()", () => { }, }); }); + + context("when preferred_username is present in the token", () => { + it("should set the email in the log event to preferred_username", async () => { + const jwt = require('jsonwebtoken'); + const token = jwt.sign({ + "sub": "1234567890", + "name": "John Doe", + "iat": 1516239022, + "tid": "123456", + "exp": 631005334, + "preferred_username": "test_username" + }, "testSignature"); + + const logEvent: ILogEvent = {} + + await getValidJwt(`Bearer ${token}`, logEvent, DEFAULT_TENANT_ID, DEFAULT_CLIENT_ID); + + expect(logEvent.email).toBe("test_username"); + }); + }); + + context("when preferred_username and unique_name are present in the token", () => { + it("should set the email in the log event to preferred_username", async () => { + const jwt = require('jsonwebtoken'); + const token = jwt.sign({ + "sub": "1234567890", + "name": "John Doe", + "iat": 1516239022, + "tid": "123456", + "exp": 631005334, + "preferred_username": "test_username", + "unique_name": "test_unique_name" + }, "testSignature"); + + const logEvent: ILogEvent = {} + + await getValidJwt(`Bearer ${token}`, logEvent, DEFAULT_TENANT_ID, DEFAULT_CLIENT_ID); + + expect(logEvent.email).toBe("test_username"); + }); + }); + + context("when unique_name is present in the token without preferred_username", () => { + it("should set the email in the log event to unique_name", async () => { + const jwt = require('jsonwebtoken'); + const token = jwt.sign({ + "sub": "1234567890", + "name": "John Doe", + "iat": 1516239022, + "tid": "123456", + "exp": 631005334, + "unique_name": "test_unique_name" + }, "testSignature"); + + const logEvent: ILogEvent = {} + + await getValidJwt(`Bearer ${token}`, logEvent, DEFAULT_TENANT_ID, DEFAULT_CLIENT_ID); + + expect(logEvent.email).toBe("test_unique_name"); + }); + }); });