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>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 16:41:18 -06:00
1 parent 8653dbeaf9
commit ea214dd9ce
11 files changed
+533 -3

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
import pytest
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from .models import Feed, Subscription
from .test_opml import SIMPLE_OPML
User = get_user_model()
@pytest.fixture
def alice(db):
return User.objects.create_user("alice", password="pw-alice-123")
@pytest.fixture
def bob(db):
return User.objects.create_user("bob", password="pw-bob-123")
@pytest.fixture
def alices_subscription(alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example")
return Subscription.objects.create(user=alice, feed=feed)
@pytest.mark.django_db
def test_subscription_list_requires_login(client):
response = client.get(reverse("feeds:subscription_list"))
assert response.status_code == 302
@pytest.mark.django_db
def test_subscription_list_shows_only_own_subscriptions(client, alice, bob, alices_subscription):
client.login(username="bob", password="pw-bob-123")
response = client.get(reverse("feeds:subscription_list"))
assert "Example" not in response.content.decode()
@pytest.mark.django_db
def test_unsubscribe(client, alice, alices_subscription):
client.login(username="alice", password="pw-alice-123")
response = client.post(reverse("feeds:unsubscribe", args=[alices_subscription.pk]))
assert response.status_code == 302
assert not Subscription.objects.filter(pk=alices_subscription.pk).exists()
@pytest.mark.django_db
def test_user_cannot_unsubscribe_another_users_subscription(
client, alice, bob, alices_subscription
):
client.login(username="bob", password="pw-bob-123")
response = client.post(reverse("feeds:unsubscribe", args=[alices_subscription.pk]))
assert response.status_code == 404
assert Subscription.objects.filter(pk=alices_subscription.pk).exists()
@pytest.mark.django_db
def test_import_opml_view_creates_subscriptions(client, alice):
client.login(username="alice", password="pw-alice-123")
upload = SimpleUploadedFile("subs.opml", SIMPLE_OPML, content_type="text/x-opml")
response = client.post(reverse("feeds:import_opml"), {"opml_file": upload})
assert response.status_code == 302
assert Subscription.objects.filter(user=alice).count() == 2
@pytest.mark.django_db
def test_import_opml_view_rejects_malformed_file(client, alice):
client.login(username="alice", password="pw-alice-123")
upload = SimpleUploadedFile("subs.opml", b"not xml at all", content_type="text/x-opml")
response = client.post(reverse("feeds:import_opml"), {"opml_file": upload})
assert response.status_code == 302
assert not Subscription.objects.filter(user=alice).exists()
@pytest.mark.django_db
def test_import_opml_dispatches_background_task_above_threshold(client, alice, monkeypatch):
client.login(username="alice", password="pw-alice-123")
dispatched = {}
def fake_delay(user_id, content):
dispatched["user_id"] = user_id
dispatched["content"] = content
from . import views as feeds_views
monkeypatch.setattr(feeds_views.import_opml_task, "delay", fake_delay)
monkeypatch.setattr(feeds_views, "OPML_ASYNC_THRESHOLD", 1)
upload = SimpleUploadedFile("subs.opml", SIMPLE_OPML, content_type="text/x-opml")
response = client.post(reverse("feeds:import_opml"), {"opml_file": upload})
assert response.status_code == 302
assert dispatched["user_id"] == alice.id
# The background task was dispatched instead of a synchronous import.
assert not Subscription.objects.filter(user=alice).exists()
@pytest.mark.django_db
def test_export_opml_view_returns_downloadable_file(client, alice, alices_subscription):
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("feeds:export_opml"))
assert response.status_code == 200
assert response["Content-Type"] == "text/x-opml"
assert "attachment" in response["Content-Disposition"]
assert b"example.com/feed.xml" in response.content
@pytest.mark.django_db
def test_import_opml_task_runs_import(alice):
from .tasks import import_opml_task
result = import_opml_task(alice.id, SIMPLE_OPML.decode("utf-8"))
assert result["created"] == 2
assert Subscription.objects.filter(user=alice).count() == 2