diora-web/radio/tests.py

101 lines
5 KiB
Python
Raw Normal View History

Podcasts im Browser: die Podcast-Seite der App statt des alten Podcast-Tabs Der Podcast-Tab der Web-App zeigt jetzt dieselbe Oberfläche wie die Android-App: native-podcast.js aus diora-android, unverändert kopiert nach static/podcasts/ (tools/sync_podcast_ui.py, mit Quell-Commit im Kopf), dazu die Podcast-Teile von native.css. static/js/podcasts-web.js spielt im Browser die Rolle der App (window.DioraPodcastHost statt DioraNative): - Zustand aus /podcasts/state/, in localStorage für einen schnellen Start; Änderungen sofort lokal und der Reihe nach an den Server (Outbox, übersteht Neuladen und Offline), wie Podcasts.kt, inkl. „immer sortiert halten“ - eigener Player (nicht das Radio-Audio-Element), Warteschlange = Playlist, wie RadioService.EpisodeTracker: Stelle fortsetzen, Intro/Outro, Fortschritt alle 10 s und bei Pause, „gehört“ kurz vor Schluss (auch bei „nächste“), automatisch weiter, nach Pause etwas zurück (0,8/3/10/20 s), Tempo und Lautstärke (bis 1) pro Podcast, Sleep-Timer nur während des Abspielens mit Ausblenden - Sperrbildschirm und Medientasten über navigator.mediaSession - Abspiel-Einstellungen dieses Geräts in localStorage (wie PlaybackPrefs) - keine Downloads (die Oberfläche blendet sie aus) - Radio und Podcast nie gleichzeitig: eine Folge stoppt das Radio, ein Sender legt die Folge weg - Bottom-Sheet und Mini-Player-Dock über der Radio-Leiste; auf breiten Bildschirmen eine mittige Spalte (760 px) In der App und der Desktop-App (window.DioraNative vorhanden) tut podcasts-web.js nichts. Entfernt: der alte Podcast-Tab (Markup, ~1000 Zeilen in app.js, CSS), die Podcast-Leiste im Player, INITIAL_PODCAST_FEEDS und das Herunterladen von Folgen in den Browser-Cache (der Service Worker räumt diora-podcast-v1 ab). Service-Worker-Cache auf v52, neue Dateien im App-Shell. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 10:26:32 +02:00
from pathlib import Path
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
class PlayerPagePodcastsTests(TestCase):
"""The podcast tab is the app's podcast page (static/podcasts/, copied from
diora-android) on a browser host (static/js/podcasts-web.js)."""
def test_logged_in_page_loads_the_podcast_host(self):
user = User.objects.create_user('pod', password='pw-for-tests-only')
self.client.force_login(user)
html = self.client.get('/').content.decode()
self.assertIn('id="tab-podcasts"', html)
self.assertIn('<script src="/static/js/podcasts-web.js"></script>', html)
self.assertIn('dpw-loading', html)
# The old podcast tab and its player bar are gone.
self.assertNotIn('podcast-toolbar', html)
self.assertNotIn('podcast-seek-bar', html)
self.assertNotIn('INITIAL_PODCAST_FEEDS', html)
def test_logged_out_page_asks_to_log_in(self):
html = self.client.get('/').content.decode()
self.assertIn('um Podcasts zu abonnieren.', html)
Podcasts im Browser: die Podcast-Seite der App statt des alten Podcast-Tabs Der Podcast-Tab der Web-App zeigt jetzt dieselbe Oberfläche wie die Android-App: native-podcast.js aus diora-android, unverändert kopiert nach static/podcasts/ (tools/sync_podcast_ui.py, mit Quell-Commit im Kopf), dazu die Podcast-Teile von native.css. static/js/podcasts-web.js spielt im Browser die Rolle der App (window.DioraPodcastHost statt DioraNative): - Zustand aus /podcasts/state/, in localStorage für einen schnellen Start; Änderungen sofort lokal und der Reihe nach an den Server (Outbox, übersteht Neuladen und Offline), wie Podcasts.kt, inkl. „immer sortiert halten“ - eigener Player (nicht das Radio-Audio-Element), Warteschlange = Playlist, wie RadioService.EpisodeTracker: Stelle fortsetzen, Intro/Outro, Fortschritt alle 10 s und bei Pause, „gehört“ kurz vor Schluss (auch bei „nächste“), automatisch weiter, nach Pause etwas zurück (0,8/3/10/20 s), Tempo und Lautstärke (bis 1) pro Podcast, Sleep-Timer nur während des Abspielens mit Ausblenden - Sperrbildschirm und Medientasten über navigator.mediaSession - Abspiel-Einstellungen dieses Geräts in localStorage (wie PlaybackPrefs) - keine Downloads (die Oberfläche blendet sie aus) - Radio und Podcast nie gleichzeitig: eine Folge stoppt das Radio, ein Sender legt die Folge weg - Bottom-Sheet und Mini-Player-Dock über der Radio-Leiste; auf breiten Bildschirmen eine mittige Spalte (760 px) In der App und der Desktop-App (window.DioraNative vorhanden) tut podcasts-web.js nichts. Entfernt: der alte Podcast-Tab (Markup, ~1000 Zeilen in app.js, CSS), die Podcast-Leiste im Player, INITIAL_PODCAST_FEEDS und das Herunterladen von Folgen in den Browser-Cache (der Service Worker räumt diora-podcast-v1 ab). Service-Worker-Cache auf v52, neue Dateien im App-Shell. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 10:26:32 +02:00
self.assertNotIn('dpw-loading', html)
def test_podcast_ui_files_are_there(self):
static = Path(settings.BASE_DIR) / 'static'
host = (static / 'js' / 'podcasts-web.js').read_text(encoding='utf-8')
for name in ('native-podcast.js', 'podcast-ui.css', 'podcasts-web.css'):
path = static / 'podcasts' / name
self.assertTrue(path.exists(), name)
if name != 'podcasts-web.css':
self.assertIn('Copied from diora-android', path.read_text(encoding='utf-8')[:300])
self.assertIn('/static/podcasts/' + name, host)
sw = (static / 'js' / 'sw.js').read_text(encoding='utf-8')
for path in ('/static/js/podcasts-web.js', '/static/podcasts/native-podcast.js'):
self.assertIn(path, sw)
class SseGuardTests(TestCase):
"""/radio/sse/ opens a stream for its titles: never in a private network, and without an
account only the directory's stations (radio/streamcheck.py, docs/radio.md)."""
PUBLIC = [(2, 1, 6, '', ('93.184.216.34', 80))]
STREAM = 'http://stream.example.org/live.mp3'
def setUp(self):
from django.core.cache import cache
cache.clear()
def sse(self, url, **more):
from unittest import mock
with mock.patch('radio.views.stream_icy_metadata', return_value=iter(['Artist - Title'])):
return self.client.get('/radio/sse/', {'url': url, **more})
def test_private_addresses_never(self):
User.objects.create_user('r', password='pw12345678')
self.client.login(username='r', password='pw12345678')
for url in ['http://127.0.0.1:8000/', 'http://10.0.0.5/stream', 'http://169.254.169.254/latest/',
'http://[::1]/x', 'file:///etc/passwd', 'gopher://example.org/']:
self.assertEqual(self.sse(url).status_code, 403, url)
def test_without_account_only_the_directory(self):
from unittest import mock
with mock.patch('radio.streamcheck.socket.getaddrinfo', return_value=self.PUBLIC):
with mock.patch('radio.streamcheck.requests.get') as get:
get.return_value = mock.Mock(ok=True, json=lambda: [])
self.assertEqual(self.sse(self.STREAM).status_code, 403) # not listed: an own address
listed = [{'url': 'http://stream.example.org/pls', 'url_resolved': self.STREAM}]
get.return_value = mock.Mock(ok=True, json=lambda: listed)
self.assertEqual(self.sse(self.STREAM, station='abc-uuid').status_code, 200)
def test_featured_stations_without_account(self):
from unittest import mock
from .models import FeaturedStation
FeaturedStation.objects.create(name='F', url=self.STREAM)
with mock.patch('radio.streamcheck.socket.getaddrinfo', return_value=self.PUBLIC), \
mock.patch('radio.streamcheck.requests.get') as get:
self.assertEqual(self.sse(self.STREAM).status_code, 200)
get.assert_not_called()
def test_own_addresses_with_an_account(self):
from unittest import mock
User.objects.create_user('r', password='pw12345678')
self.client.login(username='r', password='pw12345678')
with mock.patch('radio.streamcheck.socket.getaddrinfo', return_value=self.PUBLIC), \
mock.patch('radio.streamcheck.requests.get') as get:
self.assertEqual(self.sse(self.STREAM).status_code, 200)
get.assert_not_called()
def test_the_pages_curated_stations_without_account(self):
from unittest import mock
from . import streamcheck
self.assertIn('https://ice6.somafm.com/dronezone-256-mp3', streamcheck.curated())
with mock.patch('radio.streamcheck.socket.getaddrinfo', return_value=self.PUBLIC), mock.patch('radio.streamcheck.requests.get') as get:
self.assertEqual(self.sse('https://ice6.somafm.com/dronezone-256-mp3').status_code, 200)
get.assert_not_called()