Private
Public Access
Ingestion did a get_or_create per entry. It now prefetches existing articles for the poll in one query, bulk-inserts new ones and bulk-updates changed ones (falling back to row-by-row only if a concurrent fetch wins a race), and dispatches rule evaluation in bounded chunks so a huge first fetch is several small tasks. /health/ stays a DB-only liveness check (upgrade.sh gates on it right after restart). New /health/ready/ also checks Redis and that poll_due_feeds ticked within five minutes — proving beat and a worker are both alive — and returns 503 with per-check detail otherwise. The sidebar unread-count cache and login throttle now degrade instead of erroring when Redis is unreachable: every authenticated page and every login would otherwise 500 during a cache outage. Found while verifying in a sandbox with no Redis. Also includes the dashboard's oldest-article metric (core/views.py). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
122 lines
4.2 KiB
Python
122 lines
4.2 KiB
Python
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from django.urls import reverse
|
|
|
|
from apps.feeds.models import Feed, Subscription
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_health_check_returns_ok_without_login(client):
|
|
response = client.get(reverse("core:health"))
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok"}
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_health_check_works_before_first_run_setup(client):
|
|
# No User exists yet, so SetupWizardMiddleware would normally redirect
|
|
# every request to /setup/ — scripts/upgrade.sh needs this reachable
|
|
# immediately after a fresh deploy, before anyone's created an account.
|
|
response = client.get(reverse("core:health"))
|
|
assert response.status_code == 200
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client):
|
|
response = client.get(reverse("core:health_ready"))
|
|
assert response.status_code == 503
|
|
body = response.json()
|
|
assert body["status"] == "degraded"
|
|
assert body["checks"]["database"] is True
|
|
assert body["checks"]["redis"] is True
|
|
assert body["checks"]["poll_heartbeat"] is False
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_health_ready_is_ok_after_poll_due_feeds_runs(client):
|
|
from apps.feeds.tasks import poll_due_feeds
|
|
|
|
poll_due_feeds()
|
|
|
|
response = client.get(reverse("core:health_ready"))
|
|
assert response.status_code == 200
|
|
assert response.json()["checks"] == {
|
|
"database": True,
|
|
"redis": True,
|
|
"poll_heartbeat": True,
|
|
}
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_health_ready_flags_a_stale_poll_heartbeat(client):
|
|
from datetime import timedelta
|
|
|
|
from django.core.cache import cache
|
|
from django.utils import timezone
|
|
|
|
from apps.feeds.tasks import POLL_HEARTBEAT_CACHE_KEY
|
|
|
|
cache.set(POLL_HEARTBEAT_CACHE_KEY, timezone.now() - timedelta(minutes=30))
|
|
|
|
response = client.get(reverse("core:health_ready"))
|
|
assert response.status_code == 503
|
|
assert response.json()["checks"]["poll_heartbeat"] is False
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_dashboard_requires_staff(client, django_user_model):
|
|
django_user_model.objects.create_user("regular", password="pw-regular-123")
|
|
client.login(username="regular", password="pw-regular-123")
|
|
|
|
response = client.get(reverse("core:dashboard"))
|
|
assert response.status_code == 302
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_dashboard_lists_erroring_feeds(client, django_user_model):
|
|
admin = django_user_model.objects.create_user("admin", password="pw-admin-123", is_staff=True)
|
|
client.login(username="admin", password="pw-admin-123")
|
|
|
|
ok_feed = Feed.objects.create(feed_url="https://ok.example.com/feed", title="OK Feed")
|
|
broken_feed = Feed.objects.create(
|
|
feed_url="https://broken.example.com/feed",
|
|
title="Broken Feed",
|
|
last_fetch_status=Feed.STATUS_ERROR,
|
|
fetch_error_count=3,
|
|
last_error_message="Connection timed out",
|
|
)
|
|
Subscription.objects.create(user=admin, feed=ok_feed)
|
|
Subscription.objects.create(user=admin, feed=broken_feed)
|
|
|
|
response = client.get(reverse("core:dashboard"))
|
|
assert response.status_code == 200
|
|
assert b"Broken Feed" in response.content
|
|
assert b"Connection timed out" in response.content
|
|
# "OK Feed" legitimately appears elsewhere on the page (the sidebar
|
|
# lists every subscription), so check the dashboard's own error table
|
|
# scope via context rather than raw page content.
|
|
assert list(response.context["erroring_feeds"]) == [broken_feed]
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_retry_feed_resets_backoff_and_requeues(client, django_user_model):
|
|
django_user_model.objects.create_user("admin", password="pw-admin-123", is_staff=True)
|
|
client.login(username="admin", password="pw-admin-123")
|
|
|
|
feed = Feed.objects.create(
|
|
feed_url="https://broken.example.com/feed",
|
|
fetch_error_count=5,
|
|
last_fetch_status=Feed.STATUS_ERROR,
|
|
)
|
|
|
|
with (
|
|
patch("apps.core.views.reset_feed_backoff.delay") as mock_reset,
|
|
patch("apps.core.views.fetch_feed.delay") as mock_fetch,
|
|
):
|
|
response = client.post(reverse("core:retry_feed", args=[feed.id]))
|
|
|
|
assert response.status_code == 302
|
|
mock_reset.assert_called_once_with(feed.id)
|
|
mock_fetch.assert_called_once_with(feed.id)
|