@@ -394,7 +394,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
394394 // When a route doesn't match, the handler returns a NextResponse.next() with status 200
395395 expect ( response . status ) . toBe ( 200 ) ;
396396 } ) ;
397-
397+
398398 it ( "should use the default value (true) for enableAccessTokenEndpoint when not explicitly provided" , async ( ) => {
399399 const secret = await generateSecret ( 32 ) ;
400400 const transactionStore = new TransactionStore ( {
@@ -4374,53 +4374,65 @@ ca/T0LLtgmbMmxSv/MmzIg==
43744374 const authClient = await createAuthClient ( {
43754375 signInReturnToPath : defaultReturnTo
43764376 } ) ;
4377-
4377+
43784378 // Mock the transactionStore.save method to verify the saved state
4379- const originalSave = authClient [ ' transactionStore' ] . save ;
4380- authClient [ ' transactionStore' ] . save = vi . fn ( async ( cookies , state ) => {
4379+ const originalSave = authClient [ " transactionStore" ] . save ;
4380+ authClient [ " transactionStore" ] . save = vi . fn ( async ( cookies , state ) => {
43814381 expect ( state . returnTo ) . toBe ( defaultReturnTo ) ;
4382- return originalSave . call ( authClient [ 'transactionStore' ] , cookies , state ) ;
4382+ return originalSave . call (
4383+ authClient [ "transactionStore" ] ,
4384+ cookies ,
4385+ state
4386+ ) ;
43834387 } ) ;
43844388
43854389 await authClient . startInteractiveLogin ( ) ;
4386-
4387- expect ( authClient [ ' transactionStore' ] . save ) . toHaveBeenCalled ( ) ;
4390+
4391+ expect ( authClient [ " transactionStore" ] . save ) . toHaveBeenCalled ( ) ;
43884392 } ) ;
43894393
43904394 it ( "should sanitize and use the provided returnTo parameter" , async ( ) => {
43914395 const authClient = await createAuthClient ( ) ;
43924396 const returnTo = "/custom-return-path" ;
4393-
4397+
43944398 // Mock the transactionStore.save method to verify the saved state
4395- const originalSave = authClient [ ' transactionStore' ] . save ;
4396- authClient [ ' transactionStore' ] . save = vi . fn ( async ( cookies , state ) => {
4399+ const originalSave = authClient [ " transactionStore" ] . save ;
4400+ authClient [ " transactionStore" ] . save = vi . fn ( async ( cookies , state ) => {
43974401 // The full URL is saved, not just the path
43984402 expect ( state . returnTo ) . toBe ( "https://example.com/custom-return-path" ) ;
4399- return originalSave . call ( authClient [ 'transactionStore' ] , cookies , state ) ;
4403+ return originalSave . call (
4404+ authClient [ "transactionStore" ] ,
4405+ cookies ,
4406+ state
4407+ ) ;
44004408 } ) ;
44014409
44024410 await authClient . startInteractiveLogin ( { returnTo } ) ;
4403-
4404- expect ( authClient [ ' transactionStore' ] . save ) . toHaveBeenCalled ( ) ;
4411+
4412+ expect ( authClient [ " transactionStore" ] . save ) . toHaveBeenCalled ( ) ;
44054413 } ) ;
44064414
44074415 it ( "should reject unsafe returnTo URLs" , async ( ) => {
44084416 const authClient = await createAuthClient ( {
44094417 signInReturnToPath : "/safe-path"
44104418 } ) ;
44114419 const unsafeReturnTo = "https://malicious-site.com" ;
4412-
4420+
44134421 // Mock the transactionStore.save method to verify the saved state
4414- const originalSave = authClient [ ' transactionStore' ] . save ;
4415- authClient [ ' transactionStore' ] . save = vi . fn ( async ( cookies , state ) => {
4422+ const originalSave = authClient [ " transactionStore" ] . save ;
4423+ authClient [ " transactionStore" ] . save = vi . fn ( async ( cookies , state ) => {
44164424 // Should use the default safe path instead of the malicious one
44174425 expect ( state . returnTo ) . toBe ( "/safe-path" ) ;
4418- return originalSave . call ( authClient [ 'transactionStore' ] , cookies , state ) ;
4426+ return originalSave . call (
4427+ authClient [ "transactionStore" ] ,
4428+ cookies ,
4429+ state
4430+ ) ;
44194431 } ) ;
44204432
44214433 await authClient . startInteractiveLogin ( { returnTo : unsafeReturnTo } ) ;
4422-
4423- expect ( authClient [ ' transactionStore' ] . save ) . toHaveBeenCalled ( ) ;
4434+
4435+ expect ( authClient [ " transactionStore" ] . save ) . toHaveBeenCalled ( ) ;
44244436 } ) ;
44254437
44264438 it ( "should pass authorization parameters to the authorization URL" , async ( ) => {
@@ -4429,10 +4441,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
44294441 audience : "https://api.example.com" ,
44304442 scope : "openid profile email custom_scope"
44314443 } ;
4432-
4444+
44334445 // Spy on the authorizationUrl method to verify the passed params
4434- const originalAuthorizationUrl = authClient [ ' authorizationUrl' ] ;
4435- authClient [ ' authorizationUrl' ] = vi . fn ( async ( params ) => {
4446+ const originalAuthorizationUrl = authClient [ " authorizationUrl" ] ;
4447+ authClient [ " authorizationUrl" ] = vi . fn ( async ( params ) => {
44364448 // Verify the audience is set correctly
44374449 expect ( params . get ( "audience" ) ) . toBe ( authorizationParameters . audience ) ;
44384450 // Verify the scope is set correctly
@@ -4441,8 +4453,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
44414453 } ) ;
44424454
44434455 await authClient . startInteractiveLogin ( { authorizationParameters } ) ;
4444-
4445- expect ( authClient [ ' authorizationUrl' ] ) . toHaveBeenCalled ( ) ;
4456+
4457+ expect ( authClient [ " authorizationUrl" ] ) . toHaveBeenCalled ( ) ;
44464458 } ) ;
44474459
44484460 it ( "should handle pushed authorization requests (PAR) correctly" , async ( ) => {
@@ -4452,11 +4464,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
44524464 parRequestCalled = true ;
44534465 }
44544466 } ) ;
4455-
4467+
44564468 const secret = await generateSecret ( 32 ) ;
44574469 const transactionStore = new TransactionStore ( { secret } ) ;
44584470 const sessionStore = new StatelessSessionStore ( { secret } ) ;
4459-
4471+
44604472 const authClient = new AuthClient ( {
44614473 transactionStore,
44624474 sessionStore,
@@ -4471,33 +4483,41 @@ ca/T0LLtgmbMmxSv/MmzIg==
44714483 } ,
44724484 fetch : mockFetch
44734485 } ) ;
4474-
4486+
44754487 await authClient . startInteractiveLogin ( ) ;
4476-
4488+
44774489 // Verify that PAR was used
44784490 expect ( parRequestCalled ) . toBe ( true ) ;
44794491 } ) ;
4480-
4492+
44814493 it ( "should save the transaction state with correct values" , async ( ) => {
44824494 const authClient = await createAuthClient ( ) ;
44834495 const returnTo = "/custom-path" ;
4484-
4496+
44854497 // Instead of mocking the oauth functions, we'll just check the structure of the transaction state
4486- const originalSave = authClient [ 'transactionStore' ] . save ;
4487- authClient [ 'transactionStore' ] . save = vi . fn ( async ( cookies , transactionState ) => {
4488- expect ( transactionState ) . toEqual ( expect . objectContaining ( {
4489- nonce : expect . any ( String ) ,
4490- codeVerifier : expect . any ( String ) ,
4491- responseType : "code" ,
4492- state : expect . any ( String ) ,
4493- returnTo : "https://example.com/custom-path"
4494- } ) ) ;
4495- return originalSave . call ( authClient [ 'transactionStore' ] , cookies , transactionState ) ;
4496- } ) ;
4498+ const originalSave = authClient [ "transactionStore" ] . save ;
4499+ authClient [ "transactionStore" ] . save = vi . fn (
4500+ async ( cookies , transactionState ) => {
4501+ expect ( transactionState ) . toEqual (
4502+ expect . objectContaining ( {
4503+ nonce : expect . any ( String ) ,
4504+ codeVerifier : expect . any ( String ) ,
4505+ responseType : "code" ,
4506+ state : expect . any ( String ) ,
4507+ returnTo : "https://example.com/custom-path"
4508+ } )
4509+ ) ;
4510+ return originalSave . call (
4511+ authClient [ "transactionStore" ] ,
4512+ cookies ,
4513+ transactionState
4514+ ) ;
4515+ }
4516+ ) ;
44974517
44984518 await authClient . startInteractiveLogin ( { returnTo } ) ;
4499-
4500- expect ( authClient [ ' transactionStore' ] . save ) . toHaveBeenCalled ( ) ;
4519+
4520+ expect ( authClient [ " transactionStore" ] . save ) . toHaveBeenCalled ( ) ;
45014521 } ) ;
45024522
45034523 it ( "should merge configuration authorizationParameters with method arguments" , async ( ) => {
@@ -4509,13 +4529,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
45094529 audience : configAudience
45104530 }
45114531 } ) ;
4512-
4532+
45134533 const methodScope = "openid profile email custom_scope" ;
45144534 const methodAudience = "https://custom-api.example.com" ;
4515-
4535+
45164536 // Spy on the authorizationUrl method to verify the passed params
4517- const originalAuthorizationUrl = authClient [ ' authorizationUrl' ] ;
4518- authClient [ ' authorizationUrl' ] = vi . fn ( async ( params ) => {
4537+ const originalAuthorizationUrl = authClient [ " authorizationUrl" ] ;
4538+ authClient [ " authorizationUrl" ] = vi . fn ( async ( params ) => {
45194539 // Method's authorization parameters should override config
45204540 expect ( params . get ( "audience" ) ) . toBe ( methodAudience ) ;
45214541 expect ( params . get ( "scope" ) ) . toBe ( methodScope ) ;
@@ -4528,14 +4548,14 @@ ca/T0LLtgmbMmxSv/MmzIg==
45284548 audience : methodAudience
45294549 }
45304550 } ) ;
4531-
4532- expect ( authClient [ ' authorizationUrl' ] ) . toHaveBeenCalled ( ) ;
4551+
4552+ expect ( authClient [ " authorizationUrl" ] ) . toHaveBeenCalled ( ) ;
45334553 } ) ;
45344554
45354555 // Add tests for handleLogin method
45364556 it ( "should create correct options in handleLogin with returnTo parameter" , async ( ) => {
45374557 const authClient = await createAuthClient ( ) ;
4538-
4558+
45394559 // Mock startInteractiveLogin to check what options are passed to it
45404560 const originalStartInteractiveLogin = authClient . startInteractiveLogin ;
45414561 authClient . startInteractiveLogin = vi . fn ( async ( options ) => {
@@ -4546,19 +4566,21 @@ ca/T0LLtgmbMmxSv/MmzIg==
45464566 return originalStartInteractiveLogin . call ( authClient , options ) ;
45474567 } ) ;
45484568
4549- const reqUrl = new URL ( "https://example.com/auth/login?foo=bar&returnTo=custom-return" ) ;
4569+ const reqUrl = new URL (
4570+ "https://example.com/auth/login?foo=bar&returnTo=custom-return"
4571+ ) ;
45504572 const req = new NextRequest ( reqUrl , { method : "GET" } ) ;
4551-
4573+
45524574 await authClient . handleLogin ( req ) ;
4553-
4575+
45544576 expect ( authClient . startInteractiveLogin ) . toHaveBeenCalled ( ) ;
45554577 } ) ;
45564578
45574579 it ( "should handle PAR correctly in handleLogin by not forwarding params" , async ( ) => {
45584580 const authClient = await createAuthClient ( {
45594581 pushedAuthorizationRequests : true
45604582 } ) ;
4561-
4583+
45624584 // Mock startInteractiveLogin to check what options are passed to it
45634585 const originalStartInteractiveLogin = authClient . startInteractiveLogin ;
45644586 authClient . startInteractiveLogin = vi . fn ( async ( options ) => {
@@ -4569,11 +4591,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
45694591 return originalStartInteractiveLogin . call ( authClient , options ) ;
45704592 } ) ;
45714593
4572- const reqUrl = new URL ( "https://example.com/auth/login?foo=bar&returnTo=custom-return" ) ;
4594+ const reqUrl = new URL (
4595+ "https://example.com/auth/login?foo=bar&returnTo=custom-return"
4596+ ) ;
45734597 const req = new NextRequest ( reqUrl , { method : "GET" } ) ;
4574-
4598+
45754599 await authClient . handleLogin ( req ) ;
4576-
4600+
45774601 expect ( authClient . startInteractiveLogin ) . toHaveBeenCalled ( ) ;
45784602 } ) ;
45794603 } ) ;
0 commit comments