feat/Update_Controllers_to_Allow_for_Premium_Membership
This commit is contained in:
@@ -11,7 +11,7 @@ if str(ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
|
||||
def load_app_module(monkeypatch, *, skip_model_load=True, ollama_model=None, service_token=None):
|
||||
def load_app_module(monkeypatch, *, skip_model_load=True, ollama_model=None, service_token=None, external_ai_enabled=False):
|
||||
if skip_model_load:
|
||||
monkeypatch.setenv("AI_SERVICE_SKIP_MODEL_LOAD", "1")
|
||||
else:
|
||||
@@ -26,6 +26,10 @@ def load_app_module(monkeypatch, *, skip_model_load=True, ollama_model=None, ser
|
||||
monkeypatch.delenv("OLLAMA_MODEL", raising=False)
|
||||
else:
|
||||
monkeypatch.setenv("OLLAMA_MODEL", ollama_model)
|
||||
if external_ai_enabled:
|
||||
monkeypatch.setenv("EXTERNAL_AI_ENABLED", "true")
|
||||
else:
|
||||
monkeypatch.delenv("EXTERNAL_AI_ENABLED", raising=False)
|
||||
if "app" in sys.modules:
|
||||
del sys.modules["app"]
|
||||
module = importlib.import_module("app")
|
||||
@@ -227,7 +231,8 @@ def test_provider_gemini_dispatch(monkeypatch):
|
||||
monkeypatch.setenv("AI_PROVIDER", "gemini")
|
||||
monkeypatch.setenv("GEMINI_API_KEY", "test-key")
|
||||
monkeypatch.setenv("GEMINI_MODEL", "gemini-2.0-flash")
|
||||
module = load_app_module(monkeypatch)
|
||||
module = load_app_module(monkeypatch, external_ai_enabled=True)
|
||||
module._external_ai_allowed.set(True)
|
||||
|
||||
captured = {}
|
||||
payload = {"candidates": [{"content": {"parts": [{"text": '{"score": 9}'}]}}]}
|
||||
@@ -244,7 +249,8 @@ def test_provider_gemini_dispatch(monkeypatch):
|
||||
def test_provider_groq_dispatch(monkeypatch):
|
||||
monkeypatch.setenv("AI_PROVIDER", "groq")
|
||||
monkeypatch.setenv("GROQ_API_KEY", "test-key")
|
||||
module = load_app_module(monkeypatch)
|
||||
module = load_app_module(monkeypatch, external_ai_enabled=True)
|
||||
module._external_ai_allowed.set(True)
|
||||
|
||||
captured = {}
|
||||
payload = {"choices": [{"message": {"content": "rewritten CV text"}}]}
|
||||
@@ -259,7 +265,8 @@ def test_provider_groq_dispatch(monkeypatch):
|
||||
def test_provider_missing_cloud_key_raises_503(monkeypatch):
|
||||
monkeypatch.setenv("AI_PROVIDER", "gemini")
|
||||
monkeypatch.delenv("GEMINI_API_KEY", raising=False)
|
||||
module = load_app_module(monkeypatch)
|
||||
module = load_app_module(monkeypatch, external_ai_enabled=True)
|
||||
module._external_ai_allowed.set(True)
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
@@ -284,6 +291,31 @@ def test_health_reports_active_provider(monkeypatch):
|
||||
assert payload["ai_provider_configured"] is True
|
||||
|
||||
|
||||
def test_external_provider_requires_admin_gate_and_backend_consent_header(monkeypatch):
|
||||
monkeypatch.setenv("AI_PROVIDER", "gemini")
|
||||
monkeypatch.setenv("GEMINI_API_KEY", "test-key")
|
||||
module = load_app_module(monkeypatch, ollama_model="qwen2.5:7b", external_ai_enabled=True)
|
||||
captured = {}
|
||||
_install_fake_urlopen(monkeypatch, module, {"response": "rewritten locally"}, captured)
|
||||
client = TestClient(module.app)
|
||||
|
||||
local_response = client.post("/cv/rewrite", json={"instruction": "Rewrite", "text": "Synthetic CV"})
|
||||
assert local_response.status_code == 200
|
||||
assert captured["url"].startswith("http://127.0.0.1:11434/")
|
||||
assert local_response.headers["X-Ai-Provider"] == "ollama"
|
||||
|
||||
external_payload = {"candidates": [{"content": {"parts": [{"text": "rewritten externally"}]}}]}
|
||||
_install_fake_urlopen(monkeypatch, module, external_payload, captured)
|
||||
external_response = client.post(
|
||||
"/cv/rewrite",
|
||||
json={"instruction": "Rewrite", "text": "Synthetic CV"},
|
||||
headers={"X-Ai-External-Allowed": "true"},
|
||||
)
|
||||
assert external_response.status_code == 200
|
||||
assert "generativelanguage" in captured["url"]
|
||||
assert external_response.headers["X-Ai-Provider"] == "gemini"
|
||||
|
||||
|
||||
def test_service_token_rejects_calls_without_the_header(monkeypatch):
|
||||
module = load_app_module(monkeypatch, service_token="s3cret")
|
||||
client = TestClient(module.app)
|
||||
|
||||
Reference in New Issue
Block a user