From 471c67b79159ce85911efa22671be93aed78822e Mon Sep 17 00:00:00 2001 From: juliecoust Date: Wed, 10 Jul 2024 17:05:38 +0200 Subject: [PATCH] TST convergeance DONE --- .../sqlite/sqlite-project-data-source.test.ts | 6 +- .../repositories/project-repository.test.ts | 4 +- .../use-cases/auth/change-password.test.ts | 56 ++++++------ test/domain/use-cases/auth/login.test.ts | 12 +-- .../use-cases/auth/refresh-token.test.ts | 8 +- .../auth/reset-password-request.test.ts | 29 +++--- .../use-cases/project/create-project.test.ts | 15 ++-- .../domain/use-cases/user/create-user.test.ts | 88 +++---------------- test/entities/project.ts | 26 ++++++ test/mocks/user-mock.ts | 49 ++++++++++- .../middleware/user-validation.test.ts | 73 ++------------- .../routes/project-router.test.ts | 6 +- test/presentation/routes/user-router.test.ts | 40 ++------- 13 files changed, 171 insertions(+), 241 deletions(-) diff --git a/test/data/data-sources/sqlite/sqlite-project-data-source.test.ts b/test/data/data-sources/sqlite/sqlite-project-data-source.test.ts index 70ab9be..740a1e9 100644 --- a/test/data/data-sources/sqlite/sqlite-project-data-source.test.ts +++ b/test/data/data-sources/sqlite/sqlite-project-data-source.test.ts @@ -1,9 +1,10 @@ import { SQLiteProjectDataSource } from '../../../../src/data/data-sources/sqlite/sqlite-project-data-source' +import { SQLiteInstrumentModelDataSource } from '../../../../src/data/data-sources/sqlite/sqlite-instrument_model-data-source' import sqlite3 from 'sqlite3' import 'dotenv/config' import { ProjectRequestCreationModel, ProjectUpdateModel } from '../../../../src/domain/entities/project'; import fs from 'fs'; -import { data_source_projectRequestCreationModel_2, data_source_projectRequestCreationModel_3, data_source_projectRequestCreationModel_4, data_source_projectRequestCreationModel_5, data_source_projectRequestCreationModel_6, projectRequestCreationModel_3, projectUpdateModel } from '../../../entities/project'; +import { data_source_projectRequestCreationModel_2, data_source_projectRequestCreationModel_3, data_source_projectRequestCreationModel_4, data_source_projectRequestCreationModel_5, data_source_projectRequestCreationModel_6, privateProjectUpdateModel, projectRequestCreationModel_3 } from '../../../entities/project'; const config = { TEST_DBSOURCE: process.env.TEST_DBSOURCE || '', @@ -17,6 +18,7 @@ function initializeProjectDB() { throw err } }); + new SQLiteInstrumentModelDataSource(db) return new SQLiteProjectDataSource(db) } @@ -206,7 +208,7 @@ describe('SQLiteProjectDataSource', () => { expect(dataSource).toBeDefined(); }); test('should update a project', async () => { - const project_to_update: ProjectUpdateModel = projectUpdateModel + const project_to_update: ProjectUpdateModel = privateProjectUpdateModel // Call the updateOne method const updated = await dataSource.updateOne(project_to_update); diff --git a/test/domain/repositories/project-repository.test.ts b/test/domain/repositories/project-repository.test.ts index 293301e..d9299a6 100644 --- a/test/domain/repositories/project-repository.test.ts +++ b/test/domain/repositories/project-repository.test.ts @@ -4,7 +4,7 @@ import { ProjectRequestCreationModel, ProjectRequestModel, ProjectResponseModel, import { SearchResult } from "../../../src/domain/entities/search"; import { ProjectRepository } from "../../../src/domain/interfaces/repositories/project-repository"; import { ProjectRepositoryImpl } from "../../../src/domain/repositories/project-repository"; -import { projectRequestCreationModelForRepository, projectResponseModel, projectResponseModelArray, projectUpdateModel, projectUpdateModel_withBadData } from "../../entities/project"; +import { privateProjectUpdateModel, projectRequestCreationModelForRepository, projectResponseModel, projectResponseModelArray, projectUpdateModel_withBadData } from "../../entities/project"; import { MockProjectDataSource } from "../../mocks/project-mock"; import 'dotenv/config' @@ -86,7 +86,7 @@ describe("Project Repository", () => { describe("UpdateProject", () => { //TODO test("Should update a project", async () => { - const project: ProjectUpdateModel = projectUpdateModel + const project: ProjectUpdateModel = privateProjectUpdateModel jest.spyOn(mockProjectDataSource, 'updateOne').mockResolvedValue(1) diff --git a/test/domain/use-cases/auth/change-password.test.ts b/test/domain/use-cases/auth/change-password.test.ts index 39584a3..75b45c9 100644 --- a/test/domain/use-cases/auth/change-password.test.ts +++ b/test/domain/use-cases/auth/change-password.test.ts @@ -47,11 +47,11 @@ describe("Change password Use Case", () => { password: "good_current_password", new_password: "new_password" } - const expectedResponse = new Error("User is deleted"); + const expectedResponse = new Error("User cannot be used"); jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementationOnce(() => Promise.resolve(true)).mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -62,7 +62,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).not.toBeCalled(); - expect(mockUserRepository.isDeleted).toBeCalledTimes(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(1); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).not.toBeCalled(); } @@ -90,11 +90,13 @@ describe("Change password Use Case", () => { password: "good_current_password", new_password: "new_password" } - const expectedResponse = new Error("User is deleted"); + const expectedResponse = new Error("User cannot be used"); jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementationOnce(() => Promise.resolve(false)).mockImplementationOnce(() => Promise.resolve(true)) + // jest.spyOn(mockUserRepository, "isDeleted").mockImplementationOnce(() => Promise.resolve(false)).mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) + jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -105,7 +107,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).not.toBeCalled(); - expect(mockUserRepository.isDeleted).toBeCalledTimes(2); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(1); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).not.toBeCalled(); } @@ -136,20 +138,20 @@ describe("Change password Use Case", () => { } jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) 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) { expect(true).toBe(false) } + expect(mockUserRepository.isAdmin).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); + expect(mockUserRepository.verifyUserLogin).toBeCalledWith({ email: current_user.email, password: credentials.password }); + expect(mockUserRepository.changePassword).toBeCalledWith(credentials); }); @@ -179,7 +181,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -190,7 +192,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(2); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).not.toBeCalled(); } @@ -223,7 +225,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -234,7 +236,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).not.toBeCalled(); } @@ -267,7 +269,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -278,7 +280,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).toBeCalledWith({ email: current_user.email, password: credentials.password }); expect(mockUserRepository.changePassword).not.toBeCalled(); } @@ -310,7 +312,7 @@ describe("Change password Use Case", () => { const expectedResponse = new Error("Cannot change password"); jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(false)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(true)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -321,7 +323,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).toBeCalledWith({ email: current_user.email, password: credentials.password }); expect(mockUserRepository.changePassword).toBeCalledWith(credentials); } @@ -353,7 +355,7 @@ describe("Change password Use Case", () => { } jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1)) @@ -361,7 +363,7 @@ describe("Change password Use Case", () => { try { await changePasswordUseCase.execute(current_user, credentials); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).toBeCalledWith(credentials); } catch (err) { @@ -394,7 +396,7 @@ describe("Change password Use Case", () => { } jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1)) @@ -402,7 +404,7 @@ describe("Change password Use Case", () => { try { await changePasswordUseCase.execute(current_user, credentials); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(2); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).toBeCalledWith(credentials); } catch (err) { @@ -435,7 +437,7 @@ describe("Change password Use Case", () => { } jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1)) @@ -443,7 +445,7 @@ describe("Change password Use Case", () => { try { await changePasswordUseCase.execute(current_user, credentials); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).toBeCalledWith(credentials); } catch (err) { @@ -476,7 +478,7 @@ describe("Change password Use Case", () => { const expectedResponse = new Error("Cannot change password"); jest.spyOn(mockUserRepository, "isAdmin").mockImplementation(() => Promise.resolve(true)) - jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockUserRepository, "verifyUserLogin").mockImplementation(() => Promise.resolve(false)) jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(0)) @@ -487,7 +489,7 @@ describe("Change password Use Case", () => { } catch (err) { expect(err).toStrictEqual(expectedResponse); expect(mockUserRepository.isAdmin).toBeCalledWith(1); - expect(mockUserRepository.isDeleted).toBeCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toBeCalledTimes(2); expect(mockUserRepository.verifyUserLogin).not.toBeCalled(); expect(mockUserRepository.changePassword).toBeCalledWith(credentials); } diff --git a/test/domain/use-cases/auth/login.test.ts b/test/domain/use-cases/auth/login.test.ts index e755448..999ba50 100644 --- a/test/domain/use-cases/auth/login.test.ts +++ b/test/domain/use-cases/auth/login.test.ts @@ -48,7 +48,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(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" }) jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" }) const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository) @@ -76,7 +76,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(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" }) jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" }) const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository) @@ -109,7 +109,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(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" }) jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" }) const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository) @@ -118,7 +118,7 @@ describe("Create User Use Case", () => { // Should not go there expect(result).toBe(true); } catch (err) { - expect(err.message).toBe("User email not verified"); + expect(err.message).toBe("User cannot be used"); } }); @@ -162,7 +162,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(true)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) jest.spyOn(mockAuthRepository, "generateAccessToken").mockImplementation(() => { return "access_token" }) jest.spyOn(mockAuthRepository, "generateRefreshToken").mockImplementation(() => { return "refresh_token" }) const loginUserUseCase = new LoginUser(mockUserRepository, mockAuthRepository) @@ -171,7 +171,7 @@ describe("Create User Use Case", () => { // Should not go there expect(result).toBe(true); } catch (err) { - expect(err.message).toBe("User is deleted"); + expect(err.message).toBe("User cannot be used"); } }); }) diff --git a/test/domain/use-cases/auth/refresh-token.test.ts b/test/domain/use-cases/auth/refresh-token.test.ts index 028eba4..1b7a6ba 100644 --- a/test/domain/use-cases/auth/refresh-token.test.ts +++ b/test/domain/use-cases/auth/refresh-token.test.ts @@ -60,7 +60,7 @@ 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)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) const loginUserUseCase = new RefreshToken(mockUserRepository, mockAuthRepository) const result = await loginUserUseCase.execute(InputUserData); @@ -86,7 +86,7 @@ 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)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed") const loginUserUseCase = new RefreshToken(mockUserRepository, mockAuthRepository) try { @@ -128,14 +128,14 @@ 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(true)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) const loginUserUseCase = new RefreshToken(mockUserRepository, mockAuthRepository) try { const result = await loginUserUseCase.execute(InputUserData); expect(result).toBe(true); } catch (err) { - expect(err.message).toBe("User is deleted"); + expect(err.message).toBe("User cannot be used"); } }); }) diff --git a/test/domain/use-cases/auth/reset-password-request.test.ts b/test/domain/use-cases/auth/reset-password-request.test.ts index e525c22..a2dd106 100644 --- a/test/domain/use-cases/auth/reset-password-request.test.ts +++ b/test/domain/use-cases/auth/reset-password-request.test.ts @@ -24,13 +24,13 @@ describe("Change password Use Case", () => { TEST_MAIL_AUTH_PASS: "your_password", TEST_MAIL_SENDER: "your@mail.com", TEST_PORT_LOCAL: 3000, - TEST_BASE_URL_LOCAL: "http://localhost:" - + TEST_BASE_URL_LOCAL: "http://localhost:", + TEST_NODE_ENV: "TEST" } beforeEach(async () => { jest.clearAllMocks(); mockUserRepository = new MockUserRepository() - mockMailerAdapter = new NodemailerAdapter((config.TEST_BASE_URL_LOCAL + config.TEST_PORT_LOCAL), config.TEST_MAIL_SENDER) + mockMailerAdapter = new NodemailerAdapter((config.TEST_BASE_URL_LOCAL + config.TEST_PORT_LOCAL), config.TEST_MAIL_SENDER, config.TEST_NODE_ENV) mockTransporter = await mockMailerAdapter.createTransport({ host: config.TEST_MAIL_HOST, port: config.TEST_MAIL_PORT, @@ -80,7 +80,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) 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()) @@ -90,7 +90,7 @@ describe("Change password Use Case", () => { await reset_password_request.execute(InputData); expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); - expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(goten_user_1.user_id); expect(mockUserRepository.setResetPasswordCode).toHaveBeenCalledWith({ user_id: 1 }); expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); expect(mockUserRepository.generateResetPasswordToken).toHaveBeenCalledWith(goten_user_2); @@ -147,7 +147,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(true)) + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) 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()) @@ -156,11 +156,10 @@ describe("Change password Use Case", () => { const reset_password_request = new ResetPasswordRequest(mockUserRepository, mockTransporter, mockMailerAdapter) try { await reset_password_request.execute(InputData); } catch (err) { - expect(err.message).toBe("User is deleted") + expect(err.message).toBe("User cannot be used") } expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); - expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1); expect(mockUserRepository.setResetPasswordCode).not.toHaveBeenCalled(); expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1); expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled(); @@ -189,7 +188,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used"))) 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()) @@ -198,11 +197,11 @@ describe("Change password Use Case", () => { const reset_password_request = new ResetPasswordRequest(mockUserRepository, mockTransporter, mockMailerAdapter) try { await reset_password_request.execute(InputData); } catch (err) { - expect(err.message).toBe("User email is not validated") + expect(err.message).toBe("User cannot be used") } expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); - expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(goten_user_1.user_id); expect(mockUserRepository.setResetPasswordCode).not.toHaveBeenCalled(); expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1); expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled(); @@ -231,7 +230,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) 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()) @@ -244,7 +243,7 @@ describe("Change password Use Case", () => { } expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); - expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(goten_user_1.user_id); expect(mockUserRepository.setResetPasswordCode).toHaveBeenCalledWith({ user_id: 1 }); expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1); expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled(); @@ -273,7 +272,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve()) 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()) @@ -286,7 +285,7 @@ describe("Change password Use Case", () => { } expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); - expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(goten_user_1.user_id); expect(mockUserRepository.setResetPasswordCode).toHaveBeenCalledWith({ user_id: 1 }); expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); expect(mockUserRepository.generateResetPasswordToken).not.toHaveBeenCalled(); diff --git a/test/domain/use-cases/project/create-project.test.ts b/test/domain/use-cases/project/create-project.test.ts index 4157d51..9885ece 100644 --- a/test/domain/use-cases/project/create-project.test.ts +++ b/test/domain/use-cases/project/create-project.test.ts @@ -3,7 +3,7 @@ import { UserRepository } from "../../../../src/domain/interfaces/repositories/u import { CreateProject } from '../../../../src/domain/use-cases/project/create-project' import { MockUserRepository } from "../../../mocks/user-mock"; import { MockProjectRepository } from "../../../mocks/project-mock"; -import { projectRequestCreationModel, projectRequestCreationModelForRepository, projectRequestCreationModel_withmissingOverrideDepthOffset, projectResponseModel } from "../../../entities/project"; +import { privateProjectRequestCreationModel_withmissingOverrideDepthOffset, projectRequestCreationModel, projectRequestCreationModelForRepository, projectRequestCreationModel_withmissingOverrideDepthOffset, projectResponseModel } from "../../../entities/project"; import { ProjectResponseModel, PublicProjectRequestCreationModel } from "../../../../src/domain/entities/project"; import { MockInstrumentModelRepository } from "../../../mocks/instrumentModel-mock"; import { MockPrivilegeRepository } from "../../../mocks/privilege-mock"; @@ -58,7 +58,7 @@ test("Create a project without override_depth_offset", async () => { const current_user: UserUpdateModel = { user_id: 1 } - const completedInputData = { ...InputData, override_depth_offset: 1 } + const completedInputData = { ...privateProjectRequestCreationModel_withmissingOverrideDepthOffset, override_depth_offset: 1 } jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) jest.spyOn(mockProjectRepository, "computeDefaultDepthOffset").mockImplementation(() => 100) @@ -84,7 +84,7 @@ test("Create a project without override_depth_offset", async () => { expect(result).toStrictEqual(created_project); }); -test("Cannot find created project", async () => { +test("Cannot find the created project.", async () => { const InputData: PublicProjectRequestCreationModel = projectRequestCreationModel const current_user: UserUpdateModel = { user_id: 1 @@ -92,15 +92,20 @@ test("Cannot find created project", async () => { jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) jest.spyOn(mockProjectRepository, "computeDefaultDepthOffset") + jest.spyOn(mockInstrumentModelRepository, "getInstrumentByName").mockImplementation(() => Promise.resolve(instrument_model_response)) + jest.spyOn(mockUserRepository, "ensureTypedUserCanBeUsed").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "ensurePrivilegeCoherence").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "formatProjectRequestCreationModel").mockImplementation(() => projectRequestCreationModelForRepository) + jest.spyOn(mockProjectRepository, "createProject").mockImplementation(() => Promise.resolve(1)) jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(null)) const createProjectUseCase = new CreateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) - await expect(createProjectUseCase.execute(current_user, InputData)).rejects.toThrowError("Cannot find created project"); + await expect(createProjectUseCase.execute(current_user, InputData)).rejects.toThrowError("Cannot find the created project."); expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(current_user.user_id); expect(mockProjectRepository.computeDefaultDepthOffset).not.toBeCalled(); - expect(mockProjectRepository.createProject).toHaveBeenCalledWith(InputData); + expect(mockProjectRepository.createProject).toHaveBeenCalledWith(projectRequestCreationModelForRepository); expect(mockProjectRepository.getProject).toHaveBeenCalledWith({ project_id: 1 }); }); diff --git a/test/domain/use-cases/user/create-user.test.ts b/test/domain/use-cases/user/create-user.test.ts index 918d21d..8b1dd25 100644 --- a/test/domain/use-cases/user/create-user.test.ts +++ b/test/domain/use-cases/user/create-user.test.ts @@ -14,6 +14,7 @@ describe("Create User Use Case", () => { const config = { TEST_VALIDATION_TOKEN_SECRET: process.env.TEST_VALIDATION_TOKEN_SECRET || '', + TEST_NODE_ENV: process.env.TEST_NODE_ENV || '', TEST_MAIL_HOST: 'smtp.example.com', TEST_MAIL_PORT: 465, TEST_MAIL_SECURE: true, @@ -27,7 +28,7 @@ describe("Create User Use Case", () => { beforeEach(async () => { jest.clearAllMocks(); mockUserRepository = new MockUserRepository(); - mockMailerAdapter = new NodemailerAdapter((config.TEST_BASE_URL_LOCAL + config.TEST_PORT_LOCAL), config.TEST_MAIL_SENDER) + mockMailerAdapter = new NodemailerAdapter((config.TEST_BASE_URL_LOCAL + config.TEST_PORT_LOCAL), config.TEST_MAIL_SENDER, config.TEST_NODE_ENV); mockTransporter = await mockMailerAdapter.createTransport({ host: config.TEST_MAIL_HOST, port: config.TEST_MAIL_PORT, @@ -64,38 +65,26 @@ describe("Create User Use Case", () => { user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", user_creation_date: '2023-08-01 10:30:00' } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "France", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - } jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(null)).mockImplementationOnce(() => Promise.resolve(created_user)) jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(1)) jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(-1)) jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "token") jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve()) - jest.spyOn(mockUserRepository, "toPublicUser").mockImplementation(() => { return OutputData }) const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter) - const result = await createUserUseCase.execute(InputData); - - expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); - expect(mockUserRepository.createUser).toHaveBeenCalledWith(InputData); - expect(mockUserRepository.standardUpdateUser).not.toBeCalled(); - expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); - expect(mockUserRepository.generateValidationToken).toHaveBeenCalledWith(created_user); - expect(mockMailerAdapter.send_confirmation_email).toHaveBeenCalledWith(mockTransporter, created_user, "token"); - expect(mockUserRepository.toPublicUser).toHaveBeenCalledWith(created_user); + try { + await createUserUseCase.execute(InputData); - expect(result).toStrictEqual(OutputData); + expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); + expect(mockUserRepository.createUser).toHaveBeenCalledWith(InputData); + expect(mockUserRepository.standardUpdateUser).not.toBeCalled(); + expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); + expect(mockUserRepository.generateValidationToken).toHaveBeenCalledWith(created_user); + expect(mockMailerAdapter.send_confirmation_email).toHaveBeenCalledWith(mockTransporter, created_user, "token"); + } catch (err) { + expect(true).toBe(false); + } }); test("Try to add a user that already exist but is deleted", async () => { @@ -135,25 +124,12 @@ describe("Create User Use Case", () => { user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", user_creation_date: '2023-08-01 10:30:00' } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "France", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - } jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(preexistant_user)).mockImplementationOnce(() => Promise.resolve(updated_user)) jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(1)) jest.spyOn(mockUserRepository, "createUser") jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "token") jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve()) - jest.spyOn(mockUserRepository, "toPublicUser").mockImplementation(() => { return OutputData }) try { const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter) @@ -169,7 +145,6 @@ describe("Create User Use Case", () => { expect(mockUserRepository.standardUpdateUser).not.toBeCalled(); expect(mockUserRepository.generateValidationToken).not.toBeCalled(); expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled(); - expect(mockUserRepository.toPublicUser).not.toBeCalled(); } }); @@ -210,18 +185,6 @@ describe("Create User Use Case", () => { user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", user_creation_date: '2023-08-01 10:30:00' } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "France", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - } jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(preexistant_user)).mockImplementationOnce(() => Promise.resolve(updated_user)) jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(1)) @@ -229,10 +192,9 @@ describe("Create User Use Case", () => { jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1)) jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "token") jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve()) - jest.spyOn(mockUserRepository, "toPublicUser").mockImplementation(() => { return OutputData }) const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter) - const result = await createUserUseCase.execute(InputData); + await createUserUseCase.execute(InputData); expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "john@gmail.com" }); expect(mockUserRepository.createUser).not.toBeCalled(); @@ -240,9 +202,7 @@ describe("Create User Use Case", () => { expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); expect(mockUserRepository.generateValidationToken).toHaveBeenCalledWith(updated_user); expect(mockMailerAdapter.send_confirmation_email).toHaveBeenCalledWith(mockTransporter, updated_user, "token"); - expect(mockUserRepository.toPublicUser).toHaveBeenCalledWith(updated_user); - expect(result).toStrictEqual(OutputData); }); test("Try to add a user that already exist with validated email", async () => { @@ -277,7 +237,6 @@ describe("Create User Use Case", () => { jest.spyOn(mockUserRepository, "createUser") jest.spyOn(mockUserRepository, "generateValidationToken") jest.spyOn(mockMailerAdapter, "send_confirmation_email") - jest.spyOn(mockUserRepository, "toPublicUser") const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter) try { @@ -291,7 +250,6 @@ describe("Create User Use Case", () => { expect(mockUserRepository.standardUpdateUser).not.toBeCalled(); expect(mockUserRepository.generateValidationToken).not.toBeCalled(); expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled(); - expect(mockUserRepository.toPublicUser).not.toBeCalled(); expect(err).toStrictEqual(expectedResponse); } @@ -308,25 +266,12 @@ describe("Create User Use Case", () => { country: "France", user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "France", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - } jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(null)).mockImplementationOnce(() => Promise.resolve(null)) jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(1)) jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(-1)) jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "token") jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve()) - jest.spyOn(mockUserRepository, "toPublicUser").mockImplementation(() => { return OutputData }) try { const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter) @@ -341,7 +286,6 @@ describe("Create User Use Case", () => { expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); expect(mockUserRepository.generateValidationToken).not.toBeCalled(); expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled(); - expect(mockUserRepository.toPublicUser).not.toBeCalled(); } }); @@ -377,7 +321,6 @@ describe("Create User Use Case", () => { jest.spyOn(mockUserRepository, "createUser") jest.spyOn(mockUserRepository, "generateValidationToken") jest.spyOn(mockMailerAdapter, "send_confirmation_email") - jest.spyOn(mockUserRepository, "toPublicUser") const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter); try { @@ -390,7 +333,6 @@ describe("Create User Use Case", () => { expect(mockUserRepository.standardUpdateUser).toBeCalledTimes(1); expect(mockUserRepository.generateValidationToken).not.toBeCalled(); expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled(); - expect(mockUserRepository.toPublicUser).not.toBeCalled(); expect(err).toStrictEqual(expectedResponse); } @@ -428,7 +370,6 @@ describe("Create User Use Case", () => { jest.spyOn(mockUserRepository, "createUser") jest.spyOn(mockUserRepository, "generateValidationToken") jest.spyOn(mockMailerAdapter, "send_confirmation_email") - jest.spyOn(mockUserRepository, "toPublicUser") const createUserUseCase = new CreateUser(mockUserRepository, mockTransporter, mockMailerAdapter); try { @@ -443,7 +384,6 @@ describe("Create User Use Case", () => { expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 }); expect(mockUserRepository.generateValidationToken).not.toBeCalled(); expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled(); - expect(mockUserRepository.toPublicUser).not.toBeCalled(); expect(err).toStrictEqual(expectedResponse); } diff --git a/test/entities/project.ts b/test/entities/project.ts index 4aceb6d..704c15a 100644 --- a/test/entities/project.ts +++ b/test/entities/project.ts @@ -388,6 +388,27 @@ export const projectRequestCreationModel_withmissingOverrideDepthOffset = { managers: [{ user_id: 1 } as MinimalUserModel], members: [{ user_id: 2 } as MinimalUserModel], } +export const privateProjectRequestCreationModel_withmissingOverrideDepthOffset = { + root_folder_path: 'root_folder_path', + project_title: 'project_title', + project_acronym: 'project_acronym', + project_description: 'project_description', + project_information: 'project_information', + cruise: 'cruise', + ship: "['ship1', 'ship2']", + data_owner_name: 'data_owner_name', + data_owner_email: 'data_owner_email@email.fr', + operator_name: 'operator_name', + operator_email: 'operator_email@email.fr', + chief_scientist_name: 'chief_scientist_name', + chief_scientist_email: 'chief_scientist_email@email.fr', + enable_descent_filter: true, + privacy_duration: 1, + visible_duration: 1, + public_duration: 1, + instrument_model: 1, + serial_number: 'serial_number' +} export const projectResponseModel: PublicProjectResponseModel = { ...projectRequestCreationModel, @@ -421,6 +442,11 @@ export const projectUpdateModel: PublicProjectUpdateModel = { operator_name: "Edited name", project_id: 1 } +export const privateProjectUpdateModel: ProjectUpdateModel = { + operator_email: "edited_user@email.com", + operator_name: "Edited name", + project_id: 1 +} export const projectUpdateModel_withBadData: ProjectUpdateModel = { project_id: 1, diff --git a/test/mocks/user-mock.ts b/test/mocks/user-mock.ts index 07ec9ef..b464892 100644 --- a/test/mocks/user-mock.ts +++ b/test/mocks/user-mock.ts @@ -2,6 +2,14 @@ import { DecodedToken } from "../../src/domain/entities/auth"; import { SearchResult } from "../../src/domain/entities/search"; import { UserResponseModel } from "../../src/domain/entities/user"; import { UserRepository } from "../../src/domain/interfaces/repositories/user-repository"; +import { CreateUserUseCase } from "../../src/domain/interfaces/use-cases/user/create-user"; +import { UpdateUserUseCase } from "../../src/domain/interfaces/use-cases/user/update-user"; +import { ValidUserUseCase } from "../../src/domain/interfaces/use-cases/user/valid-user"; +import { MiddlewareAuth } from "../../src/presentation/interfaces/middleware/auth"; +import { DeleteUserUseCase } from "../../src/domain/interfaces/use-cases/user/delete-user"; +import { SearchUsersUseCase } from "../../src/domain/interfaces/use-cases/user/search-user"; + +import { Request, Response, NextFunction } from "express"; export class MockUserRepository implements UserRepository { isValidated(): Promise { @@ -73,4 +81,43 @@ export class MockUserRepository implements UserRepository { verifyValidationToken(): DecodedToken | null { throw new Error("Method not implemented : verifyValidationToken"); } -} \ No newline at end of file +} + + +export class MockCreateUserUseCase implements CreateUserUseCase { + execute(): Promise { + throw new Error("Method not implemented for CreateUserUseCase"); + } +} +export class MockUpdateUserUseCase implements UpdateUserUseCase { + execute(): Promise { + throw new Error("Method not implemented for UpdateUserUseCase"); + } +} + +export class MockValidUserUseCase implements ValidUserUseCase { + execute(): Promise { + throw new Error("Method not implemented for ValidUserUseCase"); + } +} + +export class MockMiddlewareAuth implements MiddlewareAuth { + auth(_: Request, __: Response, next: NextFunction): void { + next() + } + auth_refresh(): void { + throw new Error("Method not implemented for auth_refresh"); + } +} + +export class MockDeleteUserUseCase implements DeleteUserUseCase { + execute(): Promise { + throw new Error("Method not implemented for DeleteUserUseCase"); + } +} + +export class MockSearchUsersUseCase implements SearchUsersUseCase { + execute(): Promise<{ users: UserResponseModel[]; search_info: any; }> { + throw new Error("Method not implemented for SearchUsersUseCase"); + } +} diff --git a/test/presentation/middleware/user-validation.test.ts b/test/presentation/middleware/user-validation.test.ts index 367950b..9c40a35 100644 --- a/test/presentation/middleware/user-validation.test.ts +++ b/test/presentation/middleware/user-validation.test.ts @@ -11,51 +11,14 @@ import { ValidUserUseCase } from "../../../src/domain/interfaces/use-cases/user/ import { DeleteUserUseCase } from "../../../src/domain/interfaces/use-cases/user/delete-user"; import { SearchUsersUseCase } from "../../../src/domain/interfaces/use-cases/user/search-user"; -import { MiddlewareAuth } from "../../../src/presentation/interfaces/middleware/auth"; import { IMiddlewareUserValidation } from "../../../src/presentation/interfaces/middleware/user-validation"; import { MiddlewareUserValidation } from "../../../src/presentation/middleware/user-validation"; -import { Request, Response, NextFunction } from "express"; import { CountriesAdapter } from "../../../src/infra/countries/country"; import { SearchInfo } from "../../../src/domain/entities/search"; -class MockCreateUserUseCase implements CreateUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for CreateUserUseCase"); - } -} -class MockUpdateUserUseCase implements UpdateUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for UpdateUserUseCase"); - } -} - -class MockValidUserUseCase implements ValidUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for ValidUserUseCase"); - } -} - -class MockMiddlewareAuth implements MiddlewareAuth { - auth(_: Request, __: Response, next: NextFunction): void { - next() - } - auth_refresh(): void { - throw new Error("Method not implemented for auth_refresh"); - } -} - -class MockDeleteUserUseCase implements DeleteUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for DeleteUserUseCase"); - } -} - -class MockSearchUsersUseCase implements SearchUsersUseCase { - execute(): Promise<{ users: UserResponseModel[]; search_info: any; }> { - throw new Error("Method not implemented for SearchUsersUseCase"); - } -} +import { MockCreateUserUseCase, MockDeleteUserUseCase, MockMiddlewareAuth, MockSearchUsersUseCase, MockUpdateUserUseCase, MockValidUserUseCase } from "../../mocks/user-mock"; + describe("User Router", () => { let countriesAdapter: CountriesAdapter let mockMiddlewareAuth: MockMiddlewareAuth; @@ -96,20 +59,8 @@ describe("User Router", () => { user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "FR", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - - } - jest.spyOn(mockCreateUserUseCase, "execute").mockImplementation(() => Promise.resolve(OutputData)) + const OutputData = { message: "User sucessfully created." } + jest.spyOn(mockCreateUserUseCase, "execute").mockImplementation(() => Promise.resolve()) const response = await request(server).post("/users").send(InputData) @@ -138,20 +89,8 @@ describe("User Router", () => { user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "FR", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - - } - jest.spyOn(mockCreateUserUseCase, "execute").mockImplementation(() => Promise.resolve(OutputData)) + const OutputData = { message: "User sucessfully created." } + jest.spyOn(mockCreateUserUseCase, "execute").mockImplementation(() => Promise.resolve()) const response = await request(server).post("/users").send(InputData) diff --git a/test/presentation/routes/project-router.test.ts b/test/presentation/routes/project-router.test.ts index 41eb9b8..7aa5883 100644 --- a/test/presentation/routes/project-router.test.ts +++ b/test/presentation/routes/project-router.test.ts @@ -274,13 +274,13 @@ describe("Project Router", () => { }); - test("POST /projects fail for Cannot find created project reason", async () => { + test("POST /projects fail for Cannot find the created project. reason", async () => { const InputData: PublicProjectRequestCreationModel = projectRequestCreationModel - const expectedResponse = { errors: ["Cannot find created project"] } + const expectedResponse = { errors: ["Cannot find the created project."] } - jest.spyOn(mockCreateProjectUseCase, "execute").mockImplementation(() => Promise.reject(Error("Cannot find created project"))) + jest.spyOn(mockCreateProjectUseCase, "execute").mockImplementation(() => Promise.reject(Error("Cannot find the created project."))) const response = await request(server).post("/projects").send(InputData) diff --git a/test/presentation/routes/user-router.test.ts b/test/presentation/routes/user-router.test.ts index c295ffa..0ec6224 100644 --- a/test/presentation/routes/user-router.test.ts +++ b/test/presentation/routes/user-router.test.ts @@ -5,7 +5,6 @@ import UserRouter from '../../../src/presentation/routers/user-router' import { UserResponseModel, UserRequestCreationModel } from "../../../src/domain/entities/user"; import { CustomRequest, DecodedToken } from "../../../src/domain/entities/auth"; -import { SearchInfo } from "../../../src/domain/entities/search"; import { CreateUserUseCase } from "../../../src/domain/interfaces/use-cases/user/create-user"; import { UpdateUserUseCase } from "../../../src/domain/interfaces/use-cases/user/update-user"; @@ -17,29 +16,9 @@ import { MiddlewareAuth } from "../../../src/presentation/interfaces/middleware/ import { IMiddlewareUserValidation } from "../../../src/presentation/interfaces/middleware/user-validation"; import { Request, Response, NextFunction } from "express"; +import { MockCreateUserUseCase, MockSearchUsersUseCase, MockUpdateUserUseCase, MockValidUserUseCase } from "../../mocks/user-mock"; -class MockSearchUsersUseCase implements SearchUsersUseCase { - execute(): Promise<{ users: UserResponseModel[], search_info: SearchInfo }> { - throw new Error("Method not implemented for SearchUsersUseCase"); - } -} - -class MockCreateUserUseCase implements CreateUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for CreateUserUseCase"); - } -} -class MockUpdateUserUseCase implements UpdateUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for UpdateUserUseCase"); - } -} -class MockValidUserUseCase implements ValidUserUseCase { - execute(): Promise { - throw new Error("Method not implemented for ValidUserUseCase"); - } -} class MockMiddlewareAuth implements MiddlewareAuth { auth(req: Request, __: Response, next: NextFunction): void { @@ -295,20 +274,11 @@ describe("User Router", () => { country: "France", user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." } - const OutputData: UserResponseModel = { - user_id: 1, - last_name: "Smith", - first_name: "John", - email: "john@gmail.com", - is_admin: false, - valid_email: false, - organisation: "LOV", - country: "France", - user_planned_usage: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - user_creation_date: '2023-08-01 10:30:00' - } - jest.spyOn(mockCreateUserUseCase, "execute").mockImplementation(() => Promise.resolve(OutputData)) + const OutputData = { "message": "User sucessfully created." } + jest.spyOn(mockCreateUserUseCase, "execute").mockImplementation(() => Promise.resolve()) + const response = await request(server).post("/users").send(InputData) + expect(response.status).toBe(201) expect(mockCreateUserUseCase.execute).toBeCalledTimes(1) expect(response.body).toStrictEqual(OutputData)