dzen.nstarikov.ru/tests/test_rss.py
nstarikov 5ebc63dfe9
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 13s
Restore VK iframe embeds in RSS
2026-09-17 15:32:01 +03:00

81 lines
3 KiB
Python
Raw Permalink 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_keeps_vk_embed_markup_for_video(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="<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.assertIn("<iframe", content.lower())
self.assertIn("video_ext.php", content)
self.assertIn("Смотреть исходное видео VK Видео", content)
self.assertIn("<media:player", xml)
if __name__ == "__main__":
unittest.main()