dzen.nstarikov.ru/tests/test_rss.py
nstarikov f490094e08
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 21s
Embed local MP4 videos in RSS
2026-09-17 15:30:32 +03:00

92 lines
3.7 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 pathlib import Path
from tempfile import TemporaryDirectory
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_native_mp4_video_without_iframe_embed_markup(self):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
with TemporaryDirectory() as temp_dir:
video_path = Path(temp_dir) / "video.mp4"
video_path.write_bytes(b"test")
video = Video(
external_id="video-123",
source_url="https://vkvideo.ru/video-1_123",
title="Видео",
local_video_path=str(video_path),
)
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('<video controls preload="metadata"', content)
self.assertIn('type="video/mp4"', content)
self.assertIn("Смотреть исходное видео VK Видео", content)
self.assertNotIn("<media:player", xml)
if __name__ == "__main__":
unittest.main()