Skip to content

Commit 8a54dd2

Browse files
SPIKE SB Continue replacing usage of eager config object.
1 parent e5de798 commit 8a54dd2

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

src/app/api/auth/[...nextauth]/provider.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import NHSLoginAuthProvider from "@src/app/api/auth/[...nextauth]/provider";
2-
import { configProvider } from "@src/utils/config";
2+
import lazyConfig from "@src/utils/lazy-config";
3+
import { AsyncConfigMock, lazyConfigBuilder } from "@test-data/config/builders";
34

45
jest.mock("@src/utils/config");
56
jest.mock("@src/utils/auth/pem-to-crypto-key");
67
jest.mock("sanitize-data", () => ({ sanitize: jest.fn() }));
78

89
describe("provider", () => {
9-
(configProvider as jest.Mock).mockImplementation(() => ({
10-
NHS_LOGIN_URL: "",
11-
NHS_LOGIN_CLIENT_ID: "",
12-
NHS_LOGIN_SCOPE: "",
13-
NHS_LOGIN_PRIVATE_KEY: "",
14-
}));
10+
const mockedConfig = lazyConfig as AsyncConfigMock;
11+
12+
beforeEach(() => {
13+
const defaultConfig = lazyConfigBuilder().build();
14+
Object.assign(mockedConfig, defaultConfig);
15+
});
1516

1617
it("should be configured correctly", async () => {
1718
const provider = await NHSLoginAuthProvider();

src/app/api/auth/[...nextauth]/provider.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import { OIDCConfig } from "@auth/core/providers";
22
import pemToCryptoKey from "@src/utils/auth/pem-to-crypto-key";
3-
import { AppConfig, configProvider } from "@src/utils/config";
3+
import lazyConfig from "@src/utils/lazy-config";
44
import { Profile } from "next-auth";
55

66
export const NHS_LOGIN_PROVIDER_ID = "nhs-login";
77

88
const NHSLoginAuthProvider = async (): Promise<OIDCConfig<Profile>> => {
9-
const config: AppConfig = await configProvider();
10-
119
return {
1210
id: NHS_LOGIN_PROVIDER_ID,
1311
name: "NHS Login Auth Provider",
1412
type: "oidc",
15-
issuer: config.NHS_LOGIN_URL,
16-
clientId: config.NHS_LOGIN_CLIENT_ID,
17-
wellKnown: `${config.NHS_LOGIN_URL}/.well-known/openid-configuration`,
13+
issuer: (await lazyConfig.NHS_LOGIN_URL) as string,
14+
clientId: (await lazyConfig.NHS_LOGIN_CLIENT_ID) as string,
15+
wellKnown: `${await lazyConfig.NHS_LOGIN_URL}/.well-known/openid-configuration`,
1816
authorization: {
1917
params: {
20-
scope: `${config.NHS_LOGIN_SCOPE}`,
18+
scope: `${await lazyConfig.NHS_LOGIN_SCOPE}`,
2119
prompt: "none",
2220
},
2321
},
2422
token: {
25-
clientPrivateKey: await pemToCryptoKey(config.NHS_LOGIN_PRIVATE_KEY),
23+
clientPrivateKey: await pemToCryptoKey((await lazyConfig.NHS_LOGIN_PRIVATE_KEY) as string),
2624
},
2725
client: {
2826
token_endpoint_auth_method: "private_key_jwt",

src/utils/auth/get-jwt-token.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getJwtToken } from "@src/utils/auth/get-jwt-token";
22
import { AccessToken } from "@src/utils/auth/types";
3-
import { configProvider } from "@src/utils/config";
4-
import { appConfigBuilder } from "@test-data/config/builders";
3+
import lazyConfig from "@src/utils/lazy-config";
4+
import { AsyncConfigMock, lazyConfigBuilder } from "@test-data/config/builders";
55
import { getToken } from "next-auth/jwt";
66
import { cookies, headers } from "next/headers";
77

@@ -13,15 +13,14 @@ jest.mock("next/headers", () => ({
1313
headers: jest.fn(),
1414
cookies: jest.fn(),
1515
}));
16-
17-
jest.mock("@src/utils/config", () => ({
18-
configProvider: jest.fn(),
19-
}));
16+
jest.mock("sanitize-data", () => ({ sanitize: jest.fn() }));
2017

2118
describe("getJwtToken", () => {
19+
const mockedConfig = lazyConfig as AsyncConfigMock;
20+
2221
beforeEach(() => {
23-
const mockConfig = appConfigBuilder().withAUTH_SECRET("test-auth-secret");
24-
(configProvider as jest.Mock).mockResolvedValue(mockConfig);
22+
const defaultConfig = lazyConfigBuilder().withAuthSecret("test-auth-secret").build();
23+
Object.assign(mockedConfig, defaultConfig);
2524
});
2625

2726
const mockGetTokenResult = {

src/utils/auth/get-jwt-token.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { AppConfig, configProvider } from "@src/utils/config";
1+
import lazyConfig from "@src/utils/lazy-config";
22
import { JWT, getToken } from "next-auth/jwt";
33
import { cookies, headers } from "next/headers";
44

55
const getJwtToken = async (): Promise<JWT | null> => {
6-
const config: AppConfig = await configProvider();
76
const headerEntries = await headers();
87
const cookieEntries = await cookies();
98
const req = {
109
headers: Object.fromEntries(headerEntries),
1110
cookies: Object.fromEntries(cookieEntries.getAll().map((c) => [c.name, c.value])),
1211
};
1312

14-
return await getToken({ req, secret: config.AUTH_SECRET, secureCookie: true });
13+
return await getToken({ req, secret: (await lazyConfig.AUTH_SECRET) as string, secureCookie: true });
1514
};
1615
export { getJwtToken };

test-data/config/builders.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ class LazyConfigBuilder {
8888
return this.withNhsLoginClientId(value);
8989
}
9090

91+
public withNhsLoginScope(value: string): this {
92+
this._configValues.NHS_LOGIN_SCOPE = value;
93+
return this;
94+
}
95+
96+
public andNhsLoginScope(value: string): this {
97+
return this.withNhsLoginScope(value);
98+
}
99+
91100
public withNhsLoginPrivateKey(value: string): this {
92101
this._configValues.NHS_LOGIN_PRIVATE_KEY = value;
93102
return this;
@@ -125,6 +134,15 @@ class LazyConfigBuilder {
125134
return this.withEligibilityApiKey(value);
126135
}
127136

137+
public withAuthSecret(value: string): this {
138+
this._configValues.AUTH_SECRET = value;
139+
return this;
140+
}
141+
142+
public andAuthSecret(value: string): this {
143+
return this.withEligibilityApiKey(value);
144+
}
145+
128146
public build(): AsyncConfigMock {
129147
const asyncMock: AsyncConfigMock = {};
130148
for (const key in this._configValues) {

0 commit comments

Comments
 (0)