diff --git a/packages/auth/__tests__/providers/cognito/signUp.test.ts b/packages/auth/__tests__/providers/cognito/signUp.test.ts index bccf0af0078..afcdfccb580 100644 --- a/packages/auth/__tests__/providers/cognito/signUp.test.ts +++ b/packages/auth/__tests__/providers/cognito/signUp.test.ts @@ -23,6 +23,8 @@ jest.mock( '../../../src/providers/cognito/utils/clients/CognitoIdentityProvider', ); +const userId = '1234567890'; + describe('signUp', () => { const { user1 } = authAPITestParams; // assert mocks @@ -32,81 +34,184 @@ describe('signUp', () => { setUpGetConfig(Amplify); }); - beforeEach(() => { - mockSignUp.mockResolvedValue(authAPITestParams.signUpHttpCallResult); - }); + describe('Happy Path Cases:', () => { + beforeEach(() => { + mockSignUp.mockResolvedValue(authAPITestParams.signUpHttpCallResult); + }); - afterEach(() => { - mockSignUp.mockReset(); - }); + afterEach(() => { + mockSignUp.mockReset(); + }); + + it('should call SignUp service client with correct params', async () => { + await signUp({ + username: user1.username, + password: user1.password, + options: { + userAttributes: { email: user1.email }, + }, + }); + expect(mockSignUp).toHaveBeenCalledWith( + { + region: 'us-west-2', + userAgentValue: expect.any(String), + }, + { + ClientMetadata: undefined, + Password: user1.password, + UserAttributes: [{ Name: 'email', Value: user1.email }], + Username: user1.username, + ValidationData: undefined, + ClientId: '111111-aaaaa-42d8-891d-ee81a1549398', + }, + ); + expect(mockSignUp).toHaveBeenCalledTimes(1); + }); - it('should call signUp and return a result', async () => { - const result = await signUp({ - username: user1.username, - password: user1.password, - options: { - userAttributes: { email: user1.email }, - }, + it('should return `CONFIRM_SIGN_UP` step when user isn`t confirmed yet', async () => { + const result = await signUp({ + username: user1.username, + password: user1.password, + options: { + userAttributes: { email: user1.email }, + }, + }); + expect(result).toEqual({ + isSignUpComplete: false, + nextStep: { + signUpStep: 'CONFIRM_SIGN_UP', + codeDeliveryDetails: { + destination: user1.email, + deliveryMedium: 'EMAIL', + attributeName: 'email', + }, + }, + userId, + }); }); - expect(result).toEqual({ - isSignUpComplete: false, - nextStep: { - signUpStep: 'CONFIRM_SIGN_UP', - codeDeliveryDetails: { - destination: user1.email, - deliveryMedium: 'EMAIL', - attributeName: 'email', + + it('should return `DONE` step when user is confirmed', async () => { + mockSignUp.mockResolvedValue({ + UserConfirmed: true, + UserSub: userId, + }); + const result = await signUp({ + username: user1.username, + password: user1.password, + options: { + userAttributes: { email: user1.email }, + }, + }); + expect(result).toEqual({ + isSignUpComplete: true, + nextStep: { + signUpStep: 'DONE', }, - }, - userId: '1234567890', + userId, + }); }); - expect(mockSignUp).toHaveBeenCalledWith( - { - region: 'us-west-2', - userAgentValue: expect.any(String), - }, - { - ClientMetadata: undefined, - Password: user1.password, - UserAttributes: [{ Name: 'email', Value: user1.email }], - Username: user1.username, - ValidationData: undefined, - ClientId: '111111-aaaaa-42d8-891d-ee81a1549398', - }, - ); - expect(mockSignUp).toHaveBeenCalledTimes(1); - }); - it('should throw an error when username is empty', async () => { - expect.assertions(2); - try { - await signUp({ username: '', password: user1.password }); - } catch (error: any) { - expect(error).toBeInstanceOf(AuthError); - expect(error.name).toBe(AuthValidationErrorCode.EmptySignUpUsername); - } - }); + it('should return `COMPLETE_AUTO_SIGN_IN` step with `isSignUpComplete` false when autoSignIn is enabled and user isn`t confirmed yet', async () => { + // set up signUpVerificationMethod as link in auth config + (Amplify.getConfig as any).mockReturnValue({ + Auth: { + Cognito: { + userPoolClientId: '111111-aaaaa-42d8-891d-ee81a1549398', + userPoolId: 'us-west-2_zzzzz', + identityPoolId: 'us-west-2:xxxxxx', + signUpVerificationMethod: 'link', + }, + }, + }); - it('should throw an error when password is empty', async () => { - expect.assertions(2); - try { - await signUp({ username: user1.username, password: '' }); - } catch (error: any) { - expect(error).toBeInstanceOf(AuthError); - expect(error.name).toBe(AuthValidationErrorCode.EmptySignUpPassword); - } + const result = await signUp({ + username: user1.username, + password: user1.password, + options: { + userAttributes: { email: user1.email }, + autoSignIn: true, + }, + }); + + expect(result).toEqual({ + isSignUpComplete: false, + nextStep: { + signUpStep: 'COMPLETE_AUTO_SIGN_IN', + codeDeliveryDetails: { + destination: user1.email, + deliveryMedium: 'EMAIL', + attributeName: 'email', + }, + }, + userId, + }); + }); + + it('should return `COMPLETE_AUTO_SIGN_IN` step with `isSignUpComplete` true when autoSignIn is enabled and user is confirmed', async () => { + mockSignUp.mockResolvedValue({ + UserConfirmed: true, + UserSub: userId, + }); + + const result = await signUp({ + username: user1.username, + password: user1.password, + options: { + userAttributes: { email: user1.email }, + autoSignIn: true, + }, + }); + + expect(result).toEqual({ + isSignUpComplete: true, + nextStep: { + signUpStep: 'COMPLETE_AUTO_SIGN_IN', + }, + userId, + }); + }); }); - it('should throw an error when service returns an error response', async () => { - expect.assertions(2); - mockSignUp.mockImplementation(() => { - throw getMockError(SignUpException.InvalidParameterException); + describe('Error Path Cases:', () => { + beforeEach(() => { + mockSignUp.mockResolvedValue(authAPITestParams.signUpHttpCallResult); + }); + + afterEach(() => { + mockSignUp.mockReset(); + }); + + it('should throw an error when username is empty', async () => { + expect.assertions(2); + try { + await signUp({ username: '', password: user1.password }); + } catch (error: any) { + expect(error).toBeInstanceOf(AuthError); + expect(error.name).toBe(AuthValidationErrorCode.EmptySignUpUsername); + } + }); + + it('should throw an error when password is empty', async () => { + expect.assertions(2); + try { + await signUp({ username: user1.username, password: '' }); + } catch (error: any) { + expect(error).toBeInstanceOf(AuthError); + expect(error.name).toBe(AuthValidationErrorCode.EmptySignUpPassword); + } + }); + + it('should throw an error when service returns an error response', async () => { + expect.assertions(2); + mockSignUp.mockImplementation(() => { + throw getMockError(SignUpException.InvalidParameterException); + }); + try { + await signUp({ username: user1.username, password: user1.password }); + } catch (error: any) { + expect(error).toBeInstanceOf(AuthError); + expect(error.name).toBe(SignUpException.InvalidParameterException); + } }); - try { - await signUp({ username: user1.username, password: user1.password }); - } catch (error: any) { - expect(error).toBeInstanceOf(AuthError); - expect(error.name).toBe(SignUpException.InvalidParameterException); - } }); }); diff --git a/packages/auth/src/providers/cognito/apis/signUp.ts b/packages/auth/src/providers/cognito/apis/signUp.ts index aff6ac83028..69aca1b528a 100644 --- a/packages/auth/src/providers/cognito/apis/signUp.ts +++ b/packages/auth/src/providers/cognito/apis/signUp.ts @@ -96,6 +96,7 @@ export async function signUp(input: SignUpInput): Promise { nextStep: { signUpStep: 'COMPLETE_AUTO_SIGN_IN', }, + userId: UserSub, }; } else if (isSignUpComplete(clientOutput) && !isAutoSignInStarted()) { return { @@ -103,6 +104,7 @@ export async function signUp(input: SignUpInput): Promise { nextStep: { signUpStep: 'DONE', }, + userId: UserSub, }; } else if ( !isSignUpComplete(clientOutput) &&