from unittest.mock import patch 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, SubscriptionTag, Tag, UserArticleState, ) from apps.feeds.models import Feed, Subscription from .models import FilterRule from .tasks import reapply_rules_for_user_backlog User = get_user_model() def is_starred(user, article): return ArticleTag.objects.filter( user=user, article=article, tag__is_system=True, tag__name=STARRED_TAG_NAME ).exists() @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 feed(db): return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example Feed") @pytest.mark.django_db def test_create_rule(client, alice, feed): Subscription.objects.create(user=alice, feed=feed) client.login(username="alice", password="pw-alice-123") response = client.post( reverse("rules:create_rule"), { "match_field": FilterRule.FIELD_TITLE, "match_type": FilterRule.MATCH_CONTAINS, "match_value": "Python", "action": FilterRule.ACTION_STAR, "priority": 0, "is_enabled": "on", }, ) assert response.status_code == 302 assert FilterRule.objects.filter(user=alice, match_value="Python").exists() @pytest.mark.django_db def test_tag_action_requires_action_value(client, alice): client.login(username="alice", password="pw-alice-123") response = client.post( reverse("rules:create_rule"), { "match_field": FilterRule.FIELD_TITLE, "match_type": FilterRule.MATCH_CONTAINS, "match_value": "Python", "action": FilterRule.ACTION_TAG, "action_value": "", "priority": 0, }, ) assert response.status_code == 200 assert not FilterRule.objects.exists() @pytest.mark.django_db def test_user_cannot_edit_another_users_rule(client, alice, bob): rule = FilterRule.objects.create( user=alice, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) client.login(username="bob", password="pw-bob-123") response = client.get(reverse("rules:edit_rule", args=[rule.pk])) assert response.status_code == 404 @pytest.mark.django_db def test_delete_rule(client, alice): rule = FilterRule.objects.create( user=alice, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) client.login(username="alice", password="pw-alice-123") response = client.post(reverse("rules:delete_rule", args=[rule.pk])) assert response.status_code == 302 assert not FilterRule.objects.filter(pk=rule.pk).exists() @pytest.mark.django_db def test_reapply_rule_view_applies_to_unread_backlog(client, alice, feed): Subscription.objects.create(user=alice, feed=feed) article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released") rule = FilterRule.objects.create( user=alice, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) client.login(username="alice", password="pw-alice-123") response = client.post(reverse("rules:reapply_rule", args=[rule.pk])) assert response.status_code == 302 assert is_starred(alice, article) is True @pytest.mark.django_db def test_reapply_rule_view_always_queues_through_celery_never_runs_inline(client, alice, feed): # A large backlog or a pathological regex must not be able to tie up # the request/response cycle — the view should dispatch a task, not # call reapply_rules_for_user_backlog directly. Subscription.objects.create(user=alice, feed=feed) rule = FilterRule.objects.create( user=alice, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) client.login(username="alice", password="pw-alice-123") with patch("apps.rules.views.reapply_rules_for_user.delay") as mock_delay: response = client.post(reverse("rules:reapply_rule", args=[rule.pk])) assert response.status_code == 302 mock_delay.assert_called_once_with(rule.pk) @pytest.mark.django_db def test_reapply_skips_already_read_articles(alice, feed): article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released") Subscription.objects.create(user=alice, feed=feed) UserArticleState.objects.create(user=alice, article=article, is_read=True) rule = FilterRule.objects.create( user=alice, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) count = reapply_rules_for_user_backlog(rule.pk) assert count == 0 assert is_starred(alice, article) is False @pytest.mark.django_db def test_reapply_respects_feed_scope(alice): 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") Subscription.objects.create(user=alice, feed=feed_a) Subscription.objects.create(user=alice, feed=feed_b) article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python in A") article_b = Article.objects.create(feed=feed_b, guid="guid-2", title="Python in B") rule = FilterRule.objects.create( user=alice, feed=feed_a, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) reapply_rules_for_user_backlog(rule.pk) assert is_starred(alice, article_a) is True assert not UserArticleState.objects.filter(user=alice, article=article_b).exists() @pytest.mark.django_db def test_reapply_does_not_apply_a_different_feeds_scoped_rule_to_the_backlog(): # Regression test for a bug where re-applying one feed-scoped rule # loaded *every* enabled rule and ran it against the whole backlog # with no scope check — so a same-matching rule scoped to a feed # the user isn't re-applying would still fire on this backlog. 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") alice = User.objects.create_user("alice", password="pw-alice-123") Subscription.objects.create(user=alice, feed=feed_a) Subscription.objects.create(user=alice, feed=feed_b) article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python in A") triggering_rule = FilterRule.objects.create( user=alice, feed=feed_a, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) FilterRule.objects.create( user=alice, feed=feed_b, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_TAG, action_value="feed-b-only", ) reapply_rules_for_user_backlog(triggering_rule.pk) assert is_starred(alice, article_a) is True assert not Tag.objects.filter(user=alice, name="feed-b-only").exists() @pytest.mark.django_db def test_reapply_applies_a_tag_scoped_rule_only_to_that_tags_feeds(): 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") alice = User.objects.create_user("alice", password="pw-alice-123") 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) article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python in A") article_b = Article.objects.create(feed=feed_b, guid="guid-2", title="Python in B") unscoped_trigger = FilterRule.objects.create( user=alice, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="unrelated-trigger-value", action=FilterRule.ACTION_MARK_READ, ) FilterRule.objects.create( user=alice, tag=tag, match_field=FilterRule.FIELD_TITLE, match_type=FilterRule.MATCH_CONTAINS, match_value="Python", action=FilterRule.ACTION_STAR, ) reapply_rules_for_user_backlog(unscoped_trigger.pk) assert is_starred(alice, article_a) is True assert is_starred(alice, article_b) is False