File size: 2,619 Bytes
7c6ffa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Class session progress persistence + isolation tests."""

from __future__ import annotations


def _signup(client, *, email: str, password: str = "Pass123!beta", name: str = "Test User") -> str:
    resp = client.post("/auth/signup", json={"name": name, "email": email, "password": password})
    assert resp.status_code == 201, f"signup failed: {resp.status_code} {resp.text}"
    return resp.json()["access_token"]


def _auth(token: str) -> dict:
    return {"Authorization": f"Bearer {token}"}


def _payload(session_id: str, **data) -> dict:
    return {"class_session_id": session_id, "data": {"started": True, "activeStepId": "teach", **data}}


class TestClassSessionProgressPersistence:
    def test_upsert_and_list(self, auth_client):
        token = _signup(auth_client, email="cs-list@tuition.test")
        auth_client.post("/class-session-progress", headers=_auth(token), json=_payload("k|ss|humanism|M1"))
        auth_client.post("/class-session-progress", headers=_auth(token), json=_payload("k|ss|humanism|M2"))

        rows = auth_client.get("/class-session-progress", headers=_auth(token)).json()
        assert len(rows) == 2
        assert {r["class_session_id"] for r in rows} == {"k|ss|humanism|M1", "k|ss|humanism|M2"}

    def test_upsert_replaces_same_session(self, auth_client):
        token = _signup(auth_client, email="cs-replace@tuition.test")
        auth_client.post("/class-session-progress", headers=_auth(token), json=_payload("k|ss|humanism|M1"))
        auth_client.post(
            "/class-session-progress",
            headers=_auth(token),
            json=_payload("k|ss|humanism|M1", activeStepId="quiz", maxUnlockedIndex=8),
        )
        rows = auth_client.get("/class-session-progress", headers=_auth(token)).json()
        assert len(rows) == 1
        assert rows[0]["data"]["activeStepId"] == "quiz"
        assert rows[0]["data"]["maxUnlockedIndex"] == 8

    def test_requires_authentication(self, auth_client):
        assert auth_client.get("/class-session-progress").status_code in (401, 403)


class TestClassSessionProgressIsolation:
    def test_users_isolated(self, auth_client):
        token_a = _signup(auth_client, email="cs-alice@tuition.test", name="Alice")
        token_b = _signup(auth_client, email="cs-bob@tuition.test", name="Bob")
        auth_client.post("/class-session-progress", headers=_auth(token_a), json=_payload("k|ss|humanism|M1"))
        assert auth_client.get("/class-session-progress", headers=_auth(token_b)).json() == []
        assert len(auth_client.get("/class-session-progress", headers=_auth(token_a)).json()) == 1