11/**
22 * @jest -environment node
33 */
4- import { ApimConfig } from "@src/utils/apimConfig" ;
54import { generateAPIMTokenPayload } from "@src/utils/auth/apim/fetch-apim-access-token" ;
65import { APIMTokenPayload , IdToken } from "@src/utils/auth/types" ;
7- import { apimConfigBuilder } from "@test-data/config/builders" ;
6+ import lazyConfig from "@src/utils/lazy-config" ;
7+ import { AsyncConfigMock , lazyConfigBuilder } from "@test-data/config/builders" ;
88import jwt from "jsonwebtoken" ;
99
1010jest . mock ( "jsonwebtoken" , ( ) => ( {
@@ -17,18 +17,14 @@ const mockSignedJwt = "mock-signed-jwt";
1717const mockIdToken = "id-token" as IdToken ;
1818const mockNowInSeconds = 1749052001 ;
1919
20- const apimApiKey = "apim -api-key" ;
20+ const eligibilityApiKey = "eligibility -api-key" ;
2121const apimKeyId = "apim-key-id" ;
2222const apimPrivateKey = "apim-private-key" ;
23- const mockApimConfig : ApimConfig = apimConfigBuilder ( )
24- . withELIGIBILITY_API_KEY ( apimApiKey )
25- . andAPIM_AUTH_URL ( new URL ( "https://apim-test-auth-url.com/test" ) )
26- . andAPIM_KEY_ID ( apimKeyId )
27- . andAPIM_PRIVATE_KEY ( apimPrivateKey )
28- . build ( ) ;
23+ const apimAuthUrl = new URL ( "https://apim-test-auth-url.com/test" ) ;
2924
3025describe ( "generateAPIMTokenPayload" , ( ) => {
3126 let randomUUIDSpy : jest . SpyInstance ;
27+ const mockedConfig = lazyConfig as AsyncConfigMock ;
3228
3329 beforeAll ( ( ) => {
3430 randomUUIDSpy = jest . spyOn ( global . crypto , "randomUUID" ) . mockReturnValue ( mockRandomUUID ) ;
@@ -38,6 +34,13 @@ describe("generateAPIMTokenPayload", () => {
3834 randomUUIDSpy . mockClear ( ) ;
3935 jest . useFakeTimers ( ) ;
4036 jest . setSystemTime ( mockNowInSeconds * 1000 ) ;
37+ const defaultConfig = lazyConfigBuilder ( )
38+ . withEligibilityApiKey ( eligibilityApiKey )
39+ . andApimAuthUrl ( apimAuthUrl )
40+ . andApimKeyId ( apimKeyId )
41+ . andApimPrivateKey ( apimPrivateKey )
42+ . build ( ) ;
43+ Object . assign ( mockedConfig , defaultConfig ) ;
4144 } ) ;
4245
4346 afterAll ( ( ) => {
@@ -47,28 +50,28 @@ describe("generateAPIMTokenPayload", () => {
4750 } ) ;
4851
4952 describe ( "new access token requested" , ( ) => {
50- it ( "should include signed client_assertion with expected iss sub and aud values" , ( ) => {
53+ it ( "should include signed client_assertion with expected iss sub and aud values" , async ( ) => {
5154 ( jwt . sign as jest . Mock ) . mockReturnValue ( mockSignedJwt ) ;
5255
5356 const expectedClientAssertionPayloadContent = {
54- iss : mockApimConfig . ELIGIBILITY_API_KEY ,
55- sub : mockApimConfig . ELIGIBILITY_API_KEY ,
56- aud : mockApimConfig . APIM_AUTH_URL . href ,
57+ iss : eligibilityApiKey ,
58+ sub : eligibilityApiKey ,
59+ aud : apimAuthUrl . href ,
5760 jti : mockRandomUUID ,
5861 exp : mockNowInSeconds + 300 ,
5962 } ;
6063
61- const apimTokenPayload : APIMTokenPayload = generateAPIMTokenPayload ( mockApimConfig , mockIdToken ) ;
64+ const apimTokenPayload : APIMTokenPayload = await generateAPIMTokenPayload ( mockIdToken ) ;
6265 const clientAssertionJWT = apimTokenPayload . client_assertion ;
6366
64- expect ( jwt . sign ) . toHaveBeenCalledWith ( expectedClientAssertionPayloadContent , mockApimConfig . APIM_PRIVATE_KEY , {
67+ expect ( jwt . sign ) . toHaveBeenCalledWith ( expectedClientAssertionPayloadContent , apimPrivateKey , {
6568 algorithm : "RS512" ,
66- keyid : mockApimConfig . APIM_KEY_ID ,
69+ keyid : apimKeyId ,
6770 } ) ;
6871 expect ( clientAssertionJWT ) . toEqual ( mockSignedJwt ) ;
6972 } ) ;
7073
71- it ( "should use token-exchange grant type & id_token as subject_token field" , ( ) => {
74+ it ( "should use token-exchange grant type & id_token as subject_token field" , async ( ) => {
7275 // Given
7376 ( jwt . sign as jest . Mock ) . mockReturnValue ( mockSignedJwt ) ;
7477
@@ -81,7 +84,7 @@ describe("generateAPIMTokenPayload", () => {
8184 } ;
8285
8386 // When
84- const apimTokenPayload = generateAPIMTokenPayload ( mockApimConfig , mockIdToken ) ;
87+ const apimTokenPayload = await generateAPIMTokenPayload ( mockIdToken ) ;
8588
8689 // Then
8790 expect ( apimTokenPayload ) . toEqual ( expectedTokenPayload ) ;
@@ -92,9 +95,9 @@ describe("generateAPIMTokenPayload", () => {
9295 throw new Error ( "Invalid key" ) ;
9396 } ) ;
9497
95- expect ( ( ) => {
96- generateAPIMTokenPayload ( mockApimConfig , mockIdToken ) ;
97- } ) . toThrow ( "Invalid key" ) ;
98+ expect ( async ( ) => {
99+ await generateAPIMTokenPayload ( mockIdToken ) ;
100+ } ) . rejects . toThrow ( "Invalid key" ) ;
98101 } ) ;
99102 } ) ;
100103} ) ;
0 commit comments