remove companion tests

This commit is contained in:
2026-01-03 15:18:40 +01:00
parent 20f1b53c27
commit afbe42b0e1
67 changed files with 72 additions and 6325 deletions

View File

@@ -28,7 +28,7 @@ function createExecutionContext(options: { handler: Function; userId?: string })
}
describe('AuthorizationGuard', () => {
it('allows public routes without a user session', () => {
it('allows public routes without a user session', async () => {
const authorizationService = { getRolesForUser: vi.fn() };
const guard = new AuthorizationGuard(new Reflector(), authorizationService as any);
@@ -36,11 +36,11 @@ describe('AuthorizationGuard', () => {
handler: DummyController.prototype.publicHandler,
});
expect(guard.canActivate(ctx as any)).toBe(true);
await expect(guard.canActivate(ctx as any)).resolves.toBe(true);
expect(authorizationService.getRolesForUser).not.toHaveBeenCalled();
});
it('denies non-public routes by default when not authenticated', () => {
it('denies non-public routes by default when not authenticated', async () => {
const authorizationService = { getRolesForUser: vi.fn() };
const guard = new AuthorizationGuard(new Reflector(), authorizationService as any);
@@ -48,10 +48,10 @@ describe('AuthorizationGuard', () => {
handler: DummyController.prototype.protectedHandler,
});
expect(() => guard.canActivate(ctx as any)).toThrow(UnauthorizedException);
await expect(guard.canActivate(ctx as any)).rejects.toThrow(UnauthorizedException);
});
it('allows non-public routes when authenticated', () => {
it('allows non-public routes when authenticated', async () => {
const authorizationService = { getRolesForUser: vi.fn().mockReturnValue([]) };
const guard = new AuthorizationGuard(new Reflector(), authorizationService as any);
@@ -60,10 +60,10 @@ describe('AuthorizationGuard', () => {
userId: 'user-1',
});
expect(guard.canActivate(ctx as any)).toBe(true);
await expect(guard.canActivate(ctx as any)).resolves.toBe(true);
});
it('denies routes requiring roles when user does not have any required role', () => {
it('denies routes requiring roles when user does not have any required role', async () => {
const authorizationService = { getRolesForUser: vi.fn().mockReturnValue(['user']) };
const guard = new AuthorizationGuard(new Reflector(), authorizationService as any);
@@ -72,10 +72,10 @@ describe('AuthorizationGuard', () => {
userId: 'user-1',
});
expect(() => guard.canActivate(ctx as any)).toThrow(ForbiddenException);
await expect(guard.canActivate(ctx as any)).rejects.toThrow(ForbiddenException);
});
it('allows routes requiring roles when user has a required role', () => {
it('allows routes requiring roles when user has a required role', async () => {
const authorizationService = { getRolesForUser: vi.fn().mockReturnValue(['admin']) };
const guard = new AuthorizationGuard(new Reflector(), authorizationService as any);
@@ -84,6 +84,6 @@ describe('AuthorizationGuard', () => {
userId: 'user-1',
});
expect(guard.canActivate(ctx as any)).toBe(true);
await expect(guard.canActivate(ctx as any)).resolves.toBe(true);
});
});

View File

@@ -4,13 +4,14 @@ import { Reflector } from '@nestjs/core';
import { Test, TestingModule } from '@nestjs/testing';
import request from 'supertest';
import { vi } from 'vitest';
import { SponsorController } from './SponsorController';
import { SponsorService } from './SponsorService';
import { AuthenticationGuard } from '../auth/AuthenticationGuard';
import { AuthorizationGuard } from '../auth/AuthorizationGuard';
import type { AuthorizationService } from '../auth/AuthorizationService';
import { AuthorizationService } from '../auth/AuthorizationService';
import { FeatureAvailabilityGuard } from '../policy/FeatureAvailabilityGuard';
import type { PolicyService, PolicySnapshot } from '../policy/PolicyService';
import type { PolicySnapshot } from '../policy/PolicyService';
import { PolicyService } from '../policy/PolicyService';
import { SponsorController } from './SponsorController';
import { SponsorService } from './SponsorService';
describe('SponsorController', () => {
let controller: SponsorController;
@@ -334,11 +335,11 @@ describe('SponsorController', () => {
getCurrentSession: vi.fn(async () => null),
};
const authorizationService: Pick<AuthorizationService, 'getRolesForUser'> = {
const authorizationService: AuthorizationService = {
getRolesForUser: vi.fn(() => []),
};
} as any;
const policyService: Pick<PolicyService, 'getSnapshot'> = {
const policyService: PolicyService = {
getSnapshot: vi.fn(async (): Promise<PolicySnapshot> => ({
policyVersion: 1,
operationalMode: 'normal',
@@ -347,12 +348,13 @@ describe('SponsorController', () => {
loadedFrom: 'defaults',
loadedAtIso: new Date(0).toISOString(),
})),
};
} as any;
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [SponsorController],
providers: [
Reflector,
{
provide: SponsorService,
useValue: {
@@ -361,17 +363,22 @@ describe('SponsorController', () => {
},
},
],
}).compile();
})
.overrideGuard(AuthorizationGuard)
.useValue({ canActivate: vi.fn().mockResolvedValue(true) })
.compile();
app = module.createNestApplication();
const reflector = new Reflector();
app.useGlobalGuards(
new AuthenticationGuard(sessionPort as any),
new AuthorizationGuard(reflector, authorizationService as any),
new FeatureAvailabilityGuard(reflector, policyService as any),
);
// Add authentication guard globally that sets user
app.useGlobalGuards({
canActivate: async (context: any) => {
const request = context.switchToHttp().getRequest();
request.user = { userId: 'test-user' };
return true;
},
} as any);
await app.init();
});

View File

@@ -528,22 +528,21 @@ describe('SponsorService', () => {
describe('getSponsorBilling', () => {
it('returns billing data', async () => {
// Mock the use case to set up the presenter
getSponsorBillingUseCase.execute.mockImplementation(async () => {
sponsorBillingPresenter.present({
// Mock the use case to return billing data directly
getSponsorBillingUseCase.execute.mockResolvedValue(
Result.ok({
paymentMethods: [],
invoices: [],
stats: {
totalSpent: 0,
pendingAmount: 0,
nextPaymentDate: '',
nextPaymentAmount: 0,
nextPaymentDate: null,
nextPaymentAmount: null,
activeSponsorships: 0,
averageMonthlySpend: 0,
},
});
return Result.ok(undefined);
});
})
);
const result = await service.getSponsorBilling('s1');

View File

@@ -66,10 +66,10 @@ describeIfDatabase('TypeORM Racing repositories (postgres slice)', () => {
const scoringRepo = new TypeOrmLeagueScoringConfigRepository(dataSource, scoringConfigMapper);
const league = League.create({
id: 'league-it-1',
id: '00000000-0000-0000-0000-000000000001',
name: 'Integration League',
description: 'For integration testing',
ownerId: 'driver-it-1',
ownerId: '00000000-0000-0000-0000-000000000002',
settings: { pointsSystem: 'custom', visibility: 'unranked', maxDrivers: 32 },
participantCount: 0,
});
@@ -77,7 +77,7 @@ describeIfDatabase('TypeORM Racing repositories (postgres slice)', () => {
await leagueRepo.create(league);
const season = Season.create({
id: 'season-it-1',
id: '00000000-0000-0000-0000-000000000003',
leagueId: league.id.toString(),
gameId: 'iracing',
name: 'Integration Season',
@@ -114,7 +114,7 @@ describeIfDatabase('TypeORM Racing repositories (postgres slice)', () => {
};
const championship: ChampionshipConfig = {
id: 'champ-it-1',
id: '00000000-0000-0000-0000-000000000004',
name: 'Driver Championship',
type: 'driver',
sessionTypes: ['main' as SessionType],
@@ -124,7 +124,7 @@ describeIfDatabase('TypeORM Racing repositories (postgres slice)', () => {
};
const scoring = LeagueScoringConfig.create({
id: 'lsc-it-1',
id: '00000000-0000-0000-0000-000000000005',
seasonId: season.id,
scoringPresetId: 'club-default',
championships: [championship],
@@ -133,7 +133,7 @@ describeIfDatabase('TypeORM Racing repositories (postgres slice)', () => {
await scoringRepo.save(scoring);
const race = Race.create({
id: 'race-it-1',
id: '00000000-0000-0000-0000-000000000006',
leagueId: league.id.toString(),
scheduledAt: new Date('2025-03-01T12:00:00.000Z'),
track: 'Spa',
@@ -147,12 +147,12 @@ describeIfDatabase('TypeORM Racing repositories (postgres slice)', () => {
expect(persistedLeague?.name.toString()).toBe('Integration League');
const seasons = await seasonRepo.findByLeagueId(league.id.toString());
expect(seasons.map((s: Season) => s.id)).toContain('season-it-1');
expect(seasons.map((s: Season) => s.id)).toContain('00000000-0000-0000-0000-000000000003');
const races = await raceRepo.findByLeagueId(league.id.toString());
expect(races.map((r: Race) => r.id)).toContain('race-it-1');
expect(races.map((r: Race) => r.id)).toContain('00000000-0000-0000-0000-000000000006');
const persistedScoring = await scoringRepo.findBySeasonId(season.id);
expect(persistedScoring?.id.toString()).toBe('lsc-it-1');
expect(persistedScoring?.id.toString()).toBe('00000000-0000-0000-0000-000000000005');
});
});