Files
mtusi2026lab2/student-tests/run.py
T

64 lines
2.1 KiB
Python

import sys
import threading
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, Failure, arguments, check, run
def t_wrong_password(c: Client) -> None:
# Создаем пользователя
u = c.user("wrongpass")
# Пытаемся войти с неверным паролем
import base64
bad_cred = base64.b64encode((u['registration']['username'] + ':wrongpassword123').encode()).decode()
c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + bad_cred})
def t_logout(c: Client) -> None:
u = c.user("logout")
# Успешный logout
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=u)
# Повторный запрос с отозванным токеном должен дать 401
c.call('GET', '/api/v1/users/me', 401, actor=u)
def t_race_registration(c: Client) -> None:
username = 'race_' + uuid.uuid4().hex[:6]
barrier = threading.Barrier(2)
results = []
def register():
local = Client()
barrier.wait()
try:
local.call('POST', '/api/v1/users', 201, body={'username': username, 'display_name': 'Race', 'password': 'ValidPassword123'})
results.append(201)
except Failure as e:
if 'expected [201]' in str(e):
results.append(409)
else:
raise
with ThreadPoolExecutor(max_workers=2) as pool:
list(pool.map(lambda _: register(), range(2)))
check(201 in results and 409 in results, f"Race condition failed: {results}")
CASES = [
("wrong_password", t_wrong_password),
("logout", t_logout),
("race_registration", t_race_registration),
]
def main() -> None:
args = arguments("Student tests for lab2")
client = Client(args.base_url, args.ca_file)
for name, fn in CASES:
fn(client)
print(f"PASS {name}")
print("PASS: lab2 student tests")
if __name__ == "__main__":
run(main)