100 lines
5 KiB
Python
100 lines
5 KiB
Python
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)
|
|
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()
|