forked from mtusi2026/mtusi2026lab2
48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
import pytest
|
|
import httpx
|
|
import uuid
|
|
import concurrent.futures
|
|
|
|
BASE_URL = "http://localhost:8080/api/v1"
|
|
|
|
|
|
def get_unique(prefix): return f"{prefix}_{uuid.uuid4().hex[:6]}"
|
|
|
|
|
|
def register_and_login(username, password="supersecretpassword123"):
|
|
httpx.post(f"{BASE_URL}/users", json={"username": username, "display_name": "T", "password": password})
|
|
r = httpx.post(f"{BASE_URL}/auth/sessions", auth=(username, password))
|
|
return r.json()["token"], r.json()["user"]["id"]
|
|
|
|
|
|
def test_expiry_and_logout():
|
|
u = get_unique("exp")
|
|
token, _ = register_and_login(u)
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
r = httpx.delete(f"{BASE_URL}/auth/sessions/current", headers=headers)
|
|
assert r.status_code == 204
|
|
|
|
r = httpx.get(f"{BASE_URL}/chats", headers=headers)
|
|
assert r.status_code == 401
|
|
|
|
|
|
def test_invalid_password():
|
|
u = get_unique("bad")
|
|
httpx.post(f"{BASE_URL}/users", json={"username": u, "display_name": "T", "password": "supersecretpassword123"})
|
|
r = httpx.post(f"{BASE_URL}/auth/sessions", auth=(u, "wrongpassword123"))
|
|
assert r.status_code == 401
|
|
|
|
|
|
def test_concurrency():
|
|
token, _ = register_and_login(get_unique("race"))
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
c = httpx.post(f"{BASE_URL}/chats", json={"title": "Race", "member_ids": []}, headers=headers).json()
|
|
|
|
def send(): httpx.post(f"{BASE_URL}/chats/{c['id']}/messages", json={"text": "msg"}, headers=headers)
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as ex:
|
|
list(ex.map(lambda _: send(), range(100)))
|
|
|
|
r = httpx.get(f"{BASE_URL}/chats/{c['id']}/messages", params={"limit": 100}, headers=headers)
|
|
assert len(r.json()["items"]) == 100 |