You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from contextlib import contextmanager
  2. from importlib import reload
  3. from os import environ
  4. from django.test import TestCase
  5. from config.settings import base as settings
  6. class TestDatabaseUrl(TestCase):
  7. def test_sslmode_defaults_to_required(self):
  8. with setenv("DATABASE_URL", "pgsql://u:p@h/d"):
  9. self._assert_sslmode_is("require")
  10. def test_sslmode_not_set_for_sqlite(self):
  11. with setenv("DATABASE_URL", "sqlite:///some/path"):
  12. self._assert_sslmode_is(None)
  13. def test_sslmode_can_be_disabled_via_database_url(self):
  14. with setenv("DATABASE_URL", "pgsql://u:p@h/d?sslmode=disable"):
  15. self._assert_sslmode_is("disable")
  16. def test_sslmode_can_be_required_via_database_url(self):
  17. with setenv("DATABASE_URL", "pgsql://u:p@h/d?sslmode=require"):
  18. self._assert_sslmode_is("require")
  19. def test_database_url_with_complex_user(self):
  20. with setenv("DATABASE_URL", "pgsql://user%40host:p@h/d"):
  21. self._assert_user_is("user@host")
  22. def _assert_sslmode_is(self, expected):
  23. actual = settings.DATABASES["default"].get("OPTIONS", {}).get("sslmode")
  24. self.assertEqual(actual, expected)
  25. def _assert_user_is(self, expected):
  26. actual = settings.DATABASES["default"].get("USER", "")
  27. self.assertEqual(actual, expected)
  28. @contextmanager
  29. def setenv(key, value):
  30. environ[key] = value
  31. reload(settings)
  32. yield
  33. del environ[key]