Files
mtusi2026lab2/student-tests/acceptance.py

165 lines
7.6 KiB
Python

"""Additional black-box checks. Run against this repository's synthetic stack."""
import base64
import json
import subprocess
import sys
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, check
def docker(*args, input=None):
result = subprocess.run(['docker', 'compose', *args], cwd=ROOT, input=input,
text=True, capture_output=True, timeout=120)
check(result.returncode == 0, 'Docker operation failed: ' + ' '.join(args[:2]))
return result.stdout.strip()
def sql(query):
return docker('exec', '-T', 'db', 'psql', '-U', 'messenger', '-d', 'messenger',
'-At', '-v', 'ON_ERROR_STOP=1', input=query)
def wait_ready(client):
end = time.monotonic() + 90
while time.monotonic() < end:
try:
client.call('GET', '/health/ready', schema='Health')
return
except Exception:
time.sleep(0.5)
raise AssertionError('API did not recover')
def main():
c = Client()
results = []
def passed(name):
results.append({'test': name, 'result': 'PASS'})
print('PASS:', name, flush=True)
alice, bob, eve, chat = c.bootstrap()
endpoint = '/api/v1/chats/' + chat['id'] + '/messages'
registration = {'username': 'race_' + uuid.uuid4().hex[:16],
'display_name': 'Гонка', 'password': 'Synthetic:пароль-1234'}
def register(_):
return Client().call('POST', '/api/v1/users', [201, 409], body=registration)['status']
with ThreadPoolExecutor(max_workers=8) as executor:
codes = list(executor.map(register, range(8)))
check(codes.count(201) == 1 and codes.count(409) == 7, 'Registration race')
passed('concurrent registration: one 201, seven 409')
before = sql('SELECT (SELECT count(*) FROM chats), (SELECT count(*) FROM members);')
c.call('POST', '/api/v1/chats', 400, actor=alice,
body={'title': 'Rollback', 'member_ids': [alice['id'], str(uuid.uuid4())]})
after = sql('SELECT (SELECT count(*) FROM chats), (SELECT count(*) FROM members);')
check(before == after, 'Partial chat or membership committed')
passed('foreign-key failure rolls back chat and first member')
for bad in ({'text': ' '}, {'text': 42}, {'text': 'x', 'sender_id': eve['id']},
{'text': 'x' * 4001}):
c.call('POST', endpoint, 400, actor=alice, body=bad)
c.call('POST', endpoint, 400, actor=alice, raw=b'{broken',
headers={'Content-Type': 'application/json'})
c.call('POST', endpoint, 415, actor=alice, raw=b'text',
headers={'Content-Type': 'text/plain'})
c.call('GET', '/api/v1/chats/bad/messages', 401)
c.call('GET', '/api/v1/chats/bad/messages', 400, actor=alice)
c.call('POST', endpoint, 404, actor=eve, body={'text': 'hidden'})
passed('strict payload validation and authentication before resource access')
messages = [c.message(alice, chat, 'Пагинация ' + str(i))['json'] for i in range(7)]
first = c.call('GET', endpoint + '?limit=2', actor=bob, schema='MessagePage')['json']
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Другой', 'member_ids': [alice['id']]}, schema='Chat')['json']
from urllib.parse import quote
cursor = quote(first['next_cursor'], safe='')
c.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' + cursor, 400, actor=alice)
collected = first['items']
while first['next_cursor']:
first = c.call('GET', endpoint + '?limit=3&cursor=' + quote(first['next_cursor'], safe=''),
actor=bob, schema='MessagePage')['json']
collected.extend(first['items'])
check(collected == messages, 'Pagination duplicated or skipped data')
passed('keyset pagination, changing limit, cross-chat cursor rejection')
# Identical timestamps exercise UUID as the second ordering component.
sql("UPDATE messages SET created_at='2026-01-01T00:00:00Z' WHERE chat_id='" + chat['id'] + "';")
current, collected = endpoint + '?limit=2', []
while current:
chunk = c.call('GET', current, actor=alice, schema='MessagePage')['json']
collected.extend(chunk['items'])
current = endpoint + '?limit=2&cursor=' + quote(chunk['next_cursor'], safe='') if chunk['next_cursor'] else None
check([x['id'] for x in collected] == sorted(x['id'] for x in messages), 'Timestamp tie pagination')
passed('equal timestamps use UUID tie breaker')
basic = base64.b64encode((registration['username'] + ':' + registration['password']).encode()).decode()
c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': 'Basic ' + basic}, schema='Session')
bad = base64.b64encode((registration['username'] + ':wrong-password-long').encode()).decode()
c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + bad})
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
ttl = int(docker('exec', '-T', 'redis', 'redis-cli', 'TTL', 'profile:v1:' + alice['id']))
check(0 < ttl <= 60, 'Redis profile cache has no bounded TTL')
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
c.call('GET', '/api/v1/users/me', 401, actor=alice)
passed('Unicode/colon password, wrong password, cache TTL and logout with warm cache')
try:
docker('stop', 'redis')
start = time.monotonic()
c.call('GET', '/api/v1/users/me', actor=bob, schema='User')
check(time.monotonic() - start < 5, 'Unbounded Redis failure')
c.call('GET', '/api/v1/users/me', 401)
c.call('GET', '/health/ready', schema='Health')
finally:
docker('start', 'redis')
c.call('GET', '/api/v1/users/me', actor=bob, schema='User')
passed('Redis outage falls back safely and cache recovers')
try:
docker('stop', 'db')
start = time.monotonic()
c.call('GET', '/api/v1/users/me', 503, actor=bob)
check(time.monotonic() - start < 8, 'Unbounded DB failure')
c.call('GET', '/health/ready', 503)
c.call('GET', '/health/live', schema='Health')
finally:
docker('start', 'db')
wait_ready(c)
c.call('GET', '/api/v1/users/me', actor=bob, schema='User')
passed('DB outage fails closed with 503; same session works after recovery')
# A separate short-TTL process leaves the normal API configuration unchanged.
container = docker('run', '--rm', '-d', '--no-deps', '-p', '127.0.0.1:8081:8080',
'-e', 'SESSION_TTL_SECONDS=2', 'api')
try:
short = Client('http://127.0.0.1:8081')
wait_ready(short)
actor = short.user('expiry')
short.call('GET', '/api/v1/users/me', actor=actor, schema='User')
time.sleep(2.2)
short.call('GET', '/api/v1/users/me', 401, actor=actor)
finally:
subprocess.run(['docker', 'stop', container], check=True, capture_output=True, timeout=30)
passed('SESSION_TTL_SECONDS=2 expires a warm-cache session')
counts = sql('SELECT (SELECT count(*) FROM users), (SELECT count(*) FROM schema_migrations);')
docker('exec', '-T', 'api', 'python', '-m', 'src.db')
check(counts == sql('SELECT (SELECT count(*) FROM users), (SELECT count(*) FROM schema_migrations);'),
'Repeated migration changed data')
passed('migrations are repeatable without duplicate data')
(ROOT / 'evidence' / 'student-tests.json').write_text(
json.dumps(results, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
if __name__ == '__main__':
main()