54 lines
2.2 KiB
TypeScript
54 lines
2.2 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { INestApplication } from '@nestjs/common';
|
|
import request from 'supertest';
|
|
import { App } from 'supertest/types';
|
|
import { AppModule } from './../src/app.module';
|
|
import { HiboutikService } from './../src/services/hiboutik/hiboutik.service';
|
|
import { PrismaService } from './../prisma/prisma.service';
|
|
|
|
// Boots the real AppModule wiring, without database nor Hiboutik calls
|
|
describe('AppController (e2e)', () => {
|
|
let app: INestApplication<App>;
|
|
const hiboutikService = { startSync: jest.fn() };
|
|
// Rate limiting counters (ThrottlerDatabaseStorage): the database answers "not blocked" unless a test says otherwise
|
|
const notBlocked = { totalHits: 1, timeToExpire: 60, isBlocked: false, timeToBlockExpire: 0 };
|
|
const prisma = { $queryRaw: jest.fn(), throttlerRecord: { deleteMany: jest.fn().mockResolvedValue({ count: 0 }) } };
|
|
|
|
beforeEach(async () => {
|
|
hiboutikService.startSync.mockReset();
|
|
prisma.$queryRaw.mockReset().mockResolvedValue([notBlocked]);
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
})
|
|
.overrideProvider(PrismaService)
|
|
.useValue(prisma)
|
|
.overrideProvider(HiboutikService)
|
|
.useValue(hiboutikService)
|
|
.compile();
|
|
|
|
app = moduleFixture.createNestApplication();
|
|
await app.init();
|
|
});
|
|
|
|
it('POST /sync starts a synchronization (202)', () => {
|
|
hiboutikService.startSync.mockResolvedValue(true);
|
|
return request(app.getHttpServer()).post('/sync').expect(202).expect({ message: 'Hiboutik synchronization started' });
|
|
});
|
|
|
|
it('POST /sync answers 409 when a synchronization is already running', () => {
|
|
hiboutikService.startSync.mockResolvedValue(false);
|
|
return request(app.getHttpServer()).post('/sync').expect(409);
|
|
});
|
|
|
|
it('POST /sync answers 429 when the shared rate limiting counter is blocked', async () => {
|
|
prisma.$queryRaw.mockResolvedValue([{ totalHits: 6, timeToExpire: 30, isBlocked: true, timeToBlockExpire: 30 }]);
|
|
|
|
await request(app.getHttpServer()).post('/sync').expect(429).expect('Retry-After', '30');
|
|
expect(hiboutikService.startSync).not.toHaveBeenCalled();
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
});
|