forked from mtusi2026/mtusi2026lab3
[+] Completed lab 3
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""Test UTF-8 Basic credentials, exact JSON validation and chat membership."""
|
||||
import base64
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Student checks: Basic and chat validation')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
username = 'u_colon_' + uuid.uuid4().hex[:12]
|
||||
password = 'Пароль:🔑' * 3
|
||||
user = client.call('POST', '/api/v1/users', 201, schema='User',
|
||||
body={'username': username, 'display_name': 'Студент', 'password': password})['json']
|
||||
basic = base64.b64encode(f'{username}:{password}'.encode()).decode()
|
||||
auth = {'Authorization': 'Basic ' + basic}
|
||||
session = client.call('POST', '/api/v1/auth/sessions', 201,
|
||||
headers=auth, schema='Session')['json']
|
||||
check(session['user'] == user, 'UTF-8 password with colon failed')
|
||||
client.call('POST', '/api/v1/auth/sessions', 400, headers=auth, body={})
|
||||
alice = {'id': user['id'], 'token': session['token']}
|
||||
bob = client.user('bob')
|
||||
path = '/api/v1/chats'
|
||||
title = 'Проверка участников'
|
||||
members = [alice['id'], bob['id']]
|
||||
for body in (
|
||||
{'title': title, 'member_ids': [alice['id'], str(uuid.uuid4())]},
|
||||
{'title': title, 'member_ids': [alice['id'], alice['id'].upper()]},
|
||||
{'title': title, 'member_ids': [bob['id']]},
|
||||
{'title': 123, 'member_ids': members},
|
||||
{'title': title, 'member_ids': members, 'unexpected': True},
|
||||
):
|
||||
client.call('POST', path, 400, actor=alice, body=body)
|
||||
page = client.call('GET', path, actor=alice, schema='ChatPage')['json']
|
||||
check(page['items'] == [], 'invalid creation left a visible chat')
|
||||
chat = client.call('POST', path, 201, actor=alice,
|
||||
body={'title': title, 'member_ids': members}, schema='Chat')['json']
|
||||
listed = client.call('GET', path, actor=bob, schema='ChatPage')['json']
|
||||
check(listed['items'] == [chat], 'valid chat or membership is missing')
|
||||
print('PASS: UTF-8 password with colon, strict JSON, and atomic chat creation')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Exercise concurrent retries through the shared load balancer."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Student checks: concurrent idempotency on two replicas')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
|
||||
def send(_):
|
||||
return Client(args.base_url, args.ca_file).message(alice, chat, 'Повтор', key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as executor:
|
||||
responses = list(executor.map(send, range(16)))
|
||||
first = responses[0]['json']
|
||||
instances = {item['headers']['X-Instance-Id'] for item in responses}
|
||||
check(len(instances) >= 2, 'concurrent requests did not reach two API replicas')
|
||||
check(all(item['json'] == first for item in responses), 'replays differ')
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
page = client.call('GET', path, actor=bob, schema='MessagePage')['json']
|
||||
check(page['items'] == [first], 'duplicate persisted message')
|
||||
client.call('POST', path, 409, actor=alice, body={'text': 'Другой текст'},
|
||||
headers={'Idempotency-Key': key})
|
||||
independent = client.message(bob, chat, 'Повтор', key=key)['json']
|
||||
check(independent['id'] != first['id'], 'key incorrectly shared between users')
|
||||
print(f'PASS: {len(responses)} concurrent requests on {len(instances)} replicas yielded one message')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user