diff --git a/student-tests/expiry_check.py b/student-tests/expiry_check.py new file mode 100644 index 00000000..7bde51ec --- /dev/null +++ b/student-tests/expiry_check.py @@ -0,0 +1,56 @@ +"""Usage (стенд уже запущен через docker compose up -d): + python student-tests/expiry_check.py --ttl 2 + +Скрипт пересоздаёт только api с SESSION_TTL_SECONDS=, проверяет истечение сессии, +затем возвращает api с обычным TTL из .env. Volumes и данные не трогаются. +""" +import argparse +import hashlib +import os +import time + +from helpers import call, compose, new_user, wait_ready + + +def restart_api(ttl=None): + env = dict(os.environ) + if ttl is None: + env.pop('SESSION_TTL_SECONDS', None) + else: + env['SESSION_TTL_SECONDS'] = str(ttl) + compose('up', '-d', '--no-deps', '--no-build', 'api', env=env) + wait_ready() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--ttl', type=int, default=2) + args = parser.parse_args() + + print('пересоздаю api с SESSION_TTL_SECONDS=%d' % args.ttl) + restart_api(args.ttl) + try: + alice = new_user('expiry') + status = call('GET', '/api/v1/users/me', token=alice['token'])[0] + print('сразу после входа: /users/me -> %d' % status) + assert status == 200 + key = 'session:' + hashlib.sha256(alice['token'].encode()).hexdigest() + cache_ttl = int(compose('exec', '-T', 'valkey', 'valkey-cli', 'TTL', key)) + print('TTL ключа этой сессии в Valkey: %d с (-2 = ключа нет)' % cache_ttl) + assert cache_ttl <= args.ttl, 'кэш живёт дольше сессии' + + time.sleep(args.ttl + 1.5) + status, headers, _ = call('GET', '/api/v1/users/me', token=alice['token']) + print('через %.1f с: /users/me -> %d, WWW-Authenticate: %s' % (args.ttl + 1.5, status, headers['WWW-Authenticate'])) + assert status == 401 and headers['WWW-Authenticate'] == 'Bearer realm="messenger"' + status = call('DELETE', '/api/v1/auth/sessions/current', token=alice['token'])[0] + print('logout истёкшим токеном -> %d' % status) + assert status == 401 + print('PASS: сессия истекла по SESSION_TTL_SECONDS=%d, кэш не продлил её' % args.ttl) + finally: + print('возвращаю api с TTL из .env') + restart_api() + + +if __name__ == '__main__': + main() diff --git a/student-tests/failure_check.py b/student-tests/failure_check.py new file mode 100644 index 00000000..4c93d293 --- /dev/null +++ b/student-tests/failure_check.py @@ -0,0 +1,83 @@ +"""Usage (стенд уже запущен через docker compose up -d): + python student-tests/failure_check.py + +Останавливает Valkey, затем PostgreSQL (docker compose stop, без удаления volumes), +проверяет поведение API и восстановление после docker compose start. +""" +import time +import uuid + +from helpers import basic, call, compose, new_chat, new_user, wait_ready + +failures = [] + + +def timed(method, path, token=None, body=None, headers=None): + started = time.monotonic() + try: + status = call(method, path, body=body, token=token, headers=headers, timeout=10)[0] + except OSError as error: + status = type(error).__name__ + return status, time.monotonic() - started + + +def expect(title, result, allowed, max_seconds): + status, seconds = result + ok = status in allowed and seconds <= max_seconds + print(' %-48s -> %-4s за %.2f с %s' % (title, status, seconds, 'OK' if ok else 'FAIL')) + if not ok: + failures.append(title) + + +def main(): + alice = new_user('failure') + chat = new_chat(alice) + history = '/api/v1/chats/%s/messages' % chat['id'] + assert call('POST', history, {'text': 'до отказа'}, alice['token'])[0] == 201 + fake = uuid.uuid4().hex * 2 + + print('1. Valkey остановлен: кэш обходится, всё работает через PostgreSQL') + compose('stop', 'valkey') + try: + expect('первый запрос: обнаружение отказа кэша', timed('GET', '/api/v1/users/me', alice['token']), [200], 1.5) + expect('GET /users/me действующим токеном', timed('GET', '/api/v1/users/me', alice['token']), [200], 1) + expect('POST сообщение', timed('POST', history, alice['token'], {'text': 'без кэша'}), [201], 1) + expect('неизвестный токен', timed('GET', '/api/v1/users/me', fake), [401], 1) + expect('без Authorization', timed('GET', '/api/v1/users/me'), [401], 1) + expect('/health/ready (Valkey некритичен)', timed('GET', '/health/ready'), [200], 1) + started = time.monotonic() + other = new_user('nocache') + expect('регистрация и вход без кэша', (200, time.monotonic() - started), [200], 1) + expect('logout без кэша', timed('DELETE', '/api/v1/auth/sessions/current', other['token']), [204], 1) + expect('отозванный токен', timed('GET', '/api/v1/users/me', other['token']), [401], 1) + finally: + compose('up', '-d', '--wait', 'valkey') + + print('2. PostgreSQL остановлен: ограниченная ошибка 503, анонимный запрос не проходит') + compose('stop', 'postgres') + stopped_at = time.monotonic() + try: + expect('GET история', timed('GET', history, alice['token']), [503], 3) + expect('POST сообщение', timed('POST', history, alice['token'], {'text': 'при отказе БД'}), [503], 3) + expect('неизвестный токен (не 200!)', timed('GET', '/api/v1/users/me', fake), [503], 3) + expect('без Authorization', timed('GET', '/api/v1/users/me'), [401], 1) + expect('вход по Basic верным паролем', timed('POST', '/api/v1/auth/sessions', + headers=basic(alice['username'], alice['password'])), [503], 3) + expect('/health/live', timed('GET', '/health/live'), [200], 1) + expect('/health/ready', timed('GET', '/health/ready'), [503], 3) + finally: + compose('up', '-d', '--wait', 'postgres') + ready_at = wait_ready() + print('3. PostgreSQL запущен: готовность через %.1f с после остановки' % (ready_at - stopped_at)) + status = call('GET', '/api/v1/users/me', token=alice['token'])[0] + texts = [m['text'] for m in call('GET', history + '?limit=100', token=alice['token'])[2]['items']] + print(' прежний токен -> %d; история: %s' % (status, texts)) + if status != 200 or texts != ['до отказа', 'без кэша']: + failures.append('восстановление') + + print('FAIL: ' + ', '.join(failures) if failures else 'PASS: отказы Valkey и PostgreSQL обработаны') + raise SystemExit(1 if failures else 0) + + +if __name__ == '__main__': + main() diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 00000000..7654e210 --- /dev/null +++ b/student-tests/helpers.py @@ -0,0 +1,101 @@ +import base64 +import json +import os +from pathlib import Path +import subprocess +import time +import urllib.error +import urllib.parse +import urllib.request +import uuid + +BASE_URL = os.environ.get('BASE_URL', 'http://127.0.0.1:8080').rstrip('/') +ROOT = Path(__file__).resolve().parents[1] + + +def call(method, path, body=None, token=None, raw=None, headers=None, timeout=10): + all_headers = {} + if token: + all_headers['Authorization'] = 'Bearer ' + token + if body is not None: + raw = json.dumps(body, ensure_ascii=False).encode('utf-8') + if raw is not None: + all_headers['Content-Type'] = 'application/json' + all_headers.update(headers or {}) + request = urllib.request.Request(BASE_URL + path, data=raw, headers=all_headers, method=method) + try: + response = urllib.request.urlopen(request, timeout=timeout) + except urllib.error.HTTPError as error: + response = error + with response: + data = response.read() + return response.status, response.headers, json.loads(data) if data else None + + +def basic(username, password): + return {'Authorization': 'Basic ' + base64.b64encode((username + ':' + password).encode('utf-8')).decode()} + + +def register(label='user', password=None): + username = (label + '_' + uuid.uuid4().hex[:12])[:32] + password = password or 'Pw-' + uuid.uuid4().hex + status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': label, 'password': password}) + assert status == 201, status + return user, password + + +def login(username, password): + status, _, session = call('POST', '/api/v1/auth/sessions', headers=basic(username, password)) + assert status == 201, status + return session + + +def new_user(label='user'): + user, password = register(label) + session = login(user['username'], password) + return dict(user, password=password, token=session['token']) + + +def new_chat(owner, *others, title='Тестовый чат'): + members = [owner['id']] + [u['id'] for u in others] + status, _, chat = call('POST', '/api/v1/chats', {'title': title, 'member_ids': members}, owner['token']) + assert status == 201, status + return chat + + +def read_all_messages(chat_id, token, limit): + items, cursor, pages = [], None, 0 + while True: + query = '?limit=%d' % limit + ('&cursor=' + urllib.parse.quote(cursor, safe='') if cursor else '') + status, _, page = call('GET', '/api/v1/chats/%s/messages%s' % (chat_id, query), token=token) + assert status == 200, status + items += page['items'] + pages += 1 + cursor = page['next_cursor'] + if cursor is None: + return items, pages + + +def compose(*args, env=None, check=True): + result = subprocess.run(['docker', 'compose', *args], cwd=ROOT, capture_output=True, text=True, + encoding='utf-8', env=env) + if check and result.returncode != 0: + raise RuntimeError('docker compose %s failed: %s' % (' '.join(args), result.stderr.strip())) + return result.stdout.strip() + + +def sql(query, db='messenger', service='postgres'): + return compose('exec', '-T', service, 'psql', '-U', 'messenger', '-d', db, '-X', '-tA', '-v', 'ON_ERROR_STOP=1', + '-c', query) + + +def wait_ready(seconds=90): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + try: + if call('GET', '/health/ready', timeout=2)[0] == 200: + return time.monotonic() + except OSError: + pass + time.sleep(0.3) + raise AssertionError('API is not ready after %ds' % seconds) diff --git a/student-tests/sigterm_check.py b/student-tests/sigterm_check.py new file mode 100644 index 00000000..86f2ee5c --- /dev/null +++ b/student-tests/sigterm_check.py @@ -0,0 +1,75 @@ +"""Usage (стенд запущен через docker compose up -d): + python student-tests/sigterm_check.py + docker compose start api +""" +import argparse +import subprocess +import threading +import time +import urllib.error +import urllib.request +import uuid + +from helpers import BASE_URL, call, new_chat, new_user + + +def docker(*args): + return subprocess.run(['docker', *args], capture_output=True, text=True, encoding='utf-8').stdout.strip() + + +def probe_forever(results, stop_event): + while not stop_event.is_set(): + started = time.monotonic() + try: + with urllib.request.urlopen(BASE_URL + '/health/live', timeout=1) as response: + outcome = str(response.status) + except urllib.error.HTTPError as error: + outcome = str(error.code) + except Exception as error: + outcome = type(error).__name__ + results.append((started, outcome)) + time.sleep(0.02) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--container', default='grigoriev-lab2-api-1') + parser.add_argument('--stop-timeout', type=int, default=10, help='секунд до SIGKILL у docker stop') + args = parser.parse_args() + + marker = 'PRIVATE-' + uuid.uuid4().hex + alice = new_user('alice') + chat = new_chat(alice) + status = call('POST', '/api/v1/chats/%s/messages' % chat['id'], {'text': marker}, alice['token'])[0] + logs_before = docker('logs', args.container) + print('сообщение с маркером отправлено: HTTP %d; маркер в логах: %s' % (status, marker in logs_before)) + + results, stop_event = [], threading.Event() + prober = threading.Thread(target=probe_forever, args=(results, stop_event)) + prober.start() + time.sleep(1) + + sigterm_at = time.monotonic() + docker('stop', '-t', str(args.stop_timeout), args.container) + stopped_at = time.monotonic() + time.sleep(1) + stop_event.set() + prober.join() + + before = [r for t, r in results if t < sigterm_at] + after = [r for t, r in results if t >= sigterm_at] + exit_code = docker('inspect', '--format', '{{.State.ExitCode}}', args.container) + print('docker stop занял: %.2f с (лимит %d с)' % (stopped_at - sigterm_at, args.stop_timeout)) + print('код выхода контейнера: %s (0 — сам завершился по SIGTERM; 137 — убит SIGKILL)' % exit_code) + print('запросы до SIGTERM: %d, все 200: %s' % (len(before), set(before) == {'200'})) + print('запросы после SIGTERM: %d, результаты: %s' % (len(after), sorted(set(after)))) + print('ответов 5xx за всё время: %d' % sum(r.startswith('5') for _, r in results)) + + print('\nлоги остановки:') + for line in docker('logs', args.container).splitlines(): + if any(word in line for word in ('Shutting down', 'stopping', 'shutdown', 'Finished server')): + print(' ' + line) + + +if __name__ == '__main__': + main() diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..502fbd65 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,142 @@ +import unittest +import uuid + +from helpers import call, new_chat, new_user, read_all_messages + + +class ErrorFormatMixin: + def assertError(self, result, status, code): + actual, headers, body = result + self.assertEqual(actual, status) + self.assertEqual(body['error']['code'], code) + self.assertEqual(body['error']['request_id'], headers['X-Request-Id']) + self.assertNotIn('Traceback', body['error']['message']) + + +class InvalidJsonTest(ErrorFormatMixin, unittest.TestCase): + def test_malformed_json(self): + """Битый JSON -> 400 invalid_request""" + self.assertError(call('POST', '/api/v1/users', raw=b'{"username": "abc",'), 400, 'invalid_request') + + def test_json_not_object(self): + """JSON-массив вместо объекта -> 400""" + self.assertError(call('POST', '/api/v1/users', raw=b'[1, 2, 3]'), 400, 'invalid_request') + + def test_wrong_type(self): + """Число вместо строки -> 400 (без тихого приведения типов)""" + self.assertError(call('POST', '/api/v1/users', {'username': 12345, 'display_name': 'x', 'password': 'Long-enough-pass-1'}), 400, 'invalid_request') + + def test_extra_field(self): + """Лишнее поле в теле -> 400""" + body = {'username': 'u_' + uuid.uuid4().hex[:8], 'display_name': 'x', 'password': 'Long-enough-pass-1', 'is_admin': True} + self.assertError(call('POST', '/api/v1/users', body), 400, 'invalid_request') + + +class DuplicateUsernameTest(ErrorFormatMixin, unittest.TestCase): + def test_duplicate_username(self): + """Повторная регистрация того же username -> 409 conflict""" + user = new_user('dup') + again = call('POST', '/api/v1/users', {'username': user['username'], 'display_name': 'Другой', + 'password': 'Other-password-123'}) + self.assertError(again, 409, 'conflict') + + +class ForeignChatTest(ErrorFormatMixin, unittest.TestCase): + def setUp(self): + self.alice, self.bob, self.eve = new_user('alice'), new_user('bob'), new_user('eve') + self.chat = new_chat(self.alice, self.bob) + self.path = '/api/v1/chats/' + self.chat['id'] + + def test_outsider_cannot_read_chat(self): + """Посторонний читает чат -> 404, как будто чата нет""" + self.assertError(call('GET', self.path, token=self.eve['token']), 404, 'not_found') + missing = call('GET', '/api/v1/chats/' + str(uuid.uuid4()), token=self.eve['token']) + self.assertEqual(missing[2]['error']['message'], call('GET', self.path, token=self.eve['token'])[2]['error']['message']) + + def test_outsider_cannot_write_or_read_history(self): + """Посторонний пишет/читает историю -> 404""" + self.assertError(call('POST', self.path + '/messages', {'text': 'чужое'}, self.eve['token']), 404, 'not_found') + self.assertError(call('GET', self.path + '/messages', token=self.eve['token']), 404, 'not_found') + + def test_outsider_does_not_see_chat_in_list(self): + """Чужой чат не попадает в список чатов""" + status, _, page = call('GET', '/api/v1/chats?limit=100', token=self.eve['token']) + self.assertEqual(status, 200) + self.assertNotIn(self.chat['id'], [c['id'] for c in page['items']]) + + def test_member_has_access(self): + """Участник видит чат (контроль: 404 выше не из-за ошибки в тесте)""" + self.assertEqual(call('GET', self.path, token=self.bob['token'])[0], 200) + + +class EmptyMessageTest(ErrorFormatMixin, unittest.TestCase): + def setUp(self): + self.alice = new_user('alice') + self.path = '/api/v1/chats/%s/messages' % new_chat(self.alice)['id'] + + def test_empty_text(self): + """Пустой текст -> 400""" + self.assertError(call('POST', self.path, {'text': ''}, self.alice['token']), 400, 'invalid_request') + + def test_whitespace_text(self): + """Текст только из пробельных символов -> 400""" + self.assertError(call('POST', self.path, {'text': ' \t\n '}, self.alice['token']), 400, 'invalid_request') + + def test_missing_text(self): + """Тело без поля text -> 400""" + self.assertError(call('POST', self.path, {}, self.alice['token']), 400, 'invalid_request') + + def test_text_length_limit(self): + """4000 символов можно (эмодзи = 1 символ), 4001 — 400""" + self.assertEqual(call('POST', self.path, {'text': '😀' * 4000}, self.alice['token'])[0], 201) + self.assertError(call('POST', self.path, {'text': 'x' * 4001}, self.alice['token']), 400, 'invalid_request') + + +class PaginationTest(ErrorFormatMixin, unittest.TestCase): + def setUp(self): + self.alice = new_user('alice') + self.chat = new_chat(self.alice) + self.sent = [call('POST', '/api/v1/chats/%s/messages' % self.chat['id'], + {'text': 'сообщение %d' % i}, self.alice['token'])[2] for i in range(7)] + + def test_walk_several_pages(self): + """7 сообщений по 3: три страницы, без потерь, повторов и перестановок""" + items, pages = read_all_messages(self.chat['id'], self.alice['token'], limit=3) + self.assertEqual(pages, 3) + self.assertEqual([m['id'] for m in items], [m['id'] for m in self.sent]) + keys = [(m['created_at'], m['id']) for m in items] + self.assertEqual(keys, sorted(keys)) + + def test_cursor_from_other_chat(self): + """cursor от другого чата -> 400""" + _, _, page = call('GET', '/api/v1/chats/%s/messages?limit=1' % self.chat['id'], token=self.alice['token']) + other = new_chat(self.alice, title='Другой') + self.assertError(call('GET', '/api/v1/chats/%s/messages?cursor=%s' % (other['id'], page['next_cursor']), + token=self.alice['token']), 400, 'invalid_request') + + def test_invalid_cursor_and_limit(self): + """Мусорный cursor и limit вне 1..100 -> 400""" + path = '/api/v1/chats/%s/messages' % self.chat['id'] + for query in ('?cursor=not-a-cursor', '?limit=0', '?limit=101', '?limit=abc'): + with self.subTest(query=query): + self.assertError(call('GET', path + query, token=self.alice['token']), 400, 'invalid_request') + + +class IdentityTest(ErrorFormatMixin, unittest.TestCase): + def test_sender_cannot_be_forged(self): + """sender_id в теле запрещён: отправитель только из сессии""" + alice, bob = new_user('alice'), new_user('bob') + chat = new_chat(alice, bob) + forged = call('POST', '/api/v1/chats/%s/messages' % chat['id'], {'text': 'hi', 'sender_id': bob['id']}, alice['token']) + self.assertError(forged, 400, 'invalid_request') + + def test_nul_character_is_rejected(self): + """Символ NUL нельзя сохранить в PostgreSQL text -> 400, а не 500""" + alice = new_user('alice') + chat = new_chat(alice) + self.assertError(call('POST', '/api/v1/chats/%s/messages' % chat['id'], {'text': 'a\u0000b'}, alice['token']), + 400, 'invalid_request') + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_auth.py b/student-tests/test_auth.py new file mode 100644 index 00000000..6d339b3c --- /dev/null +++ b/student-tests/test_auth.py @@ -0,0 +1,126 @@ +from datetime import datetime, timezone +import unittest +import uuid + +from helpers import basic, call, login, new_user, register + + +class RegistrationTest(unittest.TestCase): + def body(self, password): + return {'username': 'reg_' + uuid.uuid4().hex[:12], 'display_name': 'Регистрация', 'password': password} + + def test_password_is_not_returned(self): + """Ответ регистрации и /users/me не содержат password/hash""" + status, _, user = call('POST', '/api/v1/users', self.body('Secret-password-1')) + self.assertEqual(status, 201) + self.assertEqual(set(user), {'id', 'username', 'display_name', 'created_at'}) + + def test_password_length_limits(self): + """Пароль 12..128 символов; 11 и 129 -> 400; эмодзи считается одним символом""" + self.assertEqual(call('POST', '/api/v1/users', self.body('x' * 11))[0], 400) + self.assertEqual(call('POST', '/api/v1/users', self.body('x' * 129))[0], 400) + self.assertEqual(call('POST', '/api/v1/users', self.body('x' * 128))[0], 201) + self.assertEqual(call('POST', '/api/v1/users', self.body('🔑' * 12))[0], 201) + + def test_password_required_and_not_blank(self): + """Без password или из одних пробелов -> 400""" + body = self.body('x') + del body['password'] + self.assertEqual(call('POST', '/api/v1/users', body)[0], 400) + self.assertEqual(call('POST', '/api/v1/users', self.body(' ' * 20))[0], 400) + + +class LoginTest(unittest.TestCase): + def setUp(self): + self.user, self.password = register('login') + + def assertBasicChallenge(self, result): + status, headers, body = result + self.assertEqual(status, 401) + self.assertEqual(headers['WWW-Authenticate'], 'Basic realm="messenger", charset="UTF-8"') + self.assertEqual(body['error']['code'], 'unauthorized') + return body['error']['message'] + + def test_login_returns_opaque_session(self): + """Вход: 201, token_type=Bearer, токен случайный и не содержит username/UUID""" + first = login(self.user['username'], self.password) + second = login(self.user['username'], self.password) + self.assertEqual(first['token_type'], 'Bearer') + self.assertEqual(first['user'], self.user) + self.assertGreaterEqual(len(first['token']), 43) + self.assertNotEqual(first['token'], second['token']) + self.assertNotIn(self.user['username'], first['token']) + self.assertNotIn(self.user['id'], first['token']) + expires = datetime.fromisoformat(first['expires_at'].replace('Z', '+00:00')) + self.assertGreater(expires, datetime.now(timezone.utc)) + + def test_wrong_password_and_unknown_user_look_the_same(self): + """Неверный пароль и несуществующий пользователь -> одинаковый 401 с Basic challenge""" + wrong = self.assertBasicChallenge(call('POST', '/api/v1/auth/sessions', + headers=basic(self.user['username'], 'Wrong-password-1'))) + unknown = self.assertBasicChallenge(call('POST', '/api/v1/auth/sessions', + headers=basic('nobody_' + uuid.uuid4().hex[:8], 'Wrong-password-1'))) + self.assertEqual(wrong, unknown) + + def test_malformed_basic(self): + """Нет заголовка, не base64, нет двоеточия, Bearer вместо Basic -> 401 Basic""" + for header in (None, 'Basic', 'Basic !!!not-base64!!!', 'Basic ' + 'bm9jb2xvbg==', 'Bearer abc'): + with self.subTest(header=header): + headers = {'Authorization': header} if header else {} + self.assertBasicChallenge(call('POST', '/api/v1/auth/sessions', headers=headers)) + + def test_password_with_colon_and_unicode(self): + """Двоеточие и не-ASCII в пароле допустимы: делим только по первому ':'""" + user, password = register('colon', password='пароль:с:двоеточием🔑') + self.assertEqual(login(user['username'], password)['user']['id'], user['id']) + + def test_login_rejects_body(self): + """POST /auth/sessions принимает только заголовок, JSON body -> 400""" + status = call('POST', '/api/v1/auth/sessions', {'username': self.user['username']}, + headers=basic(self.user['username'], self.password))[0] + self.assertEqual(status, 400) + + +class BearerTest(unittest.TestCase): + def setUp(self): + self.alice = new_user('alice') + self.eve = new_user('eve') + + def assertBearerChallenge(self, headers): + status, response_headers, _ = call('GET', '/api/v1/users/me', headers=headers) + self.assertEqual(status, 401) + self.assertEqual(response_headers['WWW-Authenticate'], 'Bearer realm="messenger"') + + def test_only_bearer_is_accepted(self): + """Нет заголовка, Basic, X-User-Id, мусорный токен -> 401 Bearer""" + self.assertBearerChallenge({}) + self.assertBearerChallenge(basic(self.alice['username'], self.alice['password'])) + self.assertBearerChallenge({'X-User-Id': self.alice['id']}) + self.assertBearerChallenge({'Authorization': 'Bearer ' + uuid.uuid4().hex}) + self.assertBearerChallenge({'Authorization': 'Bearer'}) + + def test_scheme_is_case_insensitive(self): + """Схема аутентификации нечувствительна к регистру (RFC 9110)""" + status, _, me = call('GET', '/api/v1/users/me', headers={'Authorization': 'bearer ' + self.alice['token']}) + self.assertEqual((status, me['id']), (200, self.alice['id'])) + + def test_x_user_id_does_not_override_session(self): + """X-User-Id игнорируется: владелец определяется только токеном""" + status, _, me = call('GET', '/api/v1/users/me', token=self.alice['token'], headers={'X-User-Id': self.eve['id']}) + self.assertEqual((status, me['id']), (200, self.alice['id'])) + + +class LogoutTest(unittest.TestCase): + def test_logout_revokes_only_current_session(self): + """Logout -> 204 без тела; этот токен -> 401, вторая сессия того же пользователя работает""" + alice = new_user('alice') + other = login(alice['username'], alice['password'])['token'] + status, _, body = call('DELETE', '/api/v1/auth/sessions/current', token=alice['token']) + self.assertEqual((status, body), (204, None)) + self.assertEqual(call('GET', '/api/v1/users/me', token=alice['token'])[0], 401) + self.assertEqual(call('DELETE', '/api/v1/auth/sessions/current', token=alice['token'])[0], 401) + self.assertEqual(call('GET', '/api/v1/users/me', token=other)[0], 200) + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..4d258446 --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,68 @@ +from concurrent.futures import ThreadPoolExecutor +import threading +import unittest +import uuid + +from helpers import basic, call, new_chat, new_user, read_all_messages + +CLIENTS = 10 +REGISTRATIONS = 20 +PER_CLIENT = 10 + + +class ConcurrencyTest(unittest.TestCase): + def test_100_messages_from_10_clients(self): + """100 параллельных сообщений от 10 клиентов -> ровно 100 уникальных в истории""" + users = [new_user('client%d' % i) for i in range(CLIENTS)] + chat = new_chat(users[0], *users[1:], title='Нагрузка') + path = '/api/v1/chats/%s/messages' % chat['id'] + barrier = threading.Barrier(CLIENTS) + + def client(index): + barrier.wait() + results = [] + for n in range(PER_CLIENT): + status, _, message = call('POST', path, {'text': 'client %d msg %d' % (index, n)}, users[index]['token']) + results.append((status, message)) + return results + + with ThreadPoolExecutor(max_workers=CLIENTS) as pool: + responses = [r for batch in pool.map(client, range(CLIENTS)) for r in batch] + + self.assertEqual([s for s, _ in responses], [201] * CLIENTS * PER_CLIENT) + sent_ids = {m['id'] for _, m in responses} + items, pages = read_all_messages(chat['id'], users[0]['token'], limit=30) + read_ids = [m['id'] for m in items] + keys = [(m['created_at'], m['id']) for m in items] + + self.assertEqual(len(sent_ids), 100, 'сервер выдал повторяющиеся id') + self.assertEqual(len(read_ids), 100, 'в истории не ровно 100 сообщений') + self.assertEqual(set(read_ids), sent_ids, 'история не совпадает с подтверждёнными сообщениями') + self.assertEqual(len({m['text'] for m in items}), 100, 'потерялся или задвоился текст') + self.assertEqual(keys, sorted(keys), 'история не упорядочена по (created_at, id)') + print('\n отправлено 201: %d, уникальных id: %d, прочитано: %d за %d страниц(ы)' + % (len(responses), len(sent_ids), len(read_ids), pages)) + + def test_parallel_registration_of_same_username(self): + """20 одновременных регистраций одного username -> ровно один 201, остальные 409; вход только с паролем победителя""" + username = 'race_' + uuid.uuid4().hex[:12] + barrier = threading.Barrier(REGISTRATIONS) + + def register(index): + password = 'Race-password-%02d' % index + barrier.wait() + status = call('POST', '/api/v1/users', {'username': username, 'display_name': 'Гонка', 'password': password})[0] + return status, password + + with ThreadPoolExecutor(max_workers=REGISTRATIONS) as pool: + results = list(pool.map(register, range(REGISTRATIONS))) + statuses = sorted(status for status, _ in results) + self.assertEqual(statuses, [201] + [409] * (REGISTRATIONS - 1)) + winner = [password for status, password in results if status == 201][0] + loser = [password for status, password in results if status == 409][0] + self.assertEqual(call('POST', '/api/v1/auth/sessions', headers=basic(username, winner))[0], 201) + self.assertEqual(call('POST', '/api/v1/auth/sessions', headers=basic(username, loser))[0], 401) + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_db.py b/student-tests/test_db.py new file mode 100644 index 00000000..5997fd83 --- /dev/null +++ b/student-tests/test_db.py @@ -0,0 +1,58 @@ +import hashlib +import time +import unittest +import uuid + +from helpers import call, compose, new_chat, new_user, sql + + +class DatabaseTest(unittest.TestCase): + def test_chat_creation_is_rolled_back(self): + """Неизвестный участник: INSERT chats выполнен, INSERT chat_members упал по FK, всё откатано""" + alice = new_user('alice') + chats_before = int(sql('SELECT count(*) FROM chats')) + members_before = int(sql('SELECT count(*) FROM chat_members')) + ghost = str(uuid.uuid4()) + since = sql("SELECT to_char(now() - interval '1 second', 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"')") + status, _, body = call('POST', '/api/v1/chats', {'title': 'Откат', 'member_ids': [alice['id'], ghost]}, + alice['token']) + self.assertEqual(status, 400, body) + time.sleep(0.5) + log = compose('logs', '--no-log-prefix', '--since', since, 'postgres') + self.assertIn('violates foreign key constraint', log) + self.assertIn(ghost, log) + self.assertEqual(int(sql('SELECT count(*) FROM chats')), chats_before) + self.assertEqual(int(sql('SELECT count(*) FROM chat_members')), members_before) + + def test_secrets_are_not_stored_in_plaintext(self): + """В users лежит argon2id-хеш, в sessions — sha256 токена, не сам токен""" + alice = new_user('alice') + password_hash = sql("SELECT password_hash FROM users WHERE id = '%s'" % alice['id']) + self.assertTrue(password_hash.startswith('$argon2id$'), password_hash[:10]) + self.assertNotIn(alice['password'], password_hash) + digest = hashlib.sha256(alice['token'].encode()).hexdigest() + self.assertEqual(sql("SELECT count(*) FROM sessions WHERE token_hash = '\\x%s'" % digest), '1') + dump = sql("SELECT string_agg(encode(token_hash, 'escape'), '') FROM sessions WHERE user_id = '%s'" % alice['id']) + self.assertNotIn(alice['token'], dump) + + def test_migrations_are_idempotent(self): + """Повторный запуск миграций ничего не меняет и завершается успешно""" + before = sql('SELECT version, applied_at FROM schema_migrations ORDER BY version') + users = sql('SELECT count(*) FROM users') + output = compose('run', '--rm', '--no-deps', 'migrate') + self.assertIn('skip 0001_init: already applied', output) + self.assertEqual(sql('SELECT version, applied_at FROM schema_migrations ORDER BY version'), before) + self.assertEqual(sql('SELECT count(*) FROM users'), users) + + def test_history_query_uses_index(self): + """Запрос страницы истории идёт по индексу (chat_id, created_at, id), без Sort""" + alice = new_user('alice') + chat = new_chat(alice) + plan = sql("SET enable_seqscan = off; SET enable_bitmapscan = off; EXPLAIN SELECT id FROM messages WHERE chat_id = '%s' " + "ORDER BY created_at, id LIMIT 51" % chat['id']) + self.assertIn('messages_chat_history_idx', plan) + self.assertNotIn('Sort', plan) + + +if __name__ == '__main__': + unittest.main()