|
| 1 | +import os |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +os.environ.setdefault("FLASK_DEBUG", "1") |
| 7 | + |
| 8 | +from policyengine_api.api import app |
| 9 | +from tests.fixtures.simulation_analysis_prompt_fixtures import valid_input_us |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def client(): |
| 14 | + app.config["TESTING"] = True |
| 15 | + with app.test_client() as test_client: |
| 16 | + yield test_client |
| 17 | + |
| 18 | + |
| 19 | +def test_ai_prompt_rejects_requests_without_api_key(client, monkeypatch): |
| 20 | + monkeypatch.setenv("POLICYENGINE_API_AI_ANALYSIS_API_KEY", "secret-key") |
| 21 | + |
| 22 | + response = client.post( |
| 23 | + "/us/ai-prompts/simulation_analysis", |
| 24 | + json=valid_input_us, |
| 25 | + environ_base={"REMOTE_ADDR": "203.0.113.10"}, |
| 26 | + ) |
| 27 | + |
| 28 | + assert response.status_code == 401 |
| 29 | + assert "API key required" in response.json["message"] |
| 30 | + |
| 31 | + |
| 32 | +def test_ai_prompt_allows_requests_with_api_key(client, monkeypatch): |
| 33 | + monkeypatch.setenv("POLICYENGINE_API_AI_ANALYSIS_API_KEY", "secret-key") |
| 34 | + |
| 35 | + with patch( |
| 36 | + "policyengine_api.routes.ai_prompt_routes.ai_prompt_service.get_prompt", |
| 37 | + return_value="Prompt text", |
| 38 | + ) as mock_get_prompt: |
| 39 | + response = client.post( |
| 40 | + "/us/ai-prompts/simulation_analysis", |
| 41 | + json=valid_input_us, |
| 42 | + headers={"X-PolicyEngine-Api-Key": "secret-key"}, |
| 43 | + environ_base={"REMOTE_ADDR": "203.0.113.10"}, |
| 44 | + ) |
| 45 | + |
| 46 | + assert response.status_code == 200 |
| 47 | + assert response.json["result"] == "Prompt text" |
| 48 | + mock_get_prompt.assert_called_once() |
| 49 | + |
| 50 | + |
| 51 | +def test_tracer_analysis_rejects_requests_without_api_key(client, monkeypatch): |
| 52 | + monkeypatch.setenv("POLICYENGINE_API_AI_ANALYSIS_API_KEY", "secret-key") |
| 53 | + |
| 54 | + response = client.post( |
| 55 | + "/us/tracer-analysis", |
| 56 | + json={ |
| 57 | + "household_id": 1500, |
| 58 | + "policy_id": 2, |
| 59 | + "variable": "disposable_income", |
| 60 | + }, |
| 61 | + environ_base={"REMOTE_ADDR": "203.0.113.10"}, |
| 62 | + ) |
| 63 | + |
| 64 | + assert response.status_code == 401 |
| 65 | + assert "API key required" in response.json["message"] |
| 66 | + |
| 67 | + |
| 68 | +def test_tracer_analysis_allows_requests_with_api_key(client, monkeypatch): |
| 69 | + monkeypatch.setenv("POLICYENGINE_API_AI_ANALYSIS_API_KEY", "secret-key") |
| 70 | + |
| 71 | + with patch( |
| 72 | + "policyengine_api.routes.tracer_analysis_routes.tracer_analysis_service.execute_analysis", |
| 73 | + return_value=("Existing analysis", "static"), |
| 74 | + ) as mock_execute_analysis: |
| 75 | + response = client.post( |
| 76 | + "/us/tracer-analysis", |
| 77 | + json={ |
| 78 | + "household_id": 1500, |
| 79 | + "policy_id": 2, |
| 80 | + "variable": "disposable_income", |
| 81 | + }, |
| 82 | + headers={"X-PolicyEngine-Api-Key": "secret-key"}, |
| 83 | + environ_base={"REMOTE_ADDR": "203.0.113.10"}, |
| 84 | + ) |
| 85 | + |
| 86 | + assert response.status_code == 200 |
| 87 | + assert response.json["result"] == "Existing analysis" |
| 88 | + mock_execute_analysis.assert_called_once_with( |
| 89 | + "us", 1500, 2, "disposable_income" |
| 90 | + ) |
0 commit comments