'use strict'; const assert = require('node:assert/strict'); const { readFileSync } = require('node:fs'); const { randomUUID, createHash } = require('node:crypto'); const { execFileSync } = require('node:child_process'); const http = require('node:http'); const base = process.env.BASE_URL || 'http://localhost:8084'; const png = readFileSync('tests/fixtures/sample.png'); const sha = bytes => createHash('sha256').update(bytes).digest('hex'); const pause = ms => new Promise(resolve => setTimeout(resolve, ms)); async function call(method, path, expected = 200, actor, data, headers = {}) { const response = await fetch(base + path, { method, headers: { ...(actor ? { Authorization: 'Bearer ' + actor.token } : {}), ...(data === undefined ? {} : { 'Content-Type': Buffer.isBuffer(data) ? 'image/png' : 'application/json' }), ...headers }, body: data === undefined ? undefined : Buffer.isBuffer(data) ? data : JSON.stringify(data), signal: AbortSignal.timeout(12000) }); assert.equal(response.status, expected, method + ' ' + path.split('?')[0]); assert.ok(response.headers.get('x-request-id')); assert.ok(response.headers.get('x-instance-id')); const bytes = Buffer.from(await response.arrayBuffer()); const json = response.headers.get('content-type')?.includes('application/json') ? JSON.parse(bytes) : null; if (expected >= 400) assert.equal(json.error.request_id, response.headers.get('x-request-id')); return { data: json, bytes, headers: response.headers, instance: response.headers.get('x-instance-id') }; } async function user() { const username = 'u_' + randomUUID().replaceAll('-', '').slice(0, 14), password = 'Secret-' + randomUUID(); const created = (await call('POST', '/api/v1/users', 201, null, { username, password, display_name: 'Студент' })).data; const login = (await call('POST', '/api/v1/auth/sessions', 201, null, undefined, { Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64') })).data; return { id: created.id, token: login.token }; } const docker = (...args) => execFileSync('docker', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); const internal = script => docker('compose', 'exec', '-T', 'api1', 'node', '-e', script); function control(action, target) { const python = process.env.LAB_PYTHON || (process.platform === 'win32' ? 'py' : 'python3'); execFileSync(python, ['scripts/control.py', action], { stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, ...(target ? { TARGET_INSTANCE_ID: target } : {}) } }); } async function ready(actor) { for (let i = 0; i < 40; i++) { try { await call('GET', '/api/v1/users/me', 200, actor); return; } catch { await pause(250); } } throw new Error('Load balancer did not recover'); } function crc32(buffer) { let value = 0xffffffff; for (const byte of buffer) { value ^= byte; for (let bit = 0; bit < 8; bit++) value = (value >>> 1) ^ ((value & 1) ? 0xedb88320 : 0); } return (value ^ 0xffffffff) >>> 0; } function chunkedTooLarge(path, actor) { return new Promise((resolve, reject) => { const req = http.request(base + path, { method: 'POST', headers: { Authorization: 'Bearer ' + actor.token, 'Content-Type': 'image/png', 'Transfer-Encoding': 'chunked' } }, res => { let body = ''; res.on('data', chunk => body += chunk); res.on('end', () => { try { assert.equal(res.statusCode, 413); assert.equal(JSON.parse(body).error.code, 'payload_too_large'); resolve(); } catch (error) { reject(error); } }); }); req.on('error', error => { if (!req.res) reject(error); }); req.setTimeout(15000, () => req.destroy(new Error('Chunked upload timeout'))); req.write(png); for (let i = 0; i < 11; i++) req.write(Buffer.alloc(1024 * 1024)); req.end(); }); } async function main() { const a = await user(), b = await user(), outsider = await user(); const chat = (await call('POST', '/api/v1/chats', 201, a, { title: 'Изображения', member_ids: [a.id, b.id] })).data; const uploadPath = '/api/v1/chats/' + chat.id + '/attachments'; const messages = '/api/v1/chats/' + chat.id + '/messages'; const uploaded = await call('POST', uploadPath + '?filename=' + encodeURIComponent('Фото 😀.png'), 201, a, png); const att = uploaded.data, content = '/api/v1/attachments/' + att.id + '/content'; assert.equal(att.status, 'ready'); assert.equal(att.sha256, sha(png)); assert.equal(att.size_bytes, png.length); assert.equal(att.object_key, undefined); const download = await call('GET', content, 200, b); assert.deepEqual(download.bytes, png); assert.equal(download.headers.get('content-type'), 'image/png'); for (const suffix of ['', '/content']) await call('GET', '/api/v1/attachments/' + att.id + suffix, 404, outsider); await call('GET', content, 401); await call('GET', content + '?variant=thumbnail', 404, a); const key = randomUUID(), body = { text: '', attachment_ids: [att.id] }; const concurrent = await Promise.all(Array.from({ length: 12 }, () => call('POST', messages, 201, a, body, { 'Idempotency-Key': key }))); for (const result of concurrent) assert.deepEqual(result.data, concurrent[0].data); assert.equal(new Set(concurrent.map(r => r.instance)).size, 2); assert.deepEqual(concurrent[0].data.attachment_ids, [att.id]); assert.equal((await call('GET', messages, 200, b)).data.items.length, 1); await call('POST', messages, 409, a, { text: 'Changed', attachment_ids: [att.id] }, { 'Idempotency-Key': key }); const textKey = randomUUID(); const plain = await call('POST', messages, 201, a, { text: 'Text only' }, { 'Idempotency-Key': textKey }); const explicit = await call('POST', messages, 201, a, { text: 'Text only', attachment_ids: [] }, { 'Idempotency-Key': textKey }); assert.deepEqual(plain.data, explicit.data); await call('POST', messages, 400, a, { text: '', attachment_ids: [att.id, att.id] }, { 'Idempotency-Key': randomUUID() }); const otherChat = (await call('POST', '/api/v1/chats', 201, a, { title: 'Other', member_ids: [a.id] })).data; await call('POST', '/api/v1/chats/' + otherChat.id + '/messages', 404, a, body, { 'Idempotency-Key': randomUUID() }); for (const name of ['../bad.png', 'a/b.png', 'a\\b.png', 'a\0.png', '\u0085', 'a\n.png', 'x'.repeat(256)]) await call('POST', uploadPath + '?filename=' + encodeURIComponent(name), 400, a, png); await call('POST', uploadPath, 400, a, png); await call('POST', uploadPath + '?filename=x.png', 415, a, Buffer.from('not an image')); await call('POST', uploadPath + '?filename=x.png', 415, a, png.subarray(0, 60)); await call('POST', uploadPath + '?filename=x.jpg', 415, a, png, { 'Content-Type': 'image/jpeg' }); await call('POST', uploadPath + '?filename=x.png', 415, a, png, { 'Content-Type': 'text/plain' }); await call('POST', uploadPath + '?filename=x.png', 413, a, Buffer.alloc(10485761)); await chunkedTooLarge(uploadPath + '?filename=large.png', a); const huge = Buffer.from(png); huge.writeUInt32BE(8001, 16); huge.writeUInt32BE(5000, 20); huge.writeUInt32BE(crc32(huge.subarray(12, 29)), 29); await call('POST', uploadPath + '?filename=huge.png', 413, a, huge); const jpeg = Buffer.from(internal("require('sharp')({create:{width:20,height:10,channels:3,background:'red'}}).jpeg().toBuffer().then(b=>console.log(b.toString('base64')))"), 'base64'); const jpg = await call('POST', uploadPath + '?filename=real.jpg', 201, b, jpeg, { 'Content-Type': 'image/jpeg' }); assert.deepEqual((await call('GET', '/api/v1/attachments/' + jpg.data.id + '/content', 200, a)).bytes, jpeg); // Resolve the private storage key inside the API container; never expose it in public metadata. const anonymous = JSON.parse(internal(`const {pool}=require('./src/db');pool.query('SELECT object_key FROM attachments WHERE id=$1',['${att.id}']).then(async r=>{const response=await fetch('http://s3:9000/messenger-attachments/'+r.rows[0].object_key);console.log(JSON.stringify({status:response.status}));}).finally(()=>pool.end());`)); assert.equal(anonymous.status, 403); console.log('PASS: PNG/JPEG exact bytes and SHA-256; private bucket 403; member access; invalid filename/MIME/corrupt file; >10 MiB including chunked and >40M pixels; attachment scope; 12 concurrent sends make 1 message.'); if (process.argv.includes('--lifecycle')) { let target = uploaded.instance; for (let round = 0; round < 2; round++) { // Stop the exact process that accepted this upload, then download only through LB. const fresh = round === 0 ? uploaded : await call('POST', uploadPath + '?filename=second.png', 201, a, png); target = fresh.instance; const started = Date.now(); control('stop-one', target); try { let ok = false; for (let i = 0; i < 30 && Date.now() - started < 10000; i++) { try { const bytes = await call('GET', '/api/v1/attachments/' + fresh.data.id + '/content', 200, b); assert.notEqual(bytes.instance, target); assert.deepEqual(bytes.bytes, png); ok = true; break; } catch { await pause(100); } } assert.ok(ok, 'Download after uploader failure exceeded 10 seconds'); console.log(JSON.stringify({ scenario: 'upload-stop-uploader-download', recovery_ms: Date.now() - started, same_bytes: true })); } finally { control('restore'); await ready(a); } } control('restart'); await ready(a); assert.deepEqual((await call('GET', content, 200, b)).bytes, png); assert.deepEqual((await call('POST', messages, 201, a, body, { 'Idempotency-Key': key })).data, concurrent[0].data); console.log('PASS: full restart preserves metadata, original bytes, session and idempotent attachment message.'); docker('compose', 'stop', 's3'); try { const checks = JSON.parse(internal(`Promise.all(['/health/live','/health/ready'].map(async p=>{const r=await fetch('http://127.0.0.1:8080'+p);return r.status})).then(s=>console.log(JSON.stringify(s)))`)); assert.deepEqual(checks, [200, 503]); console.log('PASS: stopped S3 makes API readiness 503 while liveness remains 200.'); } finally { docker('compose', 'up', '-d', '--wait', 's3'); await ready(a); } assert.deepEqual((await call('GET', content, 200, a)).bytes, png); console.log('PASS: S3 recovery restores download without data loss.'); } } main().catch(error => { console.error('FAIL: ' + error.message); process.exitCode = 1; });