Private
Public Access
An OPML upload was read fully into memory, parsed into a whole tree, walked recursively, and (for large imports) embedded in a Celery message. Nothing bounded raw size, ZIP entries, nesting depth, outline count or feed count. Uploads are capped at 5 MB (checked on the reported size before reading) and ZIPs at 50 entries, with the decompressed member bounded by what it actually yields rather than only its header. Parsing now streams with iterparse and aborts the moment a limit is crossed — 24 levels deep, 20,000 outlines, 5,000 feeds — so a hostile file never builds a huge or deep tree; a 50,000-level nesting bomb is rejected cleanly. Limit errors are shown to the user. Large imports go to the worker by cache reference (deleted once read, expires after an hour) instead of as the whole document inside the broker message. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
260 lines
9.1 KiB
Python
260 lines
9.1 KiB
Python
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_edit_subscription_updates_title(client, alice, alices_subscription):
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
response = client.post(
|
|
reverse("feeds:edit_subscription", args=[alices_subscription.pk]),
|
|
{"custom_title": "My Example Feed"},
|
|
)
|
|
|
|
assert response.status_code == 302
|
|
alices_subscription.refresh_from_db()
|
|
assert alices_subscription.custom_title == "My Example Feed"
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_user_cannot_edit_another_users_subscription(client, alice, bob, alices_subscription):
|
|
client.login(username="bob", password="pw-bob-123")
|
|
response = client.get(reverse("feeds:edit_subscription", args=[alices_subscription.pk]))
|
|
assert response.status_code == 404
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_add_tag_to_subscription_creates_tag_and_link(client, alice, alices_subscription):
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
response = client.post(
|
|
reverse("feeds:add_tag_to_subscription", args=[alices_subscription.pk]),
|
|
{"tag_name": "backend"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
tag = Tag.objects.get(user=alice, name="backend")
|
|
assert SubscriptionTag.objects.filter(
|
|
user=alice, subscription=alices_subscription, tag=tag
|
|
).exists()
|
|
assert b"backend" in response.content
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_remove_tag_from_subscription(client, alice, alices_subscription):
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
tag = Tag.objects.create(user=alice, name="backend")
|
|
SubscriptionTag.objects.create(user=alice, subscription=alices_subscription, tag=tag)
|
|
|
|
response = client.post(
|
|
reverse("feeds:remove_tag_from_subscription", args=[alices_subscription.pk, tag.pk])
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert not SubscriptionTag.objects.filter(
|
|
user=alice, subscription=alices_subscription, tag=tag
|
|
).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_user_cannot_tag_another_users_subscription(client, alice, bob, alices_subscription):
|
|
client.login(username="bob", password="pw-bob-123")
|
|
response = client.post(
|
|
reverse("feeds:add_tag_to_subscription", args=[alices_subscription.pk]),
|
|
{"tag_name": "backend"},
|
|
)
|
|
assert response.status_code == 404
|
|
|
|
|
|
@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_accepts_a_freshrss_export_zip(client, alice):
|
|
import zipfile
|
|
from io import BytesIO
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
buf = BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as archive:
|
|
archive.writestr("feeds_2026-10-30.opml.xml", SIMPLE_OPML)
|
|
archive.writestr("starred_2026-10-30.json", b'{"items": []}')
|
|
upload = SimpleUploadedFile(
|
|
"freshrss_export.zip", buf.getvalue(), content_type="application/zip"
|
|
)
|
|
|
|
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, upload_key):
|
|
dispatched["user_id"] = user_id
|
|
dispatched["upload_key"] = upload_key
|
|
|
|
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 task gets a reference to the document, not the document itself.
|
|
from django.core.cache import cache
|
|
|
|
assert dispatched["upload_key"].startswith("opml-import:")
|
|
assert cache.get(dispatched["upload_key"]) == SIMPLE_OPML
|
|
# 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 django.core.cache import cache
|
|
|
|
from .tasks import import_opml_task
|
|
|
|
cache.set("opml-import:test", SIMPLE_OPML, timeout=60)
|
|
|
|
result = import_opml_task(alice.id, "opml-import:test")
|
|
|
|
assert result["created"] == 2
|
|
assert Subscription.objects.filter(user=alice).count() == 2
|
|
assert cache.get("opml-import:test") is None, "the stored upload is removed once read"
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_import_opml_task_does_nothing_if_the_upload_expired(alice):
|
|
from .tasks import import_opml_task
|
|
|
|
assert import_opml_task(alice.id, "opml-import:gone") is None
|
|
assert not Subscription.objects.filter(user=alice).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_import_opml_rejects_an_oversized_upload(client, alice, monkeypatch):
|
|
from . import forms
|
|
|
|
monkeypatch.setattr(forms, "MAX_UPLOAD_BYTES", 100)
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
upload = SimpleUploadedFile("big.opml", SIMPLE_OPML, content_type="text/x-opml")
|
|
response = client.post(reverse("feeds:import_opml"), {"opml_file": upload})
|
|
|
|
assert response.status_code == 200
|
|
assert "too large" in response.content.decode()
|
|
assert not Subscription.objects.filter(user=alice).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_import_opml_reports_a_limit_error_instead_of_a_generic_one(client, alice, monkeypatch):
|
|
from . import opml
|
|
|
|
monkeypatch.setattr(opml, "MAX_OPML_FEEDS", 1)
|
|
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}, follow=True)
|
|
|
|
assert "too many feeds" in response.content.decode()
|
|
assert not Subscription.objects.filter(user=alice).exists()
|