Skip to content

Commit

Permalink
feat(tests): Add test for forget password flow with multiple password…
Browse files Browse the repository at this point in the history
… resets, including test for isUsed flag reset
  • Loading branch information
sanjaysah101 committed Sep 16, 2024
1 parent b70dfea commit db08e82
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/api/v1/auth/__test__/reset-password.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ const user = {
};

describe('Reset Password', () => {
let verifiedOTPResponse: Response;

beforeAll(async () => {
const response = await signUp(user);
expectSignUpSuccess(response);
Expand All @@ -51,33 +49,39 @@ describe('Reset Password', () => {
expectBadRequestResponseForValidationError(res);
});

it('should verify OTP successfully', async () => {
const res = await forgetPassword(user.email);
expectForgetPasswordSuccess(res);
// Running these test twice to ensure that after user can successfully reset password
// should reset password again after isUsed flag is reset
for (let i = 0; i < 2; i++) {
let verifiedOTPResponse: Response;

const userResponse = await findUserByUsername(user.username);
expectFindUserByUsernameSuccess(userResponse, user);
const userDetails: GetUser = userResponse.body.user;
it('should verify OTP successfully', async () => {
const res = await forgetPassword(user.email);
expectForgetPasswordSuccess(res);

const otpData = await retrieveOTP(userDetails.id, 'sendForgetPasswordOTP');
verifiedOTPResponse = await verifyOTP(otpData, user.email);
expectOTPVerificationSuccess(verifiedOTPResponse);
});
const userResponse = await findUserByUsername(user.username);
expectFindUserByUsernameSuccess(userResponse, user);
const userDetails: GetUser = userResponse.body.user;

it('should reset password successfully', async () => {
const { token } = verifiedOTPResponse.body;
const otpData = await retrieveOTP(userDetails.id, 'sendForgetPasswordOTP');
verifiedOTPResponse = await verifyOTP(otpData, user.email);
expectOTPVerificationSuccess(verifiedOTPResponse);
});

const res = await resetPassword({ token, password: 'ValidPassword123@', confirmPassword: 'ValidPassword123@' });
expectResetPasswordSuccess(res);
});
it('should reset password successfully', async () => {
const { token } = verifiedOTPResponse.body;

it('should not login with old password', async () => {
const res = await login(user);
expectLoginFailed(res);
});
const res = await resetPassword({ token, password: 'ValidPassword123@', confirmPassword: 'ValidPassword123@' });
expectResetPasswordSuccess(res);
});

it('should login with new password', async () => {
const res = await login({ ...user, password: 'ValidPassword123@' });
expectLoginSuccess(res);
});
it('should not login with old password', async () => {
const res = await login(user);
expectLoginFailed(res);
});

it('should login with new password', async () => {
const res = await login({ ...user, password: 'ValidPassword123@' });
expectLoginSuccess(res);
});
}
});
1 change: 1 addition & 0 deletions src/api/v1/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class TokenService {
userId,
action,
token,
isUsed: false,
expiryTime: new Date(Date.now() + FIVE_MINUTES_IN_MS),
};

Expand Down
1 change: 1 addition & 0 deletions src/api/v1/token/token.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const createTokenSchema = tokenSchema.pick({
action: true,
token: true,
expiryTime: true,
isUsed: true,
});

export const updateTokenSchema = tokenSchema
Expand Down

0 comments on commit db08e82

Please sign in to comment.