import pytest import requests import responses from django.utils import timezone from apps.articles.models import Article, UserArticleState from apps.feeds.models import Feed from apps.feeds.services.fetcher import ( MAX_CONTENT_HTML_CHARS, MAX_ENTRIES_PER_POLL, effective_interval_minutes, fetch_and_ingest, sanitize_html, ) FEED_URL = "https://example.com/feed.xml" GOOD_FEED_XML = """ Example Blog https://example.com First post https://example.com/1 https://example.com/1 Mon, 01 Jan 2024 00:00:00 GMT <p>Hello <script>alert(1)</script>world</p> """ NO_GUID_FEED_XML = """ No Guid Blog Only a link https://example.com/only-link Mon, 01 Jan 2024 00:00:00 GMT Body """ MALFORMED_BUT_SALVAGEABLE_XML = """ Slightly Broken Unescaped & ampersand but still has entries guid-1 """ TOTAL_GARBAGE = "this is not xml at all {{{" @pytest.fixture def feed(db): return Feed.objects.create(feed_url=FEED_URL) @pytest.mark.django_db @responses.activate def test_fetch_ingests_new_articles_and_sanitizes_html(feed): responses.add( responses.GET, FEED_URL, body=GOOD_FEED_XML, status=200, headers={"ETag": '"abc123"', "Last-Modified": "Mon, 01 Jan 2024 00:00:00 GMT"}, ) result = fetch_and_ingest(feed) feed.save() assert result.status == Feed.STATUS_OK assert len(result.new_article_ids) == 1 assert feed.title == "Example Blog" assert feed.etag == '"abc123"' assert feed.last_modified == "Mon, 01 Jan 2024 00:00:00 GMT" article = Article.objects.get(feed=feed, guid="https://example.com/1") assert "