1+ import { ApimHttpError } from "@src/utils/auth/apim/exceptions" ;
12import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials" ;
23import { getToken } from "@src/utils/auth/callbacks/get-token" ;
34import { MaxAgeInSeconds } from "@src/utils/auth/types" ;
@@ -8,7 +9,6 @@ import { Account, Profile } from "next-auth";
89import { JWT } from "next-auth/jwt" ;
910import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers" ;
1011import { headers } from "next/headers" ;
11- import { ApimHttpError } from "@src/utils/auth/apim/exceptions" ;
1212
1313jest . mock ( "@project/auth" , ( ) => ( {
1414 auth : jest . fn ( ) ,
@@ -172,6 +172,44 @@ describe("getToken", () => {
172172
173173 expect ( result ) . toBeNull ( ) ;
174174 } ) ;
175+
176+ it ( "should still return login token even if fetching APIM credentials fails" , async ( ) => {
177+ ( getOrRefreshApimCredentials as jest . Mock ) . mockRejectedValue ( new ApimHttpError ( "Error getting APIM token" ) ) ;
178+
179+ ( jwtDecode as jest . Mock ) . mockReturnValue ( {
180+ jti : "jti_test" ,
181+ } ) ;
182+ const token = { apim : { } , nhs_login : { id_token : "id-token" } } as JWT ;
183+
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+
195+ const maxAgeInSeconds = 600 as MaxAgeInSeconds ;
196+
197+ const result = await getToken ( token , account , profile , mockConfig , maxAgeInSeconds ) ;
198+
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+ } ) ;
212+ } ) ;
175213 } ) ;
176214
177215 describe ( "when AUTH APIM is not available" , ( ) => {
0 commit comments