File size: 1,414 Bytes
4f2020d
 
 
 
 
 
 
 
 
 
c30608d
 
 
 
4f2020d
 
 
 
 
 
c30608d
4f2020d
 
c30608d
 
4f2020d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for admin cache management endpoint."""

import os
import pytest
from unittest.mock import patch, MagicMock, AsyncMock
from fastapi.testclient import TestClient


@pytest.fixture
def client():
    mock_ddinter = MagicMock()
    mock_ddinter.connect = AsyncMock()
    mock_ddinter.close = AsyncMock()
    mock_ddinter.health_check = AsyncMock(return_value=True)
    mock_severity = MagicMock()
    mock_severity.load_model = MagicMock()
    mock_severity.is_loaded.return_value = True

    with (
        patch.dict(os.environ, {"API_KEY": "test-key"}),
        patch("app.main.ddinter_db.client", mock_ddinter),
        patch("app.main.severity_classifier", mock_severity),
        patch("app.main.ner_model"),
        patch("app.api.health.ddinter_db.client", mock_ddinter),
        patch("app.services.interaction_checker.ddinter_db.client", mock_ddinter),
        patch("app.services.interaction_checker.severity_classifier", mock_severity),
    ):
        from app.main import app
        yield TestClient(app)


class TestAdminCacheClear:
    def test_clears_cache_with_valid_key(self, client):
        resp = client.post("/admin/cache/clear", headers={"X-API-Key": "test-key"})
        assert resp.status_code == 200
        assert resp.json()["status"] == "ok"

    def test_rejects_without_key(self, client):
        resp = client.post("/admin/cache/clear")
        assert resp.status_code == 401