import os import runpy import unittest from pathlib import Path from unittest.mock import patch class ProductionDefaultSettingsTests(unittest.TestCase): def load_defaults(self): settings_path = Path(__file__).with_name("settings.py") with patch.dict(os.environ, {"DJANGO_DEBUG": "1"}, clear=True): return runpy.run_path(str(settings_path)) def test_current_public_domain_is_the_only_default_identity(self): settings = self.load_defaults() self.assertEqual(settings["ALLOWED_HOSTS"], ["vpn.ucvl.cn", "localhost", "127.0.0.1"]) self.assertEqual(settings["CSRF_TRUSTED_ORIGINS"], ["https://vpn.ucvl.cn"]) self.assertEqual(settings["PUBLIC_BASE_URL"], "https://vpn.ucvl.cn") self.assertEqual(settings["VPN_PUBLIC_GATEWAY_HOST"], "vpn.ucvl.cn") self.assertEqual(settings["VPN_PUBLIC_GATEWAY_IPV4"], "47.104.134.190") def test_environment_still_overrides_every_public_default(self): settings_path = Path(__file__).with_name("settings.py") environment = { "DJANGO_DEBUG": "1", "DJANGO_ALLOWED_HOSTS": "fixture.example,localhost", "DJANGO_CSRF_TRUSTED_ORIGINS": "https://fixture.example", "VPN_PUBLIC_BASE_URL": "https://fixture.example/root/", "VPN_PUBLIC_GATEWAY_HOST": "entry.fixture.example", "VPN_PUBLIC_GATEWAY_IPV4": "198.51.100.25", } with patch.dict(os.environ, environment, clear=True): settings = runpy.run_path(str(settings_path)) self.assertEqual(settings["ALLOWED_HOSTS"], ["fixture.example", "localhost"]) self.assertEqual(settings["CSRF_TRUSTED_ORIGINS"], ["https://fixture.example"]) self.assertEqual(settings["PUBLIC_BASE_URL"], "https://fixture.example/root") self.assertEqual(settings["VPN_PUBLIC_GATEWAY_HOST"], "entry.fixture.example") self.assertEqual(settings["VPN_PUBLIC_GATEWAY_IPV4"], "198.51.100.25") if __name__ == "__main__": unittest.main()