dzen.nstarikov.ru/tests/test_rss.py
nstarikov 4585f32c72
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 15s
Remove iframe embeds from RSS feed
2026-09-17 09:11:22 +03:00

84 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import unittest
from datetime import datetime, timedelta, timezone
from xml.etree import ElementTree
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from app.config import Settings
from app.database import Base
from app.models import Article, Video
from app.rss import build_feed_xml
class RssFeedTest(unittest.TestCase):
def test_article_title_is_metadata_only_not_generated_body_heading(self):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
article = Article(
slug="test-article",
title="Тестовый заголовок",
lead="<p>Тестовый лид.</p>",
body_html="<p>Основной текст.</p>",
cta_html="",
feature_image_url="",
status="published",
rss_guid="test-guid",
published_at=datetime.now(timezone.utc) - timedelta(minutes=1),
)
with Session(engine) as session:
session.add(article)
session.commit()
xml = build_feed_xml(session, Settings(public_base_url="https://example.test"))
root = ElementTree.fromstring(xml)
item = root.find("./channel/item")
self.assertIsNotNone(item)
self.assertEqual(item.findtext("title"), "Тестовый заголовок")
content = item.findtext("{http://purl.org/rss/1.0/modules/content/}encoded") or ""
self.assertNotIn("<h1>Тестовый заголовок</h1>", content)
self.assertIn("<p>Тестовый лид.</p>", content)
self.assertIn("<p>Основной текст.</p>", content)
def test_rss_uses_media_player_without_iframe_embed_markup(self):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
video = Video(
external_id="video-123",
source_url="https://vkvideo.ru/video-1_123",
title="Видео",
)
article = Article(
video=video,
slug="video-article",
title="Статья с видео",
lead="<p>Лид.</p>",
body_html=(
'<figure><iframe src="https://vkvideo.ru/video_ext.php?oid=-1&amp;id=123" '
'width="1280" height="720"></iframe></figure>'
"<p>Текст.</p>"
),
cta_html="",
feature_image_url="",
status="published",
rss_guid="video-guid",
published_at=datetime.now(timezone.utc) - timedelta(minutes=1),
)
with Session(engine) as session:
session.add(article)
session.commit()
xml = build_feed_xml(session, Settings(public_base_url="https://example.test"))
root = ElementTree.fromstring(xml)
item = root.find("./channel/item")
self.assertIsNotNone(item)
content = item.findtext("{http://purl.org/rss/1.0/modules/content/}encoded") or ""
self.assertNotIn("<iframe", content.lower())
self.assertIn("Смотреть исходное видео VK Видео", content)
self.assertIn("<media:player", xml)
if __name__ == "__main__":
unittest.main()