|
1 | 1 | import { VaccineType } from "@src/models/vaccine"; |
2 | 2 | import { getSSOUrlToNBSForVaccine } from "@src/services/nbs/nbs-service"; |
3 | 3 | import { generateAssertedLoginIdentityJwt } from "@src/utils/auth/generate-auth-payload"; |
4 | | -import { AppConfig, configProvider } from "@src/utils/config"; |
| 4 | +import lazyConfig from "@src/utils/lazy-config"; |
| 5 | +import { AsyncConfigMock, lazyConfigBuilder } from "@test-data/config/builders"; |
5 | 6 |
|
6 | | -jest.mock("@src/utils/config"); |
7 | 7 | jest.mock("@src/utils/auth/generate-auth-payload", () => ({ |
8 | 8 | generateAssertedLoginIdentityJwt: jest.fn(), |
9 | 9 | })); |
10 | 10 | jest.mock("sanitize-data", () => ({ sanitize: jest.fn() })); |
11 | 11 |
|
12 | | -const nbsUrlFromConfig = "https://test-nbs-url"; |
| 12 | +const nbsUrlFromConfig = new URL("https://test-nbs-url"); |
13 | 13 | const nbsBookingPathFromConfig = "/test/path/book"; |
14 | 14 |
|
15 | 15 | const mockAssertedLoginIdentityJWT = "mock-jwt"; |
16 | 16 |
|
17 | 17 | describe("getSSOUrlToNBSForVaccine", () => { |
18 | | - beforeEach(() => { |
19 | | - (configProvider as jest.Mock).mockImplementation( |
20 | | - (): Partial<AppConfig> => ({ |
21 | | - NBS_URL: nbsUrlFromConfig, |
22 | | - NBS_BOOKING_PATH: nbsBookingPathFromConfig, |
23 | | - }), |
24 | | - ); |
| 18 | + const mockedConfig = lazyConfig as AsyncConfigMock; |
25 | 19 |
|
26 | | - (generateAssertedLoginIdentityJwt as jest.Mock).mockReturnValue(mockAssertedLoginIdentityJWT); |
27 | | - }); |
| 20 | + describe("when NBS config is valid", () => { |
| 21 | + beforeEach(() => { |
| 22 | + const defaultConfig = lazyConfigBuilder() |
| 23 | + .withNbsUrl(nbsUrlFromConfig) |
| 24 | + .andNbsBookingPath(nbsBookingPathFromConfig) |
| 25 | + .build(); |
| 26 | + Object.assign(mockedConfig, defaultConfig); |
28 | 27 |
|
29 | | - it("returns sso url of NBS configured in config for RSV vaccine", async () => { |
30 | | - const nbsRedirectUrl = new URL(await getSSOUrlToNBSForVaccine(VaccineType.RSV)); |
31 | | - expect(nbsRedirectUrl.origin).toEqual(nbsUrlFromConfig); |
32 | | - expect(nbsRedirectUrl.pathname).toEqual(`${nbsBookingPathFromConfig}/rsv`); |
33 | | - }); |
| 28 | + (generateAssertedLoginIdentityJwt as jest.Mock).mockReturnValue(mockAssertedLoginIdentityJWT); |
| 29 | + }); |
34 | 30 |
|
35 | | - it("should include campaignID query param in NBS URL", async () => { |
36 | | - const nbsRedirectUrl = new URL(await getSSOUrlToNBSForVaccine(VaccineType.RSV)); |
37 | | - expect(nbsRedirectUrl.searchParams.get("wt.mc_id")).toEqual(expect.any(String)); |
38 | | - }); |
| 31 | + it("returns sso url of NBS configured in config for RSV vaccine", async () => { |
| 32 | + const nbsRedirectUrl = new URL(await getSSOUrlToNBSForVaccine(VaccineType.RSV)); |
| 33 | + expect(nbsRedirectUrl.origin).toEqual(nbsUrlFromConfig.origin); |
| 34 | + expect(nbsRedirectUrl.pathname).toEqual(`${nbsBookingPathFromConfig}/rsv`); |
| 35 | + }); |
39 | 36 |
|
40 | | - it("should include assertedLoginIdentity query param in NBS URL", async () => { |
41 | | - const nbsRedirectUrl = new URL(await getSSOUrlToNBSForVaccine(VaccineType.RSV)); |
42 | | - expect(nbsRedirectUrl.searchParams.get("assertedLoginIdentity")).toEqual(mockAssertedLoginIdentityJWT); |
43 | | - }); |
| 37 | + it("should include campaignID query param in NBS URL", async () => { |
| 38 | + const nbsRedirectUrl = new URL(await getSSOUrlToNBSForVaccine(VaccineType.RSV)); |
| 39 | + expect(nbsRedirectUrl.searchParams.get("wt.mc_id")).toEqual(expect.any(String)); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should include assertedLoginIdentity query param in NBS URL", async () => { |
| 43 | + const nbsRedirectUrl = new URL(await getSSOUrlToNBSForVaccine(VaccineType.RSV)); |
| 44 | + expect(nbsRedirectUrl.searchParams.get("assertedLoginIdentity")).toEqual(mockAssertedLoginIdentityJWT); |
| 45 | + }); |
44 | 46 |
|
45 | | - it("should redirect to SSO failure page if error occurs generating assertedLoginIdentity", async () => { |
46 | | - (generateAssertedLoginIdentityJwt as jest.Mock).mockRejectedValue( |
47 | | - new Error("Error creating SSO assertedLoginIdentity: id_token.jti attribute missing from session"), |
48 | | - ); |
| 47 | + it("should redirect to SSO failure page if error occurs generating assertedLoginIdentity", async () => { |
| 48 | + (generateAssertedLoginIdentityJwt as jest.Mock).mockRejectedValue( |
| 49 | + new Error("Error creating SSO assertedLoginIdentity: id_token.jti attribute missing from session"), |
| 50 | + ); |
49 | 51 |
|
50 | | - const nbsRedirectUrl = await getSSOUrlToNBSForVaccine(VaccineType.RSV); |
51 | | - expect(nbsRedirectUrl).toBe("/sso-failure"); |
| 52 | + const nbsRedirectUrl = await getSSOUrlToNBSForVaccine(VaccineType.RSV); |
| 53 | + expect(nbsRedirectUrl).toBe("/sso-failure"); |
| 54 | + }); |
52 | 55 | }); |
53 | 56 |
|
54 | | - it("should redirect to SSO failure page if NBS Url config invalid", async () => { |
55 | | - (configProvider as jest.Mock).mockImplementation( |
56 | | - (): Partial<AppConfig> => ({ |
57 | | - NBS_URL: "not-a-valid-url", |
58 | | - NBS_BOOKING_PATH: nbsBookingPathFromConfig, |
59 | | - }), |
60 | | - ); |
| 57 | + describe("Without valid config", () => { |
| 58 | + it("should redirect to SSO failure page if NBS Url config invalid", async () => { |
| 59 | + const defaultConfig = {}; |
| 60 | + Object.assign(mockedConfig, defaultConfig); |
61 | 61 |
|
62 | | - const nbsRedirectUrl = await getSSOUrlToNBSForVaccine(VaccineType.RSV); |
63 | | - expect(nbsRedirectUrl).toBe("/sso-failure"); |
| 62 | + const nbsRedirectUrl = await getSSOUrlToNBSForVaccine(VaccineType.RSV); |
| 63 | + expect(nbsRedirectUrl).toBe("/sso-failure"); |
| 64 | + }); |
64 | 65 | }); |
65 | 66 | }); |
0 commit comments