feat: add basic auth

This commit is contained in:
2026-10-21 10:31:23 +03:00
parent ef2bc6f8b6
commit 0563ebfb10
28 changed files with 633 additions and 176 deletions
+20 -43
View File
@@ -1,30 +1,20 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import request from 'supertest';
import { randomUUID } from 'crypto';
import { AppModule } from '../src/app.module';
import { configureApp } from '../src/configure-app';
import {
bearer,
createTestApp,
login,
PASSWORD,
registerUser,
} from './helpers';
describe('Безопасность при конкурентном доступе (e2e)', () => {
let app: INestApplication;
let server: any;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const fastifyApp =
moduleRef.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);
app = fastifyApp;
configureApp(fastifyApp);
await fastifyApp.listen(0, '127.0.0.1');
server = fastifyApp.getHttpServer();
({ app, server } = await createTestApp());
});
afterAll(async () => {
@@ -35,30 +25,17 @@ describe('Безопасность при конкурентном доступ
const CLIENTS = 10;
const MESSAGES_PER_CLIENT = 10;
const owner = await request(server)
.post('/api/v1/users')
.send({
username: `owner_${randomUUID().slice(0, 8)}`,
display_name: 'Owner',
})
.expect(201);
const owner = await registerUser(server, 'owner');
const clients = await Promise.all(
Array.from({ length: CLIENTS }, (_, i) =>
request(server)
.post('/api/v1/users')
.send({
username: `racer${i}_${randomUUID().slice(0, 6)}`,
display_name: `Racer ${i}`,
})
.expect(201),
registerUser(server, `racer${i}`),
),
);
const memberIds = [owner.body.id, ...clients.map((c) => c.body.id)];
const memberIds = [owner.id, ...clients.map((c) => c.id)];
const chat = await request(server)
.post('/api/v1/chats')
.set('X-User-Id', owner.body.id)
.set('Authorization', bearer(owner))
.send({ title: 'Race', member_ids: memberIds })
.expect(201);
@@ -68,7 +45,7 @@ describe('Безопасность при конкурентном доступ
sendTasks.push(
request(server)
.post(`/api/v1/chats/${chat.body.id}/messages`)
.set('X-User-Id', clients[c].body.id)
.set('Authorization', bearer(clients[c]))
.send({ text: `client-${c}-message-${m}` }),
);
}
@@ -88,7 +65,7 @@ describe('Безопасность при конкурентном доступ
const res: request.Response = await request(server)
.get(`/api/v1/chats/${chat.body.id}/messages`)
.query({ limit: 25, ...(cursor ? { cursor } : {}) })
.set('X-User-Id', owner.body.id)
.set('Authorization', bearer(owner))
.expect(200);
collected.push(...res.body.items);
cursor = res.body.next_cursor;
@@ -105,7 +82,7 @@ describe('Безопасность при конкурентном доступ
await request(server)
.get(`/api/v1/chats/${chat.body.id}/messages`)
.query({ limit: 100 })
.set('X-User-Id', owner.body.id)
.set('Authorization', bearer(owner))
.expect(200)
.then((res) => res.body.items);
const sortKeys = fullHistory.map((m) => `${m.created_at}#${m.id}`);
@@ -121,7 +98,7 @@ describe('Безопасность при конкурентном доступ
Array.from({ length: attempts }, () =>
request(server)
.post('/api/v1/users')
.send({ username, display_name: 'Racer' }),
.send({ username, display_name: 'Racer', password: PASSWORD }),
),
);
@@ -130,12 +107,12 @@ describe('Безопасность при конкурентном доступ
expect(succeeded.length).toBe(1);
expect(conflicted.length).toBe(attempts - 1);
expect(succeeded.length + conflicted.length).toBe(attempts);
const listRes = await request(server)
const token = await login(server, username);
const me = await request(server)
.get('/api/v1/users/me')
.set('X-User-Id', succeeded[0].body.id)
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(listRes.body.username).toBe(username);
expect(me.body.id).toBe(succeeded[0].body.id);
});
});