Files
Evection/apps/feeds/test_opml.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

168 lines
5.8 KiB
Python

from io import BytesIO
import pytest
from django.contrib.auth import get_user_model
from apps.articles.models import SubscriptionTag, Tag
from .models import Feed, Subscription
from .opml import export_opml, import_opml, parse_opml
User = get_user_model()
SIMPLE_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head><title>Subscriptions</title></head>
<body>
<outline text="Tech">
<outline type="rss" text="Example Blog" title="Example Blog"
xmlUrl="https://example.com/feed.xml" htmlUrl="https://example.com"/>
</outline>
<outline type="rss" text="Untagged Feed" title="Untagged Feed"
xmlUrl="https://untagged.example.com/feed.xml"/>
</body>
</opml>
"""
NESTED_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<body>
<outline text="Parent">
<outline text="Child">
<outline type="rss" text="Deeply nested feed"
xmlUrl="https://nested.example.com/feed.xml"/>
</outline>
</outline>
</body>
</opml>
"""
CATEGORY_ATTR_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<body>
<outline type="rss" text="Multi-tag feed" category="Tech,Backend"
xmlUrl="https://multi.example.com/feed.xml"/>
</body>
</opml>
"""
MALFORMED_XML = b"<opml><body><outline"
@pytest.fixture
def alice(db):
return User.objects.create_user("alice", password="pw-alice-123")
class TestParseOPML:
def test_parses_tagged_and_untagged_entries(self):
entries = parse_opml(BytesIO(SIMPLE_OPML))
assert len(entries) == 2
tagged = next(e for e in entries if e["feed_url"] == "https://example.com/feed.xml")
assert tagged["tag_names"] == ["Tech"]
assert tagged["title"] == "Example Blog"
untagged = next(
e for e in entries if e["feed_url"] == "https://untagged.example.com/feed.xml"
)
assert untagged["tag_names"] == []
def test_nesting_produces_a_tag_per_level(self):
entries = parse_opml(BytesIO(NESTED_OPML))
assert len(entries) == 1
# Unlike the old single-folder scheme, each enclosing level becomes
# its own tag rather than collapsing to the nearest one.
assert entries[0]["tag_names"] == ["Parent", "Child"]
def test_category_attribute_produces_multiple_tags(self):
entries = parse_opml(BytesIO(CATEGORY_ATTR_OPML))
assert len(entries) == 1
assert entries[0]["tag_names"] == ["Tech", "Backend"]
def test_malformed_xml_raises(self):
with pytest.raises(Exception):
parse_opml(BytesIO(MALFORMED_XML))
@pytest.mark.django_db
class TestImportOPML:
def test_creates_feeds_tags_and_subscriptions(self, alice):
result = import_opml(alice, BytesIO(SIMPLE_OPML))
assert result == {"total": 2, "created": 2, "already_subscribed": 0}
assert Tag.objects.filter(user=alice, name="Tech").exists()
tagged_sub = Subscription.objects.get(
user=alice, feed__feed_url="https://example.com/feed.xml"
)
assert SubscriptionTag.objects.filter(
user=alice, subscription=tagged_sub, tag__name="Tech"
).exists()
untagged_sub = Subscription.objects.get(
user=alice, feed__feed_url="https://untagged.example.com/feed.xml"
)
assert not SubscriptionTag.objects.filter(user=alice, subscription=untagged_sub).exists()
def test_reimporting_the_same_file_does_not_duplicate(self, alice):
import_opml(alice, BytesIO(SIMPLE_OPML))
result = import_opml(alice, BytesIO(SIMPLE_OPML))
assert result == {"total": 2, "created": 0, "already_subscribed": 2}
assert Subscription.objects.filter(user=alice).count() == 2
assert Tag.objects.filter(user=alice, name="Tech").count() == 1
def test_reuses_existing_feed_across_users(self, alice):
bob = User.objects.create_user("bob", password="pw-bob-123")
import_opml(alice, BytesIO(SIMPLE_OPML))
import_opml(bob, BytesIO(SIMPLE_OPML))
assert Feed.objects.filter(feed_url="https://example.com/feed.xml").count() == 1
assert (
Subscription.objects.filter(feed__feed_url="https://example.com/feed.xml").count() == 2
)
@pytest.mark.django_db
class TestExportOPML:
def test_export_round_trips_through_import(self, alice):
import_opml(alice, BytesIO(SIMPLE_OPML))
exported = export_opml(alice)
assert b'category="Tech"' in exported
bob = User.objects.create_user("bob", password="pw-bob-123")
result = import_opml(bob, BytesIO(exported))
assert result["created"] == 2
tagged_sub = Subscription.objects.get(
user=bob, feed__feed_url="https://example.com/feed.xml"
)
assert SubscriptionTag.objects.filter(
user=bob, subscription=tagged_sub, tag__name="Tech"
).exists()
untagged_sub = Subscription.objects.get(
user=bob, feed__feed_url="https://untagged.example.com/feed.xml"
)
assert not SubscriptionTag.objects.filter(user=bob, subscription=untagged_sub).exists()
def test_export_represents_multiple_tags_as_comma_separated_category(self, alice):
feed = Feed.objects.create(feed_url="https://multi.example.com/feed.xml")
subscription = Subscription.objects.create(user=alice, feed=feed)
for name in ["Tech", "Backend"]:
tag, _ = Tag.objects.get_or_create(user=alice, name=name)
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=tag)
exported = export_opml(alice)
# Alphabetical (export_opml orders by tag name), not insertion order.
assert b'category="Backend,Tech"' in exported
def test_export_with_no_subscriptions(self, alice):
exported = export_opml(alice)
assert b"<opml" in exported
assert b"<outline" not in exported