Выполнение lab4 на 4
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"""Message attachment ACL, validation and idempotency. Synthetic data only."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Messages with attachments')
|
||||
base = args.base_url or 'http://127.0.0.1:8084'
|
||||
c = Client(base, args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
first, _ = c.attachment(alice, chat)
|
||||
second, _ = c.attachment(alice, chat)
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
ids = [second['id'], first['id']]
|
||||
key = uuid.uuid4().hex
|
||||
created = c.message(bob, chat, '', key=key, attachments=ids)['json']
|
||||
check(created['attachment_ids'] == ids and created['text'] == '', 'attachment-only message/order lost')
|
||||
replay = c.call('POST', path, 201, actor=bob,
|
||||
body={'attachment_ids': ids, 'text': ''}, headers={'Idempotency-Key': key}, schema='Message')['json']
|
||||
check(replay == created, 'JSON field order changed replay')
|
||||
c.call('POST', path, 409, actor=bob, body={'text': '', 'attachment_ids': list(reversed(ids))}, headers={'Idempotency-Key': key})
|
||||
c.call('POST', path, 409, actor=bob, body={'text': '', 'attachment_ids': ids[:1]}, headers={'Idempotency-Key': key})
|
||||
print('PASS: attachment-only message, non-owner participant, list order, replay and changed-list 409')
|
||||
text_key = uuid.uuid4().hex
|
||||
text = c.message(alice, chat, 'text-only', key=text_key)['json']
|
||||
check(text['attachment_ids'] == [], 'text-only attachment_ids must be []')
|
||||
check(c.message(alice, chat, 'text-only', key=text_key, attachments=[])['json'] == text,
|
||||
'omitted attachment_ids differs from []')
|
||||
print('PASS: text-only compatibility; omitted attachment_ids and [] replay identically')
|
||||
invalid_bodies = [
|
||||
{'text': ''}, {'text': ' \t\n', 'attachment_ids': []},
|
||||
{'text': '', 'attachment_ids': [first['id'], first['id']]},
|
||||
{'text': '', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
|
||||
{'text': '', 'attachment_ids': ['not-uuid']},
|
||||
{'text': 'x', 'attachment_ids': None}, {'text': 'x', 'attachment_ids': 'wrong'},
|
||||
{'text': 'x', 'attachment_ids': [None]}, {'text': 'x', 'unexpected': True},
|
||||
]
|
||||
for body in invalid_bodies:
|
||||
c.call('POST', path, 400, actor=alice, body=body, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
print('PASS: empty message, duplicates, >10 IDs, bad UUID/types/null/unknown fields return 400')
|
||||
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Other chat', 'member_ids': [alice['id'], eve['id']]}, schema='Chat')['json']
|
||||
foreign, _ = c.attachment(alice, other)
|
||||
retry_key = uuid.uuid4().hex
|
||||
for attachment_id in (foreign['id'], str(uuid.uuid4())):
|
||||
c.call('POST', path, 404, actor=alice, body={'text': '', 'attachment_ids': [attachment_id]},
|
||||
headers={'Idempotency-Key': retry_key})
|
||||
c.message(alice, chat, 'corrected retry', key=retry_key, attachments=[first['id']])
|
||||
c.call('POST', path, 404, actor=eve, body={'text': '', 'attachment_ids': [first['id']]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
print('PASS: foreign-chat/unknown attachment and outsider return 404; rejected request does not consume key')
|
||||
ten_ids = ids + [c.attachment(alice, chat)[0]['id'] for _ in range(8)]
|
||||
ten = c.message(alice, chat, '', attachments=ten_ids)['json']
|
||||
check(ten['attachment_ids'] == ten_ids, 'ten attachments were not preserved')
|
||||
print('PASS: exactly ten distinct attachments are accepted')
|
||||
concurrent_key = uuid.uuid4().hex
|
||||
|
||||
def send(_):
|
||||
local = Client(base, args.ca_file)
|
||||
return local.message(alice, chat, 'concurrent', key=concurrent_key, attachments=[first['id']])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as executor:
|
||||
results = list(executor.map(send, range(16)))
|
||||
check(all(result['json'] == results[0]['json'] for result in results), 'concurrent replies differ')
|
||||
instances = {result['headers']['X-Instance-Id'] for result in results}
|
||||
check(len(instances) >= 2, 'concurrent replay did not reach two replicas')
|
||||
history = c.call('GET', path, actor=bob, schema='MessagePage')['json']['items']
|
||||
check(len(history) == 5, 'rejected requests or retries left extra messages')
|
||||
matching = [message for message in history if message['id'] == results[0]['json']['id']]
|
||||
check(len(matching) == 1, 'concurrent retry created duplicates in history')
|
||||
check(next(message for message in history if message['id'] == created['id']) == created, 'history lost attachments')
|
||||
print('PASS: 16 concurrent retries across two replicas return one message, history preserves attachment order')
|
||||
print('PASS: message attachment checks completed')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user