Files
Evection/apps/feeds/test_opml.py
T
ksmithandClaude Sonnet 5 ea214dd9ce Add OPML import/export and a subscriptions management page
Implements OPML parsing with defusedxml (untrusted upload, XXE risk)
and export with the stdlib's plain ElementTree (trusted, app-generated
output). Nesting deeper than one level flattens into its nearest
enclosing folder, matching the app's flat-folder model. Import reuses
existing Feed/Subscription rows via get_or_create, so re-importing the
same file is idempotent and cross-user Feed dedup is preserved.

Large imports (>50 entries) dispatch to a Celery task instead of
blocking the request; newly imported feeds aren't fetched immediately
since they have no last_fetched_at, so the next poll_due_feeds beat
cycle picks them up naturally.

Adds a Subscriptions page (replacing the standalone "Add feed" link in
nav) listing current subscriptions with unsubscribe, plus Import/
Export OPML actions.

Verified live: imported a 2-feed/1-folder OPML file against the
running dev server (via an authenticated session, since this browser
tool has no file-picker automation), confirmed the resulting
subscriptions and folder in the UI, exported and inspected the
resulting OPML structure, and unsubscribed from one feed.

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

129 lines
4.4 KiB
Python

from io import BytesIO
import pytest
from django.contrib.auth import get_user_model
from .models import Feed, Folder, 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="Unfoldered Feed" title="Unfoldered Feed"
xmlUrl="https://unfoldered.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>
"""
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_foldered_and_unfoldered_entries(self):
entries = parse_opml(BytesIO(SIMPLE_OPML))
assert len(entries) == 2
foldered = next(e for e in entries if e["feed_url"] == "https://example.com/feed.xml")
assert foldered["folder_name"] == "Tech"
assert foldered["title"] == "Example Blog"
unfoldered = next(
e for e in entries if e["feed_url"] == "https://unfoldered.example.com/feed.xml"
)
assert unfoldered["folder_name"] is None
def test_flattens_nesting_deeper_than_one_level(self):
entries = parse_opml(BytesIO(NESTED_OPML))
assert len(entries) == 1
# Deepest enclosing folder name wins rather than a path.
assert entries[0]["folder_name"] == "Child"
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_folders_and_subscriptions(self, alice):
result = import_opml(alice, BytesIO(SIMPLE_OPML))
assert result == {"total": 2, "created": 2, "already_subscribed": 0}
assert Folder.objects.filter(user=alice, name="Tech").exists()
assert Subscription.objects.filter(
user=alice, feed__feed_url="https://example.com/feed.xml"
).exists()
unfoldered_sub = Subscription.objects.get(
user=alice, feed__feed_url="https://unfoldered.example.com/feed.xml"
)
assert unfoldered_sub.folder is None
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 Folder.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)
bob = User.objects.create_user("bob", password="pw-bob-123")
result = import_opml(bob, BytesIO(exported))
assert result["created"] == 2
assert Folder.objects.filter(user=bob, name="Tech").exists()
assert Subscription.objects.filter(
user=bob, feed__feed_url="https://example.com/feed.xml", folder__name="Tech"
).exists()
assert Subscription.objects.filter(
user=bob, feed__feed_url="https://unfoldered.example.com/feed.xml", folder__isnull=True
).exists()
def test_export_with_no_subscriptions(self, alice):
exported = export_opml(alice)
assert b"<opml" in exported
assert b"<outline" not in exported