''' Tests do not test the scheduled tasks that perform live requests to index media or download content. They only check for compliance of web interface and check the matching code logic is working as expected. ''' import logging from urllib.parse import urlsplit from django.conf import settings from django.test import TestCase, Client from django.utils import timezone from background_task.models import Task from .models import Source, Media class FrontEndTestCase(TestCase): def setUp(self): # Disable general logging for test case logging.disable(logging.CRITICAL) def test_dashboard(self): c = Client() response = c.get('/') self.assertEqual(response.status_code, 200) def test_validate_source(self): test_source_types = { 'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL, 'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST, } test_sources = { 'youtube-channel': { 'valid': ( 'https://www.youtube.com/testchannel', 'https://www.youtube.com/c/testchannel', ), 'invalid_schema': ( 'http://www.youtube.com/c/playlist', 'ftp://www.youtube.com/c/playlist', ), 'invalid_domain': ( 'https://www.test.com/c/testchannel', 'https://www.example.com/c/testchannel', ), 'invalid_path': ( 'https://www.youtube.com/test/invalid', 'https://www.youtube.com/c/test/invalid', ), 'invalid_is_playlist': ( 'https://www.youtube.com/c/playlist', 'https://www.youtube.com/c/playlist', ), }, 'youtube-playlist': { 'valid': ( 'https://www.youtube.com/playlist?list=testplaylist' 'https://www.youtube.com/watch?v=testvideo&list=testplaylist' ), 'invalid_schema': ( 'http://www.youtube.com/playlist?list=testplaylist', 'ftp://www.youtube.com/playlist?list=testplaylist', ), 'invalid_domain': ( 'https://www.test.com/playlist?list=testplaylist', 'https://www.example.com/playlist?list=testplaylist', ), 'invalid_path': ( 'https://www.youtube.com/notplaylist?list=testplaylist', 'https://www.youtube.com/c/notplaylist?list=testplaylist', ), 'invalid_is_channel': ( 'https://www.youtube.com/testchannel', 'https://www.youtube.com/c/testchannel', ), } } c = Client() for source_type in test_sources.keys(): response = c.get(f'/source-validate/{source_type}') self.assertEqual(response.status_code, 200) response = c.get('/source-validate/invalid') self.assertEqual(response.status_code, 404) for (source_type, tests) in test_sources.items(): for test, field in tests.items(): source_type_char = test_source_types.get(source_type) data = {'source_url': field, 'source_type': source_type_char} response = c.post(f'/source-validate/{source_type}', data) if test == 'valid': # Valid source tests should bounce to /source-add self.assertEqual(response.status_code, 302) url_parts = urlsplit(response.url) self.assertEqual(url_parts.path, '/source-add') else: # Invalid source tests should reload the page with an error message self.assertEqual(response.status_code, 200) self.assertIn('