Files
Evection/apps/feeds/tests.py
T
ksmithandClaude Sonnet 5 d200ab80f7 Add core data models and the feed fetch pipeline
Implements Folder/Feed/Subscription (feeds app) and Article/
UserArticleState/Tag/ArticleTag (articles app), preserving the
shared-Feed/shared-Article dedup design: a feed is fetched once
regardless of subscriber count, and per-user read/star/save/hide state
is created lazily rather than eagerly per subscriber.

Adds the fetch pipeline: feedparser-based parsing with conditional GET
(ETag/Last-Modified), nh3 HTML sanitization, guid-based dedup with
content-hash change detection that never resets a user's read/star
state, and exponential backoff on fetch errors respecting a configured
minimum interval floor. Wired into Celery via poll_due_feeds/fetch_feed
tasks, plus a minimal "add feed by URL" view that fetches synchronously
so new subscribers see results immediately.

Verified live against a real feed (hnrss.org) for ingestion, 304
dedup, and error backoff, in addition to the unit/integration test
suite (28 tests, all passing).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 16:03:20 -06:00

101 lines
3.1 KiB
Python

import pytest
import responses
from django.contrib.auth import get_user_model
from django.urls import reverse
from .models import Feed, Folder, Subscription
User = get_user_model()
@pytest.fixture
def two_users(db):
alice = User.objects.create_user("alice", password="pw-alice-123")
bob = User.objects.create_user("bob", password="pw-bob-123")
return alice, bob
@pytest.mark.django_db
def test_subscribing_to_same_feed_url_reuses_one_feed_row(two_users):
alice, bob = two_users
feed, created = Feed.objects.get_or_create(feed_url="https://example.com/feed.xml")
assert created
Subscription.objects.create(user=alice, feed=feed)
feed_again, created_again = Feed.objects.get_or_create(feed_url="https://example.com/feed.xml")
assert not created_again
assert feed_again.pk == feed.pk
Subscription.objects.create(user=bob, feed=feed_again)
assert Feed.objects.count() == 1
assert Subscription.objects.filter(feed=feed).count() == 2
@pytest.mark.django_db
def test_a_user_cannot_subscribe_to_the_same_feed_twice(two_users):
alice, _ = two_users
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
with pytest.raises(Exception):
Subscription.objects.create(user=alice, feed=feed)
@pytest.mark.django_db
def test_folder_names_are_scoped_per_user(two_users):
alice, bob = two_users
Folder.objects.create(user=alice, name="Tech")
# Same folder name for a different user must be allowed.
Folder.objects.create(user=bob, name="Tech")
assert Folder.objects.filter(name="Tech").count() == 2
with pytest.raises(Exception):
Folder.objects.create(user=alice, name="Tech")
GOOD_FEED_XML = """<?xml version="1.0"?>
<rss version="2.0"><channel>
<title>Example Blog</title>
<item><title>First post</title><guid>guid-1</guid></item>
</channel></rss>
"""
@pytest.mark.django_db
@responses.activate
def test_add_feed_view_subscribes_and_fetches_immediately(client, two_users):
alice, _ = two_users
client.login(username="alice", password="pw-alice-123")
responses.add(responses.GET, "https://example.com/feed.xml", body=GOOD_FEED_XML, status=200)
response = client.post(reverse("feeds:add_feed"), {"feed_url": "https://example.com/feed.xml"})
assert response.status_code == 302
assert Subscription.objects.filter(
user=alice, feed__feed_url="https://example.com/feed.xml"
).exists()
@pytest.mark.django_db
def test_add_feed_view_requires_login(client):
response = client.get(reverse("feeds:add_feed"))
assert response.status_code == 302
@pytest.mark.django_db
@responses.activate
def test_add_feed_view_rejects_duplicate_subscription(client, two_users):
alice, _ = two_users
client.login(username="alice", password="pw-alice-123")
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
response = client.post(reverse("feeds:add_feed"), {"feed_url": "https://example.com/feed.xml"})
assert response.status_code == 302
assert Subscription.objects.filter(user=alice, feed=feed).count() == 1