from html.parser import HTMLParser import json import os from pathlib import Path import runpy import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] class HomeElements(HTMLParser): def __init__(self): super().__init__() self.links = [] self.buttons = [] self.headings = [] self.selection_url = None def handle_starttag(self, tag, attrs): attrs = dict(attrs) if tag == 'a': self.links.append(attrs.get('href')) if tag == 'button': self.buttons.append(attrs) if tag in ('h1', 'h2'): self.headings.append(tag) if 'data-selection-url' in attrs: self.selection_url = attrs['data-selection-url'] class HomeTest(unittest.TestCase): def setUp(self): self.directory = tempfile.TemporaryDirectory() self.addCleanup(self.directory.cleanup) self.workdir = Path(self.directory.name) (self.workdir / 'config.json').write_text(json.dumps({ 'database': {'type': 'SQLite', 'name': 'test.db'}, 'upload_location': 'uploads' })) subprocess.run([sys.executable, str(ROOT / 'sc_database_generator.py')], cwd=self.workdir, check=True, capture_output=True) original = Path.cwd() try: os.chdir(self.workdir) self.app = runpy.run_path(str(ROOT / 'sc_webserver.py'))['app'] finally: os.chdir(original) self.app.config['TESTING'] = True self.client = self.app.test_client() def test_working_actions_are_grouped_and_all_destinations_work(self): response = self.client.get('/') self.assertEqual(response.status_code, 200) elements = HomeElements() elements.feed(response.get_data(as_text=True)) self.assertCountEqual(elements.links, ['/', '/media', '/upload/photo', '/upload/video', '/configure/stream', '/youtube/download', '/youtube/jobs']) self.assertEqual(elements.buttons, []) # No deceptively enabled placeholder controls. self.assertEqual(elements.headings.count('h1'), 1) self.assertEqual(elements.selection_url, '/media/status') for link in elements.links: with self.subTest(link=link): self.assertEqual(self.client.get(link).status_code, 200) self.assertIn(b'COMING SOON', response.data) self.assertIn(b'TV & screen controls', response.data) self.assertIn(b'