ucast/ucast/tests/test_feed.py

85 lines
2.6 KiB
Python

import feedparser
import pytest
from django.test.client import Client
from django.utils.feedgenerator import rfc2822_date
from ucast import feed
from ucast.models import Video
@pytest.mark.django_db
def test_feed(feed_key):
client = Client()
response = client.get(f"/feed/ThetaDev?key={feed_key}")
parsed_feed = feedparser.parse(response.getvalue())
assert parsed_feed["bozo"] is False
assert parsed_feed["namespaces"] == {
"": "http://www.w3.org/2005/Atom",
"itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd",
}
meta = parsed_feed["feed"]
assert meta["title"] == "ThetaDev"
assert meta["link"] == "https://www.youtube.com/channel/UCGiJh0NZ52wRhYKYnuZI08Q"
assert (
meta["subtitle"]
== "I'm ThetaDev. I love creating cool projects using electronics, 3D printers and other awesome tech-based stuff."
)
assert meta["updated"] == "Sun, 15 May 2022 22:16:25 +0000"
assert (
meta["image"]["href"]
== f"http://testserver/files/avatar/ThetaDev.jpg?key={feed_key}"
)
assert len(parsed_feed["entries"]) == 4
video_ids = ["ZPxEr4YdWt8", "_I5IFObm_-k", "mmEDPbbSnaY", "Cda4zS-1j-k"]
for i, entry in enumerate(parsed_feed["entries"]):
video = Video.objects.get(video_id=video_ids[i])
assert entry["title"] == video.title
assert entry["link"] == video.get_absolute_url()
assert entry["summary"] == feed.PodcastFeedType._xml_escape(
video.description
).replace("<br>", "<br />")
assert entry["published"] == rfc2822_date(video.published)
assert entry["id"] == video.get_absolute_url()
assert (
entry["image"]["href"]
== f"http://testserver/files/cover/ThetaDev/{video.slug}.png?key={feed_key}"
)
assert entry["itunes_duration"] == feed.PodcastFeedType._format_secs(
video.duration
)
@pytest.mark.parametrize(
"text,expect",
[
("Hello <World>", "Hello &lt;World&gt;"),
(
"go to https://example.org/test",
'go to <a href="https://example.org/test">https://example.org/test</a>',
),
("line1\nline2\nline3", "line1<br>line2<br>line3"),
],
)
def test_xml_escape(text: str, expect: str):
escaped = feed.PodcastFeedType._xml_escape(text)
assert escaped == expect
@pytest.mark.parametrize(
"secs,expect",
[
(0, "00:00:00"),
(16, "00:00:16"),
(100, "00:01:40"),
(3800, "01:03:20"),
],
)
def test_format_secs(secs: int, expect: str):
time_str = feed.PodcastFeedType._format_secs(secs)
assert time_str == expect