forked from mtusi2026/mtusi2026lab2
314 lines
8.9 KiB
TypeScript
314 lines
8.9 KiB
TypeScript
import { HealthService } from './../src/health/health.service.js';
|
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { INestApplication, ValidationPipe } from '@nestjs/common';
|
|
import request from 'supertest';
|
|
import { App } from 'supertest/types';
|
|
import { AppModule } from './../src/app.module.js';
|
|
import { RequestIdInterceptor } from './../src/common/http/request-id.interceptor.js';
|
|
import { ApiExceptionFilter } from './../src/common/errors/api-exception.filter.js';
|
|
|
|
describe('Foundation HTTP contract', () => {
|
|
let app: INestApplication<App>;
|
|
|
|
beforeEach(async () => {
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
}).compile();
|
|
|
|
app = moduleFixture.createNestApplication();
|
|
|
|
app.useGlobalInterceptors(new RequestIdInterceptor());
|
|
app.useGlobalFilters(new ApiExceptionFilter());
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
}),
|
|
);
|
|
|
|
await app.init();
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('returns liveness status with request id', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.get('/health/live')
|
|
.expect(200);
|
|
|
|
expect(response.body).toEqual({ status: 'ok' });
|
|
expect(response.headers['x-request-id']).toEqual(expect.any(String));
|
|
expect(response.headers['x-request-id']).not.toHaveLength(0);
|
|
});
|
|
|
|
it('returns readiness status with request id', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.get('/health/ready')
|
|
.expect(200);
|
|
|
|
expect(response.body).toEqual({ status: 'ok' });
|
|
expect(response.headers['x-request-id']).toEqual(expect.any(String));
|
|
});
|
|
|
|
it('keeps liveness alive while readiness is draining', async () => {
|
|
app.get(HealthService).setDraining();
|
|
|
|
const liveResponse = await request(app.getHttpServer())
|
|
.get('/health/live')
|
|
.expect(200);
|
|
expect(liveResponse.body).toEqual({ status: 'ok' });
|
|
|
|
const readyResponse = await request(app.getHttpServer())
|
|
.get('/health/ready')
|
|
.expect(503);
|
|
const requestId = readyResponse.headers['x-request-id'];
|
|
expect(requestId).toEqual(expect.any(String));
|
|
expect(readyResponse.body).toEqual({
|
|
error: {
|
|
code: 'unavailable',
|
|
message: 'Service is shutting down',
|
|
request_id: requestId,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('returns contract error with matching request id for unknown route', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.get('/health/unknown')
|
|
.expect(404);
|
|
|
|
const requestId = response.headers['x-request-id'];
|
|
|
|
expect(requestId).toEqual(expect.any(String));
|
|
expect(response.body).toEqual({
|
|
error: {
|
|
code: 'not_found',
|
|
message: 'Resource not found',
|
|
request_id: requestId,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('registers a user and returns it from /me', async () => {
|
|
const created = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'alice',
|
|
display_name: 'Алиса',
|
|
})
|
|
.expect(201);
|
|
|
|
const user = created.body;
|
|
|
|
const current = await request(app.getHttpServer())
|
|
.get('/api/v1/users/me')
|
|
.set('X-User-Id', user.id)
|
|
.expect(200);
|
|
|
|
expect(current.body).toEqual(user);
|
|
});
|
|
|
|
it('rejects /me without X-User-Id', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.get('/api/v1/users/me')
|
|
.expect(401);
|
|
|
|
expect(response.body.error.code).toBe('unauthorized');
|
|
expect(response.headers['x-request-id']).toBe(
|
|
response.body.error.request_id,
|
|
);
|
|
});
|
|
|
|
it('rejects duplicate usernames', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'duplicate_user',
|
|
display_name: 'Первый',
|
|
})
|
|
.expect(201);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'duplicate_user',
|
|
display_name: 'Второй',
|
|
})
|
|
.expect(409);
|
|
|
|
expect(response.body.error.code).toBe('conflict');
|
|
});
|
|
|
|
it('creates a chat and hides it from a non-member', async () => {
|
|
const aliceResponse = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'alice_chat',
|
|
display_name: 'Алиса',
|
|
})
|
|
.expect(201);
|
|
|
|
const bobResponse = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'bob_chat',
|
|
display_name: 'Боб',
|
|
})
|
|
.expect(201);
|
|
|
|
const aliceId = aliceResponse.body.id;
|
|
const bobId = bobResponse.body.id;
|
|
|
|
const chatResponse = await request(app.getHttpServer())
|
|
.post('/api/v1/chats')
|
|
.set('X-User-Id', aliceId)
|
|
.send({
|
|
title: 'Учебный чат',
|
|
member_ids: [aliceId, bobId],
|
|
})
|
|
.expect(201);
|
|
|
|
expect(chatResponse.body.member_ids).toEqual([aliceId, bobId]);
|
|
|
|
await request(app.getHttpServer())
|
|
.get(`/api/v1/chats/${chatResponse.body.id}`)
|
|
.set('X-User-Id', bobId)
|
|
.expect(200);
|
|
|
|
const outsiderResponse = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'charlie_chat',
|
|
display_name: 'Чарли',
|
|
})
|
|
.expect(201);
|
|
|
|
await request(app.getHttpServer())
|
|
.get(`/api/v1/chats/${chatResponse.body.id}`)
|
|
.set('X-User-Id', outsiderResponse.body.id)
|
|
.expect(404);
|
|
});
|
|
|
|
it('rejects chat when current user is not in member_ids', async () => {
|
|
const aliceResponse = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'alice_member',
|
|
display_name: 'Алиса',
|
|
})
|
|
.expect(201);
|
|
|
|
const bobResponse = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({
|
|
username: 'bob_member',
|
|
display_name: 'Боб',
|
|
})
|
|
.expect(201);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.post('/api/v1/chats')
|
|
.set('X-User-Id', aliceResponse.body.id)
|
|
.send({
|
|
title: 'Неверный чат',
|
|
member_ids: [bobResponse.body.id],
|
|
})
|
|
.expect(400);
|
|
|
|
expect(response.body.error.code).toBe('invalid_request');
|
|
});
|
|
it('paginates chat history without repeating messages', async () => {
|
|
const alice = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({ username: 'page_alice', display_name: 'Алиса' })
|
|
.expect(201);
|
|
|
|
const bob = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({ username: 'page_bob', display_name: 'Боб' })
|
|
.expect(201);
|
|
|
|
const aliceId = alice.body.id;
|
|
const bobId = bob.body.id;
|
|
const chat = await request(app.getHttpServer())
|
|
.post('/api/v1/chats')
|
|
.set('X-User-Id', aliceId)
|
|
.send({ title: 'История', member_ids: [aliceId, bobId] })
|
|
.expect(201);
|
|
|
|
const createdMessages: Array<{ id: string }> = [];
|
|
for (const text of ['one', 'two', 'three']) {
|
|
const response = await request(app.getHttpServer())
|
|
.post(`/api/v1/chats/${chat.body.id}/messages`)
|
|
.set('X-User-Id', aliceId)
|
|
.send({ text })
|
|
.expect(201);
|
|
|
|
createdMessages.push(response.body);
|
|
}
|
|
|
|
const firstPage = await request(app.getHttpServer())
|
|
.get(`/api/v1/chats/${chat.body.id}/messages`)
|
|
.query({ limit: 2 })
|
|
.set('X-User-Id', bobId)
|
|
.expect(200);
|
|
|
|
expect(firstPage.body.items.map((item: { id: string }) => item.id)).toEqual(
|
|
createdMessages.slice(0, 2).map((item) => item.id),
|
|
);
|
|
expect(firstPage.body.next_cursor).toEqual(expect.any(String));
|
|
|
|
const secondPage = await request(app.getHttpServer())
|
|
.get(`/api/v1/chats/${chat.body.id}/messages`)
|
|
.query({ limit: 2, cursor: firstPage.body.next_cursor })
|
|
.set('X-User-Id', bobId)
|
|
.expect(200);
|
|
|
|
expect(secondPage.body.items.map((item: { id: string }) => item.id)).toEqual(
|
|
[createdMessages[2].id],
|
|
);
|
|
expect(secondPage.body.next_cursor).toBeNull();
|
|
});
|
|
|
|
it('rejects blank messages and sender spoofing fields', async () => {
|
|
const alice = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({ username: 'blank_alice', display_name: 'Алиса' })
|
|
.expect(201);
|
|
|
|
const chat = await request(app.getHttpServer())
|
|
.post('/api/v1/chats')
|
|
.set('X-User-Id', alice.body.id)
|
|
.send({ title: 'Проверка', member_ids: [alice.body.id] })
|
|
.expect(201);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.post(`/api/v1/chats/${chat.body.id}/messages`)
|
|
.set('X-User-Id', alice.body.id)
|
|
.send({ text: ' ', sender_id: 'spoofed' })
|
|
.expect(400);
|
|
|
|
expect(response.body.error.code).toBe('invalid_request');
|
|
});
|
|
|
|
it('rejects an invalid cursor and an invalid limit', async () => {
|
|
const alice = await request(app.getHttpServer())
|
|
.post('/api/v1/users')
|
|
.send({ username: 'cursor_alice', display_name: 'Алиса' })
|
|
.expect(201);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.get('/api/v1/chats')
|
|
.query({ limit: 101, cursor: 'not-a-cursor' })
|
|
.set('X-User-Id', alice.body.id)
|
|
.expect(400);
|
|
|
|
expect(response.body.error.code).toBe('invalid_request');
|
|
});
|
|
|
|
});
|