forked from mtusi2026/mtusi2026lab5
chore: port lab4 solution as lab5 base
This commit is contained in:
@@ -0,0 +1,686 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import {
|
||||
bearer,
|
||||
createTestApp,
|
||||
PASSWORD,
|
||||
registerUser,
|
||||
TestUser,
|
||||
} from './helpers';
|
||||
|
||||
describe('API мессенджера (e2e) — граничные случаи сверх публичного smoke', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
function createUser(label: string): Promise<TestUser> {
|
||||
return registerUser(server, label);
|
||||
}
|
||||
|
||||
describe('POST /api/v1/users', () => {
|
||||
it('отклоняет некорректный JSON с кодом 400', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send('{not valid json')
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
expect(res.body.error.request_id).toBeTruthy();
|
||||
});
|
||||
|
||||
it('отклоняет тело с лишними полями', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: 'validname1',
|
||||
display_name: 'Valid',
|
||||
password: PASSWORD,
|
||||
extra: 'nope',
|
||||
})
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('отклоняет username с заглавными буквами', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'InvalidName', display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет display_name из одних пробелов', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'wsonlyname1', display_name: ' ', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет дубликат username с кодом 409', async () => {
|
||||
const user = await createUser('dup');
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: user.username, display_name: 'Someone else', password: PASSWORD })
|
||||
.expect(409);
|
||||
expect(res.body.error.code).toBe('conflict');
|
||||
});
|
||||
|
||||
it('никогда не возвращает поле пароля или его хэша', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `nopass_${randomUUID().slice(0, 8)}`,
|
||||
display_name: 'X',
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(201);
|
||||
expect(res.body).not.toHaveProperty('password');
|
||||
expect(res.body).not.toHaveProperty('password_hash');
|
||||
});
|
||||
|
||||
it('отклоняет username короче 3 символов', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'ab', display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет username длиннее 32 символов', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'a'.repeat(33), display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет username с недопустимыми символами (дефис)', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'has-hyphen', display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('принимает username ровно на границах длины 3 и 32 символа', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `a${randomUUID().slice(0, 2)}`,
|
||||
display_name: 'X',
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(201);
|
||||
const longest = ('z' + randomUUID().replace(/-/g, '')).slice(0, 32);
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: longest, display_name: 'X', password: PASSWORD })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('отклоняет display_name длиннее 100 Unicode code points', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `toolongname_${randomUUID().slice(0, 6)}`,
|
||||
display_name: 'x'.repeat(101),
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('принимает display_name ровно из 100 code points, считая эмодзи вне BMP за один символ', async () => {
|
||||
const displayName = '👋'.repeat(100);
|
||||
expect([...displayName].length).toBe(100);
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `emojiname_${randomUUID().slice(0, 6)}`,
|
||||
display_name: displayName,
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('отклоняет display_name из 101 code point, состоящий из эмодзи вне BMP', async () => {
|
||||
const displayName = '👋'.repeat(100) + 'x';
|
||||
expect([...displayName].length).toBe(101);
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `emojitoolong_${randomUUID().slice(0, 6)}`,
|
||||
display_name: displayName,
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('контроль доступа', () => {
|
||||
it('возвращает 401 раньше 404, если и актор, и ресурс невалидны', async () => {
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${randomUUID()}`)
|
||||
.set('Authorization', 'Bearer not-a-token')
|
||||
.expect(401);
|
||||
expect(res.body.error.code).toBe('unauthorized');
|
||||
});
|
||||
|
||||
it('возвращает 404 (не 403), когда чат запрашивает не его участник', async () => {
|
||||
const alice = await createUser('alice');
|
||||
const bob = await createUser('bob');
|
||||
const eve = await createUser('eve');
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Private', member_ids: [alice.id, bob.id] })
|
||||
.expect(201);
|
||||
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${chat.body.id}`)
|
||||
.set('Authorization', bearer(eve))
|
||||
.expect(404);
|
||||
expect(res.body.error.code).toBe('not_found');
|
||||
});
|
||||
|
||||
it('отклоняет создание чата, если вызывающий не входит в member_ids', async () => {
|
||||
const alice = await createUser('alice2');
|
||||
const bob = await createUser('bob2');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Missing caller', member_ids: [bob.id] })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет создание чата с дублирующимися member_ids', async () => {
|
||||
const alice = await createUser('alice3');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({
|
||||
title: 'Dup members',
|
||||
member_ids: [alice.id, alice.id],
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('считает один UUID в разном регистре дубликатом и узнаёт вызывающего в верхнем регистре', async () => {
|
||||
const alice = await createUser('alicecase');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Case', member_ids: [alice.id, alice.id.toUpperCase()] })
|
||||
.expect(400);
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Upper', member_ids: [alice.id.toUpperCase()] })
|
||||
.expect(201);
|
||||
expect(res.body.member_ids).toEqual([alice.id]);
|
||||
});
|
||||
|
||||
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)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({
|
||||
title: 'Unknown member',
|
||||
member_ids: [alice.id, randomUUID()],
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет создание чата с пустым/пробельным title', async () => {
|
||||
const alice = await createUser('alice4b');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: ' ', member_ids: [alice.id] })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('возвращает 400 для синтаксически некорректного chat_id (не UUID)', async () => {
|
||||
const alice = await createUser('alice4c');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats/not-a-uuid')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('возвращает 404 для корректного по формату, но несуществующего chat_id', async () => {
|
||||
const alice = await createUser('alice4d');
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${randomUUID()}`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/chats (пагинация)', () => {
|
||||
it('использует limit=50 по умолчанию, если limit не передан', async () => {
|
||||
const alice = await createUser('pgalice1');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Only chat', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
expect(res.body.items.length).toBeGreaterThanOrEqual(1);
|
||||
expect(res.body.items.length).toBeLessThanOrEqual(50);
|
||||
});
|
||||
|
||||
it('отклоняет нечисловой limit', async () => {
|
||||
const alice = await createUser('pgalice2');
|
||||
await request(server)
|
||||
.get('/api/v1/chats?limit=abc')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет limit=0', async () => {
|
||||
const alice = await createUser('pgalice3');
|
||||
await request(server)
|
||||
.get('/api/v1/chats?limit=0')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет повторённые limit и cursor', async () => {
|
||||
const alice = await createUser('pgrepeat');
|
||||
for (const query of ['limit=1&limit=2', 'cursor=a&cursor=b']) {
|
||||
await request(server)
|
||||
.get(`/api/v1/chats?${query}`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
}
|
||||
});
|
||||
|
||||
it('отклоняет cursor, не являющийся корректным base64url JSON', async () => {
|
||||
const alice = await createUser('pgalice4');
|
||||
await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.query({ cursor: 'not-a-real-cursor!!!' })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет корректно закодированный cursor с id чата, который никогда не существовал', async () => {
|
||||
const alice = await createUser('pgalice4b');
|
||||
const bogusCursor = Buffer.from(
|
||||
JSON.stringify({ id: randomUUID() }),
|
||||
'utf8',
|
||||
).toString('base64url');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.query({ cursor: bogusCursor })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('обходит весь список чатов по страницам без пропусков и дублей', async () => {
|
||||
const alice = await createUser('pgalice5');
|
||||
const titles = ['c1', 'c2', 'c3', 'c4', 'c5'];
|
||||
for (const title of titles) {
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title, member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
}
|
||||
|
||||
const seen: string[] = [];
|
||||
let cursor: string | null = null;
|
||||
do {
|
||||
const res: request.Response = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.query({ limit: 2, ...(cursor ? { cursor } : {}) })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
seen.push(
|
||||
...res.body.items.map((c: { title: string }) => c.title),
|
||||
);
|
||||
cursor = res.body.next_cursor;
|
||||
} while (cursor);
|
||||
|
||||
expect(seen).toEqual(titles);
|
||||
});
|
||||
});
|
||||
|
||||
describe('сообщения', () => {
|
||||
async function createChat(alice: TestUser, bob: TestUser) {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Chat', member_ids: [alice.id, bob.id] })
|
||||
.expect(201);
|
||||
return res.body as { id: string };
|
||||
}
|
||||
|
||||
it('не принимает sender_id, переданный клиентом', async () => {
|
||||
const alice = await createUser('alice5');
|
||||
const bob = await createUser('bob5');
|
||||
const chat = await createChat(alice, bob);
|
||||
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'hi', sender_id: bob.id })
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('считает attachment_ids: [] равным отсутствию поля, неизвестный ID — 404, неверный тип — 400', async () => {
|
||||
const alice = await createUser('aliceatt');
|
||||
const bob = await createUser('bobatt');
|
||||
const chat = await createChat(alice, bob);
|
||||
const path = `/api/v1/chats/${chat.id}/messages`;
|
||||
const key = randomUUID();
|
||||
const first = await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', key)
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'att' })
|
||||
.expect(201);
|
||||
const replay = await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', key)
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ attachment_ids: [], text: 'att' })
|
||||
.expect(201);
|
||||
expect(replay.body).toEqual(first.body);
|
||||
await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x', attachment_ids: [randomUUID()] })
|
||||
.expect(404);
|
||||
for (const attachment_ids of [null, 'x']) {
|
||||
await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x', attachment_ids })
|
||||
.expect(400);
|
||||
}
|
||||
});
|
||||
|
||||
it('отклоняет пустое сообщение', async () => {
|
||||
const alice = await createUser('alice6');
|
||||
const bob = await createUser('bob6');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: '' })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет сообщение из одних пробелов', async () => {
|
||||
const alice = await createUser('alice7');
|
||||
const bob = await createUser('bob7');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: ' \n\t ' })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет отправку в чат, где вызывающий не участник', async () => {
|
||||
const alice = await createUser('alice8');
|
||||
const bob = await createUser('bob8');
|
||||
const eve = await createUser('eve8');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(eve))
|
||||
.send({ text: 'intrusion' })
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
it('отклоняет limit=0 при чтении истории', async () => {
|
||||
const alice = await createUser('alice9');
|
||||
const bob = await createUser('bob9');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages?limit=0`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет limit=101 при чтении истории', async () => {
|
||||
const alice = await createUser('alice10');
|
||||
const bob = await createUser('bob10');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages?limit=101`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет нечисловой limit', async () => {
|
||||
const alice = await createUser('alice10b');
|
||||
const bob = await createUser('bob10b');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages?limit=abc`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет текст сообщения длиннее 4000 Unicode code points', async () => {
|
||||
const alice = await createUser('alice10c');
|
||||
const bob = await createUser('bob10c');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x'.repeat(4001) })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('принимает текст сообщения ровно из 4000 Unicode code points', async () => {
|
||||
const alice = await createUser('alice10d');
|
||||
const bob = await createUser('bob10d');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x'.repeat(4000) })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('отклоняет текст сообщения свыше 4000 code points, состоящий из эмодзи вне BMP', async () => {
|
||||
const alice = await createUser('alice10e');
|
||||
const bob = await createUser('bob10e');
|
||||
const chat = await createChat(alice, bob);
|
||||
const text = '👋'.repeat(4001);
|
||||
expect([...text].length).toBe(4001);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет cursor, не являющийся корректным base64url JSON', async () => {
|
||||
const alice = await createUser('alice10f');
|
||||
const bob = await createUser('bob10f');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages`)
|
||||
.query({ cursor: '!!!not-valid!!!' })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет корректно закодированный cursor с id сообщения, которое никогда не существовало в этом чате', async () => {
|
||||
const alice = await createUser('alice10i');
|
||||
const bob = await createUser('bob10i');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'only message' })
|
||||
.expect(201);
|
||||
|
||||
const bogusCursor = Buffer.from(
|
||||
JSON.stringify({ chatId: chat.id, id: randomUUID() }),
|
||||
'utf8',
|
||||
).toString('base64url');
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages`)
|
||||
.query({ cursor: bogusCursor })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('возвращает 400 для синтаксически некорректного chat_id при отправке сообщения', async () => {
|
||||
const alice = await createUser('alice10g');
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats/not-a-uuid/messages')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'hi' })
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('возвращает 400 для синтаксически некорректного chat_id при чтении истории', async () => {
|
||||
const alice = await createUser('alice10h');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats/not-a-uuid/messages')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('отклоняет cursor, взятый из другого чата', async () => {
|
||||
const alice = await createUser('alice11');
|
||||
const bob = await createUser('bob11');
|
||||
const chatA = await createChat(alice, bob);
|
||||
const chatB = await createChat(alice, bob);
|
||||
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chatA.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'first' })
|
||||
.expect(201);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chatA.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'second' })
|
||||
.expect(201);
|
||||
|
||||
const page1 = await request(server)
|
||||
.get(`/api/v1/chats/${chatA.id}/messages?limit=1`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
expect(page1.body.next_cursor).toBeTruthy();
|
||||
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${chatB.id}/messages`)
|
||||
.query({ cursor: page1.body.next_cursor })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('обходит всю историю по страницам без пропусков и дублей', async () => {
|
||||
const alice = await createUser('alice12');
|
||||
const bob = await createUser('bob12');
|
||||
const chat = await createChat(alice, bob);
|
||||
const sentTexts = ['m1', 'm2', 'm3', 'm4', 'm5'];
|
||||
for (const text of sentTexts) {
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text })
|
||||
.expect(201);
|
||||
}
|
||||
|
||||
const seen: string[] = [];
|
||||
let cursor: string | null = null;
|
||||
do {
|
||||
const res: request.Response = await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages`)
|
||||
.query({ limit: 2, ...(cursor ? { cursor } : {}) })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
seen.push(
|
||||
...res.body.items.map((m: { text: string }) => m.text),
|
||||
);
|
||||
cursor = res.body.next_cursor;
|
||||
} while (cursor);
|
||||
|
||||
expect(seen).toEqual(sentTexts);
|
||||
});
|
||||
});
|
||||
|
||||
describe('X-Request-Id', () => {
|
||||
it('присутствует и уникален для независимых запросов', async () => {
|
||||
const first = await request(server).get('/health/live').expect(200);
|
||||
const second = await request(server)
|
||||
.get('/health/live')
|
||||
.expect(200);
|
||||
expect(first.headers['x-request-id']).toBeTruthy();
|
||||
expect(second.headers['x-request-id']).toBeTruthy();
|
||||
expect(first.headers['x-request-id']).not.toBe(
|
||||
second.headers['x-request-id'],
|
||||
);
|
||||
});
|
||||
|
||||
it('совпадает с request_id внутри тела ошибки', async () => {
|
||||
const res = await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.expect(401);
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,287 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import request from 'supertest';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import { jpeg, SAMPLE_PNG } from './images';
|
||||
|
||||
describe('Скачивание и привязка вложений (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
let alice: TestUser;
|
||||
let bob: TestUser;
|
||||
let eve: TestUser;
|
||||
let chatId: string;
|
||||
let otherChatId: string;
|
||||
|
||||
const createChat = async (owner: TestUser, members: TestUser[]) => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(owner))
|
||||
.send({ title: 'Files', member_ids: members.map((m) => m.id) })
|
||||
.expect(201);
|
||||
return res.body.id as string;
|
||||
};
|
||||
|
||||
const upload = async (
|
||||
user = alice,
|
||||
chat = chatId,
|
||||
body: Buffer = SAMPLE_PNG,
|
||||
type = 'image/png',
|
||||
) => {
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chat}/attachments?filename=${encodeURIComponent('фото 1.png')}`)
|
||||
.set('Authorization', bearer(user))
|
||||
.set('Content-Type', type)
|
||||
.send(body)
|
||||
.expect(201);
|
||||
return res.body;
|
||||
};
|
||||
|
||||
const get = (path: string, user: TestUser | null = bob) => {
|
||||
const req = request(server).get(path).buffer(true).parse(binaryParser);
|
||||
return user ? req.set('Authorization', bearer(user)) : req;
|
||||
};
|
||||
|
||||
const send = (
|
||||
body: unknown,
|
||||
{ user = alice, chat = chatId, key = randomUUID() }: { user?: TestUser; chat?: string; key?: string } = {},
|
||||
) =>
|
||||
request(server)
|
||||
.post(`/api/v1/chats/${chat}/messages`)
|
||||
.set('Authorization', bearer(user))
|
||||
.set('Idempotency-Key', key)
|
||||
.send(body as object);
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
alice = await registerUser(server, 'acc_alice');
|
||||
bob = await registerUser(server, 'acc_bob');
|
||||
eve = await registerUser(server, 'acc_eve');
|
||||
chatId = await createChat(alice, [alice, bob]);
|
||||
otherChatId = await createChat(eve, [eve]);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('чтение', () => {
|
||||
it('участник получает метаданные и оригинал побайтно', async () => {
|
||||
const attachment = await upload();
|
||||
const meta = await request(server)
|
||||
.get(`/api/v1/attachments/${attachment.id}`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(200);
|
||||
expect(meta.body).toEqual(attachment);
|
||||
const res = await get(`/api/v1/attachments/${attachment.id}/content`).expect(200);
|
||||
expect(res.headers['content-type']).toBe('image/png');
|
||||
expect(res.headers['content-length']).toBe(String(SAMPLE_PNG.length));
|
||||
expect(res.headers['x-content-type-options']).toBe('nosniff');
|
||||
expect(res.headers['x-request-id']).toBeTruthy();
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
expect(res.headers['location']).toBeUndefined();
|
||||
expect(createHash('sha256').update(res.body).digest('hex')).toBe(attachment.sha256);
|
||||
});
|
||||
|
||||
it('JPEG отдаётся с истинным Content-Type', async () => {
|
||||
const body = await jpeg();
|
||||
const attachment = await upload(alice, chatId, body, 'image/jpeg');
|
||||
const res = await get(`/api/v1/attachments/${attachment.id}/content`).expect(200);
|
||||
expect(res.headers['content-type']).toBe('image/jpeg');
|
||||
expect(Buffer.compare(res.body, body)).toBe(0);
|
||||
});
|
||||
|
||||
it('посторонний и неизвестный ID получают одинаковый 404', async () => {
|
||||
const attachment = await upload();
|
||||
for (const suffix of ['', '/content']) {
|
||||
const foreign = await get(`/api/v1/attachments/${attachment.id}${suffix}`, eve);
|
||||
const unknown = await get(`/api/v1/attachments/${randomUUID()}${suffix}`, eve);
|
||||
expect(foreign.status).toBe(404);
|
||||
expect(unknown.status).toBe(404);
|
||||
const parsed = JSON.parse(foreign.body.toString());
|
||||
expect(parsed.error.code).toBe('not_found');
|
||||
expect(parsed.error.message).toBe(JSON.parse(unknown.body.toString()).error.message);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
['nil', '00000000-0000-0000-0000-000000000000'],
|
||||
['v7', '018f6f3a-7b1c-7def-8abc-0123456789ab'],
|
||||
])('UUID %s корректного формата — 404, а не 400', async (_name, id) => {
|
||||
await get(`/api/v1/attachments/${id}`).expect(404);
|
||||
await get(`/api/v1/attachments/${id}/content`).expect(404);
|
||||
await get(`/api/v1/chats/${id}`, alice).expect(404);
|
||||
await send({ text: 'x', attachment_ids: [id] }).expect(404);
|
||||
await send({ text: 'x' }, { chat: id }).expect(404);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${id}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG)
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
it('без токена 401, неверный UUID 400', async () => {
|
||||
const attachment = await upload();
|
||||
for (const suffix of ['', '/content']) {
|
||||
await get(`/api/v1/attachments/${attachment.id}${suffix}`, null).expect(401);
|
||||
await get(`/api/v1/attachments/not-a-uuid${suffix}`).expect(400);
|
||||
}
|
||||
});
|
||||
|
||||
it('variant: original явно, отсутствующий 404, неизвестный и повторённый 400', async () => {
|
||||
const { id } = await upload();
|
||||
await get(`/api/v1/attachments/${id}/content?variant=original`).expect(200);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=thumbnail`).expect(404);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=nonsense`).expect(400);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=`).expect(400);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=original&variant=original`).expect(400);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=thumbnail`, eve).expect(404);
|
||||
});
|
||||
|
||||
it('нет доступа к S3 — метаданные 200, content 503 с заголовками', async () => {
|
||||
const attachment = await upload();
|
||||
const { app: down, server: downServer } = await createTestApp({
|
||||
S3_ENDPOINT: 'http://127.0.0.1:9',
|
||||
});
|
||||
try {
|
||||
await request(downServer)
|
||||
.get(`/api/v1/attachments/${attachment.id}`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(200);
|
||||
const res = await request(downServer)
|
||||
.get(`/api/v1/attachments/${attachment.id}/content`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(503);
|
||||
expect(res.body.error.code).toBe('unavailable');
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
} finally {
|
||||
await down.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('объект пропал из S3 — 500 без внутренних деталей', async () => {
|
||||
const attachment = await upload();
|
||||
const row = await app.get(PrismaService).attachment.findUniqueOrThrow({
|
||||
where: { id: attachment.id },
|
||||
});
|
||||
await app.get(S3Service).delete(row.objectKey);
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/attachments/${attachment.id}/content`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(500);
|
||||
expect(res.body.error.code).toBe('internal_error');
|
||||
expect(JSON.stringify(res.body)).not.toContain(row.objectKey);
|
||||
});
|
||||
});
|
||||
|
||||
describe('attachment_ids в сообщениях', () => {
|
||||
it('text="" с вложением принимается, Message читается вторым участником', async () => {
|
||||
const attachment = await upload();
|
||||
const created = await send({ text: '', attachment_ids: [attachment.id] }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual([attachment.id]);
|
||||
const page = await request(server)
|
||||
.get(`/api/v1/chats/${chatId}/messages?limit=100`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(200);
|
||||
expect(page.body.items).toContainEqual(created.body);
|
||||
});
|
||||
|
||||
it('порядок [B, A] сохраняется в ответе, истории и повторе; перестановка с тем же ключом — 409', async () => {
|
||||
const a = await upload();
|
||||
const b = await upload();
|
||||
const key = randomUUID();
|
||||
const body = { text: 'order', attachment_ids: [b.id, a.id] };
|
||||
const created = await send(body, { key }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual([b.id, a.id]);
|
||||
const replay = await send(body, { key }).expect(201);
|
||||
expect(replay.body).toEqual(created.body);
|
||||
const page = await request(server)
|
||||
.get(`/api/v1/chats/${chatId}/messages?limit=100`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
expect(page.body.items.find((m: any) => m.id === created.body.id)).toEqual(created.body);
|
||||
const conflict = await send({ text: 'order', attachment_ids: [a.id, b.id] }, { key }).expect(409);
|
||||
expect(conflict.body.error.code).toBe('conflict');
|
||||
});
|
||||
|
||||
it('отсутствующий attachment_ids и [] равны при повторе, изменение списка — 409', async () => {
|
||||
const attachment = await upload();
|
||||
const key = randomUUID();
|
||||
const first = await send({ text: 'same' }, { key }).expect(201);
|
||||
expect(first.body.attachment_ids).toEqual([]);
|
||||
const replay = await send({ attachment_ids: [], text: 'same' }, { key }).expect(201);
|
||||
expect(replay.body).toEqual(first.body);
|
||||
await send({ text: 'same', attachment_ids: [attachment.id] }, { key }).expect(409);
|
||||
});
|
||||
|
||||
it('вложение другого чата и неизвестное — одинаковый 404, сообщение не создаётся', async () => {
|
||||
const foreign = await upload(eve, otherChatId);
|
||||
const key = randomUUID();
|
||||
const first = await send({ text: 'x', attachment_ids: [foreign.id] }, { key }).expect(404);
|
||||
const second = await send({ text: 'x', attachment_ids: [randomUUID()] }).expect(404);
|
||||
expect(first.body.error.code).toBe('not_found');
|
||||
expect(first.body.error.message).toBe(second.body.error.message);
|
||||
await send({ text: 'x' }, { key }).expect(201);
|
||||
});
|
||||
|
||||
it('смесь своего и чужого вложения — 404', async () => {
|
||||
const own = await upload();
|
||||
const foreign = await upload(eve, otherChatId);
|
||||
await send({ text: 'x', attachment_ids: [own.id, foreign.id] }).expect(404);
|
||||
});
|
||||
|
||||
it('посторонний не может отправить вложение в чужой чат', async () => {
|
||||
const attachment = await upload();
|
||||
await send({ text: 'x', attachment_ids: [attachment.id] }, { user: eve }).expect(404);
|
||||
});
|
||||
|
||||
it('любой участник может сослаться на вложение чата, вложение можно использовать повторно', async () => {
|
||||
const attachment = await upload();
|
||||
await send({ text: 'first', attachment_ids: [attachment.id] }).expect(201);
|
||||
const res = await send({ text: 'second', attachment_ids: [attachment.id] }, { user: bob }).expect(201);
|
||||
expect(res.body.sender_id).toBe(bob.id);
|
||||
expect(res.body.attachment_ids).toEqual([attachment.id]);
|
||||
});
|
||||
|
||||
it('ровно 10 вложений принимается, 11 — 400', async () => {
|
||||
const attachment = await upload();
|
||||
const links = await Promise.all(Array.from({ length: 9 }, () => upload()));
|
||||
const ten = [attachment.id, ...links.map((l) => l.id)];
|
||||
const created = await send({ text: 'ten', attachment_ids: ten }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual(ten);
|
||||
await send({ text: 'eleven', attachment_ids: [...ten, randomUUID()] }).expect(400);
|
||||
});
|
||||
|
||||
it('UUID в другом регистре — тот же ID и дубль', async () => {
|
||||
const attachment = await upload();
|
||||
const upper = attachment.id.toUpperCase();
|
||||
const created = await send({ text: 'case', attachment_ids: [upper] }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual([attachment.id]);
|
||||
await send({ text: 'dup', attachment_ids: [attachment.id, upper] }).expect(400);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['text отсутствует', (id: string) => ({ attachment_ids: [id] })],
|
||||
['text null', (id: string) => ({ text: null, attachment_ids: [id] })],
|
||||
['attachment_ids null', () => ({ text: 'x', attachment_ids: null })],
|
||||
['attachment_ids строка', (id: string) => ({ text: 'x', attachment_ids: id })],
|
||||
['элемент не UUID', () => ({ text: 'x', attachment_ids: ['nope'] })],
|
||||
['элемент число', () => ({ text: 'x', attachment_ids: [1] })],
|
||||
['дубликат', (id: string) => ({ text: 'x', attachment_ids: [id, id] })],
|
||||
['пустой текст без вложений', () => ({ text: '', attachment_ids: [] })],
|
||||
['пробельный текст без вложений', () => ({ text: ' ' })],
|
||||
])('%s — 400', async (_name, build) => {
|
||||
const attachment = await upload();
|
||||
await send(build(attachment.id)).expect(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function binaryParser(res: any, done: (error: Error | null, body: Buffer) => void): void {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
res.on('end', () => done(null, Buffer.concat(chunks)));
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import Ajv2020 from 'ajv/dist/2020';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { readdirSync } from 'fs';
|
||||
import { connect } from 'net';
|
||||
import { tmpdir } from 'os';
|
||||
import request from 'supertest';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { loadDocument } from '../src/docs/swagger';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import {
|
||||
corruptedJpegs,
|
||||
corruptedPngs,
|
||||
jpeg,
|
||||
png,
|
||||
pngHeaderOnly,
|
||||
pngOfExactSize,
|
||||
SAMPLE_PNG,
|
||||
} from './images';
|
||||
|
||||
const MAX_BYTES = 10 * 1024 * 1024;
|
||||
|
||||
describe('Загрузка вложений (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
let alice: TestUser;
|
||||
let eve: TestUser;
|
||||
let chatId: string;
|
||||
|
||||
const upload = (
|
||||
body: Buffer | undefined,
|
||||
{
|
||||
type = 'image/png',
|
||||
filename = 'sample.png',
|
||||
user = alice,
|
||||
chat = chatId,
|
||||
}: {
|
||||
type?: string | null;
|
||||
filename?: string | null;
|
||||
user?: TestUser | null;
|
||||
chat?: string;
|
||||
} = {},
|
||||
) => {
|
||||
const query = filename === null ? '' : `?filename=${encodeURIComponent(filename)}`;
|
||||
const req = request(server).post(`/api/v1/chats/${chat}/attachments${query}`);
|
||||
if (user) {
|
||||
req.set('Authorization', bearer(user));
|
||||
}
|
||||
if (type) {
|
||||
req.set('Content-Type', type);
|
||||
}
|
||||
return body ? req.send(body) : req;
|
||||
};
|
||||
|
||||
const expectError = (res: request.Response, status: number, code: string) => {
|
||||
expect({ status: res.status, code: res.body.error?.code }).toEqual({ status, code });
|
||||
expect(res.headers['x-request-id']).toBeTruthy();
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
alice = await registerUser(server, 'att_alice');
|
||||
eve = await registerUser(server, 'att_eve');
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Files', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
chatId = chat.body.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('успешная загрузка', () => {
|
||||
it('возвращает 201 Attachment по контракту и сохраняет метаданные', async () => {
|
||||
const res = await upload(SAMPLE_PNG).expect(201);
|
||||
const document: any = loadDocument();
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
ajv.addSchema(document, 'contract');
|
||||
const validate = ajv.compile({ $ref: 'contract#/components/schemas/Attachment' });
|
||||
expect({ valid: validate(res.body), errors: validate.errors }).toEqual({
|
||||
valid: true,
|
||||
errors: null,
|
||||
});
|
||||
expect(res.body).toMatchObject({
|
||||
chat_id: chatId,
|
||||
owner_id: alice.id,
|
||||
filename: 'sample.png',
|
||||
content_type: 'image/png',
|
||||
size_bytes: SAMPLE_PNG.length,
|
||||
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
|
||||
status: 'ready',
|
||||
variants: [],
|
||||
error_code: null,
|
||||
});
|
||||
const row = await app.get(PrismaService).attachment.findUnique({
|
||||
where: { id: res.body.id },
|
||||
});
|
||||
expect(row?.objectKey).toBe(`attachments/${res.body.id}`);
|
||||
expect(JSON.stringify(res.body)).not.toContain('attachments/');
|
||||
});
|
||||
|
||||
it('принимает реальный JPEG', async () => {
|
||||
const res = await upload(await jpeg(), {
|
||||
type: 'image/jpeg',
|
||||
filename: 'фото 1.jpg',
|
||||
}).expect(201);
|
||||
expect(res.body).toMatchObject({
|
||||
content_type: 'image/jpeg',
|
||||
filename: 'фото 1.jpg',
|
||||
});
|
||||
});
|
||||
|
||||
it('игнорирует параметры и регистр Content-Type', async () => {
|
||||
await upload(SAMPLE_PNG, { type: 'IMAGE/PNG; charset=binary' }).expect(201);
|
||||
});
|
||||
|
||||
it('принимает файл ровно 10 MiB, filename ровно 255 символов и PNG с байтами после IEND', async () => {
|
||||
const exact = pngOfExactSize(MAX_BYTES);
|
||||
expect(exact.length).toBe(MAX_BYTES);
|
||||
const res = await upload(exact).expect(201);
|
||||
expect(res.body.size_bytes).toBe(MAX_BYTES);
|
||||
await upload(SAMPLE_PNG, { filename: `${'ф'.repeat(251)}.png` }).expect(201);
|
||||
await upload(Buffer.concat([png(4, 4), Buffer.from('tail')])).expect(201);
|
||||
});
|
||||
|
||||
it('принимает ровно 40 млн пикселей', async () => {
|
||||
const res = await upload(await bigPng(8000, 5000));
|
||||
expect(res.status).toBe(201);
|
||||
});
|
||||
});
|
||||
|
||||
describe('порядок проверок и доступ', () => {
|
||||
it('без токена — 401 раньше любых проверок содержимого', async () => {
|
||||
const cases: [string | null, string | null][] = [
|
||||
['image/png', 'a.png'],
|
||||
['text/plain', 'a.png'],
|
||||
['application/json', 'a.png'],
|
||||
['image/png', '../a.png'],
|
||||
[null, null],
|
||||
];
|
||||
for (const [type, filename] of cases) {
|
||||
const res = await upload(Buffer.from('{bad'), { type, filename, user: null });
|
||||
expectError(res, 401, 'unauthorized');
|
||||
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
|
||||
}
|
||||
});
|
||||
|
||||
it('посторонний и несуществующий чат — одинаковый 404, чужой файл не сохраняется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
expectError(await upload(SAMPLE_PNG, { user: eve }), 404, 'not_found');
|
||||
expectError(await upload(SAMPLE_PNG, { chat: randomUUID() }), 404, 'not_found');
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
});
|
||||
|
||||
it('неверный UUID чата — 400', async () => {
|
||||
expectError(await upload(SAMPLE_PNG, { chat: 'not-a-uuid' }), 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('filename', () => {
|
||||
const invalid = {
|
||||
'отсутствует': null,
|
||||
'пустой': '',
|
||||
'только пробелы': ' ',
|
||||
'256 символов': 'x'.repeat(256),
|
||||
'путь через /': '../x.png',
|
||||
'путь через \\': 'a\\b.png',
|
||||
'NUL': 'a\u0000b.png',
|
||||
'перевод строки': 'a\nb.png',
|
||||
'DEL': 'bad\u007f.png',
|
||||
'C1 U+0085': 'c1\u0085.png',
|
||||
'разделитель строк U+2028': 'a
b.png',
|
||||
};
|
||||
it.each(Object.entries(invalid))('%s — 400', async (_name, filename) => {
|
||||
expectError(await upload(SAMPLE_PNG, { filename }), 400, 'invalid_request');
|
||||
});
|
||||
|
||||
it('повторённый параметр — 400', async () => {
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png&filename=b.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG);
|
||||
expectError(res, 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('тип содержимого', () => {
|
||||
it.each(['text/plain', 'application/json', 'application/octet-stream', 'multipart/form-data; boundary=x', 'image/gif', 'image/webp'])(
|
||||
'Content-Type %s — 415',
|
||||
async (type) => {
|
||||
expectError(await upload(SAMPLE_PNG, { type }), 415, 'unsupported_media_type');
|
||||
},
|
||||
);
|
||||
|
||||
it('без Content-Type — 415', async () => {
|
||||
expectError(await upload(SAMPLE_PNG, { type: null }), 415, 'unsupported_media_type');
|
||||
});
|
||||
|
||||
it('подмена MIME в обе стороны и чужие форматы — 415', async () => {
|
||||
const realJpeg = await jpeg();
|
||||
expectError(await upload(SAMPLE_PNG, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
|
||||
expectError(await upload(realJpeg, { type: 'image/png' }), 415, 'unsupported_media_type');
|
||||
for (const body of [
|
||||
Buffer.from('просто текст'),
|
||||
Buffer.from('GIF89a\u0001\u0000\u0001\u0000'),
|
||||
Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>'),
|
||||
]) {
|
||||
expectError(await upload(body), 415, 'unsupported_media_type');
|
||||
}
|
||||
});
|
||||
|
||||
it('пустое тело — 400', async () => {
|
||||
expectError(await upload(undefined), 400, 'invalid_request');
|
||||
expectError(await upload(Buffer.alloc(0)), 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('повреждённые изображения', () => {
|
||||
it.each(Object.entries(corruptedPngs()))('PNG: %s — 415', async (_name, body) => {
|
||||
expectError(await upload(body), 415, 'unsupported_media_type');
|
||||
});
|
||||
|
||||
it('JPEG: все повреждения — 415', async () => {
|
||||
for (const body of Object.values(corruptedJpegs(await jpeg()))) {
|
||||
expectError(await upload(body, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('лимиты', () => {
|
||||
it('10 MiB + 1 байт — 413 и без чтения объявленного тела', async () => {
|
||||
expectError(await upload(pngOfExactSize(MAX_BYTES + 1)), 413, 'payload_too_large');
|
||||
});
|
||||
|
||||
it('превышение при chunked без Content-Length — 413', async () => {
|
||||
const res = await new Promise<request.Response>((resolve, reject) => {
|
||||
const req = upload(undefined);
|
||||
req.set('Transfer-Encoding', 'chunked');
|
||||
req.write(pngOfExactSize(MAX_BYTES));
|
||||
req.write(Buffer.alloc(1024));
|
||||
req.end((error, response) => (response ? resolve(response) : reject(error)));
|
||||
});
|
||||
expectError(res, 413, 'payload_too_large');
|
||||
});
|
||||
|
||||
it('превышение лимита пикселей — 413 до декодирования', async () => {
|
||||
for (const body of [pngHeaderOnly(8001, 5000), pngHeaderOnly(20000, 10000)]) {
|
||||
expectError(await upload(body), 413, 'payload_too_large');
|
||||
}
|
||||
});
|
||||
|
||||
it('оборванная загрузка не оставляет временных файлов и строк в БД', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
const { port } = server.address();
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const socket = connect(port, '127.0.0.1', () => {
|
||||
socket.write(
|
||||
`POST /api/v1/chats/${chatId}/attachments?filename=a.png HTTP/1.1\r\n` +
|
||||
`Host: test\r\nAuthorization: ${bearer(alice)}\r\n` +
|
||||
`Content-Type: image/png\r\nContent-Length: ${SAMPLE_PNG.length}\r\n\r\n`,
|
||||
);
|
||||
socket.write(SAMPLE_PNG.subarray(0, 1000));
|
||||
setTimeout(() => {
|
||||
socket.destroy();
|
||||
resolve();
|
||||
}, 300);
|
||||
});
|
||||
socket.on('error', reject);
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
expect(activeTempDirs()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('отказы', () => {
|
||||
it('S3 недоступен — 503 с заголовками, метаданные не создаются', async () => {
|
||||
const { app: down, server: downServer } = await createTestApp({
|
||||
S3_ENDPOINT: 'http://127.0.0.1:9',
|
||||
});
|
||||
try {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
const started = Date.now();
|
||||
const res = await request(downServer)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG);
|
||||
expectError(res, 503, 'unavailable');
|
||||
expect(Date.now() - started).toBeLessThan(3000);
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
} finally {
|
||||
await down.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('явный отказ INSERT после PUT удаляет объект', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('rejected', {
|
||||
code: 'P2003',
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
const res = await upload(SAMPLE_PNG);
|
||||
expect(res.status).toBe(500);
|
||||
expect(remove).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('необработанная ошибка PostgreSQL при INSERT — 503, объект не удаляется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientUnknownRequestError('server closed the connection', {
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('при неясном исходе COMMIT объект не удаляется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('connection lost', {
|
||||
code: 'P1017',
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function activeTempDirs(): string[] {
|
||||
return readdirSync(tmpdir()).filter((name) => name.startsWith('messenger-upload-'));
|
||||
}
|
||||
|
||||
async function bigPng(width: number, height: number): Promise<Buffer> {
|
||||
const { default: sharp } = await import('sharp');
|
||||
return sharp({ create: { width, height, channels: 3, background: '#336699' } })
|
||||
.png({ compressionLevel: 9 })
|
||||
.toBuffer();
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import {
|
||||
basic,
|
||||
bearer,
|
||||
createTestApp,
|
||||
login,
|
||||
PASSWORD,
|
||||
registerUser,
|
||||
} from './helpers';
|
||||
|
||||
describe('Аутентификация и сессии (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('регистрация', () => {
|
||||
it('требует password', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: `nopw_${randomUUID().slice(0, 8)}`, display_name: 'X' })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет password короче 12 code points', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `short_${randomUUID().slice(0, 8)}`,
|
||||
display_name: 'X',
|
||||
password: 'x'.repeat(11),
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('хранит пароль как Argon2id-хеш, а не открытым текстом', async () => {
|
||||
const user = await registerUser(server, 'hash');
|
||||
const row = await app
|
||||
.get(PrismaService)
|
||||
.user.findUniqueOrThrow({ where: { id: user.id } });
|
||||
expect(row.passwordHash.startsWith('$argon2id$')).toBe(true);
|
||||
expect(row.passwordHash).not.toContain(PASSWORD);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/v1/auth/sessions', () => {
|
||||
it('выдаёт непрозрачный Bearer-токен с expires_at и пользователем', async () => {
|
||||
const user = await registerUser(server, 'login');
|
||||
const res = await request(server)
|
||||
.post('/api/v1/auth/sessions')
|
||||
.set('Authorization', basic(user.username, PASSWORD))
|
||||
.expect(201);
|
||||
expect(res.body.token_type).toBe('Bearer');
|
||||
expect(res.body.token.length).toBeGreaterThanOrEqual(43);
|
||||
expect(res.body.token).not.toContain(user.id);
|
||||
expect(res.body.user.id).toBe(user.id);
|
||||
const expiresIn = Date.parse(res.body.expires_at) - Date.now();
|
||||
expect(expiresIn).toBeGreaterThan(3500 * 1000);
|
||||
expect(expiresIn).toBeLessThanOrEqual(3600 * 1000);
|
||||
});
|
||||
|
||||
it('принимает пароль с двоеточием и не-ASCII символами', async () => {
|
||||
const username = `colon_${randomUUID().slice(0, 8)}`;
|
||||
const password = 'пароль:с:двоеточием🔑';
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username, display_name: 'X', password })
|
||||
.expect(201);
|
||||
await login(server, username, password);
|
||||
});
|
||||
|
||||
it('отвечает одинаковым 401 на неверный пароль и неизвестного пользователя', async () => {
|
||||
const user = await registerUser(server, 'wrong');
|
||||
const wrong = await request(server)
|
||||
.post('/api/v1/auth/sessions')
|
||||
.set('Authorization', basic(user.username, 'wrong-password-123'))
|
||||
.expect(401);
|
||||
const unknown = await request(server)
|
||||
.post('/api/v1/auth/sessions')
|
||||
.set('Authorization', basic(`ghost_${randomUUID().slice(0, 8)}`, PASSWORD))
|
||||
.expect(401);
|
||||
for (const res of [wrong, unknown]) {
|
||||
expect(res.headers['www-authenticate']).toBe(
|
||||
'Basic realm="messenger", charset="UTF-8"',
|
||||
);
|
||||
expect(res.body.error.code).toBe('unauthorized');
|
||||
}
|
||||
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')
|
||||
.expect(401);
|
||||
expect(res.headers['www-authenticate']).toContain('Basic');
|
||||
});
|
||||
});
|
||||
|
||||
describe('защищённые endpoint', () => {
|
||||
it('не принимают Basic', async () => {
|
||||
const user = await registerUser(server, 'basic');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', basic(user.username, PASSWORD))
|
||||
.expect(401);
|
||||
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
|
||||
});
|
||||
|
||||
it('не принимают X-User-Id сам по себе', async () => {
|
||||
const user = await registerUser(server, 'xuid');
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('X-User-Id', user.id)
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
it('игнорируют X-User-Id рядом с Bearer', async () => {
|
||||
const alice = await registerUser(server, 'alice');
|
||||
const eve = await registerUser(server, 'eve');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('X-User-Id', eve.id)
|
||||
.expect(200);
|
||||
expect(res.body.id).toBe(alice.id);
|
||||
});
|
||||
|
||||
it('отвечают 401 на неизвестный токен', async () => {
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', 'Bearer unknown-token-value')
|
||||
.expect(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/v1/auth/sessions/current', () => {
|
||||
it('отзывает только текущий токен: 204, затем 401 на повтор', async () => {
|
||||
const user = await registerUser(server, 'logout');
|
||||
const other = await login(server, user.username);
|
||||
await request(server)
|
||||
.delete('/api/v1/auth/sessions/current')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(204);
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(401);
|
||||
await request(server)
|
||||
.delete('/api/v1/auth/sessions/current')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(401);
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', `Bearer ${other}`)
|
||||
.expect(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,119 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { randomUUID } from 'crypto';
|
||||
import {
|
||||
bearer,
|
||||
createTestApp,
|
||||
login,
|
||||
PASSWORD,
|
||||
registerUser,
|
||||
} from './helpers';
|
||||
|
||||
describe('Безопасность при конкурентном доступе (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('принимает 100 конкурентных сообщений от 10 клиентов и сохраняет ровно 100 уникальных', async () => {
|
||||
const CLIENTS = 10;
|
||||
const MESSAGES_PER_CLIENT = 10;
|
||||
|
||||
const owner = await registerUser(server, 'owner');
|
||||
const clients = await Promise.all(
|
||||
Array.from({ length: CLIENTS }, (_, i) =>
|
||||
registerUser(server, `racer${i}`),
|
||||
),
|
||||
);
|
||||
const memberIds = [owner.id, ...clients.map((c) => c.id)];
|
||||
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(owner))
|
||||
.send({ title: 'Race', member_ids: memberIds })
|
||||
.expect(201);
|
||||
|
||||
const sendTasks: Promise<request.Response>[] = [];
|
||||
for (let c = 0; c < CLIENTS; c++) {
|
||||
for (let m = 0; m < MESSAGES_PER_CLIENT; m++) {
|
||||
sendTasks.push(
|
||||
request(server)
|
||||
.post(`/api/v1/chats/${chat.body.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(clients[c]))
|
||||
.send({ text: `client-${c}-message-${m}` }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const responses = await Promise.all(sendTasks);
|
||||
for (const res of responses) {
|
||||
expect(res.status).toBe(201);
|
||||
}
|
||||
|
||||
const sentIds = new Set(responses.map((r) => r.body.id));
|
||||
expect(sentIds.size).toBe(CLIENTS * MESSAGES_PER_CLIENT);
|
||||
|
||||
const collected: { id: string; text: string }[] = [];
|
||||
let cursor: string | null = null;
|
||||
do {
|
||||
const res: request.Response = await request(server)
|
||||
.get(`/api/v1/chats/${chat.body.id}/messages`)
|
||||
.query({ limit: 25, ...(cursor ? { cursor } : {}) })
|
||||
.set('Authorization', bearer(owner))
|
||||
.expect(200);
|
||||
collected.push(...res.body.items);
|
||||
cursor = res.body.next_cursor;
|
||||
} while (cursor);
|
||||
|
||||
expect(collected.length).toBe(CLIENTS * MESSAGES_PER_CLIENT);
|
||||
const uniqueIds = new Set(collected.map((m) => m.id));
|
||||
expect(uniqueIds.size).toBe(CLIENTS * MESSAGES_PER_CLIENT);
|
||||
|
||||
const uniqueTexts = new Set(collected.map((m) => m.text));
|
||||
expect(uniqueTexts.size).toBe(CLIENTS * MESSAGES_PER_CLIENT);
|
||||
|
||||
const fullHistory: { id: string; created_at: string }[] =
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.body.id}/messages`)
|
||||
.query({ limit: 100 })
|
||||
.set('Authorization', bearer(owner))
|
||||
.expect(200)
|
||||
.then((res) => res.body.items);
|
||||
const sortKeys = fullHistory.map((m) => `${m.created_at}#${m.id}`);
|
||||
const sortedKeys = [...sortKeys].sort();
|
||||
expect(sortKeys).toEqual(sortedKeys);
|
||||
});
|
||||
|
||||
it('регистрирует ровно одного пользователя при гонке 10 запросов за один и тот же username', async () => {
|
||||
const username = `racer_username_${randomUUID().slice(0, 8)}`;
|
||||
const attempts = 10;
|
||||
|
||||
const responses = await Promise.all(
|
||||
Array.from({ length: attempts }, () =>
|
||||
request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username, display_name: 'Racer', password: PASSWORD }),
|
||||
),
|
||||
);
|
||||
|
||||
const succeeded = responses.filter((r) => r.status === 201);
|
||||
const conflicted = responses.filter((r) => r.status === 409);
|
||||
|
||||
expect(succeeded.length).toBe(1);
|
||||
expect(conflicted.length).toBe(attempts - 1);
|
||||
|
||||
const token = await login(server, username);
|
||||
const me = await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.expect(200);
|
||||
expect(me.body.id).toBe(succeeded[0].body.id);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import Ajv2020 from 'ajv/dist/2020';
|
||||
import request from 'supertest';
|
||||
import { OPERATION_EXAMPLES } from '../src/docs/examples';
|
||||
import { loadDocument, setupSwagger } from '../src/docs/swagger';
|
||||
import { createTestApp } from './helpers';
|
||||
|
||||
const CONTRACT_ID = 'https://contract.local/openapi.json';
|
||||
|
||||
describe('Swagger UI и примеры (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp({}, setupSwagger));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('отдаёт Swagger UI и документ контракта со всеми путями', async () => {
|
||||
await request(server).get('/docs').expect(200);
|
||||
const res = await request(server).get('/docs-json').expect(200);
|
||||
expect(res.body.openapi).toBe('3.1.0');
|
||||
expect(Object.keys(res.body.paths)).toEqual(
|
||||
Object.keys(loadDocument().paths),
|
||||
);
|
||||
expect(res.body.components.securitySchemes).toHaveProperty('basicAuth');
|
||||
expect(res.body.components.securitySchemes).toHaveProperty('bearerAuth');
|
||||
});
|
||||
|
||||
it('каждый пример запроса и ответа проходит JSON Schema контракта', () => {
|
||||
const document: any = loadDocument();
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
ajv.addSchema(document, CONTRACT_ID);
|
||||
let checked = 0;
|
||||
for (const [key, examples] of Object.entries(OPERATION_EXAMPLES)) {
|
||||
const [method, path] = key.split(' ');
|
||||
const operation = document.paths[path][method.toLowerCase()];
|
||||
const targets: [string, any, Record<string, any>][] = [];
|
||||
if (examples.request) {
|
||||
targets.push([
|
||||
'request',
|
||||
operation.requestBody.content['application/json'].schema,
|
||||
examples.request,
|
||||
]);
|
||||
}
|
||||
for (const [status, named] of Object.entries(examples.responses)) {
|
||||
targets.push([
|
||||
status,
|
||||
operation.responses[status].content['application/json'].schema,
|
||||
named,
|
||||
]);
|
||||
}
|
||||
for (const [where, schema, named] of targets) {
|
||||
const validate = ajv.compile({ $ref: CONTRACT_ID + schema.$ref });
|
||||
for (const [name, example] of Object.entries(named)) {
|
||||
const valid = validate(example.value);
|
||||
expect({ key, where, name, valid, errors: validate.errors }).toEqual(
|
||||
{ key, where, name, valid: true, errors: null },
|
||||
);
|
||||
checked++;
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(checked).toBeGreaterThan(40);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,166 @@
|
||||
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 { ChatsService } from '../src/chats/chats.service';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { bearer, createTestApp, registerUser } from './helpers';
|
||||
|
||||
const UNREACHABLE_REDIS = 'redis://127.0.0.1:1/0';
|
||||
const UNREACHABLE_DB = 'postgresql://test:test@127.0.0.1:1/none?connect_timeout=1';
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
describe('Уровень 4: expiry, отказы зависимостей, rollback (e2e)', () => {
|
||||
const apps: INestApplication[] = [];
|
||||
|
||||
async function start(env: Record<string, string> = {}) {
|
||||
const started = await createTestApp(env);
|
||||
apps.push(started.app);
|
||||
return started;
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
await Promise.all(apps.map((app) => app.close()));
|
||||
});
|
||||
|
||||
it('сессия истекает при коротком SESSION_TTL_SECONDS, в том числе из кэша', async () => {
|
||||
const { server } = await start({ SESSION_TTL_SECONDS: '2' });
|
||||
const user = await registerUser(server, 'ttl');
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(200);
|
||||
await sleep(2500);
|
||||
const res = await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(401);
|
||||
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
|
||||
});
|
||||
|
||||
it('без Redis вход и проверка сессии работают через PostgreSQL, logout — 503 без отзыва', async () => {
|
||||
const { server } = await start({ CACHE_URL: UNREACHABLE_REDIS });
|
||||
const user = await registerUser(server, 'noredis');
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(200);
|
||||
const res = await request(server)
|
||||
.delete('/api/v1/auth/sessions/current')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(503);
|
||||
expect(res.body.error.code).toBe('unavailable');
|
||||
await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(200);
|
||||
await request(server).get('/api/v1/users/me').expect(401);
|
||||
});
|
||||
|
||||
it('logout через реплику без связи с Redis не создаёт расхождения с репликой, у которой сессия в кэше', async () => {
|
||||
const a = await start();
|
||||
const b = await start();
|
||||
const user = await registerUser(a.server, 'partition');
|
||||
const me = (server: any, status: number) =>
|
||||
request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(status);
|
||||
const logout = (status: number) =>
|
||||
request(a.server)
|
||||
.delete('/api/v1/auth/sessions/current')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(status);
|
||||
const cache = a.app.get(CacheService) as any;
|
||||
const redis = cache.client as Redis;
|
||||
|
||||
await me(b.server, 200);
|
||||
redis.disconnect();
|
||||
await logout(503);
|
||||
await me(b.server, 200);
|
||||
await me(a.server, 200);
|
||||
|
||||
await redis.connect();
|
||||
await sleep(200);
|
||||
expect(cache.trusted).toBe(true);
|
||||
await me(b.server, 200);
|
||||
expect((await redis.keys('session:v1:*')).length).toBeGreaterThan(0);
|
||||
await logout(204);
|
||||
await me(b.server, 401);
|
||||
await me(a.server, 401);
|
||||
});
|
||||
|
||||
it('без PostgreSQL отвечает ограниченным 503 и не пускает анонима', async () => {
|
||||
const healthy = await start();
|
||||
const user = await registerUser(healthy.server, 'nodb');
|
||||
await request(healthy.server)
|
||||
.get('/api/v1/users/me')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(200);
|
||||
|
||||
const { server } = await start({ DATABASE_URL: UNREACHABLE_DB });
|
||||
const ready = await request(server).get('/health/ready').expect(503);
|
||||
expect(ready.body.error.code).toBe('unavailable');
|
||||
expect(JSON.stringify(ready.body)).not.toMatch(/127\.0\.0\.1|postgresql:|prisma/i);
|
||||
|
||||
await request(server).get('/api/v1/chats').expect(401);
|
||||
await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.set('X-User-Id', user.id)
|
||||
.expect(401);
|
||||
await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.set('Authorization', 'Bearer unknown-token-value')
|
||||
.expect(503);
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(503);
|
||||
expect(res.body.error.code).toBe('unavailable');
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `nodb_${randomUUID().slice(0, 8)}`,
|
||||
display_name: 'X',
|
||||
password: 'Long-enough-pass-1',
|
||||
})
|
||||
.expect(503);
|
||||
});
|
||||
|
||||
it('неожиданное исключение — 500 internal_error без деталей, с X-Request-Id и X-Instance-Id', async () => {
|
||||
const { app, server } = await start();
|
||||
const user = await registerUser(server, 'boom');
|
||||
jest
|
||||
.spyOn(app.get(ChatsService), 'listForMember')
|
||||
.mockRejectedValueOnce(new Error('secret internal details'));
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(500);
|
||||
expect(res.body.error.code).toBe('internal_error');
|
||||
expect(JSON.stringify(res.body)).not.toMatch(/secret/);
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
});
|
||||
|
||||
it('откатывает создание чата целиком, если один из участников не существует', async () => {
|
||||
const { app, server } = await start();
|
||||
const alice = await registerUser(server, 'rollback');
|
||||
const title = `rollback ${randomUUID()}`;
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title, member_ids: [alice.id, randomUUID()] })
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
const prisma = app.get(PrismaService);
|
||||
expect(await prisma.chat.count({ where: { title } })).toBe(0);
|
||||
expect(
|
||||
await prisma.chatMember.count({ where: { userId: alice.id } }),
|
||||
).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import {
|
||||
BucketAlreadyOwnedByYou,
|
||||
CreateBucketCommand,
|
||||
S3Client,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { execSync } from 'child_process';
|
||||
|
||||
export default async function globalSetup(): Promise<void> {
|
||||
execSync('npx prisma migrate deploy', { stdio: 'inherit' });
|
||||
const client = new S3Client({
|
||||
endpoint: process.env.S3_ENDPOINT,
|
||||
region: 'us-east-1',
|
||||
forcePathStyle: true,
|
||||
credentials: {
|
||||
accessKeyId: process.env.S3_ACCESS_KEY_ID!,
|
||||
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
|
||||
},
|
||||
});
|
||||
try {
|
||||
await client.send(new CreateBucketCommand({ Bucket: process.env.S3_BUCKET! }));
|
||||
} catch (error) {
|
||||
if (!(error instanceof BucketAlreadyOwnedByYou)) {
|
||||
throw error;
|
||||
}
|
||||
} finally {
|
||||
client.destroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { randomUUID } from 'crypto';
|
||||
import request from 'supertest';
|
||||
import { AppModule } from '../src/app.module';
|
||||
import { configureApp } from '../src/configure-app';
|
||||
|
||||
export const PASSWORD = 'Correct-Horse-Battery-42';
|
||||
|
||||
export interface TestUser {
|
||||
id: string;
|
||||
username: string;
|
||||
token: string;
|
||||
}
|
||||
|
||||
export async function createTestApp(
|
||||
env: Record<string, string> = {},
|
||||
setup: (app: NestFastifyApplication) => void = () => undefined,
|
||||
): Promise<{
|
||||
app: INestApplication;
|
||||
server: any;
|
||||
}> {
|
||||
const saved = { ...process.env };
|
||||
Object.assign(process.env, env);
|
||||
let app: NestFastifyApplication;
|
||||
try {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
app = moduleRef.createNestApplication<NestFastifyApplication>(
|
||||
new FastifyAdapter(),
|
||||
);
|
||||
} finally {
|
||||
process.env = saved;
|
||||
}
|
||||
configureApp(app);
|
||||
setup(app);
|
||||
await app.listen(0, '127.0.0.1');
|
||||
return { app, server: app.getHttpServer() };
|
||||
}
|
||||
|
||||
export function basic(username: string, password: string): string {
|
||||
return `Basic ${Buffer.from(`${username}:${password}`, 'utf8').toString('base64')}`;
|
||||
}
|
||||
|
||||
export function bearer(user: TestUser): string {
|
||||
return `Bearer ${user.token}`;
|
||||
}
|
||||
|
||||
export async function login(
|
||||
server: any,
|
||||
username: string,
|
||||
password = PASSWORD,
|
||||
): Promise<string> {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/auth/sessions')
|
||||
.set('Authorization', basic(username, password))
|
||||
.expect(201);
|
||||
return res.body.token;
|
||||
}
|
||||
|
||||
export async function registerUser(
|
||||
server: any,
|
||||
label: string,
|
||||
): Promise<TestUser> {
|
||||
const username = `u_${label}_${randomUUID().slice(0, 8)}`;
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username, display_name: `User ${label}`, password: PASSWORD })
|
||||
.expect(201);
|
||||
return { id: res.body.id, username, token: await login(server, username) };
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
describe('Идемпотентность отправки и X-Instance-Id (e2e)', () => {
|
||||
const apps: INestApplication[] = [];
|
||||
let server: any;
|
||||
let replica: any;
|
||||
let prisma: PrismaService;
|
||||
|
||||
beforeAll(async () => {
|
||||
const first = await createTestApp();
|
||||
const second = await createTestApp();
|
||||
apps.push(first.app, second.app);
|
||||
server = first.server;
|
||||
replica = second.server;
|
||||
prisma = first.app.get(PrismaService);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await Promise.all(apps.map((app) => app.close()));
|
||||
});
|
||||
|
||||
async function setup(label: string) {
|
||||
const alice = await registerUser(server, label);
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Idem', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
return { alice, path: `/api/v1/chats/${chat.body.id}/messages` };
|
||||
}
|
||||
|
||||
function send(target: any, user: TestUser, path: string, key: string | null, body: object) {
|
||||
const req = request(target).post(path).set('Authorization', bearer(user));
|
||||
return (key === null ? req : req.set('Idempotency-Key', key)).send(body);
|
||||
}
|
||||
|
||||
async function countMessages(path: string, user: TestUser): Promise<number> {
|
||||
const res = await request(server)
|
||||
.get(`${path}?limit=100`)
|
||||
.set('Authorization', bearer(user))
|
||||
.expect(200);
|
||||
return res.body.items.length;
|
||||
}
|
||||
|
||||
it('требует Idempotency-Key правильного формата', async () => {
|
||||
const { alice, path } = await setup('keyfmt');
|
||||
for (const key of [null, '', 'has space', 'semi;colon', 'slash/key', 'x'.repeat(129)]) {
|
||||
const res = await send(server, alice, path, key, { text: 'x' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
}
|
||||
await send(server, alice, path, 'A-z_0.9:' + 'k'.repeat(120), { text: 'x' }).expect(201);
|
||||
});
|
||||
|
||||
it('повтор с тем же ключом и телом возвращает тот же 201 и то же сообщение', async () => {
|
||||
const { alice, path } = await setup('replay');
|
||||
const key = randomUUID();
|
||||
const first = await send(server, alice, path, key, { text: 'Привет 👋' }).expect(201);
|
||||
const again = await send(replica, alice, path, key, { text: 'Привет 👋' }).expect(201);
|
||||
expect(again.body).toEqual(first.body);
|
||||
expect(await countMessages(path, alice)).toBe(1);
|
||||
});
|
||||
|
||||
it('тот же ключ с другим телом — 409, сообщение не создаётся', async () => {
|
||||
const { alice, path } = await setup('conflict');
|
||||
const key = randomUUID();
|
||||
await send(server, alice, path, key, { text: 'первое' }).expect(201);
|
||||
const res = await send(server, alice, path, key, { text: 'другое' }).expect(409);
|
||||
expect(res.body.error.code).toBe('conflict');
|
||||
expect(await countMessages(path, alice)).toBe(1);
|
||||
});
|
||||
|
||||
it('20 параллельных запросов с одним ключом через два экземпляра создают ровно одно сообщение', async () => {
|
||||
const { alice, path } = await setup('parallel');
|
||||
const key = randomUUID();
|
||||
const responses = await Promise.all(
|
||||
Array.from({ length: 20 }, (_, i) =>
|
||||
send(i % 2 ? replica : server, alice, path, key, { text: 'одно' }),
|
||||
),
|
||||
);
|
||||
expect(responses.map((r) => r.status)).toEqual(Array(20).fill(201));
|
||||
expect(new Set(responses.map((r) => r.body.id)).size).toBe(1);
|
||||
expect(await countMessages(path, alice)).toBe(1);
|
||||
});
|
||||
|
||||
it('область ключа — пользователь и чат: одинаковый ключ у разных пользователей и чатов независим', async () => {
|
||||
const a = await setup('scopea');
|
||||
const b = await setup('scopeb');
|
||||
const key = randomUUID();
|
||||
const one = await send(server, a.alice, a.path, key, { text: 'x' }).expect(201);
|
||||
const two = await send(server, b.alice, b.path, key, { text: 'x' }).expect(201);
|
||||
expect(one.body.id).not.toBe(two.body.id);
|
||||
});
|
||||
|
||||
it('повтор заново проверяет сессию', async () => {
|
||||
const { alice, path } = await setup('recheck');
|
||||
const key = randomUUID();
|
||||
await send(server, alice, path, key, { text: 'x' }).expect(201);
|
||||
await request(server)
|
||||
.delete('/api/v1/auth/sessions/current')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(204);
|
||||
await send(server, alice, path, key, { text: 'x' }).expect(401);
|
||||
});
|
||||
|
||||
it('после IDEMPOTENCY_TTL_SECONDS ключ можно использовать заново', async () => {
|
||||
const { app, server: short } = await createTestApp({ IDEMPOTENCY_TTL_SECONDS: '1' });
|
||||
apps.push(app);
|
||||
const { alice, path } = await setup('ttl');
|
||||
const key = randomUUID();
|
||||
const first = await send(short, alice, path, key, { text: 'x' }).expect(201);
|
||||
await sleep(1500);
|
||||
const second = await send(short, alice, path, key, { text: 'y' }).expect(201);
|
||||
expect(second.body.id).not.toBe(first.body.id);
|
||||
expect(await prisma.idempotencyKey.count({ where: { key } })).toBe(1);
|
||||
});
|
||||
|
||||
it('X-Instance-Id есть в успешных ответах, ошибках и health и стабилен в процессе', async () => {
|
||||
const ids = new Set<string>();
|
||||
for (const res of [
|
||||
await request(server).get('/health/live').expect(200),
|
||||
await request(server).get('/health/ready').expect(200),
|
||||
await request(server).get('/api/v1/users/me').expect(401),
|
||||
await request(server).post('/api/v1/users').send('{bad').set('Content-Type', 'application/json').expect(400),
|
||||
]) {
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
ids.add(res.headers['x-instance-id']);
|
||||
}
|
||||
expect(ids.size).toBe(1);
|
||||
});
|
||||
});
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import sharp from 'sharp';
|
||||
import { deflateSync } from 'zlib';
|
||||
|
||||
const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
|
||||
let c = n;
|
||||
for (let k = 0; k < 8; k++) {
|
||||
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
||||
}
|
||||
return c >>> 0;
|
||||
});
|
||||
|
||||
function crc32(data: Buffer): number {
|
||||
let c = 0xffffffff;
|
||||
for (const byte of data) {
|
||||
c = CRC_TABLE[(c ^ byte) & 255] ^ (c >>> 8);
|
||||
}
|
||||
return (c ^ 0xffffffff) >>> 0;
|
||||
}
|
||||
|
||||
export function pngChunk(type: string, data: Buffer): Buffer {
|
||||
const name = Buffer.from(type, 'latin1');
|
||||
const length = Buffer.alloc(4);
|
||||
length.writeUInt32BE(data.length);
|
||||
const crc = Buffer.alloc(4);
|
||||
crc.writeUInt32BE(crc32(Buffer.concat([name, data])));
|
||||
return Buffer.concat([length, name, data, crc]);
|
||||
}
|
||||
|
||||
const SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');
|
||||
const END = pngChunk('IEND', Buffer.alloc(0));
|
||||
|
||||
function header(width: number, height: number, colorType = 2): Buffer {
|
||||
const data = Buffer.alloc(13);
|
||||
data.writeUInt32BE(width, 0);
|
||||
data.writeUInt32BE(height, 4);
|
||||
data[8] = 8;
|
||||
data[9] = colorType;
|
||||
return pngChunk('IHDR', data);
|
||||
}
|
||||
|
||||
function rows(width: number, height: number, filter = 0): Buffer {
|
||||
const stride = width * 3 + 1;
|
||||
const raw = Buffer.alloc(stride * height, 7);
|
||||
for (let y = 0; y < height; y++) {
|
||||
raw[y * stride] = filter;
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
export function png(width: number, height: number, extra: Buffer[] = []): Buffer {
|
||||
return Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(width, height),
|
||||
...extra,
|
||||
pngChunk('IDAT', deflateSync(rows(width, height))),
|
||||
END,
|
||||
]);
|
||||
}
|
||||
|
||||
export function pngOfExactSize(size: number): Buffer {
|
||||
const base = png(4, 4);
|
||||
const padding = size - base.length - 12;
|
||||
return png(4, 4, [pngChunk('abCd', Buffer.alloc(padding))]);
|
||||
}
|
||||
|
||||
export function pngHeaderOnly(width: number, height: number): Buffer {
|
||||
return Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(width, height),
|
||||
pngChunk('IDAT', deflateSync(Buffer.alloc(16))),
|
||||
END,
|
||||
]);
|
||||
}
|
||||
|
||||
export const SAMPLE_PNG = readFileSync(
|
||||
join(__dirname, '..', 'tests', 'fixtures', 'sample.png'),
|
||||
);
|
||||
|
||||
export const JPEG_WITHOUT_DATA = Buffer.from(
|
||||
'ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9',
|
||||
'hex',
|
||||
);
|
||||
|
||||
export async function jpeg(): Promise<Buffer> {
|
||||
const noise = Buffer.alloc(320 * 200 * 3);
|
||||
for (let i = 0; i < noise.length; i++) {
|
||||
noise[i] = (i * 7919) & 255;
|
||||
}
|
||||
return sharp(noise, { raw: { width: 320, height: 200, channels: 3 } })
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
export function corruptedPngs(): Record<string, Buffer> {
|
||||
const lastRowFilter = rows(64, 64);
|
||||
lastRowFilter[63 * (64 * 3 + 1)] = 5;
|
||||
const flipped = Buffer.from(SAMPLE_PNG);
|
||||
flipped[100] ^= 255;
|
||||
return {
|
||||
'первые 33 байта sample.png': SAMPLE_PNG.subarray(0, 33),
|
||||
'половина файла': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length >> 1),
|
||||
'без последних 20 байт': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length - 20),
|
||||
'без IEND': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length - 12),
|
||||
'неверный CRC': flipped,
|
||||
'фильтр строки 5': pngFromRaw(1, 1, rows(1, 1, 5)),
|
||||
'фильтр строки 255': pngFromRaw(1, 1, rows(1, 1, 255)),
|
||||
'неверный фильтр в последней строке': pngFromRaw(64, 64, lastRowFilter),
|
||||
'мусор в IDAT с верным CRC': Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(64, 64),
|
||||
pngChunk('IDAT', Buffer.alloc(40, 0xab)),
|
||||
END,
|
||||
]),
|
||||
'данных меньше, чем строк': pngFromRaw(64, 64, rows(64, 10)),
|
||||
'палитра на 257 записей': Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(1, 1, 3),
|
||||
pngChunk('PLTE', Buffer.alloc(771)),
|
||||
pngChunk('IDAT', deflateSync(Buffer.from([0, 0]))),
|
||||
END,
|
||||
]),
|
||||
};
|
||||
}
|
||||
|
||||
export function corruptedJpegs(valid: Buffer): Record<string, Buffer> {
|
||||
const garbled = Buffer.from(valid);
|
||||
for (let i = 800; i < garbled.length - 2; i += 3) {
|
||||
garbled[i] = 0x55;
|
||||
}
|
||||
return {
|
||||
'27 байт без таблиц и данных': JPEG_WITHOUT_DATA,
|
||||
'половина файла': valid.subarray(0, valid.length >> 1),
|
||||
'без последних 20 байт': valid.subarray(0, valid.length - 20),
|
||||
'испорченные данные скана': garbled,
|
||||
};
|
||||
}
|
||||
|
||||
function pngFromRaw(width: number, height: number, raw: Buffer): Buffer {
|
||||
return Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(width, height),
|
||||
pngChunk('IDAT', deflateSync(raw)),
|
||||
END,
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"testTimeout": 30000,
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"rootDir": ".",
|
||||
"testEnvironment": "node",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"globalSetup": "<rootDir>/global-setup.ts",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
import request from 'supertest';
|
||||
import { reconcile } from '../src/attachments/reconciler';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import { SAMPLE_PNG } from './images';
|
||||
|
||||
const HOUR_MS = 60 * 60 * 1000;
|
||||
|
||||
describe('Сверка объектов S3 с метаданными (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
let prisma: PrismaService;
|
||||
let s3: S3Service;
|
||||
let alice: TestUser;
|
||||
let chatId: string;
|
||||
let dir: string;
|
||||
|
||||
const putObject = async (key: string): Promise<void> => {
|
||||
const path = join(dir, randomUUID());
|
||||
writeFileSync(path, SAMPLE_PNG);
|
||||
await s3.putFile({
|
||||
key,
|
||||
path,
|
||||
size: SAMPLE_PNG.length,
|
||||
contentType: 'image/png',
|
||||
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
|
||||
});
|
||||
};
|
||||
|
||||
const exists = async (key: string): Promise<boolean> => {
|
||||
const page = await s3.listObjects(key, 1);
|
||||
return page.objects.some((object) => object.key === key);
|
||||
};
|
||||
|
||||
const scope = () => `reconcile-test/${randomUUID()}/`;
|
||||
const later = () => new Date(Date.now() + 2 * HOUR_MS);
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
prisma = app.get(PrismaService);
|
||||
s3 = app.get(S3Service);
|
||||
dir = mkdtempSync(join(tmpdir(), 'reconcile-test-'));
|
||||
alice = await registerUser(server, 'rec_alice');
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Reconcile', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
chatId = chat.body.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('dry-run сообщает о сироте и ничего не удаляет, apply удаляет, повторный запуск ничего не делает', async () => {
|
||||
const prefix = scope();
|
||||
const key = `${prefix}orphan`;
|
||||
await putObject(key);
|
||||
const options = { graceMs: HOUR_MS, prefix, now: later() };
|
||||
|
||||
const dry = await reconcile(prisma, s3, { ...options, apply: false });
|
||||
expect(dry).toMatchObject({ mode: 'dry-run', scanned: 1, orphans: 1, deleted: 0, orphan_keys: [key] });
|
||||
expect(await exists(key)).toBe(true);
|
||||
|
||||
const applied = await reconcile(prisma, s3, { ...options, apply: true });
|
||||
expect(applied).toMatchObject({ mode: 'apply', orphans: 1, deleted: 1, failed: 0 });
|
||||
expect(await exists(key)).toBe(false);
|
||||
|
||||
const again = await reconcile(prisma, s3, { ...options, apply: true });
|
||||
expect(again).toMatchObject({ scanned: 0, orphans: 0, deleted: 0 });
|
||||
});
|
||||
|
||||
it('не трогает свежий объект внутри grace period', async () => {
|
||||
const prefix = scope();
|
||||
const key = `${prefix}fresh`;
|
||||
await putObject(key);
|
||||
const report = await reconcile(prisma, s3, { apply: true, graceMs: HOUR_MS, prefix });
|
||||
expect(report).toMatchObject({ scanned: 1, fresh_skipped: 1, orphans: 0, deleted: 0 });
|
||||
expect(await exists(key)).toBe(true);
|
||||
});
|
||||
|
||||
it('не трогает старый объект, на который есть метаданные', async () => {
|
||||
const prefix = scope();
|
||||
const key = `${prefix}referenced`;
|
||||
await putObject(key);
|
||||
await prisma.attachment.create({
|
||||
data: {
|
||||
chatId,
|
||||
ownerId: alice.id,
|
||||
filename: 'a.png',
|
||||
contentType: 'image/png',
|
||||
sizeBytes: SAMPLE_PNG.length,
|
||||
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
|
||||
objectKey: key,
|
||||
},
|
||||
});
|
||||
const report = await reconcile(prisma, s3, {
|
||||
apply: true,
|
||||
graceMs: HOUR_MS,
|
||||
prefix,
|
||||
now: later(),
|
||||
});
|
||||
expect(report).toMatchObject({ scanned: 1, referenced: 1, orphans: 0, deleted: 0 });
|
||||
expect(await exists(key)).toBe(true);
|
||||
});
|
||||
|
||||
it('при отказе PostgreSQL проход прерывается и ничего не удаляет', async () => {
|
||||
const prefix = scope();
|
||||
const key = `${prefix}orphan`;
|
||||
await putObject(key);
|
||||
const broken = {
|
||||
attachment: {
|
||||
findMany: jest.fn().mockRejectedValue(new Error('database down')),
|
||||
},
|
||||
} as unknown as PrismaService;
|
||||
await expect(
|
||||
reconcile(broken, s3, { apply: true, graceMs: HOUR_MS, prefix, now: later() }),
|
||||
).rejects.toThrow('database down');
|
||||
expect(await exists(key)).toBe(true);
|
||||
});
|
||||
|
||||
it('сбой удаления считается и повторяется на следующем проходе', async () => {
|
||||
const prefix = scope();
|
||||
const key = `${prefix}orphan`;
|
||||
await putObject(key);
|
||||
const options = { apply: true, graceMs: HOUR_MS, prefix, now: later() };
|
||||
const remove = jest.spyOn(s3, 'delete').mockRejectedValueOnce(new Error('s3 down'));
|
||||
try {
|
||||
const failed = await reconcile(prisma, s3, options);
|
||||
expect(failed).toMatchObject({ orphans: 1, deleted: 0, failed: 1 });
|
||||
expect(await exists(key)).toBe(true);
|
||||
} finally {
|
||||
remove.mockRestore();
|
||||
}
|
||||
const retried = await reconcile(prisma, s3, options);
|
||||
expect(retried).toMatchObject({ orphans: 1, deleted: 1, failed: 0 });
|
||||
expect(await exists(key)).toBe(false);
|
||||
});
|
||||
|
||||
it('обходит все страницы листинга', async () => {
|
||||
const prefix = scope();
|
||||
await Promise.all([1, 2, 3].map((n) => putObject(`${prefix}${n}`)));
|
||||
const report = await reconcile(prisma, s3, {
|
||||
apply: true,
|
||||
graceMs: HOUR_MS,
|
||||
prefix,
|
||||
now: later(),
|
||||
pageSize: 1,
|
||||
});
|
||||
expect(report).toMatchObject({ scanned: 3, orphans: 3, deleted: 3 });
|
||||
});
|
||||
|
||||
it('реальная загрузка остаётся доступной после сверки всего bucket префикса вложений', async () => {
|
||||
const upload = await request(server)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG)
|
||||
.expect(201);
|
||||
const report = await reconcile(prisma, s3, { apply: true, graceMs: HOUR_MS });
|
||||
expect(report.fresh_skipped).toBeGreaterThanOrEqual(1);
|
||||
await request(server)
|
||||
.get(`/api/v1/attachments/${upload.body.id}/content`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Готовность при отказе S3 (e2e)', () => {
|
||||
it('ready — 503 с заголовками за короткое время, live и core — 200', async () => {
|
||||
const { app, server } = await createTestApp({ S3_ENDPOINT: 'http://127.0.0.1:9' });
|
||||
try {
|
||||
const started = Date.now();
|
||||
const res = await request(server).get('/health/ready').expect(503);
|
||||
expect(Date.now() - started).toBeLessThan(2000);
|
||||
expect(res.body.error.code).toBe('unavailable');
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
await request(server).get('/health/live').expect(200);
|
||||
await request(server).get('/health/core').expect(200);
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('при рабочем S3 ready — 200', async () => {
|
||||
const { app, server } = await createTestApp();
|
||||
try {
|
||||
await request(server).get('/health/ready').expect(200);
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user