diff --git a/src/domain/use-cases/project/update-project.ts b/src/domain/use-cases/project/update-project.ts index 48c0124..daf7252 100644 --- a/src/domain/use-cases/project/update-project.ts +++ b/src/domain/use-cases/project/update-project.ts @@ -176,9 +176,6 @@ export class UpdateProject implements UpdateProjectUseCase { private async getUpdatedPrivileges(project_id: number): Promise { const privileges = await this.privilegeRepository.getPublicPrivileges({ project_id: project_id }); - if (!privileges) { - throw new Error("Cannot find privileges"); - } return privileges; } diff --git a/test/domain/use-cases/instrument_model/get-one-instrument_model.test.ts b/test/domain/use-cases/instrument_model/get-one-instrument_model.test.ts new file mode 100644 index 0000000..1e666cb --- /dev/null +++ b/test/domain/use-cases/instrument_model/get-one-instrument_model.test.ts @@ -0,0 +1,28 @@ +import { InstrumentModelRepository } from "../../../../src/domain/interfaces/repositories/instrument_model-repository"; +import { MockInstrumentModelRepository } from "../../../mocks/instrumentModel-mock"; +import { GetOneInstrumentModel } from '../../../../src/domain/use-cases/instrument_model/get-one-instrument_model'; +import { instrument_model_response } from "../../../entities/instrumentModel"; + + +let mockInstrumentModelRepository: InstrumentModelRepository; + +beforeEach(() => { + jest.clearAllMocks(); + mockInstrumentModelRepository = new MockInstrumentModelRepository() +}) + +describe("Domain - Use Cases - Get One Instrument Model", () => { + test("Get one instrument model by id : Ok ", async () => { + const getOneInstrumentModelUseCase = new GetOneInstrumentModel(mockInstrumentModelRepository) + jest.spyOn(mockInstrumentModelRepository, 'getOneInstrumentModel').mockResolvedValueOnce(instrument_model_response); + + const instrumentModel = await getOneInstrumentModelUseCase.execute(1); + expect(instrumentModel.instrument_model_id).toBe(1); + }); + test("Get one instrument model by id : Not found ", async () => { + const getOneInstrumentModelUseCase = new GetOneInstrumentModel(mockInstrumentModelRepository) + jest.spyOn(mockInstrumentModelRepository, 'getOneInstrumentModel').mockResolvedValueOnce(null); + await expect(getOneInstrumentModelUseCase.execute(2)).rejects.toThrow("Cannot find instrument_model"); + + }); +}); \ No newline at end of file diff --git a/test/domain/use-cases/instrument_model/search-instrument_model.test.ts b/test/domain/use-cases/instrument_model/search-instrument_model.test.ts new file mode 100644 index 0000000..6a70717 --- /dev/null +++ b/test/domain/use-cases/instrument_model/search-instrument_model.test.ts @@ -0,0 +1,57 @@ +import { InstrumentModelRepository } from "../../../../src/domain/interfaces/repositories/instrument_model-repository"; +import { MockInstrumentModelRepository } from "../../../mocks/instrumentModel-mock"; +import { MockSearchRepository } from "../../../mocks/search-mock"; +import { SearchInstrumentModels } from '../../../../src/domain/use-cases/instrument_model/search-instrument_model'; +import { instrument_model_response } from "../../../entities/instrumentModel"; +import { SearchRepository } from "../../../../src/domain/interfaces/repositories/search-repository"; + + +let mockInstrumentModelRepository: InstrumentModelRepository; +let mockSearchRepository: SearchRepository; + +beforeEach(() => { + jest.clearAllMocks(); + mockInstrumentModelRepository = new MockInstrumentModelRepository() + mockSearchRepository = new MockSearchRepository() +}) + +describe("Domain - Use Cases - Search Instrument Models", () => { + test("Search instrument models with filters, should be forced to empty ", async () => { + const options = { filter: [{ field: "instrument_model_name", operator: "=", value: "UVP5" }], sort_by: [], page: 1, limit: 10 } + const instruments = { items: [instrument_model_response], total: 1 } + const search_info_response = { total: 1, page: 1, pages: 1, limit: 10, total_on_page: 1 } + + jest.spyOn(mockSearchRepository, 'formatSortBy').mockReturnValueOnce([]); + jest.spyOn(mockInstrumentModelRepository, 'standardGetInstrumentModels').mockResolvedValueOnce(instruments); + jest.spyOn(mockSearchRepository, 'formatSearchInfo').mockReturnValueOnce(search_info_response); + + + const searchInstrumentModelsUseCase = new SearchInstrumentModels(mockInstrumentModelRepository, mockSearchRepository) + const { instrument_models, search_info } = await searchInstrumentModelsUseCase.execute(options); + + expect(instrument_models.length).toBe(1); + expect(search_info.total).toBe(1); + expect(mockSearchRepository.formatSortBy).toHaveBeenCalledWith([]); + expect(mockInstrumentModelRepository.standardGetInstrumentModels).toHaveBeenCalledWith({ filter: [], sort_by: [], page: 1, limit: 10 }); + expect(mockSearchRepository.formatSearchInfo).toHaveBeenCalledWith(instruments, options); + }); + test("Throw error when searching instrument models", async () => { + const options = { filter: [{ field: "instrument_model_name", operator: "=", value: "UVP5" }], sort_by: [], page: 1, limit: 10 } + const search_info_response = { total: 1, page: 1, pages: 1, limit: 10, total_on_page: 1 } + + jest.spyOn(mockSearchRepository, 'formatSortBy').mockReturnValueOnce([]); + jest.spyOn(mockInstrumentModelRepository, 'standardGetInstrumentModels').mockRejectedValueOnce(new Error()); + jest.spyOn(mockSearchRepository, 'formatSearchInfo').mockReturnValueOnce(search_info_response); + + try { + const searchInstrumentModelsUseCase = new SearchInstrumentModels(mockInstrumentModelRepository, mockSearchRepository) + await searchInstrumentModelsUseCase.execute(options); + } catch (error) { + expect(error.message).toBe("Cannot search instrument models"); + + expect(mockSearchRepository.formatSortBy).toHaveBeenCalledWith([]); + expect(mockInstrumentModelRepository.standardGetInstrumentModels).toHaveBeenCalledWith({ filter: [], sort_by: [], page: 1, limit: 10 }); + expect(mockSearchRepository.formatSearchInfo).not.toHaveBeenCalled(); + } + }); +}); diff --git a/test/domain/use-cases/project/update-project.test.ts b/test/domain/use-cases/project/update-project.test.ts index 13c7b93..02f9361 100644 --- a/test/domain/use-cases/project/update-project.test.ts +++ b/test/domain/use-cases/project/update-project.test.ts @@ -5,13 +5,14 @@ import { PrivilegeRepository } from "../../../../src/domain/interfaces/repositor import { ProjectRepository } from "../../../../src/domain/interfaces/repositories/project-repository"; import { UserRepository } from "../../../../src/domain/interfaces/repositories/user-repository"; import { UpdateProject } from '../../../../src/domain/use-cases/project/update-project' -import { projectResponseModel, projectUpdateModel } from "../../../entities/project"; +import { projectResponseModel, projectUpdateModel, projectUpdateModel_withInstrumentModel, projectUpdateModel_withPartialPrivilegeUpdate, projectUpdateModel_withPrivilegeUpdate } from "../../../entities/project"; import { MockProjectRepository } from "../../../mocks/project-mock"; import { MockUserRepository } from "../../../mocks/user-mock"; import { MockPrivilegeRepository } from "../../../mocks/privilege-mock"; import { MockInstrumentModelRepository } from "../../../mocks/instrumentModel-mock"; import { PublicPrivilege } from "../../../../src/domain/entities/privilege"; import { publicPrivileges } from "../../../entities/privilege"; +import { instrument_model_response } from "../../../entities/instrumentModel"; /* TESTED HERE */ // User is not admin : edit regular properties on himself : ok @@ -158,3 +159,290 @@ test("Cannot find updated project", async () => { expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(2) }); +test("User try to update an unexisting project", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel + + const OutputError = new Error("Cannot find project to update") + + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementationOnce(() => Promise.resolve(null)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "isAdmin") + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + try { + await updateProjectUseCase.execute(current_user, project_to_update); + expect(true).toBe(false) + } catch (err) { + expect(err).toStrictEqual(OutputError); + } + + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(1) + +}); + +test("User is not admin and have no privilege on the project", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel + + const OutputError = new Error("Logged user cannot update this property or project") + + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementationOnce(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + try { + await updateProjectUseCase.execute(current_user, project_to_update); + expect(true).toBe(false) + } catch (err) { + expect(err).toStrictEqual(OutputError); + } + + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(1) + +}); +// test admin can update privilege part +test("Admin can update privilege part", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel + const OutputData: ProjectResponseModel = projectResponseModel + const privileges: PublicPrivilege = publicPrivileges + const updated_public_project: PublicProjectResponseModel = projectResponseModel + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges").mockImplementationOnce(() => Promise.resolve(privileges)) + jest.spyOn(mockProjectRepository, "toPublicProject").mockImplementationOnce(() => updated_public_project) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + + const data = await updateProjectUseCase.execute(current_user, project_to_update); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(2) + expect(data).toStrictEqual(OutputData); +}) +// test manager can update privilege part +test("Manager can update privilege part", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel_withPrivilegeUpdate + const OutputData: ProjectResponseModel = projectResponseModel + const privileges: PublicPrivilege = publicPrivileges + const updated_public_project: PublicProjectResponseModel = projectResponseModel + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges").mockImplementationOnce(() => Promise.resolve(privileges)) + jest.spyOn(mockProjectRepository, "toPublicProject").mockImplementationOnce(() => updated_public_project) + jest.spyOn(mockUserRepository, "ensureTypedUserCanBeUsed").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "ensurePrivilegeCoherence").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "deletePrivileges").mockImplementation(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "createPrivileges").mockImplementation(() => Promise.resolve(2)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + const data = await updateProjectUseCase.execute(current_user, project_to_update); + + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(2) + expect(data).toStrictEqual(OutputData); +}) +// test member cannot update privilege part +test("Member cannot update privilege part", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel_withPrivilegeUpdate + + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockProjectRepository, "standardUpdateProject") + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges") + jest.spyOn(mockProjectRepository, "toPublicProject") + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + try { + await updateProjectUseCase.execute(current_user, project_to_update); + expect(true).toBe(false) + } catch (err) { + expect(err).toStrictEqual(new Error("Logged user cannot update privileges")); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(1) + } +}); +//update instrument model +test("Update instrument model", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel_withInstrumentModel + const OutputData: ProjectResponseModel = projectResponseModel + const updated_public_project: PublicProjectResponseModel = projectResponseModel + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges").mockImplementationOnce(() => Promise.resolve(publicPrivileges)) + jest.spyOn(mockProjectRepository, "toPublicProject").mockImplementationOnce(() => updated_public_project) + jest.spyOn(mockInstrumentModelRepository, "getInstrumentByName").mockImplementationOnce(() => Promise.resolve(instrument_model_response)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + + const data = await updateProjectUseCase.execute(current_user, project_to_update); + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(2) + expect(data).toStrictEqual(OutputData); +}); + +// privileges partialy filled +test("Update project with privileges partially filled", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel_withPartialPrivilegeUpdate + const privileges: PublicPrivilege = publicPrivileges + const updated_public_project: PublicProjectResponseModel = projectResponseModel + const expected_error = new Error("To update privilege part you must provide members, managers and contact, if you want to manage privileges more granuraly please use privilege endpoints") + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(false)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges").mockImplementationOnce(() => Promise.resolve(privileges)) + jest.spyOn(mockProjectRepository, "toPublicProject").mockImplementationOnce(() => updated_public_project) + jest.spyOn(mockUserRepository, "ensureTypedUserCanBeUsed").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "ensurePrivilegeCoherence").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "deletePrivileges").mockImplementation(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "createPrivileges").mockImplementation(() => Promise.resolve(2)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + try { + await updateProjectUseCase.execute(current_user, project_to_update); + expect(true).toBe(false) + } catch (err) { + expect(err).toStrictEqual(expected_error) + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(1) + expect(mockPrivilegeRepository.deletePrivileges).toHaveBeenCalledTimes(0) + expect(mockPrivilegeRepository.createPrivileges).toHaveBeenCalledTimes(0) + } +}); + +// privileges partialy created +test("Update project with privileges partially created", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = projectUpdateModel_withPrivilegeUpdate + const privileges: PublicPrivilege = publicPrivileges + const updated_public_project: PublicProjectResponseModel = projectResponseModel + const expected_error = new Error("Privileges partially updated, please check members, managers and contact. Other project properties were not updated due to this error, please try again.") + + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges").mockImplementationOnce(() => Promise.resolve(privileges)) + jest.spyOn(mockProjectRepository, "toPublicProject").mockImplementationOnce(() => updated_public_project) + jest.spyOn(mockUserRepository, "ensureTypedUserCanBeUsed").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "ensurePrivilegeCoherence").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "deletePrivileges").mockImplementation(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "createPrivileges").mockImplementation(() => Promise.resolve(1)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + try { + await updateProjectUseCase.execute(current_user, project_to_update); + expect(true).toBe(false) + } catch (err) { + expect(err).toStrictEqual(expected_error) + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(1) + expect(mockPrivilegeRepository.deletePrivileges).toHaveBeenCalledTimes(1) + expect(mockPrivilegeRepository.createPrivileges).toHaveBeenCalledTimes(1) + } +}); + +// please provide at least one property to update +test("Please provide at least one property to update", async () => { + const current_user: UserUpdateModel = { + user_id: 1 + } + const project_to_update: PublicProjectUpdateModel = { + project_id: 1 + } + const privileges: PublicPrivilege = publicPrivileges + const updated_public_project: PublicProjectResponseModel = projectResponseModel + const expected_error = new Error("Please provide at least one property to update") + + jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementationOnce(() => Promise.resolve()) + jest.spyOn(mockProjectRepository, "getProject").mockImplementation(() => Promise.resolve(projectResponseModel)) + jest.spyOn(mockPrivilegeRepository, "isGranted").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockPrivilegeRepository, "isManager").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockUserRepository, "isAdmin").mockImplementationOnce(() => Promise.resolve(true)) + jest.spyOn(mockProjectRepository, "standardUpdateProject").mockImplementationOnce(() => Promise.reject(new Error("Please provide at least one property to update"))) + jest.spyOn(mockPrivilegeRepository, "getPublicPrivileges").mockImplementationOnce(() => Promise.resolve(privileges)) + jest.spyOn(mockProjectRepository, "toPublicProject").mockImplementationOnce(() => updated_public_project) + jest.spyOn(mockUserRepository, "ensureTypedUserCanBeUsed").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "ensurePrivilegeCoherence").mockImplementation(() => Promise.resolve()) + jest.spyOn(mockPrivilegeRepository, "deletePrivileges").mockImplementation(() => Promise.resolve(1)) + jest.spyOn(mockPrivilegeRepository, "createPrivileges").mockImplementation(() => Promise.resolve(1)) + + const updateProjectUseCase = new UpdateProject(mockUserRepository, mockProjectRepository, mockInstrumentModelRepository, mockPrivilegeRepository) + try { + await updateProjectUseCase.execute(current_user, project_to_update); + expect(true).toBe(false) + } catch (err) { + expect(err).toStrictEqual(expected_error) + expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledTimes(1) + expect(mockUserRepository.isAdmin).toHaveBeenCalledTimes(0) + expect(mockProjectRepository.standardUpdateProject).toHaveBeenCalledTimes(1) + expect(mockProjectRepository.getProject).toHaveBeenCalledTimes(1) + expect(mockPrivilegeRepository.deletePrivileges).toHaveBeenCalledTimes(0) + expect(mockPrivilegeRepository.createPrivileges).toHaveBeenCalledTimes(0) + } +});