|
| 1 | +import { retrieveApimCredentials } from "@src/utils/auth/apim/get-apim-access-token"; |
| 2 | +import { _getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials"; |
| 3 | +import { AppConfig } from "@src/utils/config"; |
| 4 | +import { appConfigBuilder } from "@test-data/config/builders"; |
| 5 | +import { JWT } from "next-auth/jwt"; |
| 6 | + |
| 7 | +jest.mock("@src/utils/auth/apim/get-apim-access-token", () => ({ |
| 8 | + retrieveApimCredentials: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock("sanitize-data", () => ({ sanitize: jest.fn() })); |
| 12 | + |
| 13 | +describe("getOrRefreshApimCredentials", () => { |
| 14 | + describe("when AUTH APIM is available", () => { |
| 15 | + const oldNEXT_RUNTIME = process.env.NEXT_RUNTIME; |
| 16 | + |
| 17 | + const mockConfig: AppConfig = appConfigBuilder().andIS_APIM_AUTH_ENABLED(true).build(); |
| 18 | + |
| 19 | + const nowInSeconds = 1749052001; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + jest.useFakeTimers().setSystemTime(nowInSeconds * 1000); |
| 24 | + process.env.NEXT_RUNTIME = "nodejs"; |
| 25 | + }); |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + (retrieveApimCredentials as jest.Mock).mockResolvedValue({ |
| 29 | + accessToken: "new-apim-access-token", |
| 30 | + expiresAt: nowInSeconds + 1111, |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + jest.resetAllMocks(); |
| 36 | + process.env.NEXT_RUNTIME = oldNEXT_RUNTIME; |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll(() => { |
| 40 | + jest.useRealTimers(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should return undefined and logs error if token does not contain id_token", async () => { |
| 44 | + const token = { apim: {}, nhs_login: {} } as JWT; |
| 45 | + |
| 46 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 47 | + |
| 48 | + expect(result).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should return new APIM creds if stored creds are empty", async () => { |
| 52 | + const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT; |
| 53 | + |
| 54 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 55 | + |
| 56 | + expect(result).toMatchObject({ |
| 57 | + accessToken: "new-apim-access-token", |
| 58 | + expiresAt: nowInSeconds + 1111, |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should return stored APIM creds if fresh", async () => { |
| 63 | + const token = { |
| 64 | + apim: { |
| 65 | + access_token: "old-access-token", |
| 66 | + expires_at: nowInSeconds + 60, |
| 67 | + }, |
| 68 | + nhs_login: { id_token: "old-id-token" }, |
| 69 | + } as JWT; |
| 70 | + |
| 71 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 72 | + |
| 73 | + expect(result).toEqual({ |
| 74 | + accessToken: "old-access-token", |
| 75 | + expiresAt: nowInSeconds + 60, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should return new APIM creds if expired", async () => { |
| 80 | + const token = { |
| 81 | + apim: { access_token: "old-access-token", expires_at: nowInSeconds - 60 }, |
| 82 | + nhs_login: { id_token: "id-token" }, |
| 83 | + } as JWT; |
| 84 | + |
| 85 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 86 | + |
| 87 | + expect(result).toEqual({ |
| 88 | + accessToken: "new-apim-access-token", |
| 89 | + expiresAt: nowInSeconds + 1111, |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("when invoked from Edge runtime", () => { |
| 94 | + it("should return stored APIM creds without checking expiry", async () => { |
| 95 | + process.env.NEXT_RUNTIME = "edge"; |
| 96 | + const token = { |
| 97 | + apim: { access_token: "stored-access-token", expires_at: 88 }, |
| 98 | + nhs_login: { id_token: "id-token" }, |
| 99 | + } as JWT; |
| 100 | + |
| 101 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 102 | + |
| 103 | + expect(result).toEqual({ |
| 104 | + accessToken: "stored-access-token", |
| 105 | + expiresAt: 88, |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should return undefined if APIM creds empty", async () => { |
| 110 | + process.env.NEXT_RUNTIME = "edge"; |
| 111 | + const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT; |
| 112 | + |
| 113 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 114 | + expect(result).toBeUndefined(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("when AUTH APIM is not available", () => { |
| 120 | + const mockConfig: AppConfig = appConfigBuilder().andIS_APIM_AUTH_ENABLED(false).build(); |
| 121 | + |
| 122 | + const nowInSeconds = 1749052001; |
| 123 | + |
| 124 | + beforeEach(() => { |
| 125 | + jest.clearAllMocks(); |
| 126 | + jest.useFakeTimers().setSystemTime(nowInSeconds * 1000); |
| 127 | + }); |
| 128 | + |
| 129 | + afterEach(() => { |
| 130 | + jest.resetAllMocks(); |
| 131 | + }); |
| 132 | + |
| 133 | + afterAll(() => { |
| 134 | + jest.useRealTimers(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should return undefined if APIM auth is not enabled", async () => { |
| 138 | + const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT; |
| 139 | + |
| 140 | + const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds); |
| 141 | + |
| 142 | + expect(result).toBeUndefined(); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments