Private
Public Access
add_feed ran fetch_and_ingest synchronously during the POST request, so a slow or large feed tied up a Gunicorn worker for the duration of the fetch (up to the 15s HTTP timeout, plus parse/ingest time) and a user could repeat it with distinct URLs to reduce request capacity. The subscription is now created synchronously as before, but the first fetch is dispatched via fetch_feed.delay() — the same task the periodic poll uses, so the save/error bookkeeping is unchanged. The user sees an immediate redirect with a message that the fetch is in progress rather than waiting on it in the request/response cycle. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
import pytest
|
|
import responses
|
|
from django.contrib.auth import get_user_model
|
|
from django.urls import reverse
|
|
|
|
from .models import Feed, 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)
|
|
|
|
|
|
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_enqueues_the_initial_fetch(client, two_users):
|
|
"""The subscription is created synchronously; the first fetch is
|
|
dispatched via fetch_feed.delay() (T3) rather than run inline — in
|
|
tests CELERY_TASK_ALWAYS_EAGER still runs it immediately, so the
|
|
article it ingests is visible by the time the response returns."""
|
|
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
|
|
subscription = Subscription.objects.get(
|
|
user=alice, feed__feed_url="https://example.com/feed.xml"
|
|
)
|
|
assert subscription.feed.articles.filter(guid="guid-1").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_creates_tags_from_comma_separated_tag_names(client, two_users):
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
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", "tag_names": "Tech, backend"},
|
|
)
|
|
|
|
assert response.status_code == 302
|
|
subscription = Subscription.objects.get(
|
|
user=alice, feed__feed_url="https://example.com/feed.xml"
|
|
)
|
|
tag_names = set(
|
|
Tag.objects.filter(
|
|
subscription_tags__subscription=subscription, subscription_tags__user=alice
|
|
).values_list("name", flat=True)
|
|
)
|
|
assert tag_names == {"Tech", "backend"}
|
|
assert SubscriptionTag.objects.filter(user=alice, subscription=subscription).count() == 2
|
|
|
|
|
|
@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
|