Files
Evection/apps/feeds/tests.py
T
ksmithandClaude Sonnet 5 0e132e9ab9 Remove folders; feeds are organized by tags now
Folders are gone — organization is tags-only, via the SubscriptionTag
model added last commit. A migration converts every existing Folder into
a same-named Tag applied to its feeds, and every folder-scoped FilterRule
into an equivalent tag-scoped one, before the Folder table is dropped, so
upgrading an existing instance loses nothing (verified by seeding the
pre-removal schema directly and running the migration chain against it).

Unlike folders, a feed can carry more than one tag, so:
- apps.articles.tasks.apply_filter_rules_for_feed and apps.rules.tasks's
  backlog re-apply now scope by "does this feed carry tag X" rather than
  "does this feed belong to folder X".
- apps.feeds.opml: a feed's tags export as a comma-separated "category"
  attribute (the standard OPML way to represent multiple categories)
  instead of nesting under one folder outline. Import reads both that
  attribute and nested category outlines, turning each enclosing level
  into its own tag rather than collapsing to the nearest one.
- The sidebar groups feeds by tag instead of folder, so a feed with two
  tags now appears in both groups; untagged feeds fall into "Untagged".

AddFeedForm gained a comma-separated tag_names field for parity with the
old add-time folder picker. EditSubscriptionForm dropped its folder
field entirely — tagging a subscription happens through the tag editor
already on that page.

Verified end to end: the migration chain from empty, the data migration
against a seeded pre-removal schema (folder/subscription/rule all convert
correctly), and the running app's sidebar grouping, OPML export category
attributes, and tag editor.

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

115 lines
3.7 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_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_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