fix: fixes after review

This commit is contained in:
2026-10-22 12:41:28 +03:00
parent 07661cfe82
commit 5146ca8419
8 changed files with 112 additions and 11 deletions
+25
View File
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import { PrismaService } from '../src/db/prisma.service';
import {
bearer,
createTestApp,
@@ -208,6 +209,30 @@ describe('API мессенджера (e2e) — граничные случаи
.expect(400);
});
it('принимает ровно 100 участников и отклоняет 101', async () => {
const alice = await createUser('max');
const ids = Array.from({ length: 100 }, () => randomUUID());
await app.get(PrismaService).user.createMany({
data: ids.map((id) => ({
id,
username: `m_${id.replace(/-/g, '').slice(0, 30)}`,
displayName: 'Member',
passwordHash: 'not-a-real-hash',
})),
});
const ok = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Max', member_ids: [alice.id, ...ids.slice(0, 99)] })
.expect(201);
expect(ok.body.member_ids).toHaveLength(100);
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Too many', member_ids: [alice.id, ...ids] })
.expect(400);
});
it('отклоняет создание чата с неизвестным участником', async () => {
const alice = await createUser('alice4');
await request(server)
+12
View File
@@ -97,6 +97,18 @@ describe('Аутентификация и сессии (e2e)', () => {
expect(wrong.body.error.message).toBe(unknown.body.error.message);
});
it('отклоняет JSON body при входе даже с верным Basic', async () => {
const user = await registerUser(server, 'body');
for (const body of [{ extra: 'forbidden' }, {}]) {
const res = await request(server)
.post('/api/v1/auth/sessions')
.set('Authorization', basic(user.username, PASSWORD))
.send(body)
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
}
});
it('отвечает 401 без заголовка Authorization', async () => {
const res = await request(server)
.post('/api/v1/auth/sessions')
+26
View File
@@ -1,6 +1,8 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import Redis from 'ioredis';
import { CacheService } from '../src/cache/cache.service';
import { PrismaService } from '../src/db/prisma.service';
import { bearer, createTestApp, registerUser } from './helpers';
@@ -57,6 +59,30 @@ describe('Уровень 4: expiry, отказы зависимостей, rollb
await request(server).get('/api/v1/users/me').expect(401);
});
it('logout при обрыве связи с Redis не оставляет рабочий токен после её восстановления', async () => {
const { app, server } = await start();
const user = await registerUser(server, 'partition');
await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(200);
const cache = app.get(CacheService) as any;
const redis = cache.client as Redis;
expect((await redis.keys('session:v1:*')).length).toBeGreaterThan(0);
redis.disconnect();
await request(server)
.delete('/api/v1/auth/sessions/current')
.set('Authorization', bearer(user))
.expect(204);
await redis.connect();
await sleep(200);
await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(401);
expect(cache.trusted).toBe(true);
});
it('без PostgreSQL отвечает ограниченным 503 и не пускает анонима', async () => {
const healthy = await start();
const user = await registerUser(healthy.server, 'nodb');