@@ -29,7 +29,7 @@ const authConfig = {
29
29
30
30
// getCurrentUser is mocked so Hub is able to dispatch a mocked AuthUser
31
31
// before returning an `AuthSignInResult`
32
- const mockedGetCurrentUser = getCurrentUser as jest . Mock ;
32
+ const mockedGetCurrentUser = jest . mocked ( getCurrentUser ) ;
33
33
34
34
describe ( 'confirmSignIn API happy path cases' , ( ) => {
35
35
let handleChallengeNameSpy : jest . SpyInstance ;
@@ -706,3 +706,245 @@ describe('Cognito ASF', () => {
706
706
) ;
707
707
} ) ;
708
708
} ) ;
709
+
710
+ describe ( 'confirmSignIn MFA_SETUP challenge happy path cases' , ( ) => {
711
+ const { username, password } = authAPITestParams . user1 ;
712
+
713
+ test ( 'confirmSignIn with multiple MFA_SETUP options using SOFTWARE_TOKEN_MFA' , async ( ) => {
714
+ Amplify . configure ( {
715
+ Auth : authConfig ,
716
+ } ) ;
717
+ jest
718
+ . spyOn ( signInHelpers , 'handleUserSRPAuthFlow' )
719
+ . mockImplementationOnce (
720
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
721
+ authAPITestParams . RespondToAuthChallengeMultipleMfaSetupOutput ,
722
+ ) ;
723
+
724
+ const result = await signIn ( { username, password } ) ;
725
+
726
+ expect ( result . isSignedIn ) . toBe ( false ) ;
727
+ expect ( result . nextStep . signInStep ) . toBe (
728
+ 'CONTINUE_SIGN_IN_WITH_MFA_SETUP_SELECTION' ,
729
+ ) ;
730
+
731
+ jest . spyOn ( clients , 'associateSoftwareToken' ) . mockResolvedValueOnce ( {
732
+ SecretCode : 'secret-code' ,
733
+ Session : '12341234' ,
734
+ $metadata : { } ,
735
+ } ) ;
736
+
737
+ const selectMfaToSetupConfirmSignInResult = await confirmSignIn ( {
738
+ challengeResponse : 'TOTP' ,
739
+ } ) ;
740
+
741
+ expect ( selectMfaToSetupConfirmSignInResult . isSignedIn ) . toBe ( false ) ;
742
+ expect ( selectMfaToSetupConfirmSignInResult . nextStep . signInStep ) . toBe (
743
+ 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP' ,
744
+ ) ;
745
+
746
+ const verifySoftwareTokenSpy = jest
747
+ . spyOn ( clients , 'verifySoftwareToken' )
748
+ . mockResolvedValueOnce ( {
749
+ Session : '12341234' ,
750
+ Status : 'SUCCESS' ,
751
+ $metadata : { } ,
752
+ } ) ;
753
+
754
+ jest
755
+ . spyOn ( clients , 'respondToAuthChallenge' )
756
+ . mockImplementationOnce (
757
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
758
+ authAPITestParams . RespondToAuthChallengeCommandOutput ,
759
+ ) ;
760
+
761
+ const totpCode = '123456' ;
762
+ const confirmSignInResult = await confirmSignIn ( {
763
+ challengeResponse : totpCode ,
764
+ } ) ;
765
+
766
+ expect ( verifySoftwareTokenSpy ) . toHaveBeenCalledWith (
767
+ expect . objectContaining ( {
768
+ region : 'us-west-2' ,
769
+ } ) ,
770
+ expect . objectContaining ( {
771
+ UserCode : totpCode ,
772
+ Session : '12341234' ,
773
+ } ) ,
774
+ ) ;
775
+ expect ( confirmSignInResult . isSignedIn ) . toBe ( true ) ;
776
+ expect ( confirmSignInResult . nextStep . signInStep ) . toBe ( 'DONE' ) ;
777
+ } ) ;
778
+
779
+ test ( 'confirmSignIn with multiple MFA_SETUP options using EMAIL_OTP' , async ( ) => {
780
+ Amplify . configure ( {
781
+ Auth : authConfig ,
782
+ } ) ;
783
+
784
+ jest
785
+ . spyOn ( signInHelpers , 'handleUserSRPAuthFlow' )
786
+ . mockImplementationOnce (
787
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
788
+ authAPITestParams . RespondToAuthChallengeMultipleMfaSetupOutput ,
789
+ ) ;
790
+
791
+ const result = await signIn ( { username, password } ) ;
792
+
793
+ expect ( result . isSignedIn ) . toBe ( false ) ;
794
+ expect ( result . nextStep . signInStep ) . toBe (
795
+ 'CONTINUE_SIGN_IN_WITH_MFA_SETUP_SELECTION' ,
796
+ ) ;
797
+
798
+ const selectMfaToSetupConfirmSignInResult = await confirmSignIn ( {
799
+ challengeResponse : 'EMAIL' ,
800
+ } ) ;
801
+
802
+ expect ( selectMfaToSetupConfirmSignInResult . isSignedIn ) . toBe ( false ) ;
803
+ expect ( selectMfaToSetupConfirmSignInResult . nextStep . signInStep ) . toBe (
804
+ 'CONTINUE_SIGN_IN_WITH_EMAIL_SETUP' ,
805
+ ) ;
806
+
807
+ jest . spyOn ( signInHelpers , 'handleChallengeName' ) . mockImplementationOnce (
808
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > => ( {
809
+ ChallengeName : 'EMAIL_OTP' ,
810
+ Session : '1234234232' ,
811
+ $metadata : { } ,
812
+ ChallengeParameters : {
813
+ CODE_DELIVERY_DELIVERY_MEDIUM : 'EMAIL' ,
814
+ CODE_DELIVERY_DESTINATION : 'j***@a***' ,
815
+ } ,
816
+ } ) ,
817
+ ) ;
818
+
819
+ const setupEmailConfirmSignInResult = await confirmSignIn ( {
820
+ challengeResponse : 'j***@a***' ,
821
+ } ) ;
822
+
823
+ expect ( setupEmailConfirmSignInResult . nextStep . signInStep ) . toBe (
824
+ 'CONFIRM_SIGN_IN_WITH_EMAIL_CODE' ,
825
+ ) ;
826
+
827
+ jest
828
+ . spyOn ( clients , 'respondToAuthChallenge' )
829
+ . mockImplementationOnce (
830
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
831
+ authAPITestParams . RespondToAuthChallengeCommandOutput ,
832
+ ) ;
833
+
834
+ const confirmSignInResult = await confirmSignIn ( {
835
+ challengeResponse : '123456' ,
836
+ } ) ;
837
+
838
+ expect ( confirmSignInResult . isSignedIn ) . toBe ( true ) ;
839
+ expect ( confirmSignInResult . nextStep . signInStep ) . toBe ( 'DONE' ) ;
840
+ } ) ;
841
+
842
+ test ( 'confirmSignIn with single MFA_SETUP option using EMAIL_OTP' , async ( ) => {
843
+ Amplify . configure ( {
844
+ Auth : authConfig ,
845
+ } ) ;
846
+
847
+ jest
848
+ . spyOn ( signInHelpers , 'handleUserSRPAuthFlow' )
849
+ . mockImplementationOnce (
850
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
851
+ authAPITestParams . RespondToAuthChallengeEmailMfaSetupOutput ,
852
+ ) ;
853
+
854
+ const result = await signIn ( { username, password } ) ;
855
+
856
+ expect ( result . isSignedIn ) . toBe ( false ) ;
857
+ expect ( result . nextStep . signInStep ) . toBe (
858
+ 'CONTINUE_SIGN_IN_WITH_EMAIL_SETUP' ,
859
+ ) ;
860
+
861
+ jest . spyOn ( signInHelpers , 'handleChallengeName' ) . mockImplementationOnce (
862
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > => ( {
863
+ ChallengeName : 'EMAIL_OTP' ,
864
+ Session : '1234234232' ,
865
+ $metadata : { } ,
866
+ ChallengeParameters : {
867
+ CODE_DELIVERY_DELIVERY_MEDIUM : 'EMAIL' ,
868
+ CODE_DELIVERY_DESTINATION : 'j***@a***' ,
869
+ } ,
870
+ } ) ,
871
+ ) ;
872
+
873
+ const setupEmailConfirmSignInResult = await confirmSignIn ( {
874
+ challengeResponse : 'j***@a***' ,
875
+ } ) ;
876
+
877
+ expect ( setupEmailConfirmSignInResult . nextStep . signInStep ) . toBe (
878
+ 'CONFIRM_SIGN_IN_WITH_EMAIL_CODE' ,
879
+ ) ;
880
+
881
+ jest
882
+ . spyOn ( signInHelpers , 'handleChallengeName' )
883
+ . mockImplementationOnce (
884
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
885
+ authAPITestParams . RespondToAuthChallengeCommandOutput ,
886
+ ) ;
887
+
888
+ const confirmSignInResult = await confirmSignIn ( {
889
+ challengeResponse : '123456' ,
890
+ } ) ;
891
+
892
+ expect ( confirmSignInResult . isSignedIn ) . toBe ( true ) ;
893
+ expect ( confirmSignInResult . nextStep . signInStep ) . toBe ( 'DONE' ) ;
894
+ } ) ;
895
+
896
+ test ( 'confirmSignIn with single MFA_SETUP option using SOFTWARE_TOKEN_MFA' , async ( ) => {
897
+ Amplify . configure ( {
898
+ Auth : authConfig ,
899
+ } ) ;
900
+ jest
901
+ . spyOn ( signInHelpers , 'handleUserSRPAuthFlow' )
902
+ . mockImplementationOnce (
903
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
904
+ authAPITestParams . RespondToAuthChallengeTotpMfaSetupOutput ,
905
+ ) ;
906
+
907
+ jest . spyOn ( clients , 'associateSoftwareToken' ) . mockResolvedValueOnce ( {
908
+ SecretCode : 'secret-code' ,
909
+ Session : '12341234' ,
910
+ $metadata : { } ,
911
+ } ) ;
912
+
913
+ const result = await signIn ( { username, password } ) ;
914
+
915
+ expect ( result . isSignedIn ) . toBe ( false ) ;
916
+ expect ( result . nextStep . signInStep ) . toBe ( 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP' ) ;
917
+
918
+ const verifySoftwareTokenSpy = jest
919
+ . spyOn ( clients , 'verifySoftwareToken' )
920
+ . mockResolvedValueOnce ( {
921
+ Session : '12341234' ,
922
+ Status : 'SUCCESS' ,
923
+ $metadata : { } ,
924
+ } ) ;
925
+
926
+ jest
927
+ . spyOn ( clients , 'respondToAuthChallenge' )
928
+ . mockImplementationOnce (
929
+ async ( ) : Promise < RespondToAuthChallengeCommandOutput > =>
930
+ authAPITestParams . RespondToAuthChallengeCommandOutput ,
931
+ ) ;
932
+
933
+ const totpCode = '123456' ;
934
+ const confirmSignInResult = await confirmSignIn ( {
935
+ challengeResponse : totpCode ,
936
+ } ) ;
937
+
938
+ expect ( verifySoftwareTokenSpy ) . toHaveBeenCalledWith (
939
+ expect . objectContaining ( {
940
+ region : 'us-west-2' ,
941
+ } ) ,
942
+ expect . objectContaining ( {
943
+ UserCode : totpCode ,
944
+ Session : '12341234' ,
945
+ } ) ,
946
+ ) ;
947
+ expect ( confirmSignInResult . isSignedIn ) . toBe ( true ) ;
948
+ expect ( confirmSignInResult . nextStep . signInStep ) . toBe ( 'DONE' ) ;
949
+ } ) ;
950
+ } ) ;
0 commit comments