Skip to content

Commit

Permalink
TST - Delete user convergeance
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jan 30, 2024
1 parent 983d484 commit c168ead
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 8 deletions.
24 changes: 24 additions & 0 deletions test/domain/use-cases/auth/change-password.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { ChangePassword } from '../../../../src/domain/use-cases/auth/change-pas

describe("Change password Use Case", () => {
class MockUserRepository implements UserRepository {
deleteUser(): Promise<number> {
throw new Error("Method not implemented.");
}
isDeleted(): Promise<boolean> {
throw new Error("Method not implemented.");
}
generateResetPasswordToken(): string {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -95,13 +101,15 @@ describe("Change password Use Case", () => {
}

jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1))

const changePasswordUseCase = new ChangePassword(mockUserRepository)
try {
await changePasswordUseCase.execute(current_user, credentials);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).toBeCalledWith({ email: current_user.email, password: credentials.password });
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -136,6 +144,7 @@ describe("Change password Use Case", () => {


jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0))

Expand All @@ -146,6 +155,7 @@ describe("Change password Use Case", () => {
} catch (err) {
expect(err).toStrictEqual(expectedResponse);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(2);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).not.toBeCalled();
}
Expand Down Expand Up @@ -178,6 +188,7 @@ describe("Change password Use Case", () => {


jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0))

Expand All @@ -188,6 +199,7 @@ describe("Change password Use Case", () => {
} catch (err) {
expect(err).toStrictEqual(expectedResponse);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).not.toBeCalled();
}
Expand Down Expand Up @@ -220,6 +232,7 @@ describe("Change password Use Case", () => {


jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0))

Expand All @@ -230,6 +243,7 @@ describe("Change password Use Case", () => {
} catch (err) {
expect(err).toStrictEqual(expectedResponse);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).toBeCalledWith({ email: current_user.email, password: credentials.password });
expect(mockUserRepository.changePassword).not.toBeCalled();
}
Expand Down Expand Up @@ -261,6 +275,7 @@ describe("Change password Use Case", () => {
const expectedResponse = new Error("Can't change password");

jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0))

Expand All @@ -271,6 +286,7 @@ describe("Change password Use Case", () => {
} catch (err) {
expect(err).toStrictEqual(expectedResponse);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).toBeCalledWith({ email: current_user.email, password: credentials.password });
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
}
Expand Down Expand Up @@ -302,13 +318,15 @@ describe("Change password Use Case", () => {
}

jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1))

const changePasswordUseCase = new ChangePassword(mockUserRepository)
try {
await changePasswordUseCase.execute(current_user, credentials);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -341,13 +359,15 @@ describe("Change password Use Case", () => {
}

jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1))

const changePasswordUseCase = new ChangePassword(mockUserRepository)
try {
await changePasswordUseCase.execute(current_user, credentials);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(2);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -380,13 +400,15 @@ describe("Change password Use Case", () => {
}

jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1))

const changePasswordUseCase = new ChangePassword(mockUserRepository)
try {
await changePasswordUseCase.execute(current_user, credentials);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -419,6 +441,7 @@ describe("Change password Use Case", () => {
const expectedResponse = new Error("Can't change password");

jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0))

Expand All @@ -429,6 +452,7 @@ describe("Change password Use Case", () => {
} catch (err) {
expect(err).toStrictEqual(expectedResponse);
expect(mockUserRepository.isAdmin).toBeCalledWith(1);
expect(mockUserRepository.isDeleted).toBeCalledWith(1);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
}
Expand Down
9 changes: 9 additions & 0 deletions test/domain/use-cases/auth/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { AuthJwtResponseModel, AuthUserCredentialsModel, DecodedToken } from "..

describe("Create User Use Case", () => {
class MockUserRepository implements UserRepository {
deleteUser(): Promise<number> {
throw new Error("Method not implemented.");
}
isDeleted(): Promise<boolean> {
throw new Error("Method not implemented.");
}
generateResetPasswordToken(): string {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -94,6 +100,7 @@ describe("Create User Use Case", () => {

jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(OutputUserData))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" })
jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" })
const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository)
Expand Down Expand Up @@ -121,6 +128,7 @@ describe("Create User Use Case", () => {

jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(OutputUserData))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" })
jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" })
const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository)
Expand Down Expand Up @@ -153,6 +161,7 @@ describe("Create User Use Case", () => {

jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(OutputUserData))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" })
jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" })
const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository)
Expand Down
10 changes: 10 additions & 0 deletions test/domain/use-cases/auth/refresh-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { AuthJwtRefreshedResponseModel, DecodedToken } from "../../../../src/dom

describe("Create User Use Case", () => {
class MockUserRepository implements UserRepository {
deleteUser(): Promise<number> {
throw new Error("Method not implemented.");
}
isDeleted(): Promise<boolean> {
throw new Error("Method not implemented.");
}
generateResetPasswordToken(): string {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -106,6 +112,8 @@ describe("Create User Use Case", () => {

jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "refreshed_token" })
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(OutputUserData))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))

const loginUserUseCase = new RefreshToken(mockUserRepository, mockAuthRepository)
const result = await loginUserUseCase.execute(InputUserData);
expect(result).toStrictEqual(OutputAuthData);
Expand All @@ -130,6 +138,8 @@ describe("Create User Use Case", () => {

jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "refreshed_token" })
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))

const loginUserUseCase = new RefreshToken(mockUserRepository, mockAuthRepository)
try {
const result = await loginUserUseCase.execute(InputUserData);
Expand Down
14 changes: 14 additions & 0 deletions test/domain/use-cases/auth/reset-password-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { MailerWrapper } from "../../../../src/infra/mailer/nodemailer-wrapper";

describe("Change password Use Case", () => {
class MockUserRepository implements UserRepository {
deleteUser(): Promise<number> {
throw new Error("Method not implemented.");
}
isDeleted(): Promise<boolean> {
throw new Error("Method not implemented.");
}
generateResetPasswordToken(): string {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -128,6 +134,7 @@ describe("Change password Use Case", () => {
const generated_token = "token"

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(goten_user_1)).mockImplementationOnce(() => Promise.resolve(goten_user_2))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "setResetPasswordCode").mockImplementation(() => Promise.resolve(1))
jest.spyOn(mockUserRepository, "generateResetPasswordToken").mockImplementation(() => { return generated_token })
jest.spyOn(mockMailerAdapter, "send_reset_password_email").mockImplementation(() => Promise.resolve())
Expand All @@ -137,6 +144,7 @@ describe("Change password Use Case", () => {
await reset_password_request.execute(InputData);

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.setResetPasswordCode).toHaveBeenCalledWith({ user_id: 1 });
expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 });
expect(mockUserRepository.generateResetPasswordToken).toHaveBeenCalledWith(goten_user_2);
Expand Down Expand Up @@ -194,6 +202,7 @@ describe("Change password Use Case", () => {
const generated_token = "token"

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(goten_user_1)).mockImplementationOnce(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "setResetPasswordCode").mockImplementation(() => Promise.resolve(0))
jest.spyOn(mockUserRepository, "generateResetPasswordToken").mockImplementation(() => { return generated_token })
jest.spyOn(mockMailerAdapter, "send_reset_password_email").mockImplementation(() => Promise.resolve())
Expand All @@ -206,6 +215,7 @@ describe("Change password Use Case", () => {
}

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.setResetPasswordCode).not.toHaveBeenCalled();
expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1);
expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled();
Expand Down Expand Up @@ -234,6 +244,7 @@ describe("Change password Use Case", () => {
const generated_token = "token"

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(goten_user_1)).mockImplementationOnce(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "setResetPasswordCode").mockImplementation(() => Promise.resolve(0))
jest.spyOn(mockUserRepository, "generateResetPasswordToken").mockImplementation(() => { return generated_token })
jest.spyOn(mockMailerAdapter, "send_reset_password_email").mockImplementation(() => Promise.resolve())
Expand All @@ -246,6 +257,7 @@ describe("Change password Use Case", () => {
}

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.setResetPasswordCode).toHaveBeenCalledWith({ user_id: 1 });
expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1);
expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled();
Expand Down Expand Up @@ -274,6 +286,7 @@ describe("Change password Use Case", () => {
const generated_token = "token"

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(goten_user_1)).mockImplementationOnce(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "setResetPasswordCode").mockImplementation(() => Promise.resolve(1))
jest.spyOn(mockUserRepository, "generateResetPasswordToken").mockImplementation(() => { return generated_token })
jest.spyOn(mockMailerAdapter, "send_reset_password_email").mockImplementation(() => Promise.resolve())
Expand All @@ -286,6 +299,7 @@ describe("Change password Use Case", () => {
}

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.setResetPasswordCode).toHaveBeenCalledWith({ user_id: 1 });
expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 });
expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled();
Expand Down
Loading

0 comments on commit c168ead

Please sign in to comment.