import os from unittest.mock import patch from django.test import SimpleTestCase from santehray.settings import ( collect_hosts, collect_origins, env_choice, normalize_host, normalize_origin, ) class SettingsNormalizationTests(SimpleTestCase): def test_normalize_host_strips_scheme_and_port(self): self.assertEqual(normalize_host("https://preview-1.azimkin.dev:8000"), "preview-1.azimkin.dev") def test_collect_hosts_normalizes_urls_and_bare_hosts(self): with patch.dict(os.environ, {"ALLOWED_HOSTS": "https://preview-1.azimkin.dev:8000, localhost"}): self.assertEqual( collect_hosts("ALLOWED_HOSTS"), ["preview-1.azimkin.dev", "localhost"], ) def test_collect_origins_normalizes_bare_hosts_and_urls(self): with patch.dict(os.environ, {"CSRF_TRUSTED_ORIGINS": "preview-1.azimkin.dev,https://localhost:8443"}): self.assertEqual( collect_origins("CSRF_TRUSTED_ORIGINS"), ["https://preview-1.azimkin.dev", "https://localhost:8443"], ) def test_normalize_origin_preserves_explicit_scheme_and_port(self): self.assertEqual(normalize_origin("https://preview-1.azimkin.dev:8443"), "https://preview-1.azimkin.dev:8443") def test_env_choice_accepts_allowed_value(self): with patch.dict(os.environ, {"SECURE_REFERRER_POLICY": "strict-origin-when-cross-origin"}): self.assertEqual( env_choice( "SECURE_REFERRER_POLICY", "same-origin", {"same-origin", "strict-origin-when-cross-origin"}, ), "strict-origin-when-cross-origin", ) def test_env_choice_falls_back_for_invalid_value(self): with patch.dict(os.environ, {"SECURE_REFERRER_POLICY": "invalid-policy"}): self.assertEqual( env_choice( "SECURE_REFERRER_POLICY", "same-origin", {"same-origin", "strict-origin-when-cross-origin"}, ), "same-origin", )