Skip to content

Commit 695d6b8

Browse files
donna-belsey-nhsanoop-surej-nhs
authored andcommitted
VIA-254 Refactor: rename method that is no longer private
1 parent ffaddf7 commit 695d6b8

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

src/utils/auth/apim/get-apim-access-token.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fetchAPIMAccessToken } from "@src/utils/auth/apim/fetch-apim-access-token";
22
import { getApimAccessToken, retrieveApimCredentials } from "@src/utils/auth/apim/get-apim-access-token";
3-
import { _getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
3+
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
44
import { getJwtToken } from "@src/utils/auth/get-jwt-token";
55
import { AccessToken, IdToken } from "@src/utils/auth/types";
66
import { AppConfig } from "@src/utils/config";
@@ -16,7 +16,7 @@ jest.mock("@src/utils/auth/apim/fetch-apim-access-token", () => ({
1616
jest.mock("sanitize-data", () => ({ sanitize: jest.fn() }));
1717

1818
jest.mock("@src/utils/auth/apim/get-or-refresh-apim-credentials", () => ({
19-
_getOrRefreshApimCredentials: jest.fn(),
19+
getOrRefreshApimCredentials: jest.fn(),
2020
}));
2121

2222
const mockConfig: AppConfig = appConfigBuilder().build();
@@ -43,7 +43,7 @@ describe("getApimAccessToken", () => {
4343

4444
it("should pass JWT token to getOrRefresh method and return what it returns", async () => {
4545
const getOrRefreshedApimAccessToken = "apim-access-token";
46-
(_getOrRefreshApimCredentials as jest.Mock).mockResolvedValue({
46+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue({
4747
accessToken: getOrRefreshedApimAccessToken,
4848
expiresAt: nowInSeconds + 1111,
4949
});
@@ -52,7 +52,7 @@ describe("getApimAccessToken", () => {
5252

5353
const apimAccessToken = await getApimAccessToken(mockConfig);
5454

55-
expect(_getOrRefreshApimCredentials).toHaveBeenCalledWith(mockConfig, mockJwtToken, nowInSeconds);
55+
expect(getOrRefreshApimCredentials).toHaveBeenCalledWith(mockConfig, mockJwtToken, nowInSeconds);
5656
expect(apimAccessToken).toEqual(getOrRefreshedApimAccessToken as AccessToken);
5757
});
5858

@@ -63,7 +63,7 @@ describe("getApimAccessToken", () => {
6363
});
6464

6565
it("should throw error if _getOrRefresh APIM access token returns undefined", async () => {
66-
(_getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
66+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
6767

6868
(getJwtToken as jest.Mock).mockResolvedValue(mockJwtToken);
6969

src/utils/auth/apim/get-apim-access-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApimMissingTokenError } from "@src/utils/auth/apim/exceptions";
22
import { fetchAPIMAccessToken } from "@src/utils/auth/apim/fetch-apim-access-token";
3-
import { _getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
3+
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
44
import { ApimAccessCredentials, ApimTokenResponse } from "@src/utils/auth/apim/types";
55
import { getJwtToken } from "@src/utils/auth/get-jwt-token";
66
import { AccessToken, ExpiresAt, IdToken } from "@src/utils/auth/types";
@@ -26,7 +26,7 @@ const getApimAccessToken = async (config: AppConfig): Promise<AccessToken> => {
2626
}
2727

2828
const nowInSeconds = Math.floor(Date.now() / 1000);
29-
const apimAccessCredentials = await _getOrRefreshApimCredentials(config, token, nowInSeconds);
29+
const apimAccessCredentials = await getOrRefreshApimCredentials(config, token, nowInSeconds);
3030

3131
if (!apimAccessCredentials) {
3232
log.error("Error: getOrRefreshApimCredentials returned undefined");

src/utils/auth/apim/get-or-refresh-apim-credentials.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { retrieveApimCredentials } from "@src/utils/auth/apim/get-apim-access-token";
2-
import { _getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
2+
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
33
import { AppConfig } from "@src/utils/config";
44
import { appConfigBuilder } from "@test-data/config/builders";
55
import { JWT } from "next-auth/jwt";
@@ -43,15 +43,15 @@ describe("getOrRefreshApimCredentials", () => {
4343
it("should return undefined and logs error if token does not contain id_token", async () => {
4444
const token = { apim: {}, nhs_login: {} } as JWT;
4545

46-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
46+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
4747

4848
expect(result).toBeUndefined();
4949
});
5050

5151
it("should return new APIM creds if stored creds are empty", async () => {
5252
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
5353

54-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
54+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
5555

5656
expect(result).toMatchObject({
5757
accessToken: "new-apim-access-token",
@@ -68,7 +68,7 @@ describe("getOrRefreshApimCredentials", () => {
6868
nhs_login: { id_token: "old-id-token" },
6969
} as JWT;
7070

71-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
71+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
7272

7373
expect(result).toEqual({
7474
accessToken: "old-access-token",
@@ -82,7 +82,7 @@ describe("getOrRefreshApimCredentials", () => {
8282
nhs_login: { id_token: "id-token" },
8383
} as JWT;
8484

85-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
85+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
8686

8787
expect(result).toEqual({
8888
accessToken: "new-apim-access-token",
@@ -98,7 +98,7 @@ describe("getOrRefreshApimCredentials", () => {
9898
nhs_login: { id_token: "id-token" },
9999
} as JWT;
100100

101-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
101+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
102102

103103
expect(result).toEqual({
104104
accessToken: "stored-access-token",
@@ -110,7 +110,7 @@ describe("getOrRefreshApimCredentials", () => {
110110
process.env.NEXT_RUNTIME = "edge";
111111
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
112112

113-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
113+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
114114
expect(result).toBeUndefined();
115115
});
116116
});
@@ -137,7 +137,7 @@ describe("getOrRefreshApimCredentials", () => {
137137
it("should return undefined if APIM auth is not enabled", async () => {
138138
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
139139

140-
const result = await _getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
140+
const result = await getOrRefreshApimCredentials(mockConfig, token, nowInSeconds);
141141

142142
expect(result).toBeUndefined();
143143
});

src/utils/auth/apim/get-or-refresh-apim-credentials.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Logger } from "pino";
88

99
const log: Logger = logger.child({ module: "get-or-refresh-apim-credentials" });
1010

11-
async function _getOrRefreshApimCredentials(config: AppConfig, token: JWT, nowInSeconds: number) {
11+
const getOrRefreshApimCredentials = async (config: AppConfig, token: JWT, nowInSeconds: number) => {
1212
// Return the APIM creds from the token if still valid, or fetch new creds from APIM if expiring soon or empty
1313
let apimCredentials: ApimAccessCredentials | undefined;
1414

@@ -69,6 +69,6 @@ async function _getOrRefreshApimCredentials(config: AppConfig, token: JWT, nowIn
6969
}
7070
}
7171
return apimCredentials;
72-
}
72+
};
7373

74-
export { _getOrRefreshApimCredentials };
74+
export { getOrRefreshApimCredentials };

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
1+
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
22
import { getToken } from "@src/utils/auth/callbacks/get-token";
33
import { MaxAgeInSeconds } from "@src/utils/auth/types";
44
import { AppConfig } from "@src/utils/config";
@@ -14,7 +14,7 @@ jest.mock("@project/auth", () => ({
1414
}));
1515

1616
jest.mock("@src/utils/auth/apim/get-or-refresh-apim-credentials", () => ({
17-
_getOrRefreshApimCredentials: jest.fn(),
17+
getOrRefreshApimCredentials: jest.fn(),
1818
}));
1919

2020
jest.mock("next/headers", () => ({
@@ -51,7 +51,7 @@ describe("getToken", () => {
5151
});
5252

5353
beforeEach(async () => {
54-
(_getOrRefreshApimCredentials as jest.Mock).mockResolvedValue({
54+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue({
5555
accessToken: "new-apim-access-token",
5656
expiresAt: nowInSeconds + 1111,
5757
});
@@ -117,7 +117,7 @@ describe("getToken", () => {
117117

118118
const maxAgeInSeconds = 600 as MaxAgeInSeconds;
119119

120-
(_getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
120+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
121121

122122
const result = await getToken(token, account, profile, mockConfig, maxAgeInSeconds);
123123

@@ -143,7 +143,7 @@ describe("getToken", () => {
143143
apim: {},
144144
} as JWT;
145145

146-
(_getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
146+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
147147

148148
const result = await getToken(token, null, undefined, mockConfig, 300 as MaxAgeInSeconds);
149149

@@ -175,7 +175,7 @@ describe("getToken", () => {
175175

176176
// this condition is now not relevant at this layer
177177
describe("when AUTH APIM is not available", () => {
178-
(_getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
178+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
179179

180180
const mockConfig: AppConfig = appConfigBuilder()
181181
.withNHS_LOGIN_URL("https://mock.nhs.login")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NhsNumber } from "@src/models/vaccine";
2-
import { _getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
2+
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
33
import { ApimAccessCredentials } from "@src/utils/auth/apim/types";
44
import { IdToken, MaxAgeInSeconds, NowInSeconds } from "@src/utils/auth/types";
55
import { AppConfig } from "@src/utils/config";
@@ -46,7 +46,7 @@ const getToken = async (
4646
}
4747

4848
// TODO VIA-254 - can we do this only once? https://www.youtube.com/watch?v=A4I9DMSvJxg
49-
const apimAccessCredentials = await _getOrRefreshApimCredentials(config, token, nowInSeconds);
49+
const apimAccessCredentials = await getOrRefreshApimCredentials(config, token, nowInSeconds);
5050

5151
// Inspect the token (which was either returned from login or fetched from session), fill missing or blank values with defaults
5252
let updatedToken: JWT = fillMissingFieldsInTokenWithDefaultValues(token, apimAccessCredentials);

0 commit comments

Comments
 (0)