"""Additional checks against a running lab4 server.""" import sys import uuid from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, arguments args = arguments('Additional validation checks') c = Client(args.base_url, args.ca_file) a, b, eve, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] + '/messages' c.call('POST', '/api/v1/users', 400, body={'username': 'BAD', 'display_name': 'X'}) c.call('POST', '/api/v1/users', 400, body={'username': 'valid_name', 'display_name': ' '}) c.call('POST', '/api/v1/users', 400, body=dict(a['registration'], extra=True)) c.call('POST', path, 400, actor=a, body={'text': ' '}, headers={'Idempotency-Key': uuid.uuid4().hex}) c.call('POST', path, 400, actor=a, body={'text': 'ok', 'sender_id': b['id']}, headers={'Idempotency-Key': uuid.uuid4().hex}) c.call('GET', path, 404, actor=eve) c.call('GET', path + '?cursor=bad', 400, actor=a) c.call('GET', path + '?limit=1.5', 400, actor=a) c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': str(uuid.uuid4())}) for i in range(5): c.message(a, chat, 'Юникод 👋 ' + str(i)) p = c.call('GET', path + '?limit=2', actor=b, schema='MessagePage')['json'] other = c.call('POST', '/api/v1/chats', 201, actor=a, body={'title': 'other', 'member_ids': [a['id']]}, schema='Chat')['json'] c.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' + p['next_cursor'], 400, actor=a) items = p['items'] while p['next_cursor']: p = c.call('GET', path + '?limit=2&cursor=' + p['next_cursor'], actor=b, schema='MessagePage')['json'] items += p['items'] assert len(items) == len({x['id'] for x in items}) == 5 print('PASS: validation, access, Unicode, pagination and cursor scope') c.call('POST', '/api/v1/users', 400, raw=b'{', headers={'Content-Type': 'application/json'}) c.call('POST', '/api/v1/users', 409, body=a['registration']) print('PASS: malformed JSON and duplicate username') import base64 # All 128 Unicode code points, including colon, must survive KDF without truncation. registration = {'username': 'unicode_' + uuid.uuid4().hex[:12], 'display_name': 'Unicode', 'password': ':' + '🔐' * 127} u = c.call('POST', '/api/v1/users', 201, body=registration, schema='User')['json'] credential = base64.b64encode((u['username'] + ':' + registration['password']).encode()).decode() session = c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': 'Basic ' + credential}, schema='Session')['json'] assert session['user'] == u c.call('POST', '/api/v1/users', 400, body=dict(registration, password='a' * 11)) c.call('POST', '/api/v1/users', 400, body=dict(registration, password='a' * 129)) # Creating a chat with a missing participant must leave no visible chat behind. before = c.call('GET', '/api/v1/chats?limit=100', actor=a, schema='ChatPage')['json'] for members in ([a['id'], str(uuid.uuid4())], [a['id'], a['id']], [b['id']]): c.call('POST', '/api/v1/chats', 400, actor=a, body={'title': 'invalid', 'member_ids': members}) assert c.call('GET', '/api/v1/chats?limit=100', actor=a, schema='ChatPage')['json'] == before print('PASS: Unicode/colon passwords, length boundaries, invalid chat creation')