Skip to content

Commit 12d2ff0

Browse files
TASK Sonar fix: reduce duplication in getToken tests
1 parent 01ad65d commit 12d2ff0

File tree

1 file changed

+54
-103
lines changed

1 file changed

+54
-103
lines changed

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

Lines changed: 54 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,25 @@ describe("getToken", () => {
4444

4545
const nowInSeconds = 1749052001;
4646

47+
const profile = {
48+
nhs_number: "test_nhs_number",
49+
};
50+
51+
const account = {
52+
expires_at: nowInSeconds + 1000,
53+
access_token: "newAccess",
54+
refresh_token: "newRefresh",
55+
id_token: "newIdToken",
56+
} as Account;
57+
4758
beforeEach(() => {
4859
jest.clearAllMocks();
4960
jest.useFakeTimers().setSystemTime(nowInSeconds * 1000);
5061
process.env.NEXT_RUNTIME = "nodejs";
62+
63+
(jwtDecode as jest.Mock).mockReturnValue({
64+
jti: "jti_test",
65+
});
5166
});
5267

5368
afterEach(() => {
@@ -73,68 +88,32 @@ describe("getToken", () => {
7388
});
7489

7590
it("should return updated token on initial login with account profile, and APIM credentials", async () => {
76-
// Given
77-
(jwtDecode as jest.Mock).mockReturnValue({
78-
jti: "jti_test",
79-
});
8091
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
81-
const account = {
82-
expires_at: nowInSeconds + 1000,
83-
access_token: "newAccess",
84-
refresh_token: "newRefresh",
85-
id_token: "newIdToken",
86-
} as Account;
87-
const profile = {
88-
nhs_number: "test_nhs_number",
89-
};
9092
const maxAgeInSeconds = 600 as MaxAgeInSeconds;
9193

92-
// When
9394
const result = await getToken(token, account, profile, mockConfig, maxAgeInSeconds);
9495

95-
// Then
96-
expect(result).toMatchObject({
97-
user: {
98-
nhs_number: profile.nhs_number,
99-
},
100-
nhs_login: {
101-
id_token: "newIdToken",
102-
},
103-
apim: {
104-
access_token: "new-apim-access-token",
105-
expires_at: nowInSeconds + 1111,
106-
},
107-
fixedExpiry: nowInSeconds + maxAgeInSeconds,
108-
});
96+
expectResultToMatchTokenWith(
97+
result,
98+
profile.nhs_number,
99+
"newIdToken",
100+
"new-apim-access-token",
101+
nowInSeconds + 1111,
102+
maxAgeInSeconds,
103+
);
109104
});
110105

111106
// todo: check the apim assertion still holds
112107
it("should return token with empty values on initial login if account and profile are undefined", async () => {
113-
const token = {} as JWT;
114-
115-
const account = {} as Account;
116-
117-
const profile = {} as Profile;
118-
108+
const undefinedToken = {} as JWT;
109+
const undefinedAccount = {} as Account;
110+
const undefinedProfile = {} as Profile;
119111
const maxAgeInSeconds = 600 as MaxAgeInSeconds;
120-
121112
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
122113

123-
const result = await getToken(token, account, profile, mockConfig, maxAgeInSeconds);
114+
const result = await getToken(undefinedToken, undefinedAccount, undefinedProfile, mockConfig, maxAgeInSeconds);
124115

125-
expect(result).toMatchObject({
126-
user: {
127-
nhs_number: "",
128-
},
129-
nhs_login: {
130-
id_token: "",
131-
},
132-
apim: {
133-
access_token: "",
134-
expires_at: 0,
135-
},
136-
fixedExpiry: nowInSeconds + maxAgeInSeconds,
137-
});
116+
expectResultToMatchTokenWith(result, "", "", "", 0, maxAgeInSeconds);
138117
});
139118

140119
it("should fill in missing values in token with default empty string", async () => {
@@ -176,39 +155,13 @@ describe("getToken", () => {
176155
it("should still return login token even if fetching APIM credentials fails", async () => {
177156
(getOrRefreshApimCredentials as jest.Mock).mockRejectedValue(new ApimHttpError("Error getting APIM token"));
178157

179-
(jwtDecode as jest.Mock).mockReturnValue({
180-
jti: "jti_test",
181-
});
182158
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
183159

184-
const account = {
185-
expires_at: nowInSeconds + 1000,
186-
access_token: "newAccess",
187-
refresh_token: "newRefresh",
188-
id_token: "newIdToken",
189-
} as Account;
190-
191-
const profile = {
192-
nhs_number: "test_nhs_number",
193-
};
194-
195160
const maxAgeInSeconds = 600 as MaxAgeInSeconds;
196161

197162
const result = await getToken(token, account, profile, mockConfig, maxAgeInSeconds);
198163

199-
expect(result).toMatchObject({
200-
user: {
201-
nhs_number: profile.nhs_number,
202-
},
203-
nhs_login: {
204-
id_token: "newIdToken",
205-
},
206-
apim: {
207-
access_token: "",
208-
expires_at: 0,
209-
},
210-
fixedExpiry: nowInSeconds + maxAgeInSeconds,
211-
});
164+
expectResultToMatchTokenWith(result, profile.nhs_number, "newIdToken", "", 0, maxAgeInSeconds);
212165
});
213166
});
214167

@@ -218,39 +171,37 @@ describe("getToken", () => {
218171
});
219172

220173
it("should return updated token on initial login with account profile, and default empty APIM credentials", async () => {
221-
(jwtDecode as jest.Mock).mockReturnValue({
222-
jti: "jti_test",
223-
});
224174
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
225175

226-
const account = {
227-
expires_at: nowInSeconds + 1000,
228-
access_token: "newAccess",
229-
refresh_token: "newRefresh",
230-
id_token: "newIdToken",
231-
} as Account;
232-
233-
const profile = {
234-
nhs_number: "test_nhs_number",
235-
};
236-
237176
const maxAgeInSeconds = 600 as MaxAgeInSeconds;
238177

239178
const result = await getToken(token, account, profile, mockConfig, maxAgeInSeconds);
240179

241-
expect(result).toMatchObject({
242-
user: {
243-
nhs_number: profile.nhs_number,
244-
},
245-
nhs_login: {
246-
id_token: "newIdToken",
247-
},
248-
apim: {
249-
access_token: "",
250-
expires_at: 0,
251-
},
252-
fixedExpiry: nowInSeconds + maxAgeInSeconds,
253-
});
180+
expectResultToMatchTokenWith(result, profile.nhs_number, "newIdToken", "", 0, maxAgeInSeconds);
254181
});
255182
});
183+
184+
const expectResultToMatchTokenWith = (
185+
result: JWT | null,
186+
nhsNumber: string,
187+
idToken: string,
188+
apimToken: string,
189+
apimExpiresAt: number,
190+
maxAgeInSeconds: number,
191+
) => {
192+
expect(result).not.toBeNull();
193+
expect(result).toMatchObject({
194+
user: {
195+
nhs_number: nhsNumber,
196+
},
197+
nhs_login: {
198+
id_token: idToken,
199+
},
200+
apim: {
201+
access_token: apimToken,
202+
expires_at: apimExpiresAt,
203+
},
204+
fixedExpiry: nowInSeconds + maxAgeInSeconds,
205+
});
206+
};
256207
});

0 commit comments

Comments
 (0)