Skip to content

Commit

Permalink
TST - convergeance and reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jan 25, 2024
1 parent 6d7e38d commit fd5f517
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/domain/entities/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export interface CustomRequest extends Request {

export interface ChangeCredentialsModel {
user_id: number;
password: string;
password?: string;
new_password: string;
password_hash?: string;
reset_password_code?: string | null;
reset_password_token?: string | null;
}
export interface ResetCredentialsModel {
new_password: string;
reset_password_token: string | null;
}
4 changes: 2 additions & 2 deletions src/domain/interfaces/use-cases/auth/reset-password.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeCredentialsModel } from "../../../entities/auth";
import { ResetCredentialsModel } from "../../../entities/auth";
export interface ResetPasswordUseCase {
execute(credentials: ChangeCredentialsModel): Promise<void>;
execute(credentials: ResetCredentialsModel): Promise<void>;
}
4 changes: 2 additions & 2 deletions src/domain/use-cases/auth/change-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export class ChangePassword implements ChangePasswordUseCase {
nb_of_updated_user = await this.userRepository.changePassword(credentials)
if (nb_of_updated_user == 0) throw new Error("Can't change password");
} else if (current_user.user_id == credentials.user_id) {
// Check if new password is different from old password
if (credentials.password === credentials.new_password) throw new Error("New password must be different from old password");
// Check if new password exist and is different from old password
if (!credentials.password || credentials.password === credentials.new_password) throw new Error("New password must be different from old password");
// Check if old password is correct
const verifyed = await this.userRepository.verifyUserLogin({ email: current_user.email, password: credentials.password })
if (verifyed) {
Expand Down
8 changes: 5 additions & 3 deletions src/domain/use-cases/auth/reset-password.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserRepository } from "../../interfaces/repositories/user-repository";
import { ResetPasswordUseCase } from "../../interfaces/use-cases/auth/reset-password";
import { ChangeCredentialsModel, DecodedToken } from "../../entities/auth";
import { DecodedToken, ResetCredentialsModel } from "../../entities/auth";
import { UserResponseModel } from "../../entities/user";

export class ResetPassword implements ResetPasswordUseCase {
Expand All @@ -9,15 +9,15 @@ export class ResetPassword implements ResetPasswordUseCase {
constructor(userRepository: UserRepository) {
this.userRepository = userRepository
}
async execute(credentials: ChangeCredentialsModel): Promise<void> {
async execute(credentials: ResetCredentialsModel): Promise<void> {
let nb_of_updated_user: number = 0
let decoded_token: DecodedToken | null = null

//is the user reset_password_token valid ?
if (credentials.reset_password_token) {
decoded_token = this.userRepository.verifyResetPasswordToken(credentials.reset_password_token)
if (!decoded_token) throw new Error("Token is not valid");
} else throw new Error("Token is not valid");
} else throw new Error("No token provided");

// does the user bind to reset_password_code exist ?
const preexistant_user: UserResponseModel | null = await this.userRepository.getUser(
Expand All @@ -35,5 +35,7 @@ export class ResetPassword implements ResetPasswordUseCase {
// change the password
nb_of_updated_user = await this.userRepository.changePassword({ ...preexistant_user, ...credentials })
if (nb_of_updated_user == 0) throw new Error("Can't change password");
//if (updatedUser.reset_password_code !== null) throw new Error("User reset password code not reset");

}
}
1 change: 1 addition & 0 deletions src/presentation/routers/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export default function AuthRouter(
} catch (err) {
console.log(err)
if (err.message === "Token is not valid") res.status(401).send({ errors: ["Can't change password"] })
if (err.message === "No token provided") res.status(401).send({ errors: ["Can't change password"] })
if (err.message === "User does not exist or token is not valid") res.status(404).send({ errors: ["Can't change password"] })
if (err.message === "User email is not validated") res.status(403).send({ errors: ["Can't change password"] })
else res.status(500).send({ errors: ["Can't reset password"] })
Expand Down
4 changes: 2 additions & 2 deletions test/domain/repositories/user-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ describe("User Repository", () => {
const result = await userRepository.changePassword(credentials);

expect(mockBcryptAdapter.hash).toHaveBeenCalledWith("new_password")
expect(mockUserDataSource.updateOne).toHaveBeenCalledWith({ user_id: 1, password_hash: "$2b$12$mMHjmPmUFsTrYFa3WUEVs.T1vaMz4q55FTfgpB.rNiL4GTt85BRkW" })
expect(mockUserDataSource.updateOne).toHaveBeenCalledWith({ user_id: 1, password_hash: "$2b$12$mMHjmPmUFsTrYFa3WUEVs.T1vaMz4q55FTfgpB.rNiL4GTt85BRkW", reset_password_code: null })
expect(result).toBe(1)

});
Expand All @@ -530,7 +530,7 @@ describe("User Repository", () => {
const result = await userRepository.changePassword(credentials);

expect(mockBcryptAdapter.hash).toHaveBeenCalledWith("new_password")
expect(mockUserDataSource.updateOne).toHaveBeenCalledWith({ user_id: 1, password_hash: "$2b$12$mMHjmPmUFsTrYFa3WUEVs.T1vaMz4q55FTfgpB.rNiL4GTt85BRkW" })
expect(mockUserDataSource.updateOne).toHaveBeenCalledWith({ user_id: 1, password_hash: "$2b$12$mMHjmPmUFsTrYFa3WUEVs.T1vaMz4q55FTfgpB.rNiL4GTt85BRkW", reset_password_code: null })
expect(result).toBe(0)

});
Expand Down
2 changes: 1 addition & 1 deletion test/domain/use-cases/auth/reset-password-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { DecodedToken } from "../../../../src/domain/entities/auth";
import { UserRequestModel, UserResponseModel } from "../../../../src/domain/entities/user";
import { UserRepository } from "../../../../src/domain/interfaces/repositories/user-repository";
import { ResetPasswordRequest } from '../../../../src/domain/use-cases/auth/reset-password-request'
import { Transporter } from "nodemailer";

import { Transporter } from "nodemailer";
import { NodemailerAdapter } from '../../../../src/infra/mailer/nodemailer'
import { MailerWrapper } from "../../../../src/infra/mailer/nodemailer-wrapper";

Expand Down
Loading

0 comments on commit fd5f517

Please sign in to comment.