Skip to content

Commit

Permalink
10007: Fixing tests, update initiateAuth command that was missed in c…
Browse files Browse the repository at this point in the history
…hange password interactor
  • Loading branch information
rachelschneiderman committed Mar 1, 2024
1 parent bf24e30 commit 8f01ddb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 79 deletions.
93 changes: 36 additions & 57 deletions web-api/src/business/useCases/auth/changePasswordInteractor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
AuthFlowType,
ChallengeNameType,
CodeMismatchException,
ExpiredCodeException,
Expand Down Expand Up @@ -66,7 +65,7 @@ describe('changePasswordInteractor', () => {
};

applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockResolvedValue(mockInitiateAuthResponse);

applicationContext
Expand All @@ -85,7 +84,7 @@ describe('changePasswordInteractor', () => {
AuthenticationResult: {},
};
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockResolvedValue(mockInitiateAuthResponse);

await expect(
Expand All @@ -97,16 +96,12 @@ describe('changePasswordInteractor', () => {
}),
).rejects.toThrow('User is not in `FORCE_CHANGE_PASSWORD` state');

expect(applicationContext.getCognito().initiateAuth).toHaveBeenCalledWith(
{
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
PASSWORD: mockPassword,
USERNAME: mockEmail,
},
ClientId: applicationContext.environment.cognitoClientId,
},
);
expect(
applicationContext.getUserGateway().initiateAuth,
).toHaveBeenCalledWith(applicationContext, {
email: mockEmail,
password: mockPassword,
});
});

it('should update the user`s password in persistence when they are in NEW_PASSWORD_REQUIRED state and their change password request is valid', async () => {
Expand Down Expand Up @@ -279,7 +274,7 @@ describe('changePasswordInteractor', () => {
});

applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockResolvedValue(mockInitiateAuthResponse);
});

