Реализовать S3-вложения и reconciler

This commit is contained in:
Nik
2026-10-29 19:09:12 +03:00
parent ec540a1a72
commit e2aa6f129a
25 changed files with 1487 additions and 37 deletions
+51
View File
@@ -0,0 +1,51 @@
"""Negative checks for upload validation and chat-scoped access."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parents[1] / 'tests'))
from client import Client, check
def main():
client = Client('http://localhost:8080')
alice, bob, eve, chat = client.bootstrap()
sample = (Path(__file__).parents[1] / 'tests/fixtures/sample.png').read_bytes()
attachment = client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png',
201, actor=alice, raw=sample,
headers={'Content-Type': 'image/png'}, schema='Attachment',
)['json']
attachment_id = attachment['id']
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=broken.png',
415, actor=alice, raw=b'not an image',
headers={'Content-Type': 'image/png'},
)
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=wrong.jpg',
415, actor=alice, raw=sample,
headers={'Content-Type': 'image/jpeg'},
)
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=large.png',
413, actor=alice, raw=b'0' * (10 * 1024 * 1024 + 1),
headers={'Content-Type': 'image/png'},
)
client.call('GET', '/api/v1/attachments/' + attachment_id, 404, actor=eve)
client.call('GET', '/api/v1/attachments/' + attachment_id + '/content', 404, actor=eve)
other_chat = client.call(
'POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Другой чат', 'member_ids': [alice['id'], eve['id']]}, schema='Chat',
)['json']
client.message(alice, other_chat, 'без чужого вложения')
response = client.call(
'POST', '/api/v1/chats/' + other_chat['id'] + '/messages', 404, actor=alice,
body={'text': '', 'attachment_ids': [attachment_id]},
headers={'Idempotency-Key': 'foreign-attachment-check'},
)
check(response['status'] == 404, 'foreign attachment was accepted')
print('PASS: attachment validation and chat-scoped access')
if __name__ == '__main__':
main()
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
"""Проверка общего Idempotency-Key через обе API-реплики."""
import sys
import uuid
from concurrent.futures import ThreadPoolExecutor
sys.path.insert(0, 'tests')
from client import Client, check
def main():
client = Client('http://localhost:8080')
alice, _, _, chat = client.bootstrap()
key = 'student-' + uuid.uuid4().hex
body = {'text': 'одно сообщение при восьми повторах'}
def send(_):
return client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 201,
actor=alice, body=body,
headers={'Idempotency-Key': key}, schema='Message',
)
with ThreadPoolExecutor(max_workers=8) as pool:
responses = list(pool.map(send, range(8)))
ids = {item['json']['id'] for item in responses}
check(len(ids) == 1, 'concurrent retries created more than one message')
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 409,
actor=alice, body={'text': 'другой текст'},
headers={'Idempotency-Key': key},
)
print('PASS: concurrent idempotency, one message id:', next(iter(ids)))
if __name__ == '__main__':
main()
+30
View File
@@ -0,0 +1,30 @@
"""Ten concurrent bounded uploads for a small local memory check."""
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import sys
import uuid
sys.path.insert(0, str(Path(__file__).parents[1] / 'tests'))
from client import Client, check
def main():
client = Client('http://localhost:8080')
alice, _, _, chat = client.bootstrap()
data = (Path(__file__).parents[1] / 'tests/fixtures/sample.png').read_bytes()
def upload(number):
return client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=load-' + str(number) + '.png',
201, actor=alice, raw=data,
headers={'Content-Type': 'image/png'}, schema='Attachment',
)
with ThreadPoolExecutor(max_workers=10) as pool:
results = list(pool.map(upload, range(10)))
check(len(results) == 10, 'not all uploads completed')
print('PASS: 10 concurrent uploads, bytes per upload:', len(data))
if __name__ == '__main__':
main()