Skip to content

Commit

Permalink
TST convergeance DONE
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jul 10, 2024
1 parent 554637a commit 471c67b
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -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 || '',
Expand All @@ -17,6 +18,7 @@ function initializeProjectDB() {
throw err
}
});
new SQLiteInstrumentModelDataSource(db)
return new SQLiteProjectDataSource(db)
}

Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions test/domain/repositories/project-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)

Expand Down
56 changes: 29 additions & 27 deletions test/domain/use-cases/auth/change-password.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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();
}
Expand Down Expand Up @@ -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))

Expand All @@ -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();
}
Expand Down Expand Up @@ -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);

});

Expand Down Expand Up @@ -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))

Expand All @@ -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();
}
Expand Down Expand Up @@ -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))

Expand All @@ -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();
}
Expand Down Expand Up @@ -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))

Expand All @@ -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();
}
Expand Down Expand Up @@ -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))

Expand All @@ -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);
}
Expand Down Expand Up @@ -353,15 +355,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve())
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.ensureUserCanBeUsed).toBeCalledTimes(2);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -394,15 +396,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve())
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.ensureUserCanBeUsed).toBeCalledTimes(2);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -435,15 +437,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, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve())
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.ensureUserCanBeUsed).toBeCalledTimes(2);
expect(mockUserRepository.verifyUserLogin).not.toBeCalled();
expect(mockUserRepository.changePassword).toBeCalledWith(credentials);
} catch (err) {
Expand Down Expand Up @@ -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))

Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 471c67b

Please sign in to comment.