forked from mtusi2026/mtusi2026lab2
test: adapt HTTP checks to persistent Bearer sessions
This commit is contained in:
@@ -10,7 +10,9 @@ from jsonschema import Draft202012Validator, FormatChecker
|
||||
CONTRACT = json.loads(
|
||||
(Path(__file__).parents[1] / "contracts/openapi.json").read_text()
|
||||
)
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8080")
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8082")
|
||||
PASSWORD = "Synthetic-Password-🌍-123"
|
||||
TOKENS = {}
|
||||
|
||||
|
||||
def check(result: httpx.Response, status: int, schema: str) -> dict:
|
||||
@@ -38,9 +40,23 @@ def check(result: httpx.Response, status: int, schema: str) -> dict:
|
||||
|
||||
|
||||
def register(client: httpx.Client, **overrides) -> dict:
|
||||
data = {"username": "u_" + uuid4().hex[:24], "display_name": "Алиса 🌍"}
|
||||
data = {
|
||||
"username": "u_" + uuid4().hex[:24],
|
||||
"display_name": "Алиса 🌍",
|
||||
"password": PASSWORD,
|
||||
}
|
||||
data.update(overrides)
|
||||
return check(client.post("/api/v1/users", json=data), 201, "User")
|
||||
user = check(client.post("/api/v1/users", json=data), 201, "User")
|
||||
session = check(
|
||||
client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
auth=httpx.BasicAuth(data["username"], data["password"]),
|
||||
),
|
||||
201,
|
||||
"Session",
|
||||
)
|
||||
TOKENS[user["id"]] = session["token"]
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -62,7 +78,7 @@ def chat(client, actors):
|
||||
return check(
|
||||
client.post(
|
||||
"/api/v1/chats",
|
||||
headers={"X-User-Id": alice["id"]},
|
||||
headers=headers(alice),
|
||||
json={"title": "Учёба 🌍", "member_ids": [alice["id"], bob["id"]]},
|
||||
),
|
||||
201,
|
||||
@@ -71,7 +87,7 @@ def chat(client, actors):
|
||||
|
||||
|
||||
def headers(actor: dict) -> dict:
|
||||
return {"X-User-Id": actor["id"]}
|
||||
return {"Authorization": "Bearer " + TOKENS[actor["id"]]}
|
||||
|
||||
|
||||
def history(client, actor, chat_id, limit=7):
|
||||
|
||||
Reference in New Issue
Block a user