forked from mtusi2026/mtusi2026lab2
346 lines
16 KiB
Python
346 lines
16 KiB
Python
#!/usr/bin/env python3
|
|
"""Student-owned lab2 tests: sessions, validation, pagination.
|
|
|
|
One command: make student-test
|
|
Direct call: python3 student-tests/run.py --base-url http://localhost:8080
|
|
|
|
The suite complements tests/smoke.py and never weakens it. Every run creates
|
|
synthetic users, chats and messages only; no public check is imported or patched.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import datetime as dt
|
|
import http.client
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
import uuid
|
|
from pathlib import Path
|
|
from urllib.parse import quote, urlsplit
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
|
|
from client import Client, Failure, LAB, arguments, check, timestamp # noqa: E402
|
|
|
|
if LAB != 2:
|
|
print('FAIL: student tests target lab2, but lab.json declares lab%d' % LAB)
|
|
raise SystemExit(2)
|
|
|
|
CHAT_PATH = '/api/v1/chats/%s'
|
|
MESSAGES_PATH = CHAT_PATH + '/messages'
|
|
MAX_PAGES = 200
|
|
|
|
TESTS = []
|
|
|
|
|
|
def test(func):
|
|
TESTS.append(func)
|
|
return func
|
|
|
|
|
|
class Context:
|
|
"""Shared synthetic fixtures: three users, one chat between two of them."""
|
|
|
|
def __init__(self, client):
|
|
self.client = client
|
|
self.alice, self.bob, self.eve, self.chat = client.bootstrap()
|
|
|
|
def new_chat(self, title, members):
|
|
return self.client.call('POST', '/api/v1/chats', 201, actor=members[0],
|
|
body={'title': title,
|
|
'member_ids': [member['id'] for member in members]},
|
|
schema='Chat')['json']
|
|
|
|
def send(self, actor, chat, text):
|
|
return self.client.call('POST', MESSAGES_PATH % chat['id'], 201, actor=actor,
|
|
body={'text': text}, schema='Message')['json']
|
|
|
|
def page(self, actor, chat, limit, cursor=None):
|
|
query = '?limit=%d' % limit
|
|
if cursor is not None:
|
|
query += '&cursor=' + quote(cursor, safe='')
|
|
return self.client.call('GET', MESSAGES_PATH % chat['id'] + query, actor=actor,
|
|
schema='MessagePage')['json']
|
|
|
|
def traverse(self, actor, chat, limits):
|
|
"""Walk the whole history, changing the page size between requests."""
|
|
items, cursor, pages = [], None, 0
|
|
while True:
|
|
limit = limits[min(pages, len(limits) - 1)]
|
|
page = self.page(actor, chat, limit, cursor)
|
|
items.extend(page['items'])
|
|
cursor = page['next_cursor']
|
|
pages += 1
|
|
check(pages < MAX_PAGES, 'pagination did not terminate')
|
|
if cursor is None:
|
|
break
|
|
return items, pages
|
|
|
|
|
|
def ordered(records):
|
|
"""Contract order: (created_at, id) ascending, exactly as the client sorts."""
|
|
return sorted(records, key=lambda item: (timestamp(item['created_at']),
|
|
uuid.UUID(item['id']).int))
|
|
|
|
|
|
def traversal_order(records):
|
|
return [item['id'] for item in records]
|
|
|
|
|
|
# --------------------------------------------------------------------- health
|
|
@test
|
|
def health_and_request_id(ctx):
|
|
for path in ('/health/live', '/health/ready'):
|
|
result = ctx.client.call('GET', path, schema='Health')
|
|
check(result['json'] == {'status': 'ok'},
|
|
'%s must return exactly {"status":"ok"}' % path)
|
|
check(len(result['headers']['X-Request-Id']) >= 8, 'request id looks too short')
|
|
marker = 'student-' + uuid.uuid4().hex
|
|
echoed = ctx.client.call('GET', '/health/live', headers={'X-Request-Id': marker})
|
|
check(echoed['headers']['X-Request-Id'] == marker, 'client request id was not echoed')
|
|
error = ctx.client.call('GET', '/api/v1/users/me', 401, headers={'X-Request-Id': marker})
|
|
check(error['headers']['X-Request-Id'] == marker, 'error response lost the request id')
|
|
check(error['json']['error']['request_id'] == marker, 'error body request_id mismatch')
|
|
|
|
|
|
# -------------------------------------------------------------- unknown route
|
|
@test
|
|
def unknown_route_and_method_are_json_404(ctx):
|
|
ctx.client.call('GET', '/api/v1/unknown', 404)
|
|
ctx.client.call('DELETE', '/api/v1/chats', 404, actor=ctx.alice)
|
|
ctx.client.call('GET', '/api/v1/chats/', 404, actor=ctx.alice)
|
|
|
|
|
|
# --------------------------------------------------------------- user payload
|
|
@test
|
|
def malformed_json_is_rejected(ctx):
|
|
json_headers = {'Content-Type': 'application/json'}
|
|
ctx.client.call('POST', '/api/v1/users', 400, raw=b'{"username": "broken",', headers=json_headers)
|
|
ctx.client.call('POST', '/api/v1/users', 400, raw=b'[1, 2, 3]', headers=json_headers)
|
|
ctx.client.call('POST', '/api/v1/users', 400, raw=b'"just a string"', headers=json_headers)
|
|
ctx.client.call('POST', '/api/v1/users', 400, raw=b'', headers=json_headers)
|
|
ctx.client.call('POST', '/api/v1/users', 415, raw=b'{"username": "x"}',
|
|
headers={'Content-Type': 'text/plain'})
|
|
ctx.client.call('POST', '/api/v1/users', 415, raw=b'{"username": "x"}',
|
|
headers={'Content-Type': 'application/x-www-form-urlencoded'})
|
|
|
|
|
|
@test
|
|
def duplicate_username_is_409(ctx):
|
|
name = 'dup_' + uuid.uuid4().hex[:12]
|
|
body = {'username': name, 'display_name': 'Дубликат',
|
|
'password': 'T3st-' + uuid.uuid4().hex}
|
|
created = ctx.client.call('POST', '/api/v1/users', 201, body=body, schema='User')['json']
|
|
check(created['username'] == body['username'], 'username changed on creation')
|
|
conflict = ctx.client.call('POST', '/api/v1/users', 409, body=body)
|
|
check(conflict['json']['error']['code'] == 'conflict', 'duplicate username must be conflict')
|
|
|
|
|
|
@test
|
|
def user_payload_validation(ctx):
|
|
name = 'valid_' + uuid.uuid4().hex[:8]
|
|
for bad in (
|
|
{'username': name, 'display_name': 'Имя', 'password': 'ok', 'extra': 1},
|
|
{'display_name': 'Имя', 'password': 'ok'},
|
|
{'username': name, 'password': 'ok'},
|
|
{'username': name, 'display_name': ' ', 'password': 'ok'},
|
|
{'username': name, 'display_name': '', 'password': 'ok'},
|
|
{'username': name, 'display_name': 42, 'password': 'ok'},
|
|
{'username': name, 'display_name': None, 'password': 'ok'},
|
|
{'username': name, 'display_name': 'д' * 101, 'password': 'ok'},
|
|
{'username': 'AB', 'display_name': 'Имя', 'password': 'ok'},
|
|
{'username': 'with space', 'display_name': 'Имя', 'password': 'ok'},
|
|
{'username': 'юзер123', 'display_name': 'Имя', 'password': 'ok'},
|
|
{'username': 'x' * 33, 'display_name': 'Имя', 'password': 'ok'},
|
|
{'username': '', 'display_name': 'Имя', 'password': 'ok'},
|
|
{'username': 123, 'display_name': 'Имя', 'password': 'ok'},
|
|
# password too short
|
|
{'username': name, 'display_name': 'Имя', 'password': 'short'},
|
|
# password too long
|
|
{'username': name, 'display_name': 'Имя', 'password': 'д' * 129},
|
|
):
|
|
ctx.client.call('POST', '/api/v1/users', 400, body=bad)
|
|
ok = ctx.client.call('POST', '/api/v1/users', 201,
|
|
body={'username': name, 'display_name': 'Студент Юникод 👋',
|
|
'password': 'T3st-' + uuid.uuid4().hex},
|
|
schema='User')['json']
|
|
check(ok['display_name'] == 'Студент Юникод 👋', 'unicode display_name changed')
|
|
check('password' not in ok and 'token' not in ok, 'user projection leaks credentials')
|
|
|
|
|
|
# ------------------------------------------------------------- sessions
|
|
@test
|
|
def basic_login_issues_bearer_session(ctx):
|
|
reg = ctx.alice['registration']
|
|
credential = base64.b64encode((reg['username'] + ':' + reg['password']).encode()).decode()
|
|
session = ctx.client.call('POST', '/api/v1/auth/sessions', 201,
|
|
headers={'Authorization': 'Basic ' + credential},
|
|
schema='Session')['json']
|
|
check(session['user'] == ctx.alice['user'], 'session user mismatch')
|
|
check(session['token_type'] == 'Bearer', 'token type must be Bearer')
|
|
check(len(session['token']) >= 32, 'token must be opaque and long enough')
|
|
check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc),
|
|
'session already expired')
|
|
ctx.alice['token'] = session['token']
|
|
|
|
# wrong password -> 401
|
|
bad = base64.b64encode((reg['username'] + ':wrong-password').encode()).decode()
|
|
ctx.client.call('POST', '/api/v1/auth/sessions', 401,
|
|
headers={'Authorization': 'Basic ' + bad})
|
|
# unknown user -> 401
|
|
bad = base64.b64encode(('nonexistent-user:' + reg['password']).encode()).decode()
|
|
ctx.client.call('POST', '/api/v1/auth/sessions', 401,
|
|
headers={'Authorization': 'Basic ' + bad})
|
|
|
|
|
|
@test
|
|
def bearer_required_for_business_endpoints(ctx):
|
|
# no auth -> 401
|
|
ctx.client.call('GET', '/api/v1/users/me', 401)
|
|
# Basic on business endpoint -> 401
|
|
reg = ctx.alice['registration']
|
|
credential = base64.b64encode((reg['username'] + ':' + reg['password']).encode()).decode()
|
|
ctx.client.call('GET', '/api/v1/users/me', 401,
|
|
headers={'Authorization': 'Basic ' + credential})
|
|
# X-User-Id ignored -> 401
|
|
ctx.client.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': ctx.alice['id']})
|
|
# Bearer works
|
|
me = ctx.client.call('GET', '/api/v1/users/me', 200, actor=ctx.alice,
|
|
schema='User')['json']
|
|
check(me == ctx.alice['user'], 'current user mismatch')
|
|
|
|
|
|
@test
|
|
def logout_revokes_session(ctx):
|
|
reg = ctx.alice['registration']
|
|
credential = base64.b64encode((reg['username'] + ':' + reg['password']).encode()).decode()
|
|
session = ctx.client.call('POST', '/api/v1/auth/sessions', 201,
|
|
headers={'Authorization': 'Basic ' + credential},
|
|
schema='Session')['json']
|
|
token = session['token']
|
|
ctx.client.call('DELETE', '/api/v1/auth/sessions/current', 204,
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
# revoked token -> 401
|
|
ctx.client.call('GET', '/api/v1/users/me', 401,
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
ctx.client.call('DELETE', '/api/v1/auth/sessions/current', 401,
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
|
|
|
|
# -------------------------------------------------------------- message rules
|
|
@test
|
|
def foreign_chat_is_invisible(ctx):
|
|
other = ctx.new_chat('Чат без Евы', [ctx.alice, ctx.bob])
|
|
ctx.client.call('GET', CHAT_PATH % other['id'], 404, actor=ctx.eve)
|
|
ctx.client.call('GET', MESSAGES_PATH % other['id'], 404, actor=ctx.eve)
|
|
ctx.client.call('POST', MESSAGES_PATH % other['id'], 404, actor=ctx.eve,
|
|
body={'text': 'чужое сообщение'})
|
|
listed = ctx.client.call('GET', '/api/v1/chats?limit=100', actor=ctx.eve,
|
|
schema='ChatPage')['json']
|
|
check(all(item['id'] != other['id'] for item in listed['items']),
|
|
'foreign chat leaked into the list of another user')
|
|
member_view = ctx.client.call('GET', '/api/v1/chats?limit=100', actor=ctx.bob,
|
|
schema='ChatPage')['json']
|
|
check(any(item['id'] == other['id'] for item in member_view['items']),
|
|
'member cannot see the own chat')
|
|
ctx.client.call('GET', CHAT_PATH % uuid.uuid4(), 404, actor=ctx.alice)
|
|
ctx.client.call('GET', '/api/v1/chats/not-a-uuid', 400, actor=ctx.alice)
|
|
ctx.client.call('GET', MESSAGES_PATH % 'not-a-uuid', 400, actor=ctx.alice)
|
|
|
|
|
|
@test
|
|
def message_payload_validation(ctx):
|
|
"""Empty and whitespace-only text is rejected."""
|
|
for bad in (
|
|
{'text': ''},
|
|
{'text': ' '},
|
|
{'text': '\u00a0\t\n'},
|
|
{},
|
|
{'text': 123},
|
|
{'text': None},
|
|
{'text': []},
|
|
{'text': 'ок', 'sender_id': ctx.bob['id']},
|
|
{'text': 'д' * 4001},
|
|
):
|
|
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 400, actor=ctx.alice, body=bad)
|
|
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 401, body={'text': 'ок'})
|
|
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 400, actor=ctx.alice, raw=b'{"text":}',
|
|
headers={'Content-Type': 'application/json'})
|
|
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 415, actor=ctx.alice,
|
|
raw=b'{"text":"ok"}', headers={'Content-Type': 'application/xml'})
|
|
|
|
|
|
@test
|
|
def text_limits_and_unicode_roundtrip(ctx):
|
|
longest = 'д' * 3999 + '👋'
|
|
check(len(longest) == 4000, 'fixture must be exactly 4000 code points')
|
|
created = ctx.send(ctx.bob, ctx.chat, longest)
|
|
check(created['text'] == longest, '4000 code points text changed')
|
|
check(created['sender_id'] == ctx.bob['id'], 'sender must be derived from actor')
|
|
mixed = 'Привет 👋🏽 \u00a0 e\u0301 — астральные 🚀🧪 и кавычки «»'
|
|
echo = ctx.send(ctx.alice, ctx.chat, mixed)
|
|
check(echo['text'] == mixed, 'mixed unicode text changed in transit')
|
|
check(echo['chat_id'] == ctx.chat['id'], 'message bound to the wrong chat')
|
|
|
|
|
|
# ---------------------------------------------------------------- pagination
|
|
@test
|
|
def multi_page_traversal_is_exact_and_stable(ctx):
|
|
chat = ctx.new_chat('Пагинация', [ctx.bob, ctx.alice])
|
|
sent = []
|
|
for index in range(7):
|
|
actor = ctx.alice if index % 2 == 0 else ctx.bob
|
|
sent.append(ctx.send(actor, chat, 'страница-%d-👋' % index))
|
|
expected = traversal_order(ordered(sent))
|
|
walked, pages = ctx.traverse(ctx.bob, chat, [2])
|
|
check(pages >= 4, 'seven messages with limit=2 must take at least four pages')
|
|
check(traversal_order(walked) == expected, 'traversal lost, duplicated or reordered messages')
|
|
check(len(set(traversal_order(walked))) == len(walked), 'traversal returned duplicates')
|
|
switched, _ = ctx.traverse(ctx.alice, chat, [2, 3, 1])
|
|
check(traversal_order(switched) == expected, 'changing limit mid-traversal changed the order')
|
|
singles, _ = ctx.traverse(ctx.alice, chat, [1])
|
|
check(len(singles) == 7, 'limit=1 traversal must return all seven messages')
|
|
first = ctx.page(ctx.alice, chat, 2)
|
|
check(len(first['items']) == 2 and first['next_cursor'], 'first page shape is wrong')
|
|
check(first['items'][0]['id'] == expected[0], 'first page must start with the oldest message')
|
|
|
|
|
|
@test
|
|
def default_page_limit_is_fifty(ctx):
|
|
chat = ctx.new_chat('Предел страницы по умолчанию', [ctx.alice, ctx.bob])
|
|
for index in range(51):
|
|
ctx.send(ctx.alice, chat, 'default-limit-%d' % index)
|
|
page = ctx.client.call('GET', MESSAGES_PATH % chat['id'], actor=ctx.alice,
|
|
schema='MessagePage')['json']
|
|
check(len(page['items']) == 50, 'default limit must be 50, got %d' % len(page['items']))
|
|
check(page['next_cursor'], 'a page that is not the last one needs a cursor')
|
|
tail = ctx.page(ctx.alice, chat, 100, page['next_cursor'])
|
|
check(len(tail['items']) == 1, 'the remaining page must hold exactly one message')
|
|
check(tail['next_cursor'] is None, 'last page must have next_cursor null')
|
|
merged = traversal_order(page['items']) + traversal_order(tail['items'])
|
|
check(len(set(merged)) == 51, 'page boundaries duplicated or dropped messages')
|
|
|
|
|
|
def main():
|
|
args = arguments('Student-owned lab2 tests: sessions, validation, pagination')
|
|
client = Client(args.base_url, args.ca_file)
|
|
ctx = Context(client)
|
|
print('student-tests: %d cases against %s' % (len(TESTS), client.base))
|
|
failures = []
|
|
for func in TESTS:
|
|
try:
|
|
func(ctx)
|
|
except (Failure, ValueError, OSError) as exc:
|
|
failures.append(func.__name__)
|
|
print('FAIL %-42s %s' % (func.__name__, exc))
|
|
else:
|
|
print('PASS %-42s' % func.__name__)
|
|
print('%d passed, %d failed' % (len(TESTS) - len(failures), len(failures)))
|
|
return 1 if failures else 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|