File size: 2,378 Bytes
6148e16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Intervention and student intelligence pipeline authorization regressions."""

from unittest.mock import patch

from fastapi.testclient import TestClient
from pydantic import ValidationError
import pytest

from main import app
from routes.intervention_routes import CompleteStepRequest, GenerateRequest
from routes.pipeline_routes import PipelineEventPayload

pytestmark = pytest.mark.usefixtures("isolated_mock_student_auth")

client = TestClient(app)


def test_intervention_request_models_accept_happy_values_and_default_metrics():
    assert GenerateRequest(student_id="s1").student_id == "s1"
    completion = CompleteStepRequest()
    assert completion.score == 0.0 and completion.time_spent_minutes == 0


def test_intervention_step_requires_authentication_and_student_cannot_recompute():
    assert client.post("/api/intervention/s1/step/1/complete", json={}).status_code == 401
    response = client.post(
        "/api/pipeline/profile/s2/recompute",
        headers={"Authorization": "Bearer mock_token_student-1"},
    )
    assert response.status_code == 403


def test_pipeline_event_happy_path_accepts_own_event():
    response = client.post(
        "/api/pipeline/event",
        headers={"Authorization": "Bearer mock_token_student-1"},
        json={"student_id": "student-1", "event_type": "diagnostic", "event_data": {}, "occurred_at": "2026-01-01T00:00:00Z"},
    )
    assert response.status_code == 202
    assert response.json()["status"] == "accepted"


def test_pipeline_blocks_cross_student_event_and_rejects_unknown_event_type():
    response = client.post(
        "/api/pipeline/event",
        headers={"Authorization": "Bearer mock_token_student-1"},
        json={"student_id": "student-2", "event_type": "quiz", "occurred_at": "2026-01-01T00:00:00Z"},
    )
    assert response.status_code == 403
    with pytest.raises(ValidationError):
        PipelineEventPayload.model_validate({"student_id": "student-1", "event_type": "unknown", "occurred_at": "now"})


def test_pipeline_recompute_requires_staff_and_intervention_bad_step_is_rejected():
    assert client.post("/api/pipeline/profile/s1/recompute").status_code == 401
    response = client.post(
        "/api/intervention/s1/step/not-an-integer/complete",
        headers={"Authorization": "Bearer mock_token_student-1"},
        json={},
    )
    assert response.status_code == 422