Skip to content

Commit

Permalink
TST - S8 convergence User UCs
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jun 27, 2024
1 parent 51165ee commit b55a5f3
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 152 deletions.
2 changes: 1 addition & 1 deletion src/domain/use-cases/user/create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class CreateUser implements CreateUserUseCase {
// Check if a user with the given email already exists
if (preexistentUser) {
// User should not be deleted
if (preexistentUser.deleted) throw new Error("User is deleted");
if (preexistentUser.deleted !== undefined) throw new Error("User is deleted");

// If the user exists but hasn't validated their email
if (!preexistentUser.valid_email) {
Expand Down
3 changes: 2 additions & 1 deletion src/domain/use-cases/user/update-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export class UpdateUser implements UpdateUserUseCase {
await this.userRepository.ensureUserCanBeUsed(current_user.user_id);

const user = await this.userRepository.getUser({ user_id: user_to_update.user_id })
if (await this.userRepository.isDeleted(user)) throw new Error("User is deleted");
if (!user) throw new Error("Cannot find user to update");
if (user.deleted !== undefined) throw new Error("User to update is deleted");

// Update admin can update anyone
if (await this.userRepository.isAdmin(current_user.user_id)) {
Expand Down
6 changes: 3 additions & 3 deletions src/domain/use-cases/user/valid-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export class ValidUser implements ValidUserUseCase {
// Find user with confirmation code and user_id
const user_to_update = await this.userRepository.getUser({ user_id: user_id, confirmation_code: decoded_token.confirmation_code })

// User should not be deleted
if (await this.userRepository.isDeleted(user_to_update)) throw new Error("User is deleted");

// If no user found throw error
if (!user_to_update) throw new Error("Cannot find user with confirmation code");

// User should not be deleted
if (user_to_update.deleted !== undefined) throw new Error("User is deleted");

// Update validation status of the found user
const nb_updated_user = await this.userRepository.validUser({ user_id: user_to_update.user_id })
// If no user updated throw error
Expand Down
2 changes: 1 addition & 1 deletion test/domain/use-cases/project/search-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SearchProject } from '../../../../src/domain/use-cases/project/search-p

import { MockProjectRepository } from "../../../mocks/project-mock";
import { MockUserRepository } from "../../../mocks/user-mock";
import { MockSearchRepository } from "../../../mocks/search-mocks";
import { MockSearchRepository } from "../../../mocks/search-mock";
import { UserUpdateModel } from "../../../../src/domain/entities/user";
import { FilterSearchOptions, SearchOptions } from "../../../../src/domain/entities/search";
import { projectResponseModelArray } from "../../../entities/project";
Expand Down
48 changes: 21 additions & 27 deletions test/domain/use-cases/user/create-user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ describe("Create User Use Case", () => {
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'
user_creation_date: '2023-08-01 10:30:00',
deleted: '2023-08-01 10:30:00'
}
const updated_user: UserResponseModel = {
user_id: 1,
Expand Down Expand Up @@ -148,9 +149,8 @@ describe("Create User Use Case", () => {
}

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(preexistant_user)).mockImplementationOnce(() => Promise.resolve(updated_user))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(true))
jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(1))
jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(-1)) // not called
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 })
Expand All @@ -165,7 +165,6 @@ describe("Create User Use Case", () => {

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1);
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.createUser).not.toBeCalled();
expect(mockUserRepository.standardUpdateUser).not.toBeCalled();
expect(mockUserRepository.generateValidationToken).not.toBeCalled();
Expand Down Expand Up @@ -225,9 +224,9 @@ describe("Create User Use Case", () => {
}

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(preexistant_user)).mockImplementationOnce(() => Promise.resolve(updated_user))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(1))
jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(-1)) // not called
jest.spyOn(mockUserRepository, "createUser")
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 })
Expand All @@ -236,9 +235,8 @@ describe("Create User Use Case", () => {
const result = await createUserUseCase.execute(InputData);

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.createUser).not.toBeCalled();
expect(mockUserRepository.standardUpdateUser).toHaveBeenCalledWith(preexistant_user);
expect(mockUserRepository.standardUpdateUser).toBeCalledTimes(1);
expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 });
expect(mockUserRepository.generateValidationToken).toHaveBeenCalledWith(updated_user);
expect(mockMailerAdapter.send_confirmation_email).toHaveBeenCalledWith(mockTransporter, updated_user, "token");
Expand Down Expand Up @@ -275,11 +273,10 @@ describe("Create User Use Case", () => {
const expectedResponse = new Error("Valid user already exists");

jest.spyOn(mockUserRepository, "getUser").mockImplementation(() => Promise.resolve(preexistant_user))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(1))// not called
jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(-1)) // not called
jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "") // not called
jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve()) // not called
jest.spyOn(mockUserRepository, "standardUpdateUser")
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)
Expand All @@ -289,7 +286,6 @@ describe("Create User Use Case", () => {

} catch (err) {
expect(mockUserRepository.getUser).toHaveBeenCalledWith({ email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.getUser).toHaveBeenCalledTimes(1);
expect(mockUserRepository.createUser).not.toBeCalled();
expect(mockUserRepository.standardUpdateUser).not.toBeCalled();
Expand Down Expand Up @@ -377,11 +373,10 @@ describe("Create User Use Case", () => {
const expectedResponse = new Error("Cannot update preexistent user");

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(preexistant_user)).mockImplementationOnce(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(0))
jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(-1)) // not called
jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "token") // not called
jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve()) // not called
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);
Expand All @@ -392,8 +387,7 @@ describe("Create User Use Case", () => {

expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.createUser).not.toBeCalled();
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.standardUpdateUser).toHaveBeenCalledWith(preexistant_user);
expect(mockUserRepository.standardUpdateUser).toBeCalledTimes(1);
expect(mockUserRepository.generateValidationToken).not.toBeCalled();
expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled();
expect(mockUserRepository.toPublicUser).not.toBeCalled();
Expand Down Expand Up @@ -429,23 +423,23 @@ describe("Create User Use Case", () => {
const expectedResponse = new Error("Cannot find updated preexistent user");

jest.spyOn(mockUserRepository, "getUser").mockImplementationOnce(() => Promise.resolve(preexistant_user)).mockImplementationOnce(() => Promise.resolve(null))
jest.spyOn(mockUserRepository, "isDeleted").mockImplementation(() => Promise.resolve(false))
jest.spyOn(mockUserRepository, "standardUpdateUser").mockImplementation(() => Promise.resolve(1))
jest.spyOn(mockUserRepository, "createUser").mockImplementation(() => Promise.resolve(-1)) // not called
jest.spyOn(mockUserRepository, "generateValidationToken").mockImplementation(() => "token") // not called
jest.spyOn(mockMailerAdapter, "send_confirmation_email").mockImplementation(() => Promise.resolve())// not called
jest.spyOn(mockUserRepository, "changePassword").mockImplementation(() => Promise.resolve(1))
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 {
await createUserUseCase.execute(InputData);
expect(true).toBe(false)
} catch (err) {

expect(mockUserRepository.getUser).toBeCalledTimes(2);
expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(1, { email: "[email protected]" });
expect(mockUserRepository.isDeleted).toHaveBeenCalledWith(1);
expect(mockUserRepository.createUser).not.toBeCalled();
expect(mockUserRepository.standardUpdateUser).toHaveBeenCalledWith(preexistant_user);
expect(mockUserRepository.standardUpdateUser).toBeCalledTimes(1);
expect(mockUserRepository.changePassword).toBeCalledTimes(1);
expect(mockUserRepository.getUser).toHaveBeenNthCalledWith(2, { user_id: 1 });
expect(mockUserRepository.generateValidationToken).not.toBeCalled();
expect(mockMailerAdapter.send_confirmation_email).not.toBeCalled();
Expand Down
Loading

0 comments on commit b55a5f3

Please sign in to comment.