Files
vardkey/tests/test_app.py
T

50 lines
1.6 KiB
Python

"""HTTP tests for the dashboard server."""
from http.server import ThreadingHTTPServer
import json
import threading
import unittest
from urllib.error import HTTPError
from urllib.request import urlopen
from app import AppHandler
class AppHandlerTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.server = ThreadingHTTPServer(("127.0.0.1", 0), AppHandler)
cls.thread = threading.Thread(target=cls.server.serve_forever, daemon=True)
cls.thread.start()
cls.base_url = f"http://127.0.0.1:{cls.server.server_port}"
@classmethod
def tearDownClass(cls):
cls.server.shutdown()
cls.server.server_close()
cls.thread.join(timeout=2)
def test_health_endpoint_returns_json(self):
with urlopen(f"{self.base_url}/health") as response:
self.assertEqual(response.status, 200)
self.assertEqual(json.load(response), {"status": "ok"})
self.assertEqual(response.headers["X-Content-Type-Options"], "nosniff")
def test_dashboard_is_english(self):
with urlopen(self.base_url) as response:
body = response.read().decode("utf-8")
self.assertEqual(response.status, 200)
self.assertIn('<html lang="en">', body)
self.assertIn("Start annual health check", body)
self.assertNotIn("Årleg", body)
def test_unknown_path_returns_not_found(self):
with self.assertRaises(HTTPError) as error:
urlopen(f"{self.base_url}/missing")
self.assertEqual(error.exception.code, 404)
if __name__ == "__main__":
unittest.main()