This commit is contained in:
David Arranz 2025-03-04 18:27:14 +01:00
parent 2e397900e8
commit d39148cac3
18 changed files with 141 additions and 43 deletions

View File

@ -1,4 +1,4 @@
import { authUserRepository } from "@contexts/auth/infraestructure";
import { authenticatedUserRepository } from "@contexts/auth/infraestructure";
import passport from "passport";
import { ExtractJwt, Strategy as JwtStrategy } from "passport-jwt";
@ -13,7 +13,7 @@ const jwtOptions = {
passport.use(
new JwtStrategy(jwtOptions, async (payload, done) => {
try {
const userResult = await authUserRepository.findById(payload.userId);
const userResult = await authenticatedUserRepository.findById(payload.userId);
if (userResult.isFailure) {
return done(null, false);
}

View File

@ -1,41 +0,0 @@
// Pruebas unitarias: AccountService.test.ts
import { Account } from "../domain/Account";
import { IAccountRepository } from "../repositories/AccountRepository";
import { AccountService } from "../services/AccountService";
const mockAccountRepository: IAccountRepository = {
findById: jest.fn(),
save: jest.fn(),
};
describe("AccountService", () => {
let accountService: AccountService;
beforeEach(() => {
accountService = new AccountService(mockAccountRepository);
});
it("debería actualizar una cuenta existente", async () => {
const existingAccount = new Account(
{
/* datos simulados */
},
"123"
);
(mockAccountRepository.findById as jest.Mock).mockResolvedValue(existingAccount);
(mockAccountRepository.save as jest.Mock).mockResolvedValue(undefined);
const result = await accountService.updateAccountById("123", { name: "Nuevo Nombre" });
expect(result.isSuccess).toBe(true);
expect(result.data.name).toBe("Nuevo Nombre");
expect(mockAccountRepository.save).toHaveBeenCalled();
});
it("debería retornar error si la cuenta no existe", async () => {
(mockAccountRepository.findById as jest.Mock).mockResolvedValue(null);
const result = await accountService.updateAccountById("123", { name: "Nuevo Nombre" });
expect(result.isFailure).toBe(true);
expect(result.error.message).toBe("Account not found");
});
});

View File

@ -0,0 +1,67 @@
import { UniqueID } from "@common/domain";
import { Account } from "../aggregates";
import { IAccountRepository } from "../repositories";
import { AccountService } from "./account.service";
const mockAccountRepository: IAccountRepository = {
findById: jest.fn(),
create: jest.fn(),
update: jest.fn(),
};
const sampleAccount = {
id: "c5743279-e1cf-4dd5-baae-6698c8c6183c",
is_freelancer: false,
name: "Empresa XYZ",
trade_name: "XYZ Trading",
tin: "123456789",
street: "Calle Principal 123",
city: "Madrid",
state: "Madrid",
postal_code: "28001",
country: "España",
email: "contacto@xyz.com",
phone: "+34 600 123 456",
fax: "+34 600 654 321",
website: "https://xyz.com",
legal_record: "Registro Mercantil XYZ",
default_tax: 21,
status: "active",
lang_code: "es",
currency_code: "EUR",
logo: "https://xyz.com/logo.png",
};
const accountId = UniqueID.create(sampleAccount.id).data;
describe("AccountService", () => {
let accountService: AccountService;
beforeEach(() => {
accountService = new AccountService(mockAccountRepository);
});
it("debería actualizar una cuenta existente", async () => {
const existingAccount = new Account(
{
/* datos simulados */
},
"123"
);
(mockAccountRepository.findById as jest.Mock).mockResolvedValue(existingAccount);
(mockAccountRepository.create as jest.Mock).mockResolvedValue(undefined);
const result = await accountService.updateAccountById(accountId, { name: "Nuevo Nombre" });
expect(result.isSuccess).toBe(true);
expect(result.data.name).toBe("Nuevo Nombre");
expect(mockAccountRepository.save).toHaveBeenCalled();
});
it("debería retornar error si la cuenta no existe", async () => {
(mockAccountRepository.findById as jest.Mock).mockResolvedValue(null);
const result = await accountService.updateAccountById(accountId, { name: "Nuevo Nombre" });
expect(result.isFailure).toBe(true);
expect(result.error.message).toBe("Account not found");
});
});

View File

@ -0,0 +1,72 @@
import { ACCOUNT_STATUS, AccountStatus } from "./account-status";
describe("AccountStatus Value Object", () => {
describe("Creación de estados válidos", () => {
it("debería crear un estado inactivo correctamente", () => {
const status = AccountStatus.createInactive();
expect(status.getValue()).toBe(ACCOUNT_STATUS.INACTIVE);
});
it("debería crear un estado activo correctamente", () => {
const status = AccountStatus.createActive();
expect(status.getValue()).toBe(ACCOUNT_STATUS.ACTIVE);
});
it("debería crear un estado válido usando el método create", () => {
const result = AccountStatus.create(ACCOUNT_STATUS.ACTIVE);
expect(result.isSuccess).toBe(true);
expect(result.data.getValue()).toBe(ACCOUNT_STATUS.ACTIVE);
});
it("debería fallar al crear un estado inválido", () => {
const result = AccountStatus.create("invalid-status");
expect(result.isFailure).toBe(true);
expect(result.error.message).toBe("Estado de la cuenta no válido: invalid-status");
});
});
describe("Verificación de transiciones", () => {
it("debería permitir la transición de 'inactive' a 'active'", () => {
const status = AccountStatus.createInactive();
expect(status.canTransitionTo(ACCOUNT_STATUS.ACTIVE)).toBe(true);
});
it("debería permitir la transición de 'active' a 'inactive'", () => {
const status = AccountStatus.createActive();
expect(status.canTransitionTo(ACCOUNT_STATUS.INACTIVE)).toBe(true);
});
it("debería impedir transiciones no permitidas", () => {
const status = AccountStatus.createInactive();
expect(status.canTransitionTo("invalid")).toBe(false);
});
it("debería realizar una transición válida correctamente", () => {
const inactiveStatus = AccountStatus.createInactive();
const transitionResult = inactiveStatus.transitionTo(ACCOUNT_STATUS.ACTIVE);
expect(transitionResult.isSuccess).toBe(true);
expect(transitionResult.data.getValue()).toBe(ACCOUNT_STATUS.ACTIVE);
});
it("debería fallar en una transición inválida", () => {
const status = AccountStatus.createInactive();
const transitionResult = status.transitionTo("invalid");
expect(transitionResult.isFailure).toBe(true);
expect(transitionResult.error.message).toBe("Transición no permitida de inactive a invalid");
});
});
describe("Métodos auxiliares", () => {
it("debería devolver correctamente el valor con getValue()", () => {
const status = AccountStatus.createInactive();
expect(status.getValue()).toBe(ACCOUNT_STATUS.INACTIVE);
});
it("debería devolver correctamente el valor con toString()", () => {
const status = AccountStatus.createActive();
expect(status.toString()).toBe(ACCOUNT_STATUS.ACTIVE);
});
});
});