Ajout de la gestion des erreurs HTTP, implémentation de la validation des comptes, et mise à jour des dépendances. Création de tests pour les entités et services de compte, ainsi que l'ajout d'un système de limitation de taux.
This commit is contained in:
95
tests/AccountService.test.ts
Normal file
95
tests/AccountService.test.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import AccountService from "../src/domain/account/service/AccountService";
|
||||
import { AccountRepositoryInterface } from "../src/domain/account/repository/AccountRepositoryInterface";
|
||||
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
|
||||
import {
|
||||
AccountNotFoundError,
|
||||
AccountAlreadyExistsError,
|
||||
BadPasswordError,
|
||||
} from "../src/domain/account/errors/AccountErrors";
|
||||
|
||||
describe("AccountService", () => {
|
||||
let accountService: AccountService;
|
||||
let mockAccountRepository: AccountRepositoryInterface;
|
||||
|
||||
beforeEach(() => {
|
||||
mockAccountRepository = {
|
||||
findByEmail: vi.fn(),
|
||||
save: vi.fn(),
|
||||
findById: vi.fn(),
|
||||
};
|
||||
accountService = new AccountService(mockAccountRepository);
|
||||
});
|
||||
|
||||
describe("createAccount", () => {
|
||||
it("should create a new account successfully", async () => {
|
||||
const email = "test@example.com";
|
||||
const password = "password123";
|
||||
|
||||
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(null);
|
||||
vi.mocked(mockAccountRepository.save).mockResolvedValue("123");
|
||||
|
||||
const result = await accountService.register(email, password);
|
||||
|
||||
expect(result).toBeInstanceOf(AccountEntity);
|
||||
expect(result.email).toBe(email);
|
||||
expect(mockAccountRepository.findByEmail).toHaveBeenCalledWith(email);
|
||||
expect(mockAccountRepository.save).toHaveBeenCalledWith(
|
||||
expect.any(AccountEntity),
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error if account already exists", async () => {
|
||||
const email = "test@example.com";
|
||||
const password = "password123";
|
||||
const existingAccount = AccountEntity.create(email, password);
|
||||
|
||||
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(
|
||||
existingAccount,
|
||||
);
|
||||
|
||||
await expect(accountService.register(email, password)).rejects.toThrow(
|
||||
AccountAlreadyExistsError,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("login", () => {
|
||||
it("should login successfully with correct credentials", async () => {
|
||||
const email = "test@example.com";
|
||||
const password = "password123";
|
||||
const account = AccountEntity.create(email, password);
|
||||
|
||||
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(account);
|
||||
|
||||
const result = await accountService.login(email, password);
|
||||
|
||||
expect(result).toBe(account);
|
||||
expect(mockAccountRepository.findByEmail).toHaveBeenCalledWith(email);
|
||||
});
|
||||
|
||||
it("should throw error if account not found", async () => {
|
||||
const email = "test@example.com";
|
||||
const password = "password123";
|
||||
|
||||
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(null);
|
||||
|
||||
await expect(accountService.login(email, password)).rejects.toThrow(
|
||||
AccountNotFoundError,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error if password is incorrect", async () => {
|
||||
const email = "test@example.com";
|
||||
const password = "password123";
|
||||
const wrongPassword = "wrongpassword";
|
||||
const account = AccountEntity.create(email, password);
|
||||
|
||||
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(account);
|
||||
|
||||
await expect(accountService.login(email, wrongPassword)).rejects.toThrow(
|
||||
BadPasswordError,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user