"""Simple grade-3 negative scenarios; run with the API+Postgres already started.""" import base64 import time import uuid from urllib.parse import quote from client import Client, arguments, check, run def configure(parser): parser.add_argument('--expiry', action='store_true', help='Run only with SESSION_TTL_SECONDS=2') def main(): args = arguments('Lab 2 student tests', configure) c = Client(args.base_url, args.ca_file) if args.expiry: user = c.user('expiry') c.call('GET', '/api/v1/users/me', actor=user, schema='User') deadline = time.monotonic() + 12 while time.monotonic() < deadline: response = c.call('GET', '/api/v1/users/me', (200, 401), actor=user, schema='User') if response['status'] == 401: print('PASS: Bearer session expires without logout') return time.sleep(0.25) check(False, 'session did not expire; start API with SESSION_TTL_SECONDS=2') alice, _, outsider, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] login_path = '/api/v1/auth/sessions' json_header = {'Content-Type': 'application/json'} c.call('POST', '/api/v1/users', 409, body=alice['registration']) c.call('POST', '/api/v1/users', 400, raw=b'{invalid', headers=json_header) c.call('POST', '/api/v1/chats', 401, raw=b'{invalid', headers=json_header) c.call('POST', path + '/messages', 401, raw=b'{invalid', headers=json_header) c.call('GET', path, 404, actor=outsider) c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': alice['id']}) c.call('GET', path + '/messages?limit=0', 400, actor=alice) c.call('GET', '/api/v1/chats/' + chat['id'].replace('-', ''), 400, actor=alice) username = 'unicode_' + uuid.uuid4().hex[:12] password = 'Пароль:abc-123456' bad = f'{{"username":"{username}","display_name":"\\ud800","password":"{password}"}}'.encode('utf-8') c.call('POST', '/api/v1/users', 400, raw=bad, headers=json_header) registered = c.call('POST', '/api/v1/users', 201, schema='User', body={'username': username, 'display_name': 'Проверка 👋', 'password': password})['json'] credentials = base64.b64encode(f'{username}:{password}'.encode('utf-8')).decode('ascii') auth = {'Authorization': 'Basic ' + credentials} c.call('POST', login_path, 400, body={}, headers=auth) session = c.call('POST', login_path, 201, headers=auth, schema='Session')['json'] check(session['user'] == registered, 'Unicode Basic login returned the wrong user') c.message(alice, chat, 'Первая запись') c.message(alice, chat, 'Вторая запись') first = c.call('GET', path + '/messages?limit=1', actor=alice, schema='MessagePage')['json'] check(bool(first['next_cursor']), 'missing second page') c.call('GET', path + '/messages?cursor=' + quote(first['next_cursor'] + '!', safe=''), 400, actor=alice) second = c.call('GET', path + '/messages?limit=1&cursor=' + quote(first['next_cursor'], safe=''), actor=alice, schema='MessagePage')['json'] check(len(second['items']) == 1 and second['next_cursor'] is None, 'incorrect last page') check(first['items'][0]['id'] != second['items'][0]['id'], 'pagination repeated a message') c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) c.call('GET', '/api/v1/users/me', 401, actor=alice) print('PASS: validation, Unicode Basic login, access, pagination and logout') if __name__ == '__main__': run(main)