From 65f24208517c645a132592890aedd459ea2c2845 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Wed, 8 May 2019 13:45:20 -0400 Subject: [PATCH] Add unit tests for config --- app/server/tests/test_config.py | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/server/tests/test_config.py diff --git a/app/server/tests/test_config.py b/app/server/tests/test_config.py new file mode 100644 index 00000000..f1bcfd84 --- /dev/null +++ b/app/server/tests/test_config.py @@ -0,0 +1,35 @@ +from contextlib import contextmanager +from importlib import reload +from os import environ + +from django.test import TestCase + +from app import settings + + +class TestDatabaseUrl(TestCase): + def test_sslmode_defaults_to_required(self): + with setenv('DATABASE_URL', 'pgsql://u:p@h/d'): + self._assert_sslmode_is('require') + + def test_sslmode_not_set_for_sqlite(self): + with setenv('DATABASE_URL', 'sqlite:///some/path'): + self._assert_sslmode_is(None) + + def test_sslmode_can_be_set_via_database_url(self): + with setenv('DATABASE_URL', 'pgsql://u:p@h/d?sslmode=disabled'), \ + setenv('IS_HEROKU', 'False'): + self._assert_sslmode_is('disabled') + + def _assert_sslmode_is(self, expected): + reload(settings) + actual = settings.DATABASES['default'].get('OPTIONS', {}).get('sslmode') + self.assertEqual(actual, expected) + + +@contextmanager +def setenv(key, value): + environ[key] = value + yield + del environ[key] +