integration tests
This commit is contained in:
@@ -1,241 +1,282 @@
|
||||
/**
|
||||
* Integration Test: Sponsor Signup Use Case Orchestration
|
||||
*
|
||||
*
|
||||
* Tests the orchestration logic of sponsor signup-related Use Cases:
|
||||
* - CreateSponsorUseCase: Creates a new sponsor account
|
||||
* - SponsorLoginUseCase: Authenticates a sponsor
|
||||
* - SponsorLogoutUseCase: Logs out a sponsor
|
||||
* - Validates that Use Cases correctly interact with their Ports (Repositories, Event Publishers)
|
||||
* - Validates that Use Cases correctly interact with their Ports (Repositories)
|
||||
* - Uses In-Memory adapters for fast, deterministic testing
|
||||
*
|
||||
*
|
||||
* Focus: Business logic orchestration, NOT UI rendering
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
||||
import { InMemorySponsorRepository } from '../../../adapters/sponsors/persistence/inmemory/InMemorySponsorRepository';
|
||||
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
|
||||
import { CreateSponsorUseCase } from '../../../core/sponsors/use-cases/CreateSponsorUseCase';
|
||||
import { SponsorLoginUseCase } from '../../../core/sponsors/use-cases/SponsorLoginUseCase';
|
||||
import { SponsorLogoutUseCase } from '../../../core/sponsors/use-cases/SponsorLogoutUseCase';
|
||||
import { CreateSponsorCommand } from '../../../core/sponsors/ports/CreateSponsorCommand';
|
||||
import { SponsorLoginCommand } from '../../../core/sponsors/ports/SponsorLoginCommand';
|
||||
import { SponsorLogoutCommand } from '../../../core/sponsors/ports/SponsorLogoutCommand';
|
||||
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
|
||||
import { InMemorySponsorRepository } from '../../../adapters/racing/persistence/inmemory/InMemorySponsorRepository';
|
||||
import { CreateSponsorUseCase } from '../../../core/racing/application/use-cases/CreateSponsorUseCase';
|
||||
import { Sponsor } from '../../../core/racing/domain/entities/sponsor/Sponsor';
|
||||
import { Logger } from '../../../core/shared/domain/Logger';
|
||||
|
||||
describe('Sponsor Signup Use Case Orchestration', () => {
|
||||
let sponsorRepository: InMemorySponsorRepository;
|
||||
let eventPublisher: InMemoryEventPublisher;
|
||||
let createSponsorUseCase: CreateSponsorUseCase;
|
||||
let sponsorLoginUseCase: SponsorLoginUseCase;
|
||||
let sponsorLogoutUseCase: SponsorLogoutUseCase;
|
||||
let mockLogger: Logger;
|
||||
|
||||
beforeAll(() => {
|
||||
// TODO: Initialize In-Memory repositories and event publisher
|
||||
// sponsorRepository = new InMemorySponsorRepository();
|
||||
// eventPublisher = new InMemoryEventPublisher();
|
||||
// createSponsorUseCase = new CreateSponsorUseCase({
|
||||
// sponsorRepository,
|
||||
// eventPublisher,
|
||||
// });
|
||||
// sponsorLoginUseCase = new SponsorLoginUseCase({
|
||||
// sponsorRepository,
|
||||
// eventPublisher,
|
||||
// });
|
||||
// sponsorLogoutUseCase = new SponsorLogoutUseCase({
|
||||
// sponsorRepository,
|
||||
// eventPublisher,
|
||||
// });
|
||||
mockLogger = {
|
||||
info: () => {},
|
||||
debug: () => {},
|
||||
warn: () => {},
|
||||
error: () => {},
|
||||
} as unknown as Logger;
|
||||
|
||||
sponsorRepository = new InMemorySponsorRepository(mockLogger);
|
||||
createSponsorUseCase = new CreateSponsorUseCase(sponsorRepository, mockLogger);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// TODO: Clear all In-Memory repositories before each test
|
||||
// sponsorRepository.clear();
|
||||
// eventPublisher.clear();
|
||||
sponsorRepository.clear();
|
||||
});
|
||||
|
||||
describe('CreateSponsorUseCase - Success Path', () => {
|
||||
it('should create a new sponsor account with valid information', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor creates account
|
||||
// Given: No sponsor exists with the given email
|
||||
const sponsorId = 'sponsor-123';
|
||||
const sponsorData = {
|
||||
name: 'Test Company',
|
||||
contactEmail: 'test@example.com',
|
||||
websiteUrl: 'https://testcompany.com',
|
||||
logoUrl: 'https://testcompany.com/logo.png',
|
||||
};
|
||||
|
||||
// When: CreateSponsorUseCase.execute() is called with valid sponsor data
|
||||
// Then: The sponsor should be created in the repository
|
||||
const result = await createSponsorUseCase.execute(sponsorData);
|
||||
|
||||
// Then: The sponsor should be created successfully
|
||||
expect(result.isOk()).toBe(true);
|
||||
const createdSponsor = result.unwrap().sponsor;
|
||||
|
||||
// And: The sponsor should have a unique ID
|
||||
expect(createdSponsor.id.toString()).toBeDefined();
|
||||
|
||||
// And: The sponsor should have the provided company name
|
||||
expect(createdSponsor.name.toString()).toBe('Test Company');
|
||||
|
||||
// And: The sponsor should have the provided contact email
|
||||
expect(createdSponsor.contactEmail.toString()).toBe('test@example.com');
|
||||
|
||||
// And: The sponsor should have the provided website URL
|
||||
// And: The sponsor should have the provided sponsorship interests
|
||||
expect(createdSponsor.websiteUrl?.toString()).toBe('https://testcompany.com');
|
||||
|
||||
// And: The sponsor should have the provided logo URL
|
||||
expect(createdSponsor.logoUrl?.toString()).toBe('https://testcompany.com/logo.png');
|
||||
|
||||
// And: The sponsor should have a created timestamp
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
expect(createdSponsor.createdAt).toBeDefined();
|
||||
|
||||
// And: The sponsor should be retrievable from the repository
|
||||
const retrievedSponsor = await sponsorRepository.findById(createdSponsor.id.toString());
|
||||
expect(retrievedSponsor).toBeDefined();
|
||||
expect(retrievedSponsor?.name.toString()).toBe('Test Company');
|
||||
});
|
||||
|
||||
it('should create a sponsor with multiple sponsorship interests', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor creates account with multiple interests
|
||||
// Given: No sponsor exists with the given email
|
||||
// When: CreateSponsorUseCase.execute() is called with multiple sponsorship interests
|
||||
// Then: The sponsor should be created with all selected interests
|
||||
// And: Each interest should be stored correctly
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
it('should create a sponsor with minimal data', async () => {
|
||||
// Given: No sponsor exists
|
||||
const sponsorData = {
|
||||
name: 'Minimal Company',
|
||||
contactEmail: 'minimal@example.com',
|
||||
};
|
||||
|
||||
// When: CreateSponsorUseCase.execute() is called with minimal data
|
||||
const result = await createSponsorUseCase.execute(sponsorData);
|
||||
|
||||
// Then: The sponsor should be created successfully
|
||||
expect(result.isOk()).toBe(true);
|
||||
const createdSponsor = result.unwrap().sponsor;
|
||||
|
||||
// And: The sponsor should have the provided company name
|
||||
expect(createdSponsor.name.toString()).toBe('Minimal Company');
|
||||
|
||||
// And: The sponsor should have the provided contact email
|
||||
expect(createdSponsor.contactEmail.toString()).toBe('minimal@example.com');
|
||||
|
||||
// And: Optional fields should be undefined
|
||||
expect(createdSponsor.websiteUrl).toBeUndefined();
|
||||
expect(createdSponsor.logoUrl).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should create a sponsor with optional company logo', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor creates account with logo
|
||||
// Given: No sponsor exists with the given email
|
||||
// When: CreateSponsorUseCase.execute() is called with a company logo
|
||||
// Then: The sponsor should be created with the logo reference
|
||||
// And: The logo should be stored in the media repository
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
});
|
||||
it('should create a sponsor with optional fields only', async () => {
|
||||
// Given: No sponsor exists
|
||||
const sponsorData = {
|
||||
name: 'Optional Fields Company',
|
||||
contactEmail: 'optional@example.com',
|
||||
websiteUrl: 'https://optional.com',
|
||||
};
|
||||
|
||||
it('should create a sponsor with default settings', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor creates account with default settings
|
||||
// Given: No sponsor exists with the given email
|
||||
// When: CreateSponsorUseCase.execute() is called
|
||||
// Then: The sponsor should be created with default notification preferences
|
||||
// And: The sponsor should be created with default privacy settings
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
// When: CreateSponsorUseCase.execute() is called with optional fields
|
||||
const result = await createSponsorUseCase.execute(sponsorData);
|
||||
|
||||
// Then: The sponsor should be created successfully
|
||||
expect(result.isOk()).toBe(true);
|
||||
const createdSponsor = result.unwrap().sponsor;
|
||||
|
||||
// And: The sponsor should have the provided website URL
|
||||
expect(createdSponsor.websiteUrl?.toString()).toBe('https://optional.com');
|
||||
|
||||
// And: Logo URL should be undefined
|
||||
expect(createdSponsor.logoUrl).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('CreateSponsorUseCase - Validation', () => {
|
||||
it('should reject sponsor creation with duplicate email', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Duplicate email
|
||||
// Given: A sponsor exists with email "sponsor@example.com"
|
||||
const existingSponsor = Sponsor.create({
|
||||
id: 'existing-sponsor',
|
||||
name: 'Existing Company',
|
||||
contactEmail: 'sponsor@example.com',
|
||||
});
|
||||
await sponsorRepository.create(existingSponsor);
|
||||
|
||||
// When: CreateSponsorUseCase.execute() is called with the same email
|
||||
// Then: Should throw DuplicateEmailError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
const result = await createSponsorUseCase.execute({
|
||||
name: 'New Company',
|
||||
contactEmail: 'sponsor@example.com',
|
||||
});
|
||||
|
||||
// Then: Should return an error
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
});
|
||||
|
||||
it('should reject sponsor creation with invalid email format', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Invalid email format
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called with invalid email
|
||||
// Then: Should throw ValidationError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
const result = await createSponsorUseCase.execute({
|
||||
name: 'Test Company',
|
||||
contactEmail: 'invalid-email',
|
||||
});
|
||||
|
||||
// Then: Should return an error
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('VALIDATION_ERROR');
|
||||
expect(error.details.message).toContain('Invalid sponsor contact email format');
|
||||
});
|
||||
|
||||
it('should reject sponsor creation with missing required fields', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Missing required fields
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called without company name
|
||||
// Then: Should throw ValidationError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
const result = await createSponsorUseCase.execute({
|
||||
name: '',
|
||||
contactEmail: 'test@example.com',
|
||||
});
|
||||
|
||||
// Then: Should return an error
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('VALIDATION_ERROR');
|
||||
expect(error.details.message).toContain('Sponsor name is required');
|
||||
});
|
||||
|
||||
it('should reject sponsor creation with invalid website URL', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Invalid website URL
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called with invalid URL
|
||||
// Then: Should throw ValidationError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
const result = await createSponsorUseCase.execute({
|
||||
name: 'Test Company',
|
||||
contactEmail: 'test@example.com',
|
||||
websiteUrl: 'not-a-valid-url',
|
||||
});
|
||||
|
||||
// Then: Should return an error
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('VALIDATION_ERROR');
|
||||
expect(error.details.message).toContain('Invalid sponsor website URL');
|
||||
});
|
||||
|
||||
it('should reject sponsor creation with invalid password', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Invalid password
|
||||
it('should reject sponsor creation with missing email', async () => {
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called with weak password
|
||||
// Then: Should throw ValidationError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
});
|
||||
// When: CreateSponsorUseCase.execute() is called without email
|
||||
const result = await createSponsorUseCase.execute({
|
||||
name: 'Test Company',
|
||||
contactEmail: '',
|
||||
});
|
||||
|
||||
describe('SponsorLoginUseCase - Success Path', () => {
|
||||
it('should authenticate sponsor with valid credentials', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor logs in
|
||||
// Given: A sponsor exists with email "sponsor@example.com" and password "password123"
|
||||
// When: SponsorLoginUseCase.execute() is called with valid credentials
|
||||
// Then: The sponsor should be authenticated
|
||||
// And: The sponsor should receive an authentication token
|
||||
// And: EventPublisher should emit SponsorLoggedInEvent
|
||||
});
|
||||
|
||||
it('should authenticate sponsor with correct email and password', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor logs in with correct credentials
|
||||
// Given: A sponsor exists with specific credentials
|
||||
// When: SponsorLoginUseCase.execute() is called with matching credentials
|
||||
// Then: The sponsor should be authenticated
|
||||
// And: EventPublisher should emit SponsorLoggedInEvent
|
||||
});
|
||||
});
|
||||
|
||||
describe('SponsorLoginUseCase - Error Handling', () => {
|
||||
it('should reject login with non-existent email', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Non-existent sponsor
|
||||
// Given: No sponsor exists with the given email
|
||||
// When: SponsorLoginUseCase.execute() is called
|
||||
// Then: Should throw SponsorNotFoundError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
|
||||
it('should reject login with incorrect password', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Incorrect password
|
||||
// Given: A sponsor exists with email "sponsor@example.com"
|
||||
// When: SponsorLoginUseCase.execute() is called with wrong password
|
||||
// Then: Should throw InvalidCredentialsError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
|
||||
it('should reject login with invalid email format', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Invalid email format
|
||||
// Given: No sponsor exists
|
||||
// When: SponsorLoginUseCase.execute() is called with invalid email
|
||||
// Then: Should throw ValidationError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
});
|
||||
|
||||
describe('SponsorLogoutUseCase - Success Path', () => {
|
||||
it('should log out authenticated sponsor', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor logs out
|
||||
// Given: A sponsor is authenticated
|
||||
// When: SponsorLogoutUseCase.execute() is called
|
||||
// Then: The sponsor should be logged out
|
||||
// And: EventPublisher should emit SponsorLoggedOutEvent
|
||||
// Then: Should return an error
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('VALIDATION_ERROR');
|
||||
expect(error.details.message).toContain('Sponsor contact email is required');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Sponsor Data Orchestration', () => {
|
||||
it('should correctly create sponsor with sponsorship interests', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor with multiple interests
|
||||
it('should correctly create sponsor with all optional fields', async () => {
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called with interests: ["League", "Team", "Driver"]
|
||||
// Then: The sponsor should have all three interests stored
|
||||
// And: Each interest should be retrievable
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
const sponsorData = {
|
||||
name: 'Full Featured Company',
|
||||
contactEmail: 'full@example.com',
|
||||
websiteUrl: 'https://fullfeatured.com',
|
||||
logoUrl: 'https://fullfeatured.com/logo.png',
|
||||
};
|
||||
|
||||
// When: CreateSponsorUseCase.execute() is called with all fields
|
||||
const result = await createSponsorUseCase.execute(sponsorData);
|
||||
|
||||
// Then: The sponsor should be created with all fields
|
||||
expect(result.isOk()).toBe(true);
|
||||
const createdSponsor = result.unwrap().sponsor;
|
||||
|
||||
expect(createdSponsor.name.toString()).toBe('Full Featured Company');
|
||||
expect(createdSponsor.contactEmail.toString()).toBe('full@example.com');
|
||||
expect(createdSponsor.websiteUrl?.toString()).toBe('https://fullfeatured.com');
|
||||
expect(createdSponsor.logoUrl?.toString()).toBe('https://fullfeatured.com/logo.png');
|
||||
expect(createdSponsor.createdAt).toBeDefined();
|
||||
});
|
||||
|
||||
it('should correctly create sponsor with default notification preferences', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor with default notifications
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called
|
||||
// Then: The sponsor should have default notification preferences
|
||||
// And: All notification types should be enabled by default
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
it('should generate unique IDs for each sponsor', async () => {
|
||||
// Given: No sponsors exist
|
||||
const sponsorData1 = {
|
||||
name: 'Company 1',
|
||||
contactEmail: 'company1@example.com',
|
||||
};
|
||||
const sponsorData2 = {
|
||||
name: 'Company 2',
|
||||
contactEmail: 'company2@example.com',
|
||||
};
|
||||
|
||||
// When: Creating two sponsors
|
||||
const result1 = await createSponsorUseCase.execute(sponsorData1);
|
||||
const result2 = await createSponsorUseCase.execute(sponsorData2);
|
||||
|
||||
// Then: Both should succeed and have unique IDs
|
||||
expect(result1.isOk()).toBe(true);
|
||||
expect(result2.isOk()).toBe(true);
|
||||
|
||||
const sponsor1 = result1.unwrap().sponsor;
|
||||
const sponsor2 = result2.unwrap().sponsor;
|
||||
|
||||
expect(sponsor1.id.toString()).not.toBe(sponsor2.id.toString());
|
||||
});
|
||||
|
||||
it('should correctly create sponsor with default privacy settings', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Sponsor with default privacy
|
||||
it('should persist sponsor in repository after creation', async () => {
|
||||
// Given: No sponsor exists
|
||||
// When: CreateSponsorUseCase.execute() is called
|
||||
// Then: The sponsor should have default privacy settings
|
||||
// And: Public profile should be enabled by default
|
||||
// And: EventPublisher should emit SponsorCreatedEvent
|
||||
const sponsorData = {
|
||||
name: 'Persistent Company',
|
||||
contactEmail: 'persistent@example.com',
|
||||
};
|
||||
|
||||
// When: Creating a sponsor
|
||||
const result = await createSponsorUseCase.execute(sponsorData);
|
||||
|
||||
// Then: The sponsor should be retrievable from the repository
|
||||
expect(result.isOk()).toBe(true);
|
||||
const createdSponsor = result.unwrap().sponsor;
|
||||
|
||||
const retrievedSponsor = await sponsorRepository.findById(createdSponsor.id.toString());
|
||||
expect(retrievedSponsor).toBeDefined();
|
||||
expect(retrievedSponsor?.name.toString()).toBe('Persistent Company');
|
||||
expect(retrievedSponsor?.contactEmail.toString()).toBe('persistent@example.com');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user