Expand Down Expand Up @@ -314,16 +309,12 @@ describe('changePasswordInteractor', () => {
Password: mockPassword,
Username: mockEmail,
});
expect(applicationContext.getCognito().initiateAuth).toHaveBeenCalledWith(
{
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
PASSWORD: mockPassword,
USERNAME: mockEmail,
},
ClientId: applicationContext.environment.cognitoClientId,
},
);
expect(
applicationContext.getUserGateway().initiateAuth,
).toHaveBeenCalledWith(applicationContext, {
email: mockEmail,
password: mockPassword,
});
expect(result).toEqual({
accessToken: mockToken,
idToken: mockToken,
Expand All @@ -332,7 +323,7 @@ describe('changePasswordInteractor', () => {
});

it('should throw an error if initiate auth does not return the correct tokens', async () => {
applicationContext.getCognito().initiateAuth.mockResolvedValue({});
applicationContext.getUserGateway().initiateAuth.mockResolvedValue({});

await expect(
changePasswordInteractor(applicationContext, {
Expand All @@ -343,21 +334,17 @@ describe('changePasswordInteractor', () => {
}),
).rejects.toThrow(`Unable to change password for email: ${mockEmail}`);

expect(applicationContext.getCognito().initiateAuth).toHaveBeenCalledWith(
{
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
PASSWORD: mockPassword,
USERNAME: mockEmail,
},
ClientId: applicationContext.environment.cognitoClientId,
},
);
expect(
applicationContext.getUserGateway().initiateAuth,
).toHaveBeenCalledWith(applicationContext, {
email: mockEmail,
password: mockPassword,
});
});

it('should throw an InvalidRequest error if initiateAuth returns a CodeMismatchException', async () => {
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValueOnce(
new CodeMismatchException({ $metadata: {}, message: '' }),
);
Expand All @@ -371,21 +358,17 @@ describe('changePasswordInteractor', () => {
}),
).rejects.toThrow('Forgot password code is expired or incorrect');

expect(applicationContext.getCognito().initiateAuth).toHaveBeenCalledWith(
{
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
PASSWORD: mockPassword,
USERNAME: mockEmail,
},
ClientId: applicationContext.environment.cognitoClientId,
},
);
expect(
applicationContext.getUserGateway().initiateAuth,
).toHaveBeenCalledWith(applicationContext, {
email: mockEmail,
password: mockPassword,
});
});

it('should throw an InvalidRequest error if initiateAuth returns a ExpiredCodeException', async () => {
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValueOnce(
new ExpiredCodeException({ $metadata: {}, message: '' }),
);
Expand All @@ -399,16 +382,12 @@ describe('changePasswordInteractor', () => {
}),
).rejects.toThrow('Forgot password code is expired or incorrect');

expect(applicationContext.getCognito().initiateAuth).toHaveBeenCalledWith(
{
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
PASSWORD: mockPassword,
USERNAME: mockEmail,
},
ClientId: applicationContext.environment.cognitoClientId,
},
);
expect(
applicationContext.getUserGateway().initiateAuth,
).toHaveBeenCalledWith(applicationContext, {
email: mockEmail,
password: mockPassword,
});
});
});
});
17 changes: 5 additions & 12 deletions web-api/src/business/useCases/auth/changePasswordInteractor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
AuthFlowType,
ChallengeNameType,
} from '@aws-sdk/client-cognito-identity-provider';
import { ChallengeNameType } from '@aws-sdk/client-cognito-identity-provider';
import { ChangePasswordForm } from '@shared/business/entities/ChangePasswordForm';
import { InvalidEntityError, NotFoundError } from '@web-api/errors/errors';
import { MESSAGE_TYPES } from '@web-api/gateways/worker/workerRouter';
Expand Down Expand Up @@ -47,14 +44,10 @@ export const changePasswordInteractor = async (

if (tempPassword) {
const initiateAuthResult = await applicationContext
.getCognito()
.initiateAuth({
AuthFlow: AuthFlowType.USER_PASSWORD_AUTH,
AuthParameters: {
PASSWORD: tempPassword,
USERNAME: email,
},
ClientId: applicationContext.environment.cognitoClientId,
.getUserGateway()
.initiateAuth(applicationContext, {
email,
password: tempPassword,
});

if (
Expand Down
20 changes: 10 additions & 10 deletions web-api/src/business/useCases/auth/loginInteractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('loginInteractor', () => {
ChallengeName: ChallengeNameType.NEW_PASSWORD_REQUIRED,
};
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockResolvedValue(mockNewPasswordRequiredResponse);

await expect(
Expand All @@ -41,7 +41,7 @@ describe('loginInteractor', () => {
message: '',
});
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValue(mockWrongEmailOrPasswordError);

await expect(
Expand All @@ -60,7 +60,7 @@ describe('loginInteractor', () => {
message: 'Password attempts exceeded',
});
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValue(mockTooManyAttemptsError);

await expect(
Expand All @@ -79,7 +79,7 @@ describe('loginInteractor', () => {
message: '',
});
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValue(mockWrongEmailOrPasswordError);

await expect(
Expand All @@ -97,7 +97,7 @@ describe('loginInteractor', () => {
'Totally unexpected, unhandled error.',
);
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValue(mockWrongEmailOrPasswordError);

await expect(
Expand All @@ -108,11 +108,11 @@ describe('loginInteractor', () => {
).rejects.toThrow(mockWrongEmailOrPasswordError);
});

it('should throw an error if initiateAuth does not return access, id, and refresh tokens', async () => {
it('should throw an error when initiateAuth does not return access, id, and refresh tokens', async () => {
const mockEmail = '[email protected]';
const mockPassword = 'MyPa$Sword!';
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockResolvedValue({ AuthenticationResult: {} });

await expect(
Expand All @@ -131,7 +131,7 @@ describe('loginInteractor', () => {
message: '',
});
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValue(mockWrongEmailOrPasswordError);
applicationContext.getUserGateway().getUserByEmail.mockResolvedValue({
email: mockEmail,
Expand All @@ -154,7 +154,7 @@ describe('loginInteractor', () => {
message: '',
});
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockRejectedValue(mockWrongEmailOrPasswordError);
applicationContext
.getUserGateway()
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('loginInteractor', () => {
},
};
applicationContext
.getCognito()
.getUserGateway()
.initiateAuth.mockResolvedValue(mockSuccessFullLoginResponse);

const result = await loginInteractor(applicationContext, {
Expand Down

0 comments on commit 8f01ddb

Please sign in to comment.