Skip to content

Commit

Permalink
feat(cb2-10665): add unit tests to check email is correct in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
LGin-BJSS committed Mar 20, 2024
1 parent 9351f62 commit d37c352
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/unit/services/tokens.unitTest.ts
Original file line number Diff line number Diff line change
@@ -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) };
Expand Down Expand Up @@ -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");
});
});
});

0 comments on commit d37c352

Please sign in to comment.