Files
Evection/apps/reader/tests.py
T
ksmithandClaude Sonnet 5 753ae9efc7 Make Starred and Saved-for-later pre-configured system tags
UserArticleState.is_starred/is_saved_for_later/starred_at are gone —
starring and saving now add/remove an ArticleTag against a per-user
"Starred" / "Saved for later" Tag, created lazily on first use and marked
Tag.is_system=True. is_read/is_hidden stay on UserArticleState since
they're genuine reading state, not categorization, which is what made
these two candidates for unifying into the general tag mechanism.

System tags are excluded from the Manage Tags page and from delete_tag
(get_object_or_404 now requires is_system=False, so attempting to delete
one 404s) and from the subscription-tag autocomplete, since they
categorize articles, not feeds. The star/save icons are still exactly
where they were — this only changes what powers them.

apps.rules.engine's star action now does get_system_tag + ArticleTag.
get_or_create instead of setting a boolean field. A data migration
converts every existing is_starred=True/is_saved_for_later=True row into
its ArticleTag equivalent before the fields are dropped — verified by
seeding the pre-removal schema directly (including a row with both flags
set) and confirming the migration produces the right tags.

apps.articles.queries.with_user_state gained one more query (tag lookup
for the current page's articles) to stay in the same "fixed number of
queries regardless of article count" shape it already had.

Verified end to end against a running dev server: toggling star/saved,
the starred/saved filters, the sidebar's Favourites count, and that the
Starred tag doesn't appear on the Manage Tags page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 21:36:44 -06:00

167 lines
6.2 KiB
Python

import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
from apps.articles.models import STARRED_TAG_NAME, Article, ArticleTag, UserArticleState
from apps.feeds.models import Feed, Subscription
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_article(alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example")
Subscription.objects.create(user=alice, feed=feed)
return Article.objects.create(feed=feed, guid="guid-1", title="Hello world")
@pytest.mark.django_db
def test_stream_requires_login(client):
response = client.get(reverse("reader:stream"))
assert response.status_code == 302
@pytest.mark.django_db
def test_stream_shows_only_own_subscriptions(client, alice, bob, alices_article):
client.login(username="bob", password="pw-bob-123")
response = client.get(reverse("reader:stream"))
assert alices_article.title not in response.content.decode()
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
assert alices_article.title in response.content.decode()
@pytest.mark.django_db
def test_unread_filter_excludes_read_articles(client, alice, alices_article):
UserArticleState.objects.create(user=alice, article=alices_article, is_read=True)
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"), {"filter": "unread"})
assert alices_article.title not in response.content.decode()
response = client.get(reverse("reader:stream"), {"filter": "all"})
assert alices_article.title in response.content.decode()
@pytest.mark.django_db
def test_hidden_articles_are_excluded_from_every_filter(client, alice, alices_article):
UserArticleState.objects.create(user=alice, article=alices_article, is_hidden=True)
client.login(username="alice", password="pw-alice-123")
for active_filter in ["unread", "all"]:
response = client.get(reverse("reader:stream"), {"filter": active_filter})
assert alices_article.title not in response.content.decode()
@pytest.mark.django_db
def test_viewing_article_detail_marks_it_read(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
assert not UserArticleState.objects.filter(user=alice, article=alices_article).exists()
response = client.get(reverse("reader:article_detail", args=[alices_article.pk]))
assert response.status_code == 200
state = UserArticleState.objects.get(user=alice, article=alices_article)
assert state.is_read is True
assert state.read_at is not None
@pytest.mark.django_db
def test_user_cannot_view_another_users_article(client, alice, bob, alices_article):
client.login(username="bob", password="pw-bob-123")
response = client.get(reverse("reader:article_detail", args=[alices_article.pk]))
assert response.status_code == 404
@pytest.mark.django_db
def test_toggle_star_flips_state_and_persists(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
url = reverse("reader:toggle_star", args=[alices_article.pk])
response = client.post(url)
assert response.status_code == 200
assert ArticleTag.objects.filter(
user=alice,
article=alices_article,
tag__is_system=True,
tag__name=STARRED_TAG_NAME,
).exists()
client.post(url)
assert not ArticleTag.objects.filter(
user=alice,
article=alices_article,
tag__is_system=True,
tag__name=STARRED_TAG_NAME,
).exists()
@pytest.mark.django_db
def test_toggle_read_requires_post(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:toggle_read", args=[alices_article.pk]))
assert response.status_code == 405
@pytest.mark.django_db
def test_user_cannot_toggle_state_on_another_users_article(client, alice, bob, alices_article):
client.login(username="bob", password="pw-bob-123")
response = client.post(reverse("reader:toggle_star", args=[alices_article.pk]))
assert response.status_code == 404
assert not UserArticleState.objects.filter(article=alices_article).exists()
@pytest.mark.django_db
def test_stream_remembers_last_selected_filter(client, alice, alices_article):
from apps.articles.models import get_system_tag
ArticleTag.objects.create(
user=alice, article=alices_article, tag=get_system_tag(alice, STARRED_TAG_NAME)
)
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"), {"filter": "starred"})
assert response.context["active_filter"] == "starred"
# No ?filter= this time — e.g. a sidebar feed/tag link, or just "/" —
# should fall back to "starred" rather than resetting to "unread".
response = client.get(reverse("reader:stream"))
assert response.context["active_filter"] == "starred"
@pytest.mark.django_db
def test_tag_filter_includes_articles_from_a_tagged_feed(client, alice, alices_article):
from apps.articles.models import SubscriptionTag, Tag
from apps.feeds.models import Subscription
client.login(username="alice", password="pw-alice-123")
tag = Tag.objects.create(user=alice, name="backend")
subscription = Subscription.objects.get(user=alice, feed=alices_article.feed)
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=tag)
response = client.get(reverse("reader:stream"), {"filter": "all", "tag": tag.pk})
assert alices_article.title in response.content.decode()
@pytest.mark.django_db
def test_saved_filter_only_shows_saved_articles(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
client.post(reverse("reader:toggle_saved", args=[alices_article.pk]))
response = client.get(reverse("reader:stream"), {"filter": "saved"})
assert alices_article.title in response.content.decode()
response = client.get(reverse("reader:stream"), {"filter": "starred"})
assert alices_article.title not in response.content.decode()