"""Real PostgreSQL + private S3 integration; synthetic images only.""" import hashlib import io import struct import uuid import zlib from PIL import Image from psycopg import sql from test_api import client, user, login def image_bytes(kind='PNG'): stream = io.BytesIO() Image.new('RGB',(320,200),(45,110,190)).save(stream,format=kind) return stream.getvalue() def actors(c): a,ab=user(c) b,bb=user(c) e,eb=user(c) ah,_=login(c,ab) bh,_=login(c,bb) eh,_=login(c,eb) chat=c.post('/api/v1/chats',headers=ah,json={'title':'Файлы','member_ids':[a['id'],b['id']]}).json() return ah,bh,eh,chat def upload(c,headers,chat,raw=None,mime='image/png',filename='Фото.png'): raw=image_bytes() if raw is None else raw return c.post('/api/v1/chats/'+chat['id']+'/attachments',params={'filename':filename},content=raw, headers={**headers,'Content-Type':mime}) def test_original_png_jpeg_and_message(client): ah,bh,eh,chat=actors(client) for kind,mime in [('PNG','image/png'),('JPEG','image/jpeg')]: raw=image_bytes(kind) r=upload(client,ah,chat,raw,mime) assert r.status_code==201 att=r.json() assert att['status']=='ready' and att['variants']==[] and att['error_code'] is None assert att['sha256']==hashlib.sha256(raw).hexdigest() and att['size_bytes']==len(raw) assert 'object_key' not in att and 'bucket' not in att path='/api/v1/attachments/'+att['id'] assert client.get(path,headers=bh).json()==att content=client.get(path+'/content',headers=bh) assert content.status_code==200 and content.content==raw assert content.headers['Content-Type']==mime for suffix in ['', '/content']: assert client.get(path+suffix,headers=eh).status_code==404 assert client.get(path+'/content?variant=thumbnail',headers=bh).status_code==400 response=client.post('/api/v1/chats/'+chat['id']+'/messages',headers={**ah,'Idempotency-Key':uuid.uuid4().hex}, json={'text':'','attachment_ids':[att['id']]}) assert response.status_code==201 assert response.json()['attachment_ids']==[att['id']] def test_invalid_media_corruption_limits_and_filename(client): ah,_,_,chat=actors(client) cases=[(b'not an image','image/png',415),(image_bytes(),'image/jpeg',415), (image_bytes('JPEG'),'image/png',415),(image_bytes()[:40],'image/png',415), (image_bytes(),'text/plain',415),(b'x'*(10485760+1),'image/png',413)] for raw,mime,expected in cases: response=upload(client,ah,chat,raw,mime) assert response.status_code==expected assert response.json()['error']['request_id']==response.headers['X-Request-Id'] # Valid PNG header advertises >40Mp; reject before attempting decompression. header=struct.pack('!IIBBBBB',10001,4000,8,2,0,0,0) chunk=lambda tag,data: struct.pack('!I',len(data))+tag+data+struct.pack('!I',zlib.crc32(tag+data)&0xffffffff) bomb=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',header)+chunk(b'IDAT',zlib.compress(b''))+chunk(b'IEND',b'') assert upload(client,ah,chat,bomb).status_code==413 for filename in ['../x.png','a\\b.png','bad\x00.png','bad\x1f.png','bad\x7f.png',' ','x'*256]: assert upload(client,ah,chat,filename=filename).status_code==400 def test_foreign_attachment_binding_and_normalized_replay(client): ah,bh,eh,chat=actors(client) att=upload(client,ah,chat).json() other=client.post('/api/v1/chats',headers=bh,json={'title':'Другой чат','member_ids':[chat['member_ids'][0],chat['member_ids'][1]]}).json() path='/api/v1/chats/'+other['id']+'/messages' for aid in [att['id'],str(uuid.uuid4())]: response=client.post(path,headers={**bh,'Idempotency-Key':uuid.uuid4().hex},json={'text':'test','attachment_ids':[aid]}) assert response.status_code==404 assert upload(client,eh,chat).status_code==404 h={**ah,'Idempotency-Key':uuid.uuid4().hex} path='/api/v1/chats/'+chat['id']+'/messages' first=client.post(path,headers=h,json={'text':'same'}) replay=client.post(path,headers=h,json={'attachment_ids':[],'text':'same'}) assert first.status_code==replay.status_code==201 and first.json()==replay.json() conflict=client.post(path,headers=h,json={'text':'same','attachment_ids':[att['id']]}) assert conflict.status_code==409 duplicates=client.post(path,headers={**ah,'Idempotency-Key':uuid.uuid4().hex},json={'text':'','attachment_ids':[att['id'],att['id']]}) assert duplicates.status_code==400 def test_compensate_s3_when_metadata_insert_fails(client): ah,_,_,chat=actors(client) db=client.app.state.db store=client.app.state.storage before={item['Key'] for page in store.pages() for item in page.get('Contents',[])} function='fail_attachment_'+uuid.uuid4().hex trigger='fail_attachment_'+uuid.uuid4().hex filename='fault_'+uuid.uuid4().hex+'.png' with db.pool.connection() as conn: conn.execute(sql.SQL("CREATE FUNCTION {}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.filename={} THEN RAISE EXCEPTION 'synthetic metadata failure'; END IF; RETURN NEW; END $$").format(sql.Identifier(function),sql.Literal(filename))) conn.execute(sql.SQL('CREATE TRIGGER {} BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION {}()').format(sql.Identifier(trigger),sql.Identifier(function))) try: response=upload(client,ah,chat,filename=filename) assert response.status_code==503 with db.pool.connection() as conn: assert conn.execute('SELECT count(*) AS n FROM attachments WHERE filename=%s',(filename,)).fetchone()['n']==0 after={item['Key'] for page in store.pages() for item in page.get('Contents',[])} assert after==before finally: with db.pool.connection() as conn: conn.execute(sql.SQL('DROP TRIGGER IF EXISTS {} ON attachments').format(sql.Identifier(trigger))) conn.execute(sql.SQL('DROP FUNCTION IF EXISTS {}()').format(sql.Identifier(function))) def test_compensation_preserves_committed_object(client): from messenger.attachments import compensate ah,_,_,chat=actors(client) att=upload(client,ah,chat).json() with client.app.state.db.pool.connection() as conn: key=conn.execute('SELECT object_key FROM attachments WHERE id=%s',(att['id'],)).fetchone()['object_key'] # A retry after an unknown COMMIT result must never delete an already tracked object. compensate(client.app.state.db,client.app.state.storage,key) content=client.get('/api/v1/attachments/'+att['id']+'/content',headers=ah) assert content.status_code==200 and content.content==image_bytes() def test_stream_close_even_if_headers_cannot_be_sent(client): import asyncio import pytest from messenger.attachments import ObjectResponse ah,_,_,chat=actors(client) att=upload(client,ah,chat).json() with client.app.state.db.pool.connection() as conn: key=conn.execute('SELECT object_key FROM attachments WHERE id=%s',(att['id'],)).fetchone()['object_key'] store=client.app.state.storage opened=store.get(key) response=ObjectResponse(opened,media_type='image/png') async def fail_send(message): raise OSError('synthetic disconnected transport') async def receive(): return {'type':'http.disconnect'} with pytest.raises(Exception): asyncio.run(response({'type':'http','asgi':{'spec_version':'2.4'}},receive,fail_send)) assert opened.closed acquired=0 try: for _ in range(8): assert store.gate.acquire(blocking=False) acquired+=1 finally: for _ in range(acquired): store.gate.release() def test_chunked_upload_is_limited_without_content_length(client): ah,_,_,chat=actors(client) chunks=iter([b'x'*1048576]*10+[b'x']) response=client.post('/api/v1/chats/'+chat['id']+'/attachments?filename=chunked.png', content=chunks,headers={**ah,'Content-Type':'image/png'}) assert response.status_code==413 assert response.json()['error']['code']=='payload_too_large'