forked from mtusi2026/mtusi2026lab3
68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
"""Student tests run against an already-running compose stand.
|
|
|
|
Start the stand first (secrets + `docker compose up -d --build`), then:
|
|
|
|
.venv\\Scripts\\python -m pytest student-tests -v
|
|
# or: make test-student PYTHON=.venv/bin/python
|
|
|
|
Override the target with BASE_URL (default http://localhost:8080).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def base_url() -> str:
|
|
return BASE_URL.rstrip("/")
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def client(base_url):
|
|
from http_client import Client
|
|
|
|
return Client(base_url)
|
|
|
|
|
|
def _register_and_login(client, label: str) -> dict:
|
|
suffix = os.urandom(4).hex()
|
|
username = f"s_{label}_{suffix}"
|
|
password = "T3st-" + os.urandom(8).hex()
|
|
registered = client.register(username, password, "Студент " + label)
|
|
assert registered.status == 201, registered.body
|
|
user = registered.json
|
|
session = client.login(username, password)
|
|
assert session.status == 201, session.body
|
|
payload = session.json
|
|
return {
|
|
"id": user["id"],
|
|
"username": username,
|
|
"password": password,
|
|
"user": user,
|
|
"token": payload["token"],
|
|
}
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def alice(client):
|
|
return _register_and_login(client, "alice")
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def bob(client):
|
|
return _register_and_login(client, "bob")
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def chat(client, alice, bob):
|
|
response = client.create_chat(alice["token"], [alice["id"], bob["id"]])
|
|
assert response.status == 201, response.body
|
|
return response.json
|