@@ -15,154 +15,221 @@ jest.mock("@src/utils/auth/apim/get-apim-access-token", () => ({
1515jest . mock ( "jwt-decode" ) ;
1616
1717describe ( "getToken" , ( ) => {
18- const mockConfig : AppConfig = appConfigBuilder ( )
19- . withNHS_LOGIN_URL ( "https://mock.nhs.login" )
20- . andNHS_LOGIN_CLIENT_ID ( "mock-client-id" )
21- . andNHS_LOGIN_PRIVATE_KEY ( "mock-private-key" )
22- . andIS_APIM_AVAILABLE ( true ) // TODO VIA-254 - Test with false
23- . build ( ) ;
24-
25- const nowInSeconds = 1749052001 ;
26-
27- beforeEach ( ( ) => {
28- jest . clearAllMocks ( ) ;
29- jest . useFakeTimers ( ) ;
30- jest . setSystemTime ( nowInSeconds * 1000 ) ;
31- } ) ;
32-
33- afterEach ( ( ) => {
34- jest . resetAllMocks ( ) ;
35- } ) ;
36-
37- afterAll ( ( ) => {
38- jest . useRealTimers ( ) ;
39- } ) ;
40-
41- it ( "should return null and logs error if token is falsy" , async ( ) => {
42- const result = await getToken ( null as unknown as JWT , null , undefined , mockConfig , 300 ) ;
43- expect ( result ) . toBeNull ( ) ;
44- } ) ;
45-
46- it ( "should return updated token on initial login with account profile, and APIM creds" , async ( ) => {
47- ( jwtDecode as jest . Mock ) . mockReturnValue ( {
48- jti : "jti_test" ,
18+ describe ( "when AUTH APIM is available" , ( ) => {
19+ const mockConfig : AppConfig = appConfigBuilder ( )
20+ . withNHS_LOGIN_URL ( "https://mock.nhs.login" )
21+ . andNHS_LOGIN_CLIENT_ID ( "mock-client-id" )
22+ . andNHS_LOGIN_PRIVATE_KEY ( "mock-private-key" )
23+ . andIS_APIM_AVAILABLE ( true )
24+ . build ( ) ;
25+
26+ const nowInSeconds = 1749052001 ;
27+
28+ beforeEach ( ( ) => {
29+ jest . clearAllMocks ( ) ;
30+ jest . useFakeTimers ( ) ;
31+ jest . setSystemTime ( nowInSeconds * 1000 ) ;
4932 } ) ;
50- const token = { apim : { } , nhs_login : { id_token : "id-token" } } as JWT ;
51- ( getNewAccessTokenFromApim as jest . Mock ) . mockResolvedValue ( {
52- accessToken : "test-apim-access-token" ,
53- refreshToken : "test-apim-refresh-token" ,
54- expiresIn : "test-apim-expires-in" ,
55- refreshTokenExpiresIn : "test-apim-refresh-token-expires-in" ,
33+
34+ afterEach ( ( ) => {
35+ jest . resetAllMocks ( ) ;
5636 } ) ;
5737
58- const account = {
59- expires_at : nowInSeconds + 1000 ,
60- access_token : "newAccess" ,
61- refresh_token : "newRefresh" ,
62- id_token : "newIdToken" ,
63- } as Account ;
38+ afterAll ( ( ) => {
39+ jest . useRealTimers ( ) ;
40+ } ) ;
6441
65- const profile = {
66- nhs_number : "test_nhs_number" ,
67- birthdate : "test_birthdate" ,
68- } ;
42+ it ( "should return null and logs error if token is falsy" , async ( ) => {
43+ const result = await getToken ( null as unknown as JWT , null , undefined , mockConfig , 300 ) ;
44+ expect ( result ) . toBeNull ( ) ;
45+ } ) ;
6946
70- const maxAgeInSeconds = 600 ;
47+ it ( "should return updated token on initial login with account profile, and APIM credentials" , async ( ) => {
48+ ( jwtDecode as jest . Mock ) . mockReturnValue ( {
49+ jti : "jti_test" ,
50+ } ) ;
51+ const token = { apim : { } , nhs_login : { id_token : "id-token" } } as JWT ;
52+ ( getNewAccessTokenFromApim as jest . Mock ) . mockResolvedValue ( {
53+ accessToken : "test-apim-access-token" ,
54+ refreshToken : "test-apim-refresh-token" ,
55+ expiresIn : "test-apim-expires-in" ,
56+ refreshTokenExpiresIn : "test-apim-refresh-token-expires-in" ,
57+ } ) ;
58+
59+ const account = {
60+ expires_at : nowInSeconds + 1000 ,
61+ access_token : "newAccess" ,
62+ refresh_token : "newRefresh" ,
63+ id_token : "newIdToken" ,
64+ } as Account ;
65+
66+ const profile = {
67+ nhs_number : "test_nhs_number" ,
68+ birthdate : "test_birthdate" ,
69+ } ;
70+
71+ const maxAgeInSeconds = 600 ;
72+
73+ const result = await getToken ( token , account , profile , mockConfig , maxAgeInSeconds ) ;
74+
75+ expect ( result ) . toMatchObject ( {
76+ user : {
77+ nhs_number : profile . nhs_number ,
78+ birthdate : profile . birthdate ,
79+ } ,
80+ nhs_login : {
81+ id_token : "newIdToken" ,
82+ } ,
83+ apim : {
84+ access_token : "test-apim-access-token" ,
85+ expires_in : "test-apim-expires-in" ,
86+ refresh_token : "test-apim-refresh-token" ,
87+ refresh_token_expires_in : "test-apim-refresh-token-expires-in" ,
88+ } ,
89+ fixedExpiry : nowInSeconds + maxAgeInSeconds ,
90+ } ) ;
91+ } ) ;
7192
72- const result = await getToken ( token , account , profile , mockConfig , maxAgeInSeconds ) ;
93+ it ( "should return token with empty values on initial login if account and profile are undefined" , async ( ) => {
94+ const token = { } as JWT ;
95+
96+ const account = { } as Account ;
97+
98+ const profile = { } as Profile ;
99+
100+ const maxAgeInSeconds = 600 ;
101+
102+ const result = await getToken ( token , account , profile , mockConfig , maxAgeInSeconds ) ;
103+
104+ expect ( result ) . toMatchObject ( {
105+ user : {
106+ nhs_number : "" ,
107+ birthdate : "" ,
108+ } ,
109+ nhs_login : {
110+ id_token : "" ,
111+ } ,
112+ apim : {
113+ access_token : "" ,
114+ expires_in : "" ,
115+ refresh_token : "" ,
116+ refresh_token_expires_in : "" ,
117+ } ,
118+ fixedExpiry : nowInSeconds + maxAgeInSeconds ,
119+ } ) ;
120+ } ) ;
73121
74- expect ( result ) . toMatchObject ( {
75- user : {
76- nhs_number : profile . nhs_number ,
77- birthdate : profile . birthdate ,
78- } ,
79- nhs_login : {
80- id_token : "newIdToken" ,
81- } ,
82- apim : {
83- access_token : "test-apim-access-token" ,
84- expires_in : "test-apim-expires-in" ,
85- refresh_token : "test-apim-refresh-token" ,
86- refresh_token_expires_in : "test-apim-refresh-token-expires-in" ,
87- } ,
88- fixedExpiry : nowInSeconds + maxAgeInSeconds ,
122+ it ( "should fill in missing values in token with default empty string" , async ( ) => {
123+ const token = {
124+ user : { } ,
125+ nhs_login : { } ,
126+ apim : { } ,
127+ } as JWT ;
128+
129+ const result = await getToken ( token , null , undefined , mockConfig , 300 ) ;
130+
131+ expect ( result ) . toMatchObject ( {
132+ user : {
133+ nhs_number : "" ,
134+ birthdate : "" ,
135+ } ,
136+ nhs_login : {
137+ id_token : "" ,
138+ } ,
139+ apim : {
140+ access_token : "" ,
141+ expires_in : "" ,
142+ refresh_token : "" ,
143+ refresh_token_expires_in : "" ,
144+ } ,
145+ } ) ;
89146 } ) ;
90- } ) ;
91147
92- it ( "should return token with empty values on initial login if account and profile are undefined" , async ( ) => {
93- const token = { } as JWT ;
148+ it ( "should return null if fixedExpiry reached" , async ( ) => {
149+ const token = {
150+ fixedExpiry : nowInSeconds - 1 ,
151+ user : { } ,
152+ } as JWT ;
94153
95- const account = { } as Account ;
154+ const result = await getToken ( token , null , undefined , mockConfig , 300 ) ;
96155
97- const profile = { } as Profile ;
156+ expect ( result ) . toBeNull ( ) ;
157+ } ) ;
98158
99- const maxAgeInSeconds = 600 ;
159+ it ( "should not update apim creds if already present (and not expired)" , async ( ) => {
160+ // Given
161+ const token = { apim : { access_token : "test-apim-access-token" } , nhs_login : { id_token : "id-token" } } as JWT ;
100162
101- const result = await getToken ( token , account , profile , mockConfig , maxAgeInSeconds ) ;
163+ // When
164+ const result = await getToken ( token , null , undefined , mockConfig , 300 ) ;
102165
103- expect ( result ) . toMatchObject ( {
104- user : {
105- nhs_number : "" ,
106- birthdate : "" ,
107- } ,
108- nhs_login : {
109- id_token : "" ,
110- } ,
111- apim : {
112- access_token : "" ,
113- expires_in : "" ,
114- refresh_token : "" ,
115- refresh_token_expires_in : "" ,
116- } ,
117- fixedExpiry : nowInSeconds + maxAgeInSeconds ,
166+ // Then
167+ expect ( result ?. apim ) . toMatchObject ( { access_token : "test-apim-access-token" } ) ;
118168 } ) ;
119169 } ) ;
120170
121- it ( "should fill in missing values in token with default empty string" , async ( ) => {
122- const token = {
123- user : { } ,
124- nhs_login : { } ,
125- apim : { } ,
126- } as JWT ;
127-
128- const result = await getToken ( token , null , undefined , mockConfig , 300 ) ;
129-
130- expect ( result ) . toMatchObject ( {
131- user : {
132- nhs_number : "" ,
133- birthdate : "" ,
134- } ,
135- nhs_login : {
136- id_token : "" ,
137- } ,
138- apim : {
139- access_token : "" ,
140- expires_in : "" ,
141- refresh_token : "" ,
142- refresh_token_expires_in : "" ,
143- } ,
144- } ) ;
145- } ) ;
171+ describe ( "when AUTH APIM is not available" , ( ) => {
172+ const mockConfig : AppConfig = appConfigBuilder ( )
173+ . withNHS_LOGIN_URL ( "https://mock.nhs.login" )
174+ . andNHS_LOGIN_CLIENT_ID ( "mock-client-id" )
175+ . andNHS_LOGIN_PRIVATE_KEY ( "mock-private-key" )
176+ . andIS_APIM_AVAILABLE ( false )
177+ . build ( ) ;
146178
147- it ( "should return null if fixedExpiry reached" , async ( ) => {
148- const token = {
149- fixedExpiry : nowInSeconds - 1 ,
150- user : { } ,
151- } as JWT ;
179+ const nowInSeconds = 1749052001 ;
152180
153- const result = await getToken ( token , null , undefined , mockConfig , 300 ) ;
181+ beforeEach ( ( ) => {
182+ jest . clearAllMocks ( ) ;
183+ jest . useFakeTimers ( ) ;
184+ jest . setSystemTime ( nowInSeconds * 1000 ) ;
185+ } ) ;
154186
155- expect ( result ) . toBeNull ( ) ;
156- } ) ;
187+ afterEach ( ( ) => {
188+ jest . resetAllMocks ( ) ;
189+ } ) ;
157190
158- it ( "should not update apim creds if already present (and not expired)" , async ( ) => {
159- // Given
160- const token = { apim : { access_token : "test-apim-access-token" } , nhs_login : { id_token : "id-token" } } as JWT ;
191+ afterAll ( ( ) => {
192+ jest . useRealTimers ( ) ;
193+ } ) ;
161194
162- // When
163- const result = await getToken ( token , null , undefined , mockConfig , 300 ) ;
195+ it ( "should return updated token on initial login with account profile, and APIM credentials" , async ( ) => {
196+ ( jwtDecode as jest . Mock ) . mockReturnValue ( {
197+ jti : "jti_test" ,
198+ } ) ;
199+ const token = { apim : { } , nhs_login : { id_token : "id-token" } } as JWT ;
164200
165- // Then
166- expect ( result ?. apim ) . toMatchObject ( { access_token : "test-apim-access-token" } ) ;
201+ const account = {
202+ expires_at : nowInSeconds + 1000 ,
203+ access_token : "newAccess" ,
204+ refresh_token : "newRefresh" ,
205+ id_token : "newIdToken" ,
206+ } as Account ;
207+
208+ const profile = {
209+ nhs_number : "test_nhs_number" ,
210+ birthdate : "test_birthdate" ,
211+ } ;
212+
213+ const maxAgeInSeconds = 600 ;
214+
215+ const result = await getToken ( token , account , profile , mockConfig , maxAgeInSeconds ) ;
216+
217+ expect ( result ) . toMatchObject ( {
218+ user : {
219+ nhs_number : profile . nhs_number ,
220+ birthdate : profile . birthdate ,
221+ } ,
222+ nhs_login : {
223+ id_token : "newIdToken" ,
224+ } ,
225+ apim : {
226+ access_token : "" ,
227+ expires_in : "" ,
228+ refresh_token : "" ,
229+ refresh_token_expires_in : "" ,
230+ } ,
231+ fixedExpiry : nowInSeconds + maxAgeInSeconds ,
232+ } ) ;
233+ } ) ;
167234 } ) ;
168235} ) ;
0 commit comments