chore: port lab5 solution as lab6 base

This commit is contained in:
2026-11-18 18:40:47 +03:00
parent 470d9d6f1e
commit 98d5787545
105 changed files with 21898 additions and 0 deletions
+686
View File
@@ -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']);
});
});
});
+302
View File
@@ -0,0 +1,302 @@
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,
processAttachment,
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(202);
expect(await processAttachment(app, res.body.id)).toBe('ready');
const ready = await request(server)
.get(`/api/v1/attachments/${res.body.id}`)
.set('Authorization', bearer(user))
.expect(200);
return ready.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(200);
await app.get(PrismaService).attachmentVariant.deleteMany({
where: { attachmentId: id, name: 'crop' },
});
await get(`/api/v1/attachments/${id}/content?variant=crop`).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)));
}
+401
View File
@@ -0,0 +1,401 @@
import { INestApplication } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import Ajv2020 from 'ajv/dist/2020';
import { createHash, randomUUID } from 'crypto';
import { mkdtempSync, readdirSync, rmSync } from 'fs';
import { connect } from 'net';
import { tmpdir } from 'os';
import { join } from 'path';
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']);
};
const uploadTmp = mkdtempSync(join(tmpdir(), 'attachments-test-'));
const activeTempDirs = (): string[] =>
readdirSync(uploadTmp).filter((name) => name.startsWith('messenger-upload-'));
beforeAll(async () => {
({ app, server } = await createTestApp({ UPLOAD_TMP_DIR: uploadTmp }));
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();
rmSync(uploadTmp, { recursive: true, force: true });
});
describe('успешная загрузка', () => {
it('возвращает 202 Attachment queued по контракту, staging и задание сохранены до ответа', async () => {
const res = await upload(SAMPLE_PNG).expect(202);
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: 'queued',
variants: [],
error_code: null,
});
const prisma = app.get(PrismaService);
const row = await prisma.attachment.findUnique({
where: { id: res.body.id },
include: { jobs: { include: { outbox: true } } },
});
expect(row).toMatchObject({
status: 'queued',
objectKey: `attachments/${res.body.id}/original`,
stagingKey: `staging/${res.body.id}`,
});
expect(row!.jobs).toHaveLength(1);
expect(row!.jobs[0]).toMatchObject({ status: 'queued', attempt: 1, pipelineVersion: 1 });
expect(row!.jobs[0].outbox).toHaveLength(1);
expect(row!.jobs[0].outbox[0].payload).toEqual({
schema_version: 1,
job_id: row!.jobs[0].id,
type: 'attachment.process.v1',
attachment_id: res.body.id,
pipeline_version: 1,
attempt: 1,
created_at: expect.any(String),
});
const staged = await app.get(S3Service).getBuffer(`staging/${res.body.id}`, MAX_BYTES);
expect(Buffer.compare(staged, SAMPLE_PNG)).toBe(0);
expect(JSON.stringify(res.body)).not.toMatch(/attachments\/|staging\//);
});
it('принимает реальный JPEG', async () => {
const res = await upload(await jpeg(), {
type: 'image/jpeg',
filename: 'фото 1.jpg',
}).expect(202);
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(202);
});
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(202);
expect(res.body.size_bytes).toBe(MAX_BYTES);
await upload(SAMPLE_PNG, { filename: `${'ф'.repeat(251)}.png` }).expect(202);
await upload(Buffer.concat([png(4, 4), Buffer.from('tail')])).expect(202);
});
it('принимает ровно 40 млн пикселей', async () => {
const res = await upload(await bigPng(8000, 5000));
expect(res.status).toBe(202);
});
});
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({ where: { chatId } });
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({ where: { chatId } })).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({ where: { chatId } });
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({ where: { chatId } })).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({ where: { chatId } });
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({ where: { chatId } })).toBe(before);
} finally {
await down.close();
}
});
it('явный отказ транзакции после PUT удаляет объект staging', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').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);
expect(remove.mock.calls[0][0]).toMatch(/^staging\//);
} 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, '$transaction').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, '$transaction').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();
}
});
});
});
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();
}
+179
View File
@@ -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);
});
});
});
+119
View File
@@ -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);
});
});
+241
View File
@@ -0,0 +1,241 @@
import { INestApplication } from '@nestjs/common';
import Ajv2020 from 'ajv/dist/2020';
import { randomUUID } from 'crypto';
import request from 'supertest';
import { PrismaService } from '../src/db/prisma.service';
import { loadDocument } from '../src/docs/swagger';
import {
basic,
bearer,
createTestApp,
PASSWORD,
processAttachment,
registerUser,
TestUser,
} from './helpers';
import { jpeg, SAMPLE_PNG } from './images';
const CONTRACT_ID = 'https://contract.local/openapi.json';
describe('Все ответы соответствуют контракту: коды, схемы, заголовки (e2e)', () => {
let app: INestApplication;
let server: any;
let alice: TestUser;
let bob: TestUser;
let eve: TestUser;
const document: any = loadDocument();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
ajv.addSchema(document, CONTRACT_ID);
const covered = new Set<string>();
const call = async (
method: 'get' | 'post' | 'delete',
template: string,
values: Record<string, string>,
expected: number,
{
user,
auth,
query = '',
body,
raw,
type,
headers = {},
}: {
user?: TestUser | null;
auth?: string;
query?: string;
body?: unknown;
raw?: Buffer | string;
type?: string;
headers?: Record<string, string>;
} = {},
) => {
const path = template.replace(/\{(\w+)\}/g, (_, name) => values[name]);
let req = request(server)[method](path + query).buffer(true).parse(binaryParser);
if (user) {
req = req.set('Authorization', bearer(user));
}
if (auth) {
req = req.set('Authorization', auth);
}
for (const [name, value] of Object.entries(headers)) {
req = req.set(name, value);
}
if (type) {
req = req.set('Content-Type', type);
}
const res = raw !== undefined ? await req.send(raw) : body !== undefined ? await req.send(body as object) : await req;
const operation = document.paths[template][method];
const label = `${method.toUpperCase()} ${template} -> ${res.status}`;
expect({ label, status: res.status }).toEqual({ label, status: expected });
expect({ label, declared: String(res.status) in operation.responses }).toEqual({ label, declared: true });
expect({ label, requestId: Boolean(res.headers['x-request-id']) }).toEqual({ label, requestId: true });
expect({ label, instanceId: Boolean(res.headers['x-instance-id']) }).toEqual({ label, instanceId: true });
const declared = operation.responses[String(res.status)];
const content = declared.content ?? {};
const type_ = (res.headers['content-type'] ?? '').split(';')[0];
if (res.status === 204) {
expect({ label, length: res.body.length }).toEqual({ label, length: 0 });
} else if (content['application/json']) {
expect({ label, type: type_ }).toEqual({ label, type: 'application/json' });
const json = JSON.parse(res.body.toString('utf8'));
const schema = content['application/json'].schema;
const validate = ajv.compile(schema.$ref ? { $ref: CONTRACT_ID + schema.$ref } : schema);
const valid = validate(json);
expect({ label, valid, errors: validate.errors }).toEqual({ label, valid: true, errors: null });
if (json && typeof json === 'object' && 'error' in json) {
expect({ label, same: json.error.request_id === res.headers['x-request-id'] }).toEqual({ label, same: true });
}
} else {
expect({ label, type: Object.keys(content).includes(type_) }).toEqual({ label, type: true });
}
covered.add(label);
return res.status === 204 || !content['application/json'] ? res.body : JSON.parse(res.body.toString('utf8'));
};
beforeAll(async () => {
({ app, server } = await createTestApp());
alice = await registerUser(server, 'ct_alice');
bob = await registerUser(server, 'ct_bob');
eve = await registerUser(server, 'ct_eve');
});
afterAll(async () => {
await app.close();
});
it('health, пользователи и сессии', async () => {
await call('get', '/health/live', {}, 200);
await call('get', '/health/ready', {}, 200);
const username = `ct_${randomUUID().slice(0, 8)}`;
await call('post', '/api/v1/users', {}, 201, { body: { username, display_name: 'C', password: PASSWORD } });
await call('post', '/api/v1/users', {}, 409, { body: { username, display_name: 'C', password: PASSWORD } });
await call('post', '/api/v1/users', {}, 400, { body: { username: 'BAD NAME', display_name: 'C', password: PASSWORD } });
await call('post', '/api/v1/users', {}, 400, { raw: '{bad', type: 'application/json' });
await call('post', '/api/v1/auth/sessions', {}, 201, { auth: basic(username, PASSWORD) });
await call('post', '/api/v1/auth/sessions', {}, 401, { auth: basic(username, 'wrong-password-123') });
await call('post', '/api/v1/auth/sessions', {}, 400, {
auth: basic(username, PASSWORD),
body: { username },
});
await call('get', '/api/v1/users/me', {}, 200, { user: alice });
await call('get', '/api/v1/users/me', {}, 401);
await call('get', '/api/v1/users/me', {}, 401, { auth: basic(username, PASSWORD) });
const temp = await registerUser(server, 'ct_tmp');
await call('delete', '/api/v1/auth/sessions/current', {}, 204, { user: temp });
await call('delete', '/api/v1/auth/sessions/current', {}, 401, { user: temp });
});
it('чаты и сообщения', async () => {
const chat = await call('post', '/api/v1/chats', {}, 201, {
user: alice,
body: { title: 'Contract', member_ids: [alice.id, bob.id] },
});
await call('post', '/api/v1/chats', {}, 400, { user: alice, body: { title: 'x', member_ids: [bob.id] } });
await call('post', '/api/v1/chats', {}, 401, { body: { title: 'x', member_ids: [] } });
await call('get', '/api/v1/chats', {}, 200, { user: bob, query: '?limit=1' });
await call('get', '/api/v1/chats', {}, 400, { user: bob, query: '?limit=0' });
await call('get', '/api/v1/chats', {}, 400, { user: bob, query: '?cursor=garbage' });
const ids = { chat_id: chat.id };
await call('get', '/api/v1/chats/{chat_id}', ids, 200, { user: bob });
await call('get', '/api/v1/chats/{chat_id}', ids, 404, { user: eve });
await call('get', '/api/v1/chats/{chat_id}', { chat_id: 'nope' }, 400, { user: bob });
const key = randomUUID();
const send = (status: number, body: unknown, user: TestUser = alice, idem: string | null = randomUUID()) =>
call('post', '/api/v1/chats/{chat_id}/messages', ids, status, {
user,
body,
headers: idem ? { 'Idempotency-Key': idem } : {},
});
await send(201, { text: 'привет' }, alice, key);
await send(201, { text: 'привет' }, alice, key);
await send(201, { text: 'второе' }, bob);
await send(409, { text: 'другое' }, alice, key);
await send(400, { text: 'без ключа' }, alice, null);
await send(400, { text: '' });
await send(400, { text: 'x', sender_id: alice.id });
await send(404, { text: 'x' }, eve);
await send(404, { text: 'x', attachment_ids: [randomUUID()] });
const page = await call('get', '/api/v1/chats/{chat_id}/messages', ids, 200, { user: bob, query: '?limit=1' });
await call('get', '/api/v1/chats/{chat_id}/messages', ids, 200, {
user: bob,
query: `?limit=1&cursor=${encodeURIComponent(page.next_cursor)}`,
});
await call('get', '/api/v1/chats/{chat_id}/messages', ids, 400, { user: bob, query: '?limit=101' });
await call('get', '/api/v1/chats/{chat_id}/messages', ids, 404, { user: eve });
});
it('вложения во всех состояниях', async () => {
const chat = await call('post', '/api/v1/chats', {}, 201, {
user: alice,
body: { title: 'Files', member_ids: [alice.id, bob.id] },
});
const ids = { chat_id: chat.id };
const upload = (status: number, body: Buffer, type = 'image/png', query = '?filename=a.png', user: TestUser | null = alice) =>
call('post', '/api/v1/chats/{chat_id}/attachments', ids, status, { user, raw: body, type, query });
const queued = await upload(202, SAMPLE_PNG);
expect(queued.status).toBe('queued');
await upload(202, await jpeg(), 'image/jpeg');
await upload(400, SAMPLE_PNG, 'image/png', '?filename=../a.png');
await upload(415, SAMPLE_PNG, 'image/jpeg');
await upload(415, Buffer.from('text'), 'text/plain');
await upload(413, Buffer.alloc(10 * 1024 * 1024 + 1), 'image/png');
await upload(404, SAMPLE_PNG, 'image/png', '?filename=a.png', eve);
await upload(401, SAMPLE_PNG, 'image/png', '?filename=a.png', null);
const one = { attachment_id: queued.id };
const meta = '/api/v1/attachments/{attachment_id}';
const content = '/api/v1/attachments/{attachment_id}/content';
expect((await call('get', meta, one, 200, { user: bob })).status).toBe('queued');
await call('get', content, one, 409, { user: bob });
await call('get', content, one, 409, { user: bob, query: '?variant=thumbnail' });
await call('post', '/api/v1/chats/{chat_id}/messages', ids, 201, {
user: alice,
body: { text: '', attachment_ids: [queued.id] },
headers: { 'Idempotency-Key': randomUUID() },
});
expect(await processAttachment(app, queued.id)).toBe('ready');
const ready = await call('get', meta, one, 200, { user: bob });
expect(ready.variants.map((v: any) => v.name)).toEqual(['thumbnail', 'crop', 'optimized']);
for (const variant of ['', '?variant=original', '?variant=thumbnail', '?variant=crop', '?variant=optimized']) {
await call('get', content, one, 200, { user: bob, query: variant });
}
await call('get', content, one, 400, { user: bob, query: '?variant=huge' });
await call('get', content, one, 404, { user: eve });
await call('get', meta, one, 404, { user: eve });
await call('get', meta, { attachment_id: 'nope' }, 400, { user: bob });
await call('get', meta, one, 401);
const broken = await upload(202, SAMPLE_PNG);
const prisma = app.get(PrismaService);
await prisma.attachmentJob.updateMany({ where: { attachmentId: broken.id }, data: { maxAttempts: 1 } });
await prisma.attachment.update({ where: { id: broken.id }, data: { sha256: 'f'.repeat(64) } });
expect(await processAttachment(app, broken.id)).toBe('failed');
const failed = await call('get', meta, { attachment_id: broken.id }, 200, { user: bob });
expect(failed).toMatchObject({ status: 'failed', error_code: 'checksum_mismatch', variants: [] });
await call('get', content, { attachment_id: broken.id }, 409, { user: bob });
await call('post', '/api/v1/chats/{chat_id}/messages', ids, 409, {
user: alice,
body: { text: '', attachment_ids: [broken.id] },
headers: { 'Idempotency-Key': randomUUID() },
});
});
it('каждая операция контракта вызвана хотя бы раз', () => {
const operations = Object.entries(document.paths).flatMap(([path, ops]: [string, any]) =>
Object.keys(ops)
.filter((method) => ['get', 'post', 'delete'].includes(method))
.map((method) => `${method.toUpperCase()} ${path}`),
);
const called = new Set([...covered].map((label) => label.split(' -> ')[0]));
expect(operations.filter((operation) => !called.has(operation))).toEqual([]);
});
});
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)));
}
+69
View File
@@ -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);
});
});
+166
View File
@@ -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);
});
});
+28
View File
@@ -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();
}
}
+110
View File
@@ -0,0 +1,110 @@
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';
import { PrismaService } from '../src/db/prisma.service';
import { parseJobMessage } from '../src/jobs/job-message';
import {
JobProcessor,
Outcome,
ProcessorHooks,
ProcessorOptions,
processorOptions,
} from '../src/jobs/processor';
import { S3Service } from '../src/storage/s3.service';
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 async function processAttachment(
app: INestApplication,
attachmentId: string,
hooks: ProcessorHooks = {},
options: Partial<ProcessorOptions> = {},
): Promise<Outcome> {
const prisma = app.get(PrismaService);
const entry = await prisma.outboxEntry.findFirstOrThrow({
where: { job: { attachmentId } },
orderBy: { id: 'desc' },
});
const message = parseJobMessage(Buffer.from(JSON.stringify(entry.payload)));
if (!message) {
throw new Error('outbox payload does not match jobs.schema.json');
}
const processor = new JobProcessor(
prisma,
app.get(S3Service),
{ ...processorOptions(), ...options },
hooks,
);
return processor.handle(message);
}
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) };
}
+139
View File
@@ -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);
});
});
+150
View File
@@ -0,0 +1,150 @@
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;
}
const foreignComponent = Buffer.from(valid);
foreignComponent[foreignComponent.indexOf(Buffer.from([0xff, 0xda])) + 5] = 250;
return {
'компонент SOS отсутствует в SOF': foreignComponent,
'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,
]);
}
+11
View File
@@ -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"
}
}
+513
View File
@@ -0,0 +1,513 @@
import { INestApplication } from '@nestjs/common';
import { createHash, randomUUID } from 'crypto';
import sharp from 'sharp';
import request from 'supertest';
import { reconcile } from '../src/attachments/reconciler';
import { ApiError } from '../src/common/filters/api-error';
import { PrismaService } from '../src/db/prisma.service';
import { jobMessage, parseJobMessage } from '../src/jobs/job-message';
import { backoffMs, recoverExpiredLeases, replayJob, republishStale } from '../src/jobs/job-store';
import { JobProcessor, processorOptions } from '../src/jobs/processor';
import { fitInside, InvalidImageError, renderVariants } from '../src/jobs/variants';
import { S3Service } from '../src/storage/s3.service';
import {
bearer,
createTestApp,
processAttachment,
registerUser,
TestUser,
} from './helpers';
import { SAMPLE_PNG } from './images';
const MAX_BYTES = 10 * 1024 * 1024;
describe('Фоновая обработка вложений (e2e)', () => {
let app: INestApplication;
let server: any;
let prisma: PrismaService;
let s3: S3Service;
let alice: TestUser;
let eve: TestUser;
let chatId: string;
const upload = async (body: Buffer = SAMPLE_PNG, type = 'image/png') => {
const res = await request(server)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', type)
.send(body)
.expect(202);
return res.body.id as string;
};
const metadata = async (id: string) =>
(await request(server).get(`/api/v1/attachments/${id}`).set('Authorization', bearer(alice)).expect(200)).body;
const content = (id: string, variant = 'original', user = alice) =>
request(server)
.get(`/api/v1/attachments/${id}/content?variant=${variant}`)
.set('Authorization', bearer(user))
.buffer(true)
.parse(binaryParser);
const job = (attachmentId: string) =>
prisma.attachmentJob.findFirstOrThrow({ where: { attachmentId } });
const makeDue = (attachmentId: string) =>
prisma.attachmentJob.updateMany({ where: { attachmentId }, data: { nextAttemptAt: new Date() } });
const events = async (attachmentId: string) =>
(
await prisma.jobEvent.findMany({
where: { job: { attachmentId } },
orderBy: { id: 'asc' },
})
).map((event) => event.event);
const sendMessage = (attachmentIds: string[]) =>
request(server)
.post(`/api/v1/chats/${chatId}/messages`)
.set('Authorization', bearer(alice))
.set('Idempotency-Key', randomUUID())
.send({ text: '', attachment_ids: attachmentIds });
beforeAll(async () => {
({ app, server } = await createTestApp());
prisma = app.get(PrismaService);
s3 = app.get(S3Service);
alice = await registerUser(server, 'job_alice');
eve = await registerUser(server, 'job_eve');
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Jobs', member_ids: [alice.id] })
.expect(201);
chatId = chat.body.id;
});
afterAll(async () => {
await app.close();
});
describe('до обработки', () => {
it('queued: метаданные доступны, content любого варианта — 409, сообщение с вложением создаётся', async () => {
const id = await upload();
expect(await metadata(id)).toMatchObject({ status: 'queued', variants: [], error_code: null });
for (const variant of ['original', 'thumbnail', 'crop', 'optimized']) {
const res = await content(id, variant).expect(409);
expect(JSON.parse(res.body.toString()).error.code).toBe('conflict');
}
await content(id, 'original', eve).expect(404);
await content(id, 'nonsense').expect(400);
const message = await sendMessage([id]).expect(201);
expect(message.body.attachment_ids).toEqual([id]);
});
});
describe('результат worker', () => {
it('ready: оригинал неизменен, thumbnail 256×160 JPEG, crop 128×128 JPEG, optimized WebP без увеличения', async () => {
const id = await upload();
expect(await processAttachment(app, id)).toBe('ready');
const meta = await metadata(id);
expect(meta.status).toBe('ready');
expect(meta.error_code).toBeNull();
expect(meta.variants.map((v: any) => [v.name, v.content_type, v.width, v.height])).toEqual([
['thumbnail', 'image/jpeg', 256, 160],
['crop', 'image/jpeg', 128, 128],
['optimized', 'image/webp', 320, 200],
]);
const original = await content(id).expect(200);
expect(Buffer.compare(original.body, SAMPLE_PNG)).toBe(0);
for (const variant of meta.variants) {
const res = await content(id, variant.name).expect(200);
expect(res.headers['content-type']).toBe(variant.content_type);
expect(res.body.length).toBe(variant.size_bytes);
expect(createHash('sha256').update(res.body).digest('hex')).toBe(variant.sha256);
const actual = await sharp(res.body).metadata();
expect([actual.format, actual.width, actual.height, actual.exif]).toEqual([
variant.content_type.split('/')[1],
variant.width,
variant.height,
undefined,
]);
}
const row = await prisma.attachment.findUniqueOrThrow({ where: { id } });
expect(row.stagingKey).toBeNull();
await expect(s3.getBuffer(`staging/${id}`, MAX_BYTES)).rejects.toThrow('missing');
expect(await events(id)).toEqual(['enqueued', 'processing', 'ready']);
});
it('маленький исходник не увеличивается в thumbnail/optimized, crop — увеличивается', async () => {
const small = await sharp({ create: { width: 100, height: 50, channels: 3, background: '#123456' } })
.png()
.toBuffer();
const id = await upload(small);
expect(await processAttachment(app, id)).toBe('ready');
const sizes = (await metadata(id)).variants.map((v: any) => [v.name, v.width, v.height]);
expect(sizes).toEqual([
['thumbnail', 100, 50],
['crop', 128, 128],
['optimized', 100, 50],
]);
});
it('прозрачность компонуется на белый, EXIF orientation применяется', async () => {
const transparent = await sharp({
create: { width: 40, height: 20, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
})
.png()
.toBuffer();
const alphaId = await upload(transparent);
expect(await processAttachment(app, alphaId)).toBe('ready');
const thumb = await content(alphaId, 'thumbnail').expect(200);
const { data } = await sharp(thumb.body).raw().toBuffer({ resolveWithObject: true });
expect(Math.min(...data)).toBeGreaterThanOrEqual(250);
const rotated = await sharp({ create: { width: 320, height: 200, channels: 3, background: '#808080' } })
.jpeg()
.withMetadata({ orientation: 6 })
.toBuffer();
const rotatedId = await upload(rotated, 'image/jpeg');
expect(await processAttachment(app, rotatedId)).toBe('ready');
const thumbnail = (await metadata(rotatedId)).variants.find((v: any) => v.name === 'thumbnail');
expect([thumbnail.width, thumbnail.height]).toEqual([160, 256]);
});
it('worker ограничивает пиксели до распаковки: превышение — InvalidImageError', async () => {
const image = await sharp({ create: { width: 300, height: 200, channels: 3, background: '#000' } })
.png()
.toBuffer();
await expect(renderVariants(image, 300 * 200 - 1)).rejects.toBeInstanceOf(InvalidImageError);
await expect(renderVariants(image, 300 * 200)).resolves.toHaveLength(3);
});
it('размеры считаются целочисленно: 319×200 → 256×160, перебор совпадает с точной формулой', () => {
expect(fitInside(319, 200, 256)).toEqual({ width: 256, height: 160 });
for (let width = 1; width <= 3000; width++) {
for (const height of [1, 7, 100, 199, 200, 256, 257, 333, 1000, 2999]) {
const longest = BigInt(Math.max(width, height));
const expected =
width <= 256 && height <= 256
? { width, height }
: width >= height
? { width: 256, height: Math.max(1, Number((BigInt(height) * 256n) / longest)) }
: { width: Math.max(1, Number((BigInt(width) * 256n) / longest)), height: 256 };
expect(fitInside(width, height, 256)).toEqual(expected);
}
}
});
it('размеры округляются вниз, минимум 1 px', () => {
expect(fitInside(320, 200, 256)).toEqual({ width: 256, height: 160 });
expect(fitInside(1000, 3, 256)).toEqual({ width: 256, height: 1 });
expect(fitInside(333, 1000, 256)).toEqual({ width: 85, height: 256 });
expect(fitInside(10, 10, 256)).toEqual({ width: 10, height: 10 });
});
});
describe('идемпотентность и повторная доставка', () => {
it('повтор уже ready-задания — no-op: второго набора вариантов нет, объекты не меняются', async () => {
const id = await upload();
expect(await processAttachment(app, id)).toBe('ready');
const before = await prisma.attachmentVariant.findMany({ where: { attachmentId: id } });
const put = jest.spyOn(s3, 'putBuffer');
try {
expect(await processAttachment(app, id)).toBe('skipped');
expect(put).not.toHaveBeenCalled();
} finally {
put.mockRestore();
}
const after = await prisma.attachmentVariant.findMany({ where: { attachmentId: id } });
expect(after).toEqual(before);
expect(after).toHaveLength(3);
});
it('пока lease действует, второй обработчик того же job не запускается', async () => {
const id = await upload();
let concurrent: string | undefined;
const outcome = await processAttachment(app, id, {
afterStore: async () => {
concurrent = await processAttachment(app, id);
},
});
expect([outcome, concurrent]).toEqual(['ready', 'skipped']);
});
it('worker упал после записи объектов, до COMMIT/ack: lease истекает, повтор завершает тот же результат', async () => {
const id = await upload();
await expect(
processAttachment(app, id, {
afterStore: async () => {
throw new Error('worker crashed');
},
}),
).rejects.toThrow('worker crashed');
expect(await job(id)).toMatchObject({ status: 'processing', attempt: 1 });
expect((await metadata(id)).status).toBe('processing');
const stored = await s3.getBuffer(`attachments/${id}/v1/thumbnail.jpg`, MAX_BYTES);
expect(await recoverExpiredLeases(prisma)).toBe(0);
const future = new Date(Date.now() + 10 * 60 * 1000);
expect(await recoverExpiredLeases(prisma, future)).toBeGreaterThanOrEqual(1);
expect(await job(id)).toMatchObject({ status: 'queued', attempt: 2 });
await makeDue(id);
expect(await processAttachment(app, id)).toBe('ready');
const variants = await prisma.attachmentVariant.findMany({ where: { attachmentId: id } });
expect(variants).toHaveLength(3);
const thumbnail = variants.find((v) => v.name === 'thumbnail')!;
expect(thumbnail.sha256).toBe(createHash('sha256').update(stored).digest('hex'));
expect(await events(id)).toEqual(['enqueued', 'processing', 'lease_expired', 'processing', 'ready']);
});
it('worker упал после COMMIT, до ack: повтор — no-op, staging убирается при повторной доставке', async () => {
const id = await upload();
await expect(
processAttachment(app, id, {
afterCommit: async () => {
throw new Error('worker crashed');
},
}),
).rejects.toThrow('worker crashed');
expect((await metadata(id)).status).toBe('ready');
expect((await prisma.attachment.findUniqueOrThrow({ where: { id } })).stagingKey).toBe(`staging/${id}`);
const put = jest.spyOn(s3, 'putBuffer');
try {
expect(await processAttachment(app, id)).toBe('skipped');
expect(put).not.toHaveBeenCalled();
} finally {
put.mockRestore();
}
expect((await prisma.attachment.findUniqueOrThrow({ where: { id } })).stagingKey).toBeNull();
expect(await prisma.attachmentVariant.count({ where: { attachmentId: id } })).toBe(3);
expect(await events(id)).toEqual(['enqueued', 'processing', 'ready']);
});
it('сообщение потеряно брокером: queued без свежей публикации публикуется повторно один раз', async () => {
const id = await upload();
const row = await job(id);
const later = new Date(Date.now() + 10 * 60 * 1000);
expect(await republishStale(prisma, 5 * 60 * 1000, later)).toBe(0);
await prisma.outboxEntry.updateMany({
where: { jobId: row.id },
data: { publishedAt: new Date() },
});
expect(await republishStale(prisma, 5 * 60 * 1000, new Date())).toBe(0);
expect(await republishStale(prisma, 5 * 60 * 1000, later)).toBeGreaterThanOrEqual(1);
expect(await prisma.outboxEntry.count({ where: { jobId: row.id } })).toBe(2);
expect(await republishStale(prisma, 5 * 60 * 1000, later)).toBe(0);
expect(await prisma.outboxEntry.count({ where: { jobId: row.id } })).toBe(2);
expect(await processAttachment(app, id)).toBe('ready');
expect(await events(id)).toEqual(['enqueued', 'republished', 'processing', 'ready']);
});
it('устаревшее сообщение, чужой attachment_id и другая pipeline_version игнорируются', async () => {
const id = await upload();
const row = await job(id);
const processor = new JobProcessor(prisma, s3, processorOptions());
const base = jobMessage(row, new Date());
expect(await processor.handle({ ...base, attachment_id: randomUUID() })).toBe('skipped');
expect(await processor.handle({ ...base, pipeline_version: 2 })).toBe('skipped');
expect(await processor.handle({ ...base, job_id: randomUUID() })).toBe('skipped');
expect((await job(id)).status).toBe('queued');
});
});
describe('ошибки, повторы и replay', () => {
it('временная ошибка S3: повтор с backoff, до срока задание не берётся, затем ready', async () => {
const id = await upload();
const put = jest
.spyOn(s3, 'putBuffer')
.mockRejectedValueOnce(new ApiError('unavailable', 'object storage unavailable'));
try {
expect(await processAttachment(app, id)).toBe('retry');
} finally {
put.mockRestore();
}
const retried = await job(id);
expect(retried).toMatchObject({ status: 'queued', attempt: 2, lastError: 'temporary failure: ApiError' });
expect(retried.nextAttemptAt.getTime()).toBeGreaterThan(Date.now());
expect((await metadata(id)).status).toBe('queued');
const outbox = await prisma.outboxEntry.findMany({ where: { jobId: retried.id }, orderBy: { id: 'asc' } });
expect(outbox).toHaveLength(2);
expect(outbox[1].availableAt).toEqual(retried.nextAttemptAt);
expect((outbox[1].payload as any).attempt).toBe(2);
expect((outbox[1].payload as any).job_id).toBe(retried.id);
expect(await processAttachment(app, id)).toBe('skipped');
await makeDue(id);
expect(await processAttachment(app, id)).toBe('ready');
expect(await events(id)).toEqual(['enqueued', 'processing', 'retry_scheduled', 'processing', 'ready']);
});
it('попытки исчерпаны — failed retry_exhausted; replay явно возвращает в очередь и доводит до ready', async () => {
const id = await upload();
await prisma.attachmentJob.updateMany({ where: { attachmentId: id }, data: { maxAttempts: 2 } });
const put = jest
.spyOn(s3, 'putBuffer')
.mockRejectedValue(new ApiError('unavailable', 'object storage unavailable'));
try {
expect(await processAttachment(app, id)).toBe('retry');
await makeDue(id);
expect(await processAttachment(app, id)).toBe('failed');
} finally {
put.mockRestore();
}
expect(await metadata(id)).toMatchObject({ status: 'failed', error_code: 'retry_exhausted', variants: [] });
const failed = await job(id);
expect(failed).toMatchObject({ status: 'failed', errorCode: 'retry_exhausted', attempt: 2 });
await sendMessage([id]).expect(409);
expect(await replayJob(prisma, failed.id, 'test-operator')).toBe('replayed');
expect(await job(id)).toMatchObject({ status: 'queued', attempt: 1, errorCode: null });
expect(await metadata(id)).toMatchObject({ status: 'queued', error_code: null });
expect(await processAttachment(app, id)).toBe('ready');
const outboxBefore = await prisma.outboxEntry.count({ where: { jobId: failed.id } });
expect(await replayJob(prisma, failed.id, 'test-operator')).toBe('already_ready');
expect(await prisma.outboxEntry.count({ where: { jobId: failed.id } })).toBe(outboxBefore);
expect(await replayJob(prisma, randomUUID(), 'test-operator')).toBe('not_found');
const replayed = await prisma.jobEvent.findFirstOrThrow({ where: { jobId: failed.id, event: 'replayed' } });
expect(replayed.detail).toBe('by test-operator; previous retry_exhausted after attempt 2');
});
it('lease истёк на последней попытке — failed retry_exhausted, а не вечный processing', async () => {
const id = await upload();
await prisma.attachmentJob.updateMany({ where: { attachmentId: id }, data: { maxAttempts: 1 } });
await expect(
processAttachment(app, id, {
afterStore: async () => {
throw new Error('worker crashed');
},
}),
).rejects.toThrow('worker crashed');
await recoverExpiredLeases(prisma, new Date(Date.now() + 10 * 60 * 1000));
expect(await job(id)).toMatchObject({ status: 'failed', errorCode: 'retry_exhausted', leaseOwner: null });
expect(await metadata(id)).toMatchObject({ status: 'failed', error_code: 'retry_exhausted' });
});
it('replay и очистка staging не теряют исходник ни при каком порядке', async () => {
const fail = async () => {
const id = await upload();
await prisma.attachmentJob.updateMany({
where: { attachmentId: id },
data: { status: 'failed', errorCode: 'retry_exhausted' },
});
await prisma.attachment.update({
where: { id },
data: { status: 'failed', errorCode: 'retry_exhausted' },
});
return id;
};
const expire = (id: string) =>
reconcile(prisma, s3, {
apply: true,
graceMs: 0,
prefix: `staging/${id}`,
stagingRetentionMs: 0,
now: new Date(Date.now() + 1000),
});
const replayedFirst = await fail();
expect(await replayJob(prisma, (await job(replayedFirst)).id, 'test-operator')).toBe('replayed');
await expire(replayedFirst);
expect(await s3.getBuffer(`staging/${replayedFirst}`, MAX_BYTES)).toHaveLength(SAMPLE_PNG.length);
expect(await processAttachment(app, replayedFirst)).toBe('ready');
const expiredFirst = await fail();
await expire(expiredFirst);
await expect(s3.getBuffer(`staging/${expiredFirst}`, MAX_BYTES)).rejects.toThrow('missing');
expect(await replayJob(prisma, (await job(expiredFirst)).id, 'test-operator')).toBe('source_missing');
expect(await job(expiredFirst)).toMatchObject({ status: 'failed' });
expect(await metadata(expiredFirst)).toMatchObject({ status: 'failed' });
});
it('replay задания в очереди не создаёт дубль', async () => {
const id = await upload();
expect(await replayJob(prisma, (await job(id)).id, 'test-operator')).toBe('not_failed');
expect(await prisma.outboxEntry.count({ where: { job: { attachmentId: id } } })).toBe(1);
});
it('нераспаковываемое изображение — постоянная ошибка: сразу failed invalid_image без повторов', async () => {
const id = await upload();
const garbage = Buffer.concat([SAMPLE_PNG.subarray(0, 64), Buffer.alloc(512, 7)]);
const sha256 = createHash('sha256').update(garbage).digest('hex');
await s3.putBuffer(`staging/${id}`, garbage, 'image/png', sha256);
await prisma.attachment.update({ where: { id }, data: { sizeBytes: garbage.length, sha256 } });
expect(await processAttachment(app, id)).toBe('failed');
expect(await metadata(id)).toMatchObject({ status: 'failed', error_code: 'invalid_image', variants: [] });
expect(await job(id)).toMatchObject({ status: 'failed', attempt: 1, errorCode: 'invalid_image' });
await content(id).expect(409);
await sendMessage([id]).expect(409);
expect(await prisma.attachment.findUniqueOrThrow({ where: { id } })).toMatchObject({
stagingKey: `staging/${id}`,
});
});
it('staging подменён — failed checksum_mismatch; staging пропал — failed staging_missing', async () => {
const changed = await upload();
const other = await sharp({ create: { width: 8, height: 8, channels: 3, background: '#ff0000' } })
.png()
.toBuffer();
await s3.putBuffer(`staging/${changed}`, other, 'image/png', createHash('sha256').update(other).digest('hex'));
expect(await processAttachment(app, changed)).toBe('failed');
expect((await metadata(changed)).error_code).toBe('checksum_mismatch');
const missing = await upload();
await s3.delete(`staging/${missing}`);
expect(await processAttachment(app, missing)).toBe('failed');
expect((await metadata(missing)).error_code).toBe('staging_missing');
});
it('превышение времени обработки — временная ошибка с повтором', async () => {
const id = await upload();
const slow = jest
.spyOn(s3, 'getBuffer')
.mockImplementationOnce(() => new Promise((resolve) => setTimeout(() => resolve(SAMPLE_PNG), 500)));
try {
expect(await processAttachment(app, id, {}, { timeoutMs: 50 })).toBe('retry');
} finally {
slow.mockRestore();
}
expect((await job(id)).lastError).toBe('processing timed out');
});
});
describe('формат сообщения и backoff', () => {
it('сообщение соответствует jobs.schema.json, лишние/неверные поля отклоняются', () => {
const valid = jobMessage(
{ id: randomUUID(), attachmentId: randomUUID(), pipelineVersion: 1, attempt: 1 },
new Date(),
);
const encode = (value: unknown) => Buffer.from(JSON.stringify(value));
expect(parseJobMessage(encode(valid))).toEqual(valid);
for (const bad of [
{ ...valid, url: 'http://evil' },
{ ...valid, job_id: 'nope' },
{ ...valid, type: 'other' },
{ ...valid, attempt: 0 },
{ ...valid, schema_version: 2 },
{ ...valid, created_at: 'yesterday' },
[valid],
]) {
expect(parseJobMessage(encode(bad))).toBeNull();
}
expect(parseJobMessage(Buffer.from('{bad'))).toBeNull();
});
it('backoff растёт экспоненциально с jitter и ограничен сверху', () => {
expect(backoffMs(1, () => 0)).toBe(500);
expect(backoffMs(1, () => 1)).toBe(1000);
expect(backoffMs(3, () => 0)).toBe(2000);
expect(backoffMs(3, () => 1)).toBe(4000);
expect(backoffMs(20, () => 1)).toBe(30000);
expect(backoffMs(20, () => 0)).toBe(15000);
});
});
});
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)));
}
+343
View File
@@ -0,0 +1,343 @@
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 { replayJob } from '../src/jobs/job-store';
import { S3Service } from '../src/storage/s3.service';
import {
bearer,
createTestApp,
processAttachment,
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,
status: 'ready',
},
});
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(202);
expect(await processAttachment(app, upload.body.id)).toBe('ready');
const report = await reconcile(prisma, s3, { apply: true, graceMs: HOUR_MS });
expect(report.fresh_skipped).toBeGreaterThanOrEqual(4);
for (const variant of ['original', 'thumbnail', 'crop', 'optimized']) {
await request(server)
.get(`/api/v1/attachments/${upload.body.id}/content?variant=${variant}`)
.set('Authorization', bearer(alice))
.expect(200);
}
});
it('после grace period объекты вложения и staging в очереди не считаются сиротами', async () => {
const queued = 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(202);
const ready = await request(server)
.post(`/api/v1/chats/${chatId}/attachments?filename=b.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG)
.expect(202);
expect(await processAttachment(app, ready.body.id)).toBe('ready');
for (const id of [queued.body.id, ready.body.id]) {
for (const prefix of [`attachments/${id}/`, `staging/${id}`]) {
const report = await reconcile(prisma, s3, {
apply: true,
graceMs: HOUR_MS,
prefix,
now: later(),
});
expect(report).toMatchObject({ orphans: 0, deleted: 0 });
}
}
expect(await exists(`staging/${queued.body.id}`)).toBe(true);
expect(await exists(`staging/${ready.body.id}`)).toBe(false);
});
it('staging failed-вложения удаляется после срока хранения', async () => {
const res = 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(202);
await prisma.attachment.update({
where: { id: res.body.id },
data: { status: 'failed', errorCode: 'invalid_image' },
});
const options = { graceMs: HOUR_MS, prefix: `staging/${res.body.id}`, now: later() };
const kept = await reconcile(prisma, s3, {
...options,
apply: true,
stagingRetentionMs: 3 * HOUR_MS,
});
expect(kept.staging_expired).toBe(0);
expect(await exists(`staging/${res.body.id}`)).toBe(true);
const expired = await reconcile(prisma, s3, {
...options,
apply: true,
stagingRetentionMs: HOUR_MS,
});
expect(expired.staging_expired).toBeGreaterThanOrEqual(1);
expect(await exists(`staging/${res.body.id}`)).toBe(false);
const row = await prisma.attachment.findUniqueOrThrow({ where: { id: res.body.id } });
expect(row.stagingKey).toBeNull();
});
it('объекты вариантов без строк у существующего вложения не удаляются: replay после сверки доводит до ready', async () => {
const res = 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(202);
const id = res.body.id;
await expect(
processAttachment(app, id, {
afterStore: async () => {
throw new Error('worker crashed');
},
}),
).rejects.toThrow('worker crashed');
const job = await prisma.attachmentJob.findFirstOrThrow({ where: { attachmentId: id } });
await prisma.attachmentJob.update({
where: { id: job.id },
data: { status: 'failed', errorCode: 'retry_exhausted', leaseOwner: null, leaseUntil: null },
});
await prisma.attachment.update({
where: { id },
data: { status: 'failed', errorCode: 'retry_exhausted' },
});
expect(await prisma.attachmentVariant.count({ where: { attachmentId: id } })).toBe(0);
const report = await reconcile(prisma, s3, {
apply: true,
graceMs: HOUR_MS,
prefix: `attachments/${id}/`,
now: later(),
});
expect(report).toMatchObject({ scanned: 4, referenced: 4, orphans: 0, deleted: 0 });
expect(await replayJob(prisma, job.id, 'test-operator')).toBe('replayed');
expect(await processAttachment(app, id)).toBe('ready');
for (const variant of ['original', 'thumbnail', 'crop', 'optimized']) {
await request(server)
.get(`/api/v1/attachments/${id}/content?variant=${variant}`)
.set('Authorization', bearer(alice))
.expect(200);
}
});
it('staging ready-вложения, оставшийся после сбоя worker, удаляется без ожидания срока хранения', async () => {
const res = 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(202);
await expect(
processAttachment(app, res.body.id, {
afterCommit: async () => {
throw new Error('worker crashed');
},
}),
).rejects.toThrow('worker crashed');
expect(await exists(`staging/${res.body.id}`)).toBe(true);
const report = await reconcile(prisma, s3, {
apply: true,
graceMs: HOUR_MS,
prefix: `staging/${res.body.id}`,
stagingRetentionMs: 24 * HOUR_MS,
});
expect(report.staging_expired).toBeGreaterThanOrEqual(1);
expect(await exists(`staging/${res.body.id}`)).toBe(false);
await request(server)
.get(`/api/v1/attachments/${res.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();
}
});
});
+206
View File
@@ -0,0 +1,206 @@
import { INestApplication } from '@nestjs/common';
import { randomUUID } from 'crypto';
import request from 'supertest';
import { bearer, createTestApp, PASSWORD, registerUser, TestUser } from './helpers';
import { SAMPLE_PNG } from './images';
type Case = [label: string, run: () => request.Test, expected: number];
describe('Каждое ограничение запроса из контракта отклоняется 400, граница принимается (e2e)', () => {
let app: INestApplication;
let server: any;
let alice: TestUser;
let bob: TestUser;
let chatId: string;
let attachmentId: string;
beforeAll(async () => {
({ app, server } = await createTestApp());
alice = await registerUser(server, 'rc_alice');
bob = await registerUser(server, 'rc_bob');
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Constraints', member_ids: [alice.id, bob.id] })
.expect(201);
chatId = chat.body.id;
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(202);
attachmentId = upload.body.id;
});
afterAll(async () => {
await app.close();
});
const check = async (cases: Case[]) => {
const mismatches: string[] = [];
for (const [label, run, expected] of cases) {
const res = await run();
const okHeaders = Boolean(res.headers['x-request-id'] && res.headers['x-instance-id']);
const okBody = expected >= 400 ? res.body?.error?.code === 'invalid_request' : true;
if (res.status !== expected || !okHeaders || !okBody) {
mismatches.push(`${label}: expected ${expected}, got ${res.status} ${JSON.stringify(res.body).slice(0, 120)}`);
}
}
expect(mismatches).toEqual([]);
};
const emoji = (n: number) => '😀'.repeat(n);
const user = (body: unknown) => () => request(server).post('/api/v1/users').send(body as object);
const name = () => `rc_${randomUUID().slice(0, 8)}`;
const valid = () => ({ username: name(), display_name: 'R', password: PASSWORD });
it('POST /users', async () => {
const drop = (field: string) => {
const body: any = valid();
delete body[field];
return body;
};
await check([
['без username', user(drop('username')), 400],
['без display_name', user(drop('display_name')), 400],
['без password', user(drop('password')), 400],
['username число', user({ ...valid(), username: 123 }), 400],
['username null', user({ ...valid(), username: null }), 400],
['display_name массив', user({ ...valid(), display_name: ['a'] }), 400],
['password число', user({ ...valid(), password: 123456789012 }), 400],
['лишнее поле', user({ ...valid(), role: 'admin' }), 400],
['лишнее поле в другом регистре', user({ ...valid(), Username: 'x' }), 400],
['username 2 символа', user({ ...valid(), username: 'ab' }), 400],
['username 33 символа', user({ ...valid(), username: 'a'.repeat(33) }), 400],
['username заглавные', user({ ...valid(), username: 'Alice_' + randomUUID().slice(0, 4) }), 400],
['username пробел', user({ ...valid(), username: 'al ice' }), 400],
['username кириллица', user({ ...valid(), username: 'алиса' }), 400],
['username дефис', user({ ...valid(), username: 'al-ice' }), 400],
['username 3 символа', user({ ...valid(), username: randomUUID().slice(0, 3).replace(/-/g, '_') }), 201],
['username 32 символа', user({ ...valid(), username: (name() + '_'.repeat(32)).slice(0, 32) }), 201],
['display_name пустой', user({ ...valid(), display_name: '' }), 400],
['display_name пробелы', user({ ...valid(), display_name: ' ' }), 400],
['display_name 101 code point', user({ ...valid(), display_name: emoji(101) }), 400],
['display_name 100 code points', user({ ...valid(), display_name: emoji(100) }), 201],
['password 11', user({ ...valid(), password: 'x'.repeat(11) }), 400],
['password 129 code points', user({ ...valid(), password: emoji(129) }), 400],
['password пробелы', user({ ...valid(), password: ' '.repeat(12) }), 400],
['password 12', user({ ...valid(), password: 'x'.repeat(12) }), 201],
['password 128 code points', user({ ...valid(), password: emoji(128) }), 201],
['тело массив', user([valid()]), 400],
['тело строка', () => request(server).post('/api/v1/users').set('Content-Type', 'application/json').send('"x"'), 400],
['тело null', () => request(server).post('/api/v1/users').set('Content-Type', 'application/json').send('null'), 400],
['битый JSON', () => request(server).post('/api/v1/users').set('Content-Type', 'application/json').send('{bad'), 400],
['мусор после JSON', () => request(server).post('/api/v1/users').set('Content-Type', 'application/json').send(JSON.stringify(valid()) + 'x'), 400],
['text/plain', () => request(server).post('/api/v1/users').set('Content-Type', 'text/plain').send(JSON.stringify(valid())), 400],
['без тела', () => request(server).post('/api/v1/users'), 400],
]);
});
it('POST /chats', async () => {
const chat = (body: unknown) => () =>
request(server).post('/api/v1/chats').set('Authorization', bearer(alice)).send(body as object);
const members = (n: number) => [alice.id, ...Array.from({ length: n - 1 }, () => randomUUID())];
await check([
['без title', chat({ member_ids: [alice.id] }), 400],
['без member_ids', chat({ title: 'x' }), 400],
['title число', chat({ title: 1, member_ids: [alice.id] }), 400],
['title пустой', chat({ title: '', member_ids: [alice.id] }), 400],
['title пробелы', chat({ title: ' ', member_ids: [alice.id] }), 400],
['title 101 code point', chat({ title: emoji(101), member_ids: [alice.id] }), 400],
['title 100 code points', chat({ title: emoji(100), member_ids: [alice.id] }), 201],
['member_ids не массив', chat({ title: 'x', member_ids: alice.id }), 400],
['member_ids пустой', chat({ title: 'x', member_ids: [] }), 400],
['member_ids 101', chat({ title: 'x', member_ids: members(101) }), 400],
['member_ids дубль', chat({ title: 'x', member_ids: [alice.id, alice.id] }), 400],
['member_ids дубль в другом регистре', chat({ title: 'x', member_ids: [alice.id, alice.id.toUpperCase()] }), 400],
['member_ids не UUID', chat({ title: 'x', member_ids: [alice.id, 'nope'] }), 400],
['member_ids число', chat({ title: 'x', member_ids: [alice.id, 1] }), 400],
['member_ids null', chat({ title: 'x', member_ids: null }), 400],
['без вызывающего', chat({ title: 'x', member_ids: [bob.id] }), 400],
['неизвестный участник', chat({ title: 'x', member_ids: [alice.id, randomUUID()] }), 400],
['лишнее поле', chat({ title: 'x', member_ids: [alice.id], owner_id: alice.id }), 400],
]);
});
it('POST /chats/{chat_id}/messages', async () => {
const send = (body: unknown, key: string | null = randomUUID(), chat = chatId) => () => {
const req = request(server).post(`/api/v1/chats/${chat}/messages`).set('Authorization', bearer(alice));
if (key !== null) {
req.set('Idempotency-Key', key);
}
return req.send(body as object);
};
await check([
['без text', send({ attachment_ids: [attachmentId] }), 400],
['text число', send({ text: 1 }), 400],
['text null', send({ text: null }), 400],
['text 4001 code point', send({ text: emoji(4001) }), 400],
['text 4000 code points', send({ text: emoji(4000) }), 201],
['text пустой без вложений', send({ text: '' }), 400],
['text пробелы без вложений', send({ text: ' ' }), 400],
['text пустой с []', send({ text: '', attachment_ids: [] }), 400],
['text пустой с вложением', send({ text: '', attachment_ids: [attachmentId] }), 201],
['attachment_ids не массив', send({ text: 'x', attachment_ids: attachmentId }), 400],
['attachment_ids null', send({ text: 'x', attachment_ids: null }), 400],
['attachment_ids 11', send({ text: 'x', attachment_ids: Array.from({ length: 11 }, () => randomUUID()) }), 400],
['attachment_ids дубль', send({ text: 'x', attachment_ids: [attachmentId, attachmentId] }), 400],
['attachment_ids не UUID', send({ text: 'x', attachment_ids: ['nope'] }), 400],
['sender_id в теле', send({ text: 'x', sender_id: alice.id }), 400],
['лишнее поле', send({ text: 'x', extra: 1 }), 400],
['тело массив', send([{ text: 'x' }]), 400],
['без Idempotency-Key', send({ text: 'x' }, null), 400],
['Idempotency-Key пустой', send({ text: 'x' }, ''), 400],
['Idempotency-Key 129', send({ text: 'x' }, 'k'.repeat(129)), 400],
['Idempotency-Key пробел', send({ text: 'x' }, 'a b'), 400],
['Idempotency-Key слэш', send({ text: 'x' }, 'a/b'), 400],
['Idempotency-Key 128 из всех допустимых символов', send({ text: 'x' }, ('Az09._:-' + randomUUID().replace(/-/g, '')).padEnd(128, 'k')), 201],
['chat_id не UUID', send({ text: 'x' }, randomUUID(), 'nope'), 400],
]);
});
it('query и path параметры', async () => {
const get = (path: string) => () => request(server).get(path).set('Authorization', bearer(alice));
const lists = ['/api/v1/chats', `/api/v1/chats/${chatId}/messages`];
const cases: Case[] = [];
for (const base of lists) {
for (const limit of ['0', '101', '-1', 'abc', '1.5', '', ' ', '1e1', '0x10', '10abc']) {
cases.push([`${base} limit=${JSON.stringify(limit)}`, get(`${base}?limit=${encodeURIComponent(limit)}`), 400]);
}
cases.push([`${base} limit дважды`, get(`${base}?limit=1&limit=2`), 400]);
cases.push([`${base} limit=1`, get(`${base}?limit=1`), 200]);
cases.push([`${base} limit=100`, get(`${base}?limit=100`), 200]);
cases.push([`${base} cursor пустой`, get(`${base}?cursor=`), 400]);
cases.push([`${base} cursor мусор`, get(`${base}?cursor=garbage`), 400]);
cases.push([`${base} cursor base64 числа`, get(`${base}?cursor=${Buffer.from('123').toString('base64url')}`), 400]);
cases.push([`${base} cursor base64 null`, get(`${base}?cursor=${Buffer.from('null').toString('base64url')}`), 400]);
}
cases.push(['chat_id не UUID', get('/api/v1/chats/nope'), 400]);
cases.push(['chat_id почти UUID', get(`/api/v1/chats/${chatId}0`), 400]);
cases.push(['attachment_id не UUID', get('/api/v1/attachments/nope'), 400]);
cases.push(['content attachment_id не UUID', get('/api/v1/attachments/nope/content'), 400]);
for (const variant of ['', 'Original', 'THUMBNAIL', 'big']) {
cases.push([`variant=${JSON.stringify(variant)}`, get(`/api/v1/attachments/${attachmentId}/content?variant=${variant}`), 400]);
}
cases.push(['variant дважды', get(`/api/v1/attachments/${attachmentId}/content?variant=crop&variant=crop`), 400]);
await check(cases);
});
it('POST /chats/{chat_id}/attachments: filename и chat_id', async () => {
const upload = (query: string, chat = chatId) => () =>
request(server)
.post(`/api/v1/chats/${chat}/attachments${query}`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG);
await check([
['без filename', upload(''), 400],
['filename пустой', upload('?filename='), 400],
['filename 256 code points', upload(`?filename=${encodeURIComponent(emoji(256))}`), 400],
['filename 255 code points', upload(`?filename=${encodeURIComponent(emoji(255))}`), 202],
['filename дважды', upload('?filename=a.png&filename=b.png'), 400],
['chat_id не UUID', upload('?filename=a.png', 'nope'), 400],
]);
});
});