File size: 2,174 Bytes
0225ffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
test_coding_routes.py β€” Tests for /api/v1/coding endpoints.
Routes covered:
  GET  /api/v1/coding/fetch           β†’ fetch coding schema (auth)
  GET  /api/v1/coding/fetch           β†’ reject unauthenticated
  POST /api/v1/coding/run_code        β†’ missing body returns 422
  POST /api/v1/coding/run_code        β†’ reject unauthenticated
"""
import pytest
from fastapi.testclient import TestClient

BASE = "/api/v1/coding"


class TestCodingAuth:
    def test_fetch_without_token_returns_401(self):
        from api.app import app
        with TestClient(app) as local_client:
            resp = local_client.get(f"{BASE}/fetch")
            assert resp.status_code == 401

    def test_run_code_without_token_returns_401(self):
        from api.app import app
        with TestClient(app) as local_client:
            resp = local_client.post(f"{BASE}/run_code", json={})
            assert resp.status_code == 401


class TestFetchCodingSchema:
    def test_fetch_with_token_returns_200_or_creates_schema(
        self, client: TestClient, auth_headers: dict
    ):
        resp = client.get(f"{BASE}/fetch", headers=auth_headers)
        # 200 if schema exists or was just created
        assert resp.status_code == 200

    def test_fetch_returns_valid_json(self, client: TestClient, auth_headers: dict):
        resp = client.get(f"{BASE}/fetch", headers=auth_headers)
        assert isinstance(resp.json(), dict)


class TestRunCode:
    def test_run_code_missing_fields_returns_422(
        self, client: TestClient, auth_headers: dict
    ):
        resp = client.post(f"{BASE}/run_code", json={}, headers=auth_headers)
        assert resp.status_code == 422

    def test_run_code_invalid_question_id_returns_error(
        self, client: TestClient, auth_headers: dict
    ):
        payload = {
            "question_id": 99999999,
            "code": "class Solution:\n    def solve(self): pass",
            "language": "python",
        }
        resp = client.post(f"{BASE}/run_code", json=payload, headers=auth_headers)
        # 404 (question not found) or 500 if internal, but NOT 200
        assert resp.status_code in (400, 404, 500), resp.text