forked from 0xWheatyz/SPARC
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2eabb1d704 |
@@ -1,388 +0,0 @@
|
|||||||
"""Tests for tracked company admin endpoints and scheduler integration.
|
|
||||||
|
|
||||||
Covers issue #1656:
|
|
||||||
- GET /admin/tracked (list tracked companies)
|
|
||||||
- POST /admin/tracked (add a tracked company)
|
|
||||||
- DELETE /admin/tracked/{company_name} (remove a tracked company)
|
|
||||||
- GET /admin/alerts (list alerts)
|
|
||||||
- scheduler.run_scheduled_analysis() integration
|
|
||||||
|
|
||||||
All tests mock the database layer and use JWT auth fixtures.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
from unittest.mock import MagicMock, patch, call
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from SPARC.api import app
|
|
||||||
from SPARC.auth import create_access_token
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def client():
|
|
||||||
"""Create test client."""
|
|
||||||
return TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def mock_db():
|
|
||||||
"""Mock the database client used by admin and auth endpoints."""
|
|
||||||
db = MagicMock()
|
|
||||||
|
|
||||||
# Default admin user for auth
|
|
||||||
db.get_user_by_id.return_value = {
|
|
||||||
"id": 1,
|
|
||||||
"email": "admin@test.com",
|
|
||||||
"role": "admin",
|
|
||||||
"created_at": datetime(2025, 1, 1, tzinfo=timezone.utc),
|
|
||||||
}
|
|
||||||
|
|
||||||
with patch("SPARC.api.get_db_client", return_value=db), \
|
|
||||||
patch("SPARC.auth.get_db_client", return_value=db):
|
|
||||||
yield db
|
|
||||||
|
|
||||||
|
|
||||||
def _admin_header():
|
|
||||||
"""Create an Authorization header with a valid admin access token."""
|
|
||||||
token = create_access_token(1, "admin@test.com", "admin")
|
|
||||||
return {"Authorization": f"Bearer {token}"}
|
|
||||||
|
|
||||||
|
|
||||||
def _user_header():
|
|
||||||
"""Create an Authorization header with a regular user access token."""
|
|
||||||
token = create_access_token(2, "user@test.com", "user")
|
|
||||||
return {"Authorization": f"Bearer {token}"}
|
|
||||||
|
|
||||||
|
|
||||||
# ---------- GET /admin/tracked ----------
|
|
||||||
|
|
||||||
class TestListTrackedCompanies:
|
|
||||||
"""GET /admin/tracked"""
|
|
||||||
|
|
||||||
def test_list_tracked_returns_companies(self, client, mock_db):
|
|
||||||
"""Admin can list tracked companies."""
|
|
||||||
mock_db.list_tracked_companies.return_value = [
|
|
||||||
{"company_name": "NVIDIA", "last_patent_count": 120, "last_analyzed": "2025-06-15"},
|
|
||||||
{"company_name": "AMD", "last_patent_count": 80, "last_analyzed": "2025-06-14"},
|
|
||||||
]
|
|
||||||
|
|
||||||
response = client.get("/admin/tracked", headers=_admin_header())
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
data = response.json()
|
|
||||||
assert len(data) == 2
|
|
||||||
assert data[0]["company_name"] == "NVIDIA"
|
|
||||||
|
|
||||||
def test_list_tracked_empty(self, client, mock_db):
|
|
||||||
"""Returns empty list when no companies are tracked."""
|
|
||||||
mock_db.list_tracked_companies.return_value = []
|
|
||||||
|
|
||||||
response = client.get("/admin/tracked", headers=_admin_header())
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json() == []
|
|
||||||
|
|
||||||
def test_list_tracked_requires_admin(self, client, mock_db):
|
|
||||||
"""Regular user cannot access tracked companies list."""
|
|
||||||
mock_db.get_user_by_id.return_value = {
|
|
||||||
"id": 2,
|
|
||||||
"email": "user@test.com",
|
|
||||||
"role": "user",
|
|
||||||
"created_at": datetime(2025, 1, 1, tzinfo=timezone.utc),
|
|
||||||
}
|
|
||||||
|
|
||||||
response = client.get("/admin/tracked", headers=_user_header())
|
|
||||||
|
|
||||||
assert response.status_code == 403
|
|
||||||
|
|
||||||
def test_list_tracked_unauthenticated(self, client):
|
|
||||||
"""Unauthenticated request returns 401."""
|
|
||||||
response = client.get("/admin/tracked")
|
|
||||||
assert response.status_code == 401
|
|
||||||
|
|
||||||
|
|
||||||
# ---------- POST /admin/tracked ----------
|
|
||||||
|
|
||||||
class TestAddTrackedCompany:
|
|
||||||
"""POST /admin/tracked"""
|
|
||||||
|
|
||||||
def test_add_tracked_company_success(self, client, mock_db):
|
|
||||||
"""Admin can add a company to tracking."""
|
|
||||||
mock_db.add_tracked_company.return_value = {
|
|
||||||
"company_name": "Intel",
|
|
||||||
"last_patent_count": 0,
|
|
||||||
"last_analyzed": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
response = client.post(
|
|
||||||
"/admin/tracked",
|
|
||||||
json={"company_name": "Intel"},
|
|
||||||
headers=_admin_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
data = response.json()
|
|
||||||
assert data["company_name"] == "Intel"
|
|
||||||
mock_db.add_tracked_company.assert_called_once_with("Intel")
|
|
||||||
|
|
||||||
def test_add_duplicate_returns_409(self, client, mock_db):
|
|
||||||
"""Adding an already-tracked company returns 409."""
|
|
||||||
mock_db.add_tracked_company.return_value = None
|
|
||||||
|
|
||||||
response = client.post(
|
|
||||||
"/admin/tracked",
|
|
||||||
json={"company_name": "NVIDIA"},
|
|
||||||
headers=_admin_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 409
|
|
||||||
assert "already tracked" in response.json()["detail"].lower()
|
|
||||||
|
|
||||||
def test_add_tracked_requires_admin(self, client, mock_db):
|
|
||||||
"""Regular user cannot add tracked companies."""
|
|
||||||
mock_db.get_user_by_id.return_value = {
|
|
||||||
"id": 2,
|
|
||||||
"email": "user@test.com",
|
|
||||||
"role": "user",
|
|
||||||
"created_at": datetime(2025, 1, 1, tzinfo=timezone.utc),
|
|
||||||
}
|
|
||||||
|
|
||||||
response = client.post(
|
|
||||||
"/admin/tracked",
|
|
||||||
json={"company_name": "Intel"},
|
|
||||||
headers=_user_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 403
|
|
||||||
|
|
||||||
def test_add_tracked_empty_name_rejected(self, client):
|
|
||||||
"""Empty company name is rejected by validation."""
|
|
||||||
response = client.post(
|
|
||||||
"/admin/tracked",
|
|
||||||
json={"company_name": ""},
|
|
||||||
headers=_admin_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 422 # Pydantic validation error
|
|
||||||
|
|
||||||
|
|
||||||
# ---------- DELETE /admin/tracked/{company_name} ----------
|
|
||||||
|
|
||||||
class TestRemoveTrackedCompany:
|
|
||||||
"""DELETE /admin/tracked/{company_name}"""
|
|
||||||
|
|
||||||
def test_remove_tracked_company_success(self, client, mock_db):
|
|
||||||
"""Admin can remove a tracked company."""
|
|
||||||
mock_db.remove_tracked_company.return_value = True
|
|
||||||
|
|
||||||
response = client.delete(
|
|
||||||
"/admin/tracked/NVIDIA",
|
|
||||||
headers=_admin_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert "Stopped tracking" in response.json()["message"]
|
|
||||||
mock_db.remove_tracked_company.assert_called_once_with("NVIDIA")
|
|
||||||
|
|
||||||
def test_remove_nonexistent_returns_404(self, client, mock_db):
|
|
||||||
"""Removing a non-tracked company returns 404."""
|
|
||||||
mock_db.remove_tracked_company.return_value = False
|
|
||||||
|
|
||||||
response = client.delete(
|
|
||||||
"/admin/tracked/UnknownCorp",
|
|
||||||
headers=_admin_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 404
|
|
||||||
assert "not found" in response.json()["detail"].lower()
|
|
||||||
|
|
||||||
def test_remove_tracked_requires_admin(self, client, mock_db):
|
|
||||||
"""Regular user cannot remove tracked companies."""
|
|
||||||
mock_db.get_user_by_id.return_value = {
|
|
||||||
"id": 2,
|
|
||||||
"email": "user@test.com",
|
|
||||||
"role": "user",
|
|
||||||
"created_at": datetime(2025, 1, 1, tzinfo=timezone.utc),
|
|
||||||
}
|
|
||||||
|
|
||||||
response = client.delete(
|
|
||||||
"/admin/tracked/NVIDIA",
|
|
||||||
headers=_user_header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 403
|
|
||||||
|
|
||||||
|
|
||||||
# ---------- GET /admin/alerts ----------
|
|
||||||
|
|
||||||
class TestListAlerts:
|
|
||||||
"""GET /admin/alerts"""
|
|
||||||
|
|
||||||
def test_list_alerts_returns_data(self, client, mock_db):
|
|
||||||
"""Admin can list alerts."""
|
|
||||||
mock_db.list_alerts.return_value = [
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"company_name": "NVIDIA",
|
|
||||||
"alert_type": "patent_count_change",
|
|
||||||
"message": "Patent count increased by 25%",
|
|
||||||
"created_at": "2025-06-15T10:00:00Z",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
response = client.get("/admin/alerts", headers=_admin_header())
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
data = response.json()
|
|
||||||
assert len(data) == 1
|
|
||||||
assert data[0]["alert_type"] == "patent_count_change"
|
|
||||||
|
|
||||||
def test_list_alerts_with_limit(self, client, mock_db):
|
|
||||||
"""Custom limit parameter is passed to the database."""
|
|
||||||
mock_db.list_alerts.return_value = []
|
|
||||||
|
|
||||||
response = client.get("/admin/alerts?limit=10", headers=_admin_header())
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
mock_db.list_alerts.assert_called_once_with(limit=10)
|
|
||||||
|
|
||||||
def test_list_alerts_requires_admin(self, client, mock_db):
|
|
||||||
"""Regular user cannot access alerts."""
|
|
||||||
mock_db.get_user_by_id.return_value = {
|
|
||||||
"id": 2,
|
|
||||||
"email": "user@test.com",
|
|
||||||
"role": "user",
|
|
||||||
"created_at": datetime(2025, 1, 1, tzinfo=timezone.utc),
|
|
||||||
}
|
|
||||||
|
|
||||||
response = client.get("/admin/alerts", headers=_user_header())
|
|
||||||
|
|
||||||
assert response.status_code == 403
|
|
||||||
|
|
||||||
|
|
||||||
# ---------- Scheduler integration ----------
|
|
||||||
|
|
||||||
class TestSchedulerIntegration:
|
|
||||||
"""Tests for scheduler.run_scheduled_analysis()."""
|
|
||||||
|
|
||||||
def test_no_tracked_companies_skips_analysis(self):
|
|
||||||
"""Scheduler does nothing when no companies are tracked."""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.list_tracked_companies.return_value = []
|
|
||||||
|
|
||||||
with patch("SPARC.scheduler.DatabaseClient", return_value=mock_db), \
|
|
||||||
patch("SPARC.scheduler.CompanyAnalyzer") as mock_analyzer_cls:
|
|
||||||
from SPARC.scheduler import run_scheduled_analysis
|
|
||||||
run_scheduled_analysis()
|
|
||||||
|
|
||||||
mock_analyzer_cls.assert_not_called()
|
|
||||||
|
|
||||||
def test_scheduler_analyzes_each_tracked_company(self):
|
|
||||||
"""Scheduler runs analysis for every tracked company."""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.list_tracked_companies.return_value = [
|
|
||||||
{"company_name": "NVIDIA", "last_patent_count": 100},
|
|
||||||
{"company_name": "AMD", "last_patent_count": 50},
|
|
||||||
]
|
|
||||||
|
|
||||||
mock_result_nvidia = MagicMock(success=True, patent_count=110)
|
|
||||||
mock_result_amd = MagicMock(success=True, patent_count=55)
|
|
||||||
mock_analyzer = MagicMock()
|
|
||||||
mock_analyzer._analyze_company_safe.side_effect = [mock_result_nvidia, mock_result_amd]
|
|
||||||
|
|
||||||
with patch("SPARC.scheduler.DatabaseClient", return_value=mock_db), \
|
|
||||||
patch("SPARC.scheduler.CompanyAnalyzer", return_value=mock_analyzer):
|
|
||||||
from SPARC.scheduler import run_scheduled_analysis
|
|
||||||
run_scheduled_analysis()
|
|
||||||
|
|
||||||
assert mock_analyzer._analyze_company_safe.call_count == 2
|
|
||||||
mock_db.update_tracked_company.assert_any_call("NVIDIA", 110)
|
|
||||||
mock_db.update_tracked_company.assert_any_call("AMD", 55)
|
|
||||||
|
|
||||||
def test_scheduler_triggers_alert_on_significant_change(self):
|
|
||||||
"""Scheduler stores an alert when patent count changes significantly."""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.list_tracked_companies.return_value = [
|
|
||||||
{"company_name": "Tesla", "last_patent_count": 100},
|
|
||||||
]
|
|
||||||
|
|
||||||
mock_result = MagicMock(success=True, patent_count=130) # 30% increase
|
|
||||||
mock_analyzer = MagicMock()
|
|
||||||
mock_analyzer._analyze_company_safe.return_value = mock_result
|
|
||||||
|
|
||||||
with patch("SPARC.scheduler.DatabaseClient", return_value=mock_db), \
|
|
||||||
patch("SPARC.scheduler.CompanyAnalyzer", return_value=mock_analyzer):
|
|
||||||
from SPARC.scheduler import run_scheduled_analysis
|
|
||||||
run_scheduled_analysis()
|
|
||||||
|
|
||||||
mock_db.store_alert.assert_called_once()
|
|
||||||
alert_kwargs = mock_db.store_alert.call_args
|
|
||||||
assert alert_kwargs[1]["company_name"] == "Tesla"
|
|
||||||
assert alert_kwargs[1]["alert_type"] == "patent_count_change"
|
|
||||||
assert alert_kwargs[1]["old_value"] == 100
|
|
||||||
assert alert_kwargs[1]["new_value"] == 130
|
|
||||||
|
|
||||||
def test_scheduler_no_alert_for_small_change(self):
|
|
||||||
"""Scheduler does not alert when change is below threshold."""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.list_tracked_companies.return_value = [
|
|
||||||
{"company_name": "Intel", "last_patent_count": 100},
|
|
||||||
]
|
|
||||||
|
|
||||||
mock_result = MagicMock(success=True, patent_count=105) # 5% increase
|
|
||||||
mock_analyzer = MagicMock()
|
|
||||||
mock_analyzer._analyze_company_safe.return_value = mock_result
|
|
||||||
|
|
||||||
with patch("SPARC.scheduler.DatabaseClient", return_value=mock_db), \
|
|
||||||
patch("SPARC.scheduler.CompanyAnalyzer", return_value=mock_analyzer):
|
|
||||||
from SPARC.scheduler import run_scheduled_analysis
|
|
||||||
run_scheduled_analysis()
|
|
||||||
|
|
||||||
mock_db.store_alert.assert_not_called()
|
|
||||||
|
|
||||||
def test_scheduler_handles_analysis_failure(self):
|
|
||||||
"""Scheduler continues when one company fails analysis."""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.list_tracked_companies.return_value = [
|
|
||||||
{"company_name": "FailCo", "last_patent_count": 50},
|
|
||||||
{"company_name": "SuccessCo", "last_patent_count": 30},
|
|
||||||
]
|
|
||||||
|
|
||||||
mock_fail_result = MagicMock(success=False, error="API timeout")
|
|
||||||
mock_ok_result = MagicMock(success=True, patent_count=35)
|
|
||||||
mock_analyzer = MagicMock()
|
|
||||||
mock_analyzer._analyze_company_safe.side_effect = [mock_fail_result, mock_ok_result]
|
|
||||||
|
|
||||||
with patch("SPARC.scheduler.DatabaseClient", return_value=mock_db), \
|
|
||||||
patch("SPARC.scheduler.CompanyAnalyzer", return_value=mock_analyzer):
|
|
||||||
from SPARC.scheduler import run_scheduled_analysis
|
|
||||||
run_scheduled_analysis()
|
|
||||||
|
|
||||||
# FailCo should not get updated, SuccessCo should
|
|
||||||
mock_db.update_tracked_company.assert_called_once_with("SuccessCo", 35)
|
|
||||||
|
|
||||||
def test_scheduler_handles_exception_in_analysis(self):
|
|
||||||
"""Scheduler continues even when analysis raises an exception."""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.list_tracked_companies.return_value = [
|
|
||||||
{"company_name": "CrashCo", "last_patent_count": 10},
|
|
||||||
{"company_name": "OKCo", "last_patent_count": 20},
|
|
||||||
]
|
|
||||||
|
|
||||||
mock_ok_result = MagicMock(success=True, patent_count=22)
|
|
||||||
mock_analyzer = MagicMock()
|
|
||||||
mock_analyzer._analyze_company_safe.side_effect = [
|
|
||||||
RuntimeError("unexpected error"),
|
|
||||||
mock_ok_result,
|
|
||||||
]
|
|
||||||
|
|
||||||
with patch("SPARC.scheduler.DatabaseClient", return_value=mock_db), \
|
|
||||||
patch("SPARC.scheduler.CompanyAnalyzer", return_value=mock_analyzer):
|
|
||||||
from SPARC.scheduler import run_scheduled_analysis
|
|
||||||
run_scheduled_analysis()
|
|
||||||
|
|
||||||
# OKCo should still be processed
|
|
||||||
mock_db.update_tracked_company.assert_called_once_with("OKCo", 22)
|
|
||||||
mock_db.close.assert_called_once()
|
|
||||||
@@ -0,0 +1,280 @@
|
|||||||
|
"""Tests for webhook notification system: retry logic and Slack/Discord payload format.
|
||||||
|
|
||||||
|
Covers issue #1657:
|
||||||
|
- Retry logic with exponential backoff in _send_with_retry
|
||||||
|
- Slack/Discord payload formatting in _build_payload
|
||||||
|
- Generic HTTP POST payload formatting
|
||||||
|
- notify() dispatching to multiple URLs
|
||||||
|
- notify_job_completed() and notify_alert() convenience helpers
|
||||||
|
"""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from unittest.mock import MagicMock, patch, call
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from SPARC.webhooks import (
|
||||||
|
MAX_RETRIES,
|
||||||
|
_build_payload,
|
||||||
|
_is_slack_url,
|
||||||
|
_send_with_retry,
|
||||||
|
notify,
|
||||||
|
notify_alert,
|
||||||
|
notify_job_completed,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestIsSlackUrl:
|
||||||
|
"""Tests for Slack/Discord URL detection."""
|
||||||
|
|
||||||
|
def test_slack_webhook_url(self):
|
||||||
|
assert _is_slack_url("https://hooks.slack.com/services/T00/B00/xxx") is True
|
||||||
|
|
||||||
|
def test_discord_webhook_url(self):
|
||||||
|
assert _is_slack_url("https://discord.com/api/webhooks/123/abc") is True
|
||||||
|
|
||||||
|
def test_generic_url(self):
|
||||||
|
assert _is_slack_url("https://example.com/webhook") is False
|
||||||
|
|
||||||
|
def test_empty_url(self):
|
||||||
|
assert _is_slack_url("") is False
|
||||||
|
|
||||||
|
|
||||||
|
class TestBuildPayload:
|
||||||
|
"""Tests for payload construction."""
|
||||||
|
|
||||||
|
def test_generic_payload_structure(self):
|
||||||
|
"""Generic payload includes event type, timestamp, and data."""
|
||||||
|
payload = _build_payload("job_completed", {"job_id": "abc123"})
|
||||||
|
|
||||||
|
assert payload["event"] == "job_completed"
|
||||||
|
assert payload["job_id"] == "abc123"
|
||||||
|
assert "timestamp" in payload
|
||||||
|
# Timestamp should be ISO format ending with Z
|
||||||
|
assert payload["timestamp"].endswith("Z")
|
||||||
|
|
||||||
|
def test_slack_payload_wraps_in_text(self):
|
||||||
|
"""Slack payload wraps content in a 'text' field."""
|
||||||
|
payload = _build_payload("patent_alert", {"company_name": "NVIDIA"}, slack=True)
|
||||||
|
|
||||||
|
assert "text" in payload
|
||||||
|
assert "patent_alert" in payload["text"]
|
||||||
|
assert "NVIDIA" in payload["text"]
|
||||||
|
# Slack payload should NOT have the event/timestamp at top level
|
||||||
|
assert "event" not in payload
|
||||||
|
assert "timestamp" not in payload
|
||||||
|
|
||||||
|
def test_generic_payload_does_not_have_text_field(self):
|
||||||
|
"""Non-Slack payload does not wrap in text."""
|
||||||
|
payload = _build_payload("job_completed", {"status": "done"})
|
||||||
|
|
||||||
|
assert "text" not in payload
|
||||||
|
assert payload["status"] == "done"
|
||||||
|
|
||||||
|
def test_slack_payload_contains_bold_header(self):
|
||||||
|
"""Slack payload starts with bold event header using Slack markdown."""
|
||||||
|
payload = _build_payload("job_completed", {"count": 5}, slack=True)
|
||||||
|
|
||||||
|
assert payload["text"].startswith("*[SPARC] job_completed*")
|
||||||
|
|
||||||
|
def test_payload_merges_all_data_keys(self):
|
||||||
|
"""All data keys are included in the generic payload."""
|
||||||
|
data = {"key1": "val1", "key2": 42, "key3": True}
|
||||||
|
payload = _build_payload("test_event", data)
|
||||||
|
|
||||||
|
assert payload["key1"] == "val1"
|
||||||
|
assert payload["key2"] == 42
|
||||||
|
assert payload["key3"] is True
|
||||||
|
|
||||||
|
|
||||||
|
class TestSendWithRetry:
|
||||||
|
"""Tests for retry logic in _send_with_retry."""
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_success_on_first_attempt(self, mock_post, mock_sleep):
|
||||||
|
"""Successful delivery on first attempt, no retries."""
|
||||||
|
mock_post.return_value = MagicMock(status_code=200)
|
||||||
|
|
||||||
|
result = _send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
assert result is True
|
||||||
|
mock_post.assert_called_once()
|
||||||
|
mock_sleep.assert_not_called()
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_success_on_second_attempt(self, mock_post, mock_sleep):
|
||||||
|
"""Fails first, succeeds on retry."""
|
||||||
|
mock_post.side_effect = [
|
||||||
|
MagicMock(status_code=500),
|
||||||
|
MagicMock(status_code=200),
|
||||||
|
]
|
||||||
|
|
||||||
|
result = _send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
assert result is True
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
mock_sleep.assert_called_once()
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_all_retries_exhausted(self, mock_post, mock_sleep):
|
||||||
|
"""Returns False after all retries fail."""
|
||||||
|
mock_post.return_value = MagicMock(status_code=500)
|
||||||
|
|
||||||
|
result = _send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
assert result is False
|
||||||
|
assert mock_post.call_count == MAX_RETRIES
|
||||||
|
assert mock_sleep.call_count == MAX_RETRIES - 1
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_exponential_backoff_timing(self, mock_post, mock_sleep):
|
||||||
|
"""Backoff wait times follow exponential pattern (2^attempt)."""
|
||||||
|
mock_post.return_value = MagicMock(status_code=500)
|
||||||
|
|
||||||
|
_send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
# With BACKOFF_BASE=2: attempt 1 -> sleep(2), attempt 2 -> sleep(4)
|
||||||
|
expected_waits = [call(2 ** i) for i in range(1, MAX_RETRIES)]
|
||||||
|
assert mock_sleep.call_args_list == expected_waits
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_network_error_triggers_retry(self, mock_post, mock_sleep):
|
||||||
|
"""Network exceptions trigger retry, not immediate failure."""
|
||||||
|
mock_post.side_effect = [
|
||||||
|
requests.ConnectionError("Connection refused"),
|
||||||
|
MagicMock(status_code=200),
|
||||||
|
]
|
||||||
|
|
||||||
|
result = _send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
assert result is True
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_timeout_error_triggers_retry(self, mock_post, mock_sleep):
|
||||||
|
"""Timeout exceptions trigger retry."""
|
||||||
|
mock_post.side_effect = [
|
||||||
|
requests.Timeout("Request timed out"),
|
||||||
|
MagicMock(status_code=200),
|
||||||
|
]
|
||||||
|
|
||||||
|
result = _send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
assert result is True
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_2xx_status_codes_accepted(self, mock_post, mock_sleep):
|
||||||
|
"""Any 2xx status code is treated as success."""
|
||||||
|
mock_post.return_value = MagicMock(status_code=204)
|
||||||
|
|
||||||
|
result = _send_with_retry("https://example.com/hook", {"event": "test"})
|
||||||
|
|
||||||
|
assert result is True
|
||||||
|
mock_post.assert_called_once()
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.time.sleep")
|
||||||
|
@patch("SPARC.webhooks.requests.post")
|
||||||
|
def test_posts_json_payload(self, mock_post, mock_sleep):
|
||||||
|
"""Payload is sent as JSON with correct timeout."""
|
||||||
|
mock_post.return_value = MagicMock(status_code=200)
|
||||||
|
payload = {"event": "test", "data": "value"}
|
||||||
|
|
||||||
|
_send_with_retry("https://example.com/hook", payload)
|
||||||
|
|
||||||
|
mock_post.assert_called_once_with(
|
||||||
|
"https://example.com/hook", json=payload, timeout=10
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestNotify:
|
||||||
|
"""Tests for the notify() dispatcher."""
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks._send_with_retry")
|
||||||
|
@patch("SPARC.webhooks.WEBHOOK_URLS", ["https://example.com/hook1", "https://example.com/hook2"])
|
||||||
|
def test_dispatches_to_all_urls(self, mock_send):
|
||||||
|
"""notify() sends to every configured webhook URL."""
|
||||||
|
mock_send.return_value = True
|
||||||
|
|
||||||
|
notify("job_completed", {"job_id": "test123"})
|
||||||
|
|
||||||
|
assert mock_send.call_count == 2
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks._send_with_retry")
|
||||||
|
@patch("SPARC.webhooks.WEBHOOK_URLS", [])
|
||||||
|
def test_no_urls_configured_returns_immediately(self, mock_send):
|
||||||
|
"""No-op when no webhook URLs are configured."""
|
||||||
|
notify("job_completed", {"job_id": "test123"})
|
||||||
|
|
||||||
|
mock_send.assert_not_called()
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks._send_with_retry")
|
||||||
|
@patch("SPARC.webhooks.WEBHOOK_URLS", [
|
||||||
|
"https://hooks.slack.com/services/T00/B00/xxx",
|
||||||
|
"https://example.com/generic",
|
||||||
|
])
|
||||||
|
def test_slack_url_gets_slack_payload(self, mock_send):
|
||||||
|
"""Slack URLs receive Slack-formatted payloads, others get generic."""
|
||||||
|
mock_send.return_value = True
|
||||||
|
|
||||||
|
notify("test_event", {"key": "val"})
|
||||||
|
|
||||||
|
# First call (Slack URL) should have "text" key
|
||||||
|
slack_payload = mock_send.call_args_list[0][0][1]
|
||||||
|
assert "text" in slack_payload
|
||||||
|
|
||||||
|
# Second call (generic URL) should have "event" key
|
||||||
|
generic_payload = mock_send.call_args_list[1][0][1]
|
||||||
|
assert "event" in generic_payload
|
||||||
|
assert generic_payload["event"] == "test_event"
|
||||||
|
|
||||||
|
|
||||||
|
class TestNotifyJobCompleted:
|
||||||
|
"""Tests for notify_job_completed() convenience function."""
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.notify")
|
||||||
|
def test_sends_correct_event_and_data(self, mock_notify):
|
||||||
|
"""Job completion sends proper event type and summary."""
|
||||||
|
notify_job_completed(
|
||||||
|
job_id="batch-001",
|
||||||
|
status="completed",
|
||||||
|
total_companies=10,
|
||||||
|
successful=8,
|
||||||
|
failed=2,
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_notify.assert_called_once()
|
||||||
|
event, data = mock_notify.call_args[0]
|
||||||
|
assert event == "job_completed"
|
||||||
|
assert data["job_id"] == "batch-001"
|
||||||
|
assert data["successful"] == 8
|
||||||
|
assert data["failed"] == 2
|
||||||
|
assert "8/10" in data["summary"]
|
||||||
|
|
||||||
|
|
||||||
|
class TestNotifyAlert:
|
||||||
|
"""Tests for notify_alert() convenience function."""
|
||||||
|
|
||||||
|
@patch("SPARC.webhooks.notify")
|
||||||
|
def test_sends_correct_event_and_data(self, mock_notify):
|
||||||
|
"""Alert notification sends patent_alert event type."""
|
||||||
|
notify_alert(
|
||||||
|
company_name="NVIDIA",
|
||||||
|
alert_type="patent_count_change",
|
||||||
|
message="Patent count increased by 30%",
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_notify.assert_called_once()
|
||||||
|
event, data = mock_notify.call_args[0]
|
||||||
|
assert event == "patent_alert"
|
||||||
|
assert data["company_name"] == "NVIDIA"
|
||||||
|
assert data["alert_type"] == "patent_count_change"
|
||||||
|
assert "30%" in data["message"]
|
||||||
Reference in New Issue
Block a user