Private
Public Access
apps.core.context_processors.sidebar runs on every authenticated page
render and called unread_counts_by_feed, a join/group-by over the
user's whole article corpus, every single time — cost grew with the
archive's size regardless of which page was being viewed.
unread_counts_by_feed now caches its {feed_id: count} result per user
for 30 seconds (apps.core.cache_utils). The reader views that change
the current user's own read state (toggle_read, mark_all_read, viewing
an article) invalidate it immediately so the sidebar never visibly lags
behind the action that just caused it; other paths (rule-driven state
changes during background ingestion) aren't explicitly invalidated and
simply self-heal within the TTL, which is an acceptable tradeoff
already made elsewhere in this app (the stream's own "new articles"
banner polls on the same 30s cadence).
Also adds a global cache.clear() autouse fixture (conftest.py) — the
test CACHES backend (LocMemCache) lives in process memory and survives
Django's per-test DB rollback, so a cached count keyed by a user's pk
could otherwise leak into an unrelated later test reusing that pk.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
346 lines
14 KiB
Python
346 lines
14 KiB
Python
import re
|
|
|
|
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()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sidebar_shows_a_tags_total_unread_count(client, alice):
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
|
|
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
|
|
sub_a = Subscription.objects.create(user=alice, feed=feed_a)
|
|
sub_b = Subscription.objects.create(user=alice, feed=feed_b)
|
|
tag = Tag.objects.create(user=alice, name="work")
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag)
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub_b, tag=tag)
|
|
for i in range(3):
|
|
Article.objects.create(feed=feed_a, guid=f"a-{i}", title=f"A{i}")
|
|
for i in range(4):
|
|
Article.objects.create(feed=feed_b, guid=f"b-{i}", title=f"B{i}")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"))
|
|
|
|
# The only "7" on the page — the individual feeds show 3 and 4.
|
|
content = response.content.decode()
|
|
assert '<span class="unread-count">7</span>' in content
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_rows_carry_their_utc_instant_for_client_side_date_grouping(client, alice):
|
|
"""The server still groups date headers in UTC, but ships each row's
|
|
raw UTC instant so date-groups.js can rebuild the headers using the
|
|
viewer's local calendar day instead."""
|
|
from django.utils import timezone
|
|
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
dated = Article.objects.create(
|
|
feed=feed, guid="dated", title="Dated", published_at=timezone.now()
|
|
)
|
|
undated = Article.objects.create(feed=feed, guid="undated", title="Undated")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"), {"filter": "all"})
|
|
content = response.content.decode()
|
|
|
|
expected_iso = dated.published_at.isoformat()
|
|
assert f'id="article-{dated.pk}"' in content
|
|
assert f'data-published-utc="{expected_iso}"' in content
|
|
|
|
undated_row = re.search(rf'<li id="article-{undated.pk}".*?>', content, flags=re.DOTALL)
|
|
assert undated_row is not None
|
|
assert 'data-published-utc=""' in undated_row.group(0)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_check_new_articles_reports_nothing_below_the_given_cursor(client, alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
existing = Article.objects.create(feed=feed, guid="existing", title="Existing")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:check_new"), {"filter": "all", "since": existing.pk})
|
|
|
|
assert response.status_code == 200
|
|
assert response.content.decode().strip() == ""
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_check_new_articles_reports_articles_created_after_the_cursor(client, alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
existing = Article.objects.create(feed=feed, guid="existing", title="Existing")
|
|
Article.objects.create(feed=feed, guid="new-1", title="New one")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:check_new"), {"filter": "all", "since": existing.pk})
|
|
|
|
content = response.content.decode()
|
|
assert "1 new article" in content
|
|
assert "click to show" in content
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_check_new_articles_respects_the_active_tag_scope(client, alice):
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
|
|
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
|
|
sub_a = Subscription.objects.create(user=alice, feed=feed_a)
|
|
Subscription.objects.create(user=alice, feed=feed_b)
|
|
tag = Tag.objects.create(user=alice, name="work")
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag)
|
|
|
|
cursor = Article.objects.create(feed=feed_a, guid="cursor", title="Cursor").pk
|
|
# New in feed_b, which isn't tagged "work" — shouldn't count for this scope.
|
|
Article.objects.create(feed=feed_b, guid="untagged-new", title="Untagged new")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(
|
|
reverse("reader:check_new"), {"filter": "all", "tag": tag.pk, "since": cursor}
|
|
)
|
|
|
|
assert response.content.decode().strip() == ""
|
|
|
|
# New in feed_a (tagged "work") — should count.
|
|
Article.objects.create(feed=feed_a, guid="tagged-new", title="Tagged new")
|
|
response = client.get(
|
|
reverse("reader:check_new"), {"filter": "all", "tag": tag.pk, "since": cursor}
|
|
)
|
|
assert "1 new article" in response.content.decode()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_stream_exposes_the_latest_article_id_and_scope_for_the_banner(client, alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
article = Article.objects.create(feed=feed, guid="g1", title="Hello")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"), {"filter": "all"})
|
|
content = response.content.decode()
|
|
|
|
assert f"since={article.pk}" in content
|
|
assert "filter=all" in content
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sidebar_unread_count_updates_immediately_after_reading_an_article(client, alice):
|
|
"""unread_counts_by_feed caches its result for a short TTL (C3) — the
|
|
reader views that change the current user's own read state must
|
|
invalidate it, or the sidebar would show a stale count until the TTL
|
|
elapses on every one of these very common actions."""
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
article = Article.objects.create(feed=feed, guid="a-1", title="Hello")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
response = client.get(reverse("reader:stream"))
|
|
assert '<span class="unread-count">1</span>' in response.content.decode()
|
|
|
|
client.get(reverse("reader:article_detail", args=[article.pk]))
|
|
|
|
response = client.get(reverse("reader:stream"))
|
|
assert "unread-count" not in response.content.decode()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sidebar_unread_count_updates_immediately_after_toggle_read(client, alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
article = Article.objects.create(feed=feed, guid="a-1", title="Hello")
|
|
UserArticleState.objects.create(user=alice, article=article, is_read=True)
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"))
|
|
assert "unread-count" not in response.content.decode()
|
|
|
|
client.post(reverse("reader:toggle_read", args=[article.pk]))
|
|
|
|
response = client.get(reverse("reader:stream"))
|
|
assert '<span class="unread-count">1</span>' in response.content.decode()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sidebar_unread_count_updates_immediately_after_mark_all_read(client, alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
Article.objects.create(feed=feed, guid="a-1", title="Hello")
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"))
|
|
assert '<span class="unread-count">1</span>' in response.content.decode()
|
|
|
|
client.post(reverse("reader:mark_all_read"))
|
|
|
|
response = client.get(reverse("reader:stream"))
|
|
assert "unread-count" not in response.content.decode()
|