import zipfile 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, extract_opml_bytes, import_opml, parse_opml User = get_user_model() SIMPLE_OPML = b""" Subscriptions """ NESTED_OPML = b""" """ CATEGORY_ATTR_OPML = b""" """ MALFORMED_XML = b" bytes: buf = BytesIO() with zipfile.ZipFile(buf, "w") as archive: archive.writestr(filename, content) return buf.getvalue() class TestExtractOPMLBytes: def test_bare_opml_is_returned_unchanged(self): assert extract_opml_bytes(SIMPLE_OPML) == SIMPLE_OPML def test_extracts_opml_from_a_freshrss_style_export_zip(self): # FreshRSS's own "Export" produces exactly this: an .opml.xml plus # a starred-articles .json we don't need for feed import. zipped = _zip_of("feeds_2026-10-30.opml.xml", SIMPLE_OPML) with zipfile.ZipFile(BytesIO(zipped), "a") as archive: archive.writestr("starred_2026-10-30.json", b'{"items": []}') assert extract_opml_bytes(zipped) == SIMPLE_OPML def test_zip_with_no_opml_member_raises(self): zipped = _zip_of("starred_2026-10-30.json", b'{"items": []}') with pytest.raises(ValueError): extract_opml_bytes(zipped) 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" """ entries = parse_opml(BytesIO(xml)) assert [e["feed_url"] for e in entries] == ["https://example.com/feed.xml"] assert entries[0]["site_url"] == ""