@@ -30,7 +30,8 @@ import {
3030} from '../constants' ;
3131
3232import { DEFAULT_AUTH0_CLIENT } from '../../src/constants' ;
33- import { GenericError } from '../../src' ;
33+ import { Auth0Client , GenericError } from '../../src' ;
34+ import { CompleteResponse } from '../../src/MyAccountApiClient' ;
3435
3536jest . mock ( 'es-cookie' ) ;
3637jest . mock ( '../../src/jwt' ) ;
@@ -522,4 +523,94 @@ describe('Auth0Client', () => {
522523 ) ;
523524 } ) ;
524525 } ) ;
526+
527+ describe ( 'handleRedirectCallback with connect_code' , ( ) => {
528+ let client : Auth0Client ;
529+ let myAccountApi : any ;
530+ let url : URL ;
531+ let completeResponse : CompleteResponse ;
532+ let transaction : any ;
533+
534+ beforeEach ( ( ) => {
535+ url = new URL ( 'https://example.com/callback' ) ;
536+ client = new Auth0Client ( { domain : 'test' , clientId : 'abc' , authorizationParams : { } } ) ;
537+ transaction = {
538+ state : 'state123' ,
539+ code_verifier : 'verifier' ,
540+ auth_session : 'session' ,
541+ redirect_uri : 'uri' ,
542+ appState : { foo : 'bar' }
543+ } ;
544+ completeResponse = {
545+ id : 'account_123' ,
546+ connection : 'google-oauth2' ,
547+ access_type : 'offline' ,
548+ scopes : [ 'email' , 'profile' ] ,
549+ created_at : '2024-06-01T12:00:00Z' ,
550+ expires_at : '2025-06-01T12:00:00Z'
551+ } ;
552+ myAccountApi = {
553+ completeAccount : jest . fn ( ) . mockResolvedValue ( completeResponse )
554+ } ;
555+ ( client as any ) . myAccountApi = myAccountApi ;
556+ ( client as any ) . connectAccountTransactionManager = {
557+ get : jest . fn ( ) ,
558+ remove : jest . fn ( )
559+ } ;
560+ } ) ;
561+
562+ it ( 'returns appState and data on success' , async ( ) => {
563+ ( client as any ) . connectAccountTransactionManager . get . mockReturnValue ( transaction ) ;
564+
565+ url . searchParams . set ( 'state' , 'state123' ) ;
566+ url . searchParams . set ( 'connect_code' , 'code' ) ;
567+
568+ const result = await client . handleRedirectCallback ( url . toString ( ) ) ;
569+
570+ expect ( myAccountApi . completeAccount ) . toHaveBeenCalledWith ( {
571+ auth_session : 'session' ,
572+ connect_code : 'code' ,
573+ redirect_uri : 'uri' ,
574+ code_verifier : 'verifier'
575+ } ) ;
576+ expect ( result ) . toEqual ( { appState : { foo : 'bar' } , ...completeResponse } ) ;
577+ expect ( ( client as any ) . connectAccountTransactionManager . remove ) . toHaveBeenCalled ( ) ;
578+ } ) ;
579+
580+ it ( 'throws GenericError if transaction is missing' , async ( ) => {
581+ ( client as any ) . connectAccountTransactionManager . get . mockReturnValue ( undefined ) ;
582+ url . searchParams . set ( 'state' , 'state123' ) ;
583+ url . searchParams . set ( 'connect_code' , 'code' ) ;
584+ await expect ( client . handleRedirectCallback ( url . toString ( ) ) ) . rejects . toThrow ( GenericError ) ;
585+ } ) ;
586+
587+ it ( 'throws GenericError if error is present' , async ( ) => {
588+ ( client as any ) . connectAccountTransactionManager . get . mockReturnValue ( transaction ) ;
589+
590+ url . searchParams . set ( 'error' , 'err' ) ;
591+ url . searchParams . set ( 'error_description' , 'desc' ) ;
592+ url . searchParams . set ( 'state' , 'state123' ) ;
593+ await expect ( client . handleRedirectCallback ( url . toString ( ) ) ) . rejects . toThrow ( GenericError ) ;
594+ expect ( ( client as any ) . connectAccountTransactionManager . remove ) . toHaveBeenCalled ( ) ;
595+ } ) ;
596+
597+ it ( 'throws GenericError on state mismatch' , async ( ) => {
598+ ( client as any ) . connectAccountTransactionManager . get . mockReturnValue ( transaction ) ;
599+
600+ url . searchParams . set ( 'state' , 'wrong-state' ) ;
601+ url . searchParams . set ( 'connect_code' , 'code' ) ;
602+ await expect ( client . handleRedirectCallback ( url . toString ( ) ) ) . rejects . toThrow ( GenericError ) ;
603+ } ) ;
604+
605+ it ( 'throws MyAccountApiError if completeAccount fails' , async ( ) => {
606+ ( client as any ) . connectAccountTransactionManager . get . mockReturnValue ( transaction ) ;
607+ const apiError = new Error ( 'API error' ) ;
608+ myAccountApi . completeAccount . mockRejectedValue ( apiError ) ;
609+
610+ url . searchParams . set ( 'state' , 'state123' ) ;
611+ url . searchParams . set ( 'connect_code' , 'code' ) ;
612+ await expect ( client . handleRedirectCallback ( url . toString ( ) ) ) . rejects . toThrow ( 'API error' ) ;
613+ expect ( ( client as any ) . connectAccountTransactionManager . remove ) . toHaveBeenCalled ( ) ;
614+ } ) ;
615+ } ) ;
525616} ) ;
0 commit comments