Skip to content

Commit

Permalink
TST - S8 convergence project UCs
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jul 1, 2024
1 parent 4e11909 commit 8a7fbee
Show file tree
Hide file tree
Showing 26 changed files with 428 additions and 192 deletions.
12 changes: 6 additions & 6 deletions test/data/data-sources/sqlite/sqlite-project-data-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sqlite3 from 'sqlite3'
import 'dotenv/config'
import { ProjectRequestCreationModel, ProjectUpdateModel } from '../../../../src/domain/entities/project';
import fs from 'fs';
import { projectRequestCreationModel_2, projectRequestCreationModel_3, projectRequestCreationModel_4, projectRequestCreationModel_5, projectRequestCreationModel_6, 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, projectRequestCreationModel_3, projectUpdateModel } from '../../../entities/project';

const config = {
TEST_DBSOURCE: process.env.TEST_DBSOURCE || '',
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('SQLiteProjectDataSource', () => {
});

test('should create a new project', async () => {
const project: ProjectRequestCreationModel = projectRequestCreationModel_2
const project: ProjectRequestCreationModel = data_source_projectRequestCreationModel_2

// Call the create method
const projectId = await dataSource.create(project);
Expand All @@ -64,7 +64,7 @@ describe('SQLiteProjectDataSource', () => {

});
test('should create a new project', async () => {
const project: ProjectRequestCreationModel = projectRequestCreationModel_3
const project: ProjectRequestCreationModel = data_source_projectRequestCreationModel_3

// Call the create method
const projectId = await dataSource.create(project);
Expand Down Expand Up @@ -142,9 +142,9 @@ describe('SQLiteProjectDataSource', () => {

test('should return all projects with sorting and filtering', async () => {
// Add bunch of projects
const project3: ProjectRequestCreationModel = projectRequestCreationModel_4
const project4: ProjectRequestCreationModel = projectRequestCreationModel_5
const project5: ProjectRequestCreationModel = projectRequestCreationModel_6
const project3: ProjectRequestCreationModel = data_source_projectRequestCreationModel_4
const project4: ProjectRequestCreationModel = data_source_projectRequestCreationModel_5
const project5: ProjectRequestCreationModel = data_source_projectRequestCreationModel_6
await dataSource.create(project3)
await dataSource.create(project4)
await dataSource.create(project5)
Expand Down
4 changes: 2 additions & 2 deletions test/domain/repositories/auth-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { JwtAdapter } from "../../../src/infra/auth/jsonwebtoken";

class MockJwtAdapter extends JwtAdapter {
sign(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for sign");
}
verify(): JwtPayload | string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for verify");
}
}

Expand Down
52 changes: 35 additions & 17 deletions test/domain/repositories/user-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,40 @@ import { decodedToken } from "../../entities/auth";

class MockUserDataSource implements UserDataSource {
deleteOne(): void {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for deleteOne");
}
updateOne(): Promise<number> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for updateOne");
}
create(): Promise<number> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for create");
}
getAll(): Promise<SearchResult<UserResponseModel>> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for getAll");
}
getOne(): Promise<UserResponseModel> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for getOne");
}
getUserLogin(): Promise<AuthUserCredentialsModel | null> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for getUserLogin");
}

}
class MockBcryptAdapter extends BcryptAdapter {
async hash(): Promise<string> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for hash");
}
// compare password
async compare(): Promise<boolean> {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for compare");
}
}
class MockJwtAdapter extends JwtAdapter {
sign(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for sign");
}
verify(): JwtPayload | string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for verify");
}
}

Expand Down Expand Up @@ -801,8 +801,8 @@ describe("User Repository", () => {
expect(result).toStrictEqual(expectedData)
});
})
describe("IsDeleted", () => {
test("Should return true for a deleted user", async () => {
describe("canUserBeUsed", () => {
test("Should return false for a deleted user", async () => {
const deletedUser: UserResponseModel = {
user_id: 1,
last_name: "anonym_1",
Expand All @@ -818,10 +818,10 @@ describe("User Repository", () => {
}
jest.spyOn(mockUserDataSource, "getOne").mockImplementation(() => Promise.resolve(deletedUser))

const result = await userRepository.isDeleted(1);
expect(result).toBe(true)
const result = await userRepository.canUserBeUse(1);
expect(result).toBe(false)
});
test("Should return false for a non deleted user", async () => {
test("Should return true for a non deleted user", async () => {
const nonDeletedUser: UserResponseModel = {
user_id: 1,
last_name: "Smith",
Expand All @@ -836,13 +836,31 @@ describe("User Repository", () => {
}
jest.spyOn(mockUserDataSource, "getOne").mockImplementation(() => Promise.resolve(nonDeletedUser))

const result = await userRepository.isDeleted(1);
const result = await userRepository.canUserBeUse(1);
expect(result).toBe(true)
});
test("Should return false for a unvalid user", async () => {
const nonDeletedUser: UserResponseModel = {
user_id: 1,
last_name: "Smith",
first_name: "John",
email: "[email protected]",
valid_email: false,
is_admin: 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(mockUserDataSource, "getOne").mockImplementation(() => Promise.resolve(nonDeletedUser))

const result = await userRepository.canUserBeUse(1);
expect(result).toBe(false)
});

test("Should return false for a non existing user", async () => {
jest.spyOn(mockUserDataSource, "getOne").mockImplementation(() => Promise.resolve(null))
const result = await userRepository.isDeleted(1);
const result = await userRepository.canUserBeUse(1);
expect(result).toBe(false)
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/domain/use-cases/auth/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { MockUserRepository } from "../../../mocks/user-mock";
describe("Create User Use Case", () => {
class MockAuthRepository implements AuthRepository {
generateAccessToken(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for generateAccessToken");
}
generateRefreshToken(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for generateRefreshToken");
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/domain/use-cases/auth/refresh-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { MockUserRepository } from "../../../mocks/user-mock";
describe("Create User Use Case", () => {
class MockAuthRepository implements AuthRepository {
generateAccessToken(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for generateAccessToken");
}
generateRefreshToken(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented for generateRefreshToken");
}
}

Expand Down
26 changes: 13 additions & 13 deletions test/domain/use-cases/auth/reset-password.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Change password Use Case", () => {
const nb_of_updated_user = 1

jest.spyOn(mockUserRepository, "verifyResetPasswordToken").mockImplementation(() => { return decoded_token })
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve())
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(preexistant_user))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(nb_of_updated_user))

Expand All @@ -62,7 +62,7 @@ describe("Change password Use Case", () => {
await reset_password.execute(InputData);

expect(mockUserRepository.verifyResetPasswordToken).toHaveBeenCalledWith(InputData.reset_password_token);
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).toHaveBeenCalledWith({ user_id: 1, reset_password_code: decoded_token.reset_password_code });
expect(mockUserRepository.changePassword).toHaveBeenCalledWith({ ...preexistant_user, ...InputData });
});
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("Change password Use Case", () => {
const nb_of_updated_user = 0

jest.spyOn(mockUserRepository, "verifyResetPasswordToken").mockImplementation(() => { return decoded_token })
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used")))
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(nb_of_updated_user))

Expand All @@ -130,11 +130,11 @@ describe("Change password Use Case", () => {
await reset_password.execute(InputData);
}
catch (error) {
expect(error.message).toBe("User is deleted");
expect(error.message).toBe("User cannot be used");
}

expect(mockUserRepository.verifyResetPasswordToken).toHaveBeenCalledWith(InputData.reset_password_token);
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).not.toHaveBeenCalled()
expect(mockUserRepository.changePassword).not.toHaveBeenCalled()
});
Expand Down Expand Up @@ -191,7 +191,7 @@ describe("Change password Use Case", () => {
const nb_of_updated_user = 0

jest.spyOn(mockUserRepository, "verifyResetPasswordToken").mockImplementation(() => { return decoded_token })
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve())
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(nb_of_updated_user))

Expand All @@ -205,7 +205,7 @@ describe("Change password Use Case", () => {
}

expect(mockUserRepository.verifyResetPasswordToken).toHaveBeenCalledWith(InputData.reset_password_token);
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).toHaveBeenCalledWith({ user_id: 1, reset_password_code: decoded_token.reset_password_code });
expect(mockUserRepository.changePassword).not.toHaveBeenCalled()
})
Expand Down Expand Up @@ -250,7 +250,7 @@ describe("Change password Use Case", () => {
const nb_of_updated_user = 0

jest.spyOn(mockUserRepository, "verifyResetPasswordToken").mockImplementation(() => { return decoded_token })
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.reject(new Error("User cannot be used")))
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(preexistant_user))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(nb_of_updated_user))

Expand All @@ -260,12 +260,12 @@ describe("Change password Use Case", () => {
await reset_password.execute(InputData);
}
catch (error) {
expect(error.message).toBe("User email is not validated");
expect(error.message).toBe("User cannot be used");
}

expect(mockUserRepository.verifyResetPasswordToken).toHaveBeenCalledWith(InputData.reset_password_token);
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).toHaveBeenCalledWith({ user_id: 1, reset_password_code: decoded_token.reset_password_code });
expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).not.toHaveBeenCalled()
expect(mockUserRepository.changePassword).not.toHaveBeenCalled()
})
test("Cannot change password", async () => {
Expand Down Expand Up @@ -308,7 +308,7 @@ describe("Change password Use Case", () => {
const nb_of_updated_user = 0

jest.spyOn(mockUserRepository, "verifyResetPasswordToken").mockImplementation(() => { return decoded_token })
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "ensureUserCanBeUsed").mockImplementation(() => Promise.resolve())
jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(preexistant_user))
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(nb_of_updated_user))

Expand All @@ -322,7 +322,7 @@ describe("Change password Use Case", () => {
}

expect(mockUserRepository.verifyResetPasswordToken).toHaveBeenCalledWith(InputData.reset_password_token);
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.ensureUserCanBeUsed).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).toHaveBeenCalledWith({ user_id: 1, reset_password_code: decoded_token.reset_password_code });
expect(mockUserRepository.changePassword).toHaveBeenCalledWith({ ...preexistant_user, ...InputData });
})
Expand Down
Loading

0 comments on commit 8a7fbee

Please sign in to comment.