refactor use cases
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
LoginUseCase,
|
||||
type LoginInput,
|
||||
type LoginResult,
|
||||
type LoginErrorCode,
|
||||
} from './LoginUseCase';
|
||||
import { EmailAddress } from '../../domain/value-objects/EmailAddress';
|
||||
import { UserId } from '../../domain/value-objects/UserId';
|
||||
import { PasswordHash } from '../../domain/value-objects/PasswordHash';
|
||||
import { LoginUseCase } from './LoginUseCase';
|
||||
import type { IAuthRepository } from '../../domain/repositories/IAuthRepository';
|
||||
import type { IPasswordHashingService } from '../../domain/services/PasswordHashingService';
|
||||
import { User } from '../../domain/entities/User';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import { User } from '../../domain/entities/User';
|
||||
import { UserId } from '../../domain/value-objects/UserId';
|
||||
import { PasswordHash } from '../../domain/value-objects/PasswordHash';
|
||||
import { EmailAddress } from '../../domain/value-objects/EmailAddress';
|
||||
|
||||
describe('LoginUseCase', () => {
|
||||
let authRepo: {
|
||||
@@ -22,129 +16,82 @@ describe('LoginUseCase', () => {
|
||||
let passwordService: {
|
||||
verify: Mock;
|
||||
};
|
||||
let logger: Logger & { error: Mock };
|
||||
let output: UseCaseOutputPort<LoginResult> & { present: Mock };
|
||||
let logger: Logger;
|
||||
let useCase: LoginUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
authRepo = {
|
||||
findByEmail: vi.fn(),
|
||||
};
|
||||
|
||||
passwordService = {
|
||||
verify: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<LoginResult> & { present: Mock };
|
||||
} as unknown as Logger;
|
||||
|
||||
useCase = new LoginUseCase(
|
||||
authRepo as unknown as IAuthRepository,
|
||||
passwordService as unknown as IPasswordHashingService,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns ok and presents user when credentials are valid', async () => {
|
||||
const input: LoginInput = {
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
|
||||
it('successfully logs in with valid credentials', async () => {
|
||||
const user = User.create({
|
||||
id: UserId.fromString('user-1'),
|
||||
id: UserId.create(),
|
||||
displayName: 'John Smith',
|
||||
email: emailVO.value,
|
||||
passwordHash: PasswordHash.fromHash('stored-hash'),
|
||||
email: 'test@example.com',
|
||||
passwordHash: PasswordHash.fromHash('hashed-password'),
|
||||
});
|
||||
|
||||
authRepo.findByEmail.mockResolvedValue(user);
|
||||
passwordService.verify.mockResolvedValue(true);
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<LoginErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(authRepo.findByEmail).toHaveBeenCalledWith(emailVO);
|
||||
expect(passwordService.verify).toHaveBeenCalledWith(input.password, 'stored-hash');
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as LoginResult;
|
||||
expect(presented.user).toBe(user);
|
||||
});
|
||||
|
||||
it('returns INVALID_CREDENTIALS when user is not found', async () => {
|
||||
const input: LoginInput = {
|
||||
email: 'missing@example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
|
||||
authRepo.findByEmail.mockResolvedValue(null);
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<LoginErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
|
||||
expect(error.code).toBe('INVALID_CREDENTIALS');
|
||||
expect(error.details?.message).toBe('Invalid credentials');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INVALID_CREDENTIALS when password is invalid', async () => {
|
||||
const input: LoginInput = {
|
||||
const result = await useCase.execute({
|
||||
email: 'test@example.com',
|
||||
password: 'wrong-password',
|
||||
};
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
|
||||
const user = User.create({
|
||||
id: UserId.fromString('user-1'),
|
||||
displayName: 'Jane Smith',
|
||||
email: emailVO.value,
|
||||
passwordHash: PasswordHash.fromHash('stored-hash'),
|
||||
password: 'Password123',
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const loginResult = result.unwrap();
|
||||
expect(loginResult.user).toBe(user);
|
||||
expect(authRepo.findByEmail).toHaveBeenCalledTimes(1);
|
||||
expect(passwordService.verify).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('returns error for invalid credentials', async () => {
|
||||
const user = User.create({
|
||||
id: UserId.create(),
|
||||
displayName: 'John Smith',
|
||||
email: 'test@example.com',
|
||||
passwordHash: PasswordHash.fromHash('hashed-password'),
|
||||
});
|
||||
authRepo.findByEmail.mockResolvedValue(user);
|
||||
passwordService.verify.mockResolvedValue(false);
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<LoginErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
|
||||
expect(error.code).toBe('INVALID_CREDENTIALS');
|
||||
expect(error.details?.message).toBe('Invalid credentials');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('wraps unexpected errors as REPOSITORY_ERROR and logs them', async () => {
|
||||
const input: LoginInput = {
|
||||
const result = await useCase.execute({
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
|
||||
authRepo.findByEmail.mockRejectedValue(new Error('DB failure'));
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<LoginErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
password: 'WrongPassword',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details?.message).toBe('DB failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
expect(result.unwrapErr().code).toBe('INVALID_CREDENTIALS');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when user does not exist', async () => {
|
||||
authRepo.findByEmail.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({
|
||||
email: 'nonexistent@example.com',
|
||||
password: 'Password123',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('INVALID_CREDENTIALS');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user