File size: 6,340 Bytes
7c6ffa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ee82b
7c6ffa6
 
d5ee82b
 
 
 
 
 
 
 
7c6ffa6
d5ee82b
7c6ffa6
 
d5ee82b
 
 
 
 
 
 
 
7c6ffa6
d5ee82b
 
7c6ffa6
 
 
 
 
 
 
 
 
 
d5ee82b
 
 
 
 
 
 
 
 
 
 
7c6ffa6
d5ee82b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c6ffa6
d5ee82b
7c6ffa6
 
d5ee82b
7c6ffa6
 
 
 
d5ee82b
 
 
 
 
 
 
 
 
 
 
7c6ffa6
d5ee82b
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Phase 4 C-F persistence: phase3 activity + Learn Anything roadmaps."""

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}"}


class TestPhase3Activity:
    def test_upsert_get_and_replace(self, auth_client):
        token = _signup(auth_client, email="p3-a@tuition.test")
        auth_client.post(
            "/phase3-activity",
            headers=_auth(token),
            json={"data": {"pyqScore": 6, "pyqMaxScore": 8, "weakTopicsAdded": ["tax"]}},
        )
        got = auth_client.get("/phase3-activity/me", headers=_auth(token))
        assert got.status_code == 200
        assert got.json()["data"]["pyqScore"] == 6

        auth_client.post("/phase3-activity", headers=_auth(token), json={"data": {"pyqScore": 8}})
        assert auth_client.get("/phase3-activity/me", headers=_auth(token)).json()["data"]["pyqScore"] == 8

    def test_404_when_empty_and_auth_required(self, auth_client):
        token = _signup(auth_client, email="p3-empty@tuition.test")
        assert auth_client.get("/phase3-activity/me", headers=_auth(token)).status_code == 404
        optional = auth_client.get(
            "/phase3-activity/me?optional=true",
            headers=_auth(token),
        )
        assert optional.status_code == 200
        assert optional.json() is None
        assert auth_client.get("/phase3-activity/me").status_code in (401, 403)

    def test_isolation(self, auth_client):
        a = _signup(auth_client, email="p3-alice@tuition.test", name="Alice")
        b = _signup(auth_client, email="p3-bob@tuition.test", name="Bob")
        auth_client.post("/phase3-activity", headers=_auth(a), json={"data": {"pyqScore": 5}})
        assert auth_client.get("/phase3-activity/me", headers=_auth(b)).status_code == 404


class TestLearnAnythingRoadmaps:
    def test_upsert_list_get(self, auth_client):
        token = _signup(auth_client, email="road-a@tuition.test")
        first = auth_client.post(
            "/learn-anything-roadmaps",
            headers=_auth(token),
            json={
                "roadmap_id": "deep-learning",
                "data": {
                    "id": "deep-learning",
                    "topic": "Deep Learning",
                    "weeklyModules": [{"week": "Week 1", "title": "Foundations", "tasks": ["Tensors"]}],
                },
            },
        )
        second = auth_client.post(
            "/learn-anything-roadmaps",
            headers=_auth(token),
            json={
                "roadmap_id": "python",
                "data": {
                    "id": "python",
                    "topic": "Python",
                    "weeklyModules": [{"week": "Week 1", "title": "Basics", "tasks": ["Variables"]}],
                },
            },
        )
        assert first.status_code == 200, first.text
        assert second.status_code == 200, second.text

        rows = auth_client.get("/learn-anything-roadmaps", headers=_auth(token)).json()
        assert {r["roadmap_id"] for r in rows} == {"deep-learning", "python"}

        one = auth_client.get("/learn-anything-roadmaps/deep-learning", headers=_auth(token))
        assert one.status_code == 200
        assert one.json()["data"]["topic"] == "Deep Learning"

    def test_upsert_replaces_same_id(self, auth_client):
        token = _signup(auth_client, email="road-replace@tuition.test")
        created = auth_client.post(
            "/learn-anything-roadmaps",
            headers=_auth(token),
            json={
                "roadmap_id": "ml",
                "data": {
                    "id": "ml",
                    "topic": "Machine Learning",
                    "weeklyModules": [{"week": "Week 1", "title": "Start", "tasks": ["Overview"]}],
                },
            },
        )
        assert created.status_code == 200, created.text
        replaced = auth_client.post(
            "/learn-anything-roadmaps",
            headers=_auth(token),
            json={
                "roadmap_id": "ml",
                "data": {
                    "id": "ml",
                    "topic": "Machine Learning",
                    "weeklyModules": [{"week": "Week 1", "title": "Start", "tasks": ["Overview"]}],
                    "topicStatus": {"Week 1::0": "covered"},
                },
            },
        )
        assert replaced.status_code == 409
        assert replaced.json()["detail"]["code"] == "ROADMAP_COMMAND_REQUIRED"
        command = auth_client.post(
            "/learn-anything-roadmaps/ml/commands",
            headers=_auth(token),
            json={
                "client_command_id": "phase3-cover-overview",
                "command": "set_topic_status",
                "topic_key": "Week 1::0",
                "status": "covered",
            },
        )
        assert command.status_code == 200, command.text
        rows = auth_client.get("/learn-anything-roadmaps", headers=_auth(token)).json()
        assert len(rows) == 1
        assert rows[0]["data"]["topicStatus"]["Week 1::0"] == "covered"

    def test_isolation_and_auth(self, auth_client):
        a = _signup(auth_client, email="road-alice@tuition.test", name="Alice")
        b = _signup(auth_client, email="road-bob@tuition.test", name="Bob")
        created = auth_client.post(
            "/learn-anything-roadmaps",
            headers=_auth(a),
            json={
                "roadmap_id": "ml",
                "data": {
                    "id": "ml",
                    "topic": "ML",
                    "weeklyModules": [{"week": "Week 1", "title": "Start", "tasks": ["Overview"]}],
                },
            },
        )
        assert created.status_code == 200, created.text
        assert auth_client.get("/learn-anything-roadmaps", headers=_auth(b)).json() == []
        assert auth_client.get("/learn-anything-roadmaps/ml", headers=_auth(b)).status_code == 404
        assert auth_client.get("/learn-anything-roadmaps").status_code in (401, 403)