import { ManagementClient } from "clients/management" ; import { chance } from "_/helpers" ; import { mockResetVerifyRequest } from "_/mocks/common" ; import { mockCognitoAdminConfirmSignUpWithError , mockCognitoAdminConfirmSignUpWithSuccess , mockCognitoAdminDeleteUserWithError , mockCognitoAdminDeleteUserWithSuccess , mockCognitoAdminSetUserPasswordWithError , mockCognitoAdminSetUserPasswordWithSuccess , mockCognitoAdminUpdateUserAttributesWithError , mockCognitoAdminUpdateUserAttributesWithSuccess , restoreCognitoAdminConfirmSignUp , restoreCognitoAdminDeleteUser , restoreCognitoAdminSetUserPassword , restoreCognitoAdminUpdateUserAttributes } from "_/mocks/vendor/aws-sdk" ; describe ( "clients/management" , ( ) => { describe ( "ManagementClient" , ( ) => { const username = chance . string ( ) ; describe ( "#verifyUser" , ( ) => { afterEach ( ( ) => { restoreCognitoAdminConfirmSignUp ( ) ; restoreCognitoAdminUpdateUserAttributes ( ) ; } ) ; it ( "should resolve with no result" , async ( ) => { mockCognitoAdminConfirmSignUpWithSuccess ( ) ; mockCognitoAdminUpdateUserAttributesWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; expect ( await mgmt . verifyUser ( username ) ) . toBeUndefined ( ) ; } ) ; it ( "should confirm user registration" , async ( ) => { const adminConfirmSignUpMock = mockCognitoAdminConfirmSignUpWithSuccess ( ) ; mockCognitoAdminUpdateUserAttributesWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; await mgmt . verifyUser ( username ) ; expect ( adminConfirmSignUpMock ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { Username : username } ) ) ; } ) ; it ( "should verify user email address" , async ( ) => { mockCognitoAdminConfirmSignUpWithSuccess ( ) ; const adminUpdateUserAttributesMock = mockCognitoAdminUpdateUserAttributesWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; await mgmt . verifyUser ( username ) ; expect ( adminUpdateUserAttributesMock ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { Username : username } ) ) ; } ) ; it ( "should reject on AWS Cognito error (confirm sign up)" , async ( done ) => { const errMock = new Error ( ) ; const adminConfirmSignUpMock = mockCognitoAdminConfirmSignUpWithError ( errMock ) ; mockCognitoAdminUpdateUserAttributesWithSuccess ( ) ; try { const mgmt = new ManagementClient ( ) ; await mgmt . verifyUser ( username ) ; done . fail ( ) ; } catch ( err ) { expect ( adminConfirmSignUpMock ) . toHaveBeenCalled ( ) ; expect ( err ) . toBe ( errMock ) ; done ( ) ; } } ) ; it ( "should reject on AWS Cognito error (update attributes)" , async ( done ) => { const errMock = new Error ( ) ; mockCognitoAdminConfirmSignUpWithSuccess ( ) ; const adminUpdateUserAttributesMock = mockCognitoAdminUpdateUserAttributesWithError ( errMock ) ; try { const mgmt = new ManagementClient ( ) ; await mgmt . verifyUser ( username ) ; done . fail ( ) ; } catch ( err ) { expect ( adminUpdateUserAttributesMock ) . toHaveBeenCalled ( ) ; expect ( err ) . toBe ( errMock ) ; done ( ) ; } } ) ; } ) ; describe ( "#deleteUser" , ( ) => { afterEach ( ( ) => { restoreCognitoAdminDeleteUser ( ) ; } ) ; it ( "should resolve with no result" , async ( ) => { mockCognitoAdminDeleteUserWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; expect ( await mgmt . deleteUser ( username ) ) . toBeUndefined ( ) ; } ) ; it ( "should delete user" , async ( ) => { const adminDeleteUserMock = mockCognitoAdminDeleteUserWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; expect ( await mgmt . deleteUser ( username ) ) . toBeUndefined ( ) ; expect ( adminDeleteUserMock ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { Username : username } ) ) ; } ) ; it ( "should reject on AWS Cognito error" , async ( done ) => { const errMock = new Error ( ) ; const adminDeleteUserMock = mockCognitoAdminDeleteUserWithError ( errMock ) ; try { const mgmt = new ManagementClient ( ) ; await mgmt . deleteUser ( username ) ; done . fail ( ) ; } catch ( err ) { expect ( adminDeleteUserMock ) . toHaveBeenCalled ( ) ; expect ( err ) . toBe ( errMock ) ; done ( ) ; } } ) ; } ) ; describe ( "#changePassword" , ( ) => { const { password } = mockResetVerifyRequest ( ) ; afterEach ( ( ) => { restoreCognitoAdminSetUserPassword ( ) ; } ) ; it ( "should resolve with no result" , async ( ) => { mockCognitoAdminSetUserPasswordWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; expect ( await mgmt . changePassword ( username , password ) ) . toBeUndefined ( ) ; } ) ; it ( "should set new user password" , async ( ) => { const adminSetUserPasswordMock = mockCognitoAdminSetUserPasswordWithSuccess ( ) ; const mgmt = new ManagementClient ( ) ; await mgmt . changePassword ( username , password ) ; expect ( adminSetUserPasswordMock ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { Username : username , Password : password , Permanent : true } ) ) ; } ) ; it ( "should reject on AWS Cognito error (get user)" , async ( done ) => { const errMock = new Error ( ) ; const adminSetUserPasswordMock = mockCognitoAdminSetUserPasswordWithError ( errMock ) ; try { const mgmt = new ManagementClient ( ) ; await mgmt . changePassword ( username , password ) ; done . fail ( ) ; } catch ( err ) { expect ( adminSetUserPasswordMock ) . toHaveBeenCalled ( ) ; expect ( err ) . toBe ( errMock ) ; done ( ) ; } } ) ; } ) ; } ) ; } ) ;