refactor use cases
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { LoginWithEmailUseCase, type LoginCommandDTO } from './LoginWithEmailUseCase';
|
||||
import {
|
||||
LoginWithEmailUseCase,
|
||||
type LoginWithEmailInput,
|
||||
type LoginWithEmailResult,
|
||||
type LoginWithEmailErrorCode,
|
||||
} from './LoginWithEmailUseCase';
|
||||
import type { IUserRepository, StoredUser } from '../../domain/repositories/IUserRepository';
|
||||
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
|
||||
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('LoginWithEmailUseCase', () => {
|
||||
let userRepository: {
|
||||
@@ -13,6 +20,8 @@ describe('LoginWithEmailUseCase', () => {
|
||||
getCurrentSession: Mock;
|
||||
clearSession: Mock;
|
||||
};
|
||||
let logger: Logger & { error: Mock };
|
||||
let output: UseCaseOutputPort<LoginWithEmailResult> & { present: Mock };
|
||||
let useCase: LoginWithEmailUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -24,14 +33,26 @@ describe('LoginWithEmailUseCase', () => {
|
||||
getCurrentSession: vi.fn(),
|
||||
clearSession: vi.fn(),
|
||||
};
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger & { error: Mock };
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<LoginWithEmailResult> & { present: Mock };
|
||||
|
||||
useCase = new LoginWithEmailUseCase(
|
||||
userRepository as unknown as IUserRepository,
|
||||
sessionPort as unknown as IdentitySessionPort,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates a session for valid credentials', async () => {
|
||||
const command: LoginCommandDTO = {
|
||||
it('returns ok and presents session result for valid credentials', async () => {
|
||||
const input: LoginWithEmailInput = {
|
||||
email: 'Test@Example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
@@ -45,7 +66,7 @@ describe('LoginWithEmailUseCase', () => {
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
const session: AuthSessionDTO = {
|
||||
const session = {
|
||||
user: {
|
||||
id: storedUser.id,
|
||||
email: storedUser.email,
|
||||
@@ -59,35 +80,59 @@ describe('LoginWithEmailUseCase', () => {
|
||||
userRepository.findByEmail.mockResolvedValue(storedUser);
|
||||
sessionPort.createSession.mockResolvedValue(session);
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
const result: Result<void, ApplicationErrorCode<LoginWithEmailErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(userRepository.findByEmail).toHaveBeenCalledWith('test@example.com');
|
||||
expect(sessionPort.createSession).toHaveBeenCalledWith({
|
||||
id: storedUser.id,
|
||||
email: storedUser.email,
|
||||
displayName: storedUser.displayName,
|
||||
primaryDriverId: undefined,
|
||||
});
|
||||
expect(result).toEqual(session);
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as LoginWithEmailResult;
|
||||
expect(presented.sessionToken).toBe('token-123');
|
||||
expect(presented.userId).toBe(storedUser.id);
|
||||
expect(presented.displayName).toBe(storedUser.displayName);
|
||||
expect(presented.email).toBe(storedUser.email);
|
||||
});
|
||||
|
||||
it('throws when email or password is missing', async () => {
|
||||
await expect(useCase.execute({ email: '', password: 'x' })).rejects.toThrow('Email and password are required');
|
||||
await expect(useCase.execute({ email: 'a@example.com', password: '' })).rejects.toThrow('Email and password are required');
|
||||
it('returns INVALID_INPUT when email or password is missing', async () => {
|
||||
const result1 = await useCase.execute({ email: '', password: 'x' });
|
||||
const result2 = await useCase.execute({ email: 'a@example.com', password: '' });
|
||||
|
||||
expect(result1.isErr()).toBe(true);
|
||||
expect(result1.unwrapErr().code).toBe('INVALID_INPUT');
|
||||
expect(result2.isErr()).toBe(true);
|
||||
expect(result2.unwrapErr().code).toBe('INVALID_INPUT');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when user does not exist', async () => {
|
||||
const command: LoginCommandDTO = {
|
||||
it('returns INVALID_CREDENTIALS when user does not exist', async () => {
|
||||
const input: LoginWithEmailInput = {
|
||||
email: 'missing@example.com',
|
||||
password: 'password',
|
||||
};
|
||||
|
||||
userRepository.findByEmail.mockResolvedValue(null);
|
||||
|
||||
await expect(useCase.execute(command)).rejects.toThrow('Invalid email or password');
|
||||
const result: Result<void, ApplicationErrorCode<LoginWithEmailErrorCode, { 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 email or password');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when password is invalid', async () => {
|
||||
const command: LoginCommandDTO = {
|
||||
it('returns INVALID_CREDENTIALS when password is invalid', async () => {
|
||||
const input: LoginWithEmailInput = {
|
||||
email: 'test@example.com',
|
||||
password: 'wrong',
|
||||
};
|
||||
@@ -103,6 +148,33 @@ describe('LoginWithEmailUseCase', () => {
|
||||
|
||||
userRepository.findByEmail.mockResolvedValue(storedUser);
|
||||
|
||||
await expect(useCase.execute(command)).rejects.toThrow('Invalid email or password');
|
||||
const result: Result<void, ApplicationErrorCode<LoginWithEmailErrorCode, { 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 email or password');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('wraps unexpected errors as REPOSITORY_ERROR and logs them', async () => {
|
||||
const input: LoginWithEmailInput = {
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
|
||||
userRepository.findByEmail.mockRejectedValue(new Error('DB failure'));
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<LoginWithEmailErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user