Skip to content

Commit 1ce493b

Browse files
committed
feat(settings): helper calculates runtime env from hosts
function rather than variable to enable dynamic runtime calculation e.g. for unit tests
1 parent d92a091 commit 1ce493b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

benefits/settings.py

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import os
66

7+
from django.conf import settings
8+
79
from benefits import sentry
810

911

@@ -24,6 +26,20 @@ def _filter_empty(ls):
2426

2527
ALLOWED_HOSTS = _filter_empty(os.environ.get("DJANGO_ALLOWED_HOSTS", "localhost,127.0.0.1").split(","))
2628

29+
30+
def RUNTIME_ENVIRONMENT():
31+
"""Helper calculates the current runtime environment from ALLOWED_HOSTS."""
32+
33+
# usage of django.conf.settings.ALLOWED_HOSTS here (rather than the module variable directly)
34+
# is to ensure dynamic calculation, e.g. for unit tests and elsewhere this setting is needed
35+
env = "dev"
36+
if "test-benefits.calitp.org" in settings.ALLOWED_HOSTS:
37+
env = "test"
38+
elif "benefits.calitp.org" in settings.ALLOWED_HOSTS:
39+
env = "prod"
40+
return env
41+
42+
2743
# Application definition
2844

2945
INSTALLED_APPS = [

tests/pytest/test_settings.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def test_runtime_environment__default(settings):
2+
assert settings.RUNTIME_ENVIRONMENT() == "dev"
3+
4+
5+
def test_runtime_environment__dev(settings):
6+
settings.ALLOWED_HOSTS = ["dev-benefits.calitp.org"]
7+
assert settings.RUNTIME_ENVIRONMENT() == "dev"
8+
9+
10+
def test_runtime_environment__local(settings):
11+
settings.ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
12+
assert settings.RUNTIME_ENVIRONMENT() == "dev"
13+
14+
15+
def test_runtime_environment__nonmatching(settings):
16+
# with only nonmatching hosts, return dev
17+
settings.ALLOWED_HOSTS = ["example.com", "example2.org"]
18+
assert settings.RUNTIME_ENVIRONMENT() == "dev"
19+
20+
21+
def test_runtime_environment__test(settings):
22+
settings.ALLOWED_HOSTS = ["test-benefits.calitp.org"]
23+
assert settings.RUNTIME_ENVIRONMENT() == "test"
24+
25+
26+
def test_runtime_environment__test_and_nonmatching(settings):
27+
# when test is specified with other nonmatching hosts, assume test
28+
settings.ALLOWED_HOSTS = ["test-benefits.calitp.org", "example.com"]
29+
assert settings.RUNTIME_ENVIRONMENT() == "test"
30+
31+
32+
def test_runtime_environment__test_and_prod(settings):
33+
# if both test and prod are specified (edge case/error in configuration), assume test
34+
settings.ALLOWED_HOSTS = ["benefits.calitp.org", "test-benefits.calitp.org"]
35+
assert settings.RUNTIME_ENVIRONMENT() == "test"
36+
37+
38+
def test_runtime_environment__prod(settings):
39+
settings.ALLOWED_HOSTS = ["benefits.calitp.org"]
40+
assert settings.RUNTIME_ENVIRONMENT() == "prod"
41+
42+
43+
def test_runtime_environment__prod_and_nonmatching(settings):
44+
# when prod is specified with other nonmatching hosts, assume prod
45+
settings.ALLOWED_HOSTS = ["benefits.calitp.org", "https://example.com"]
46+
assert settings.RUNTIME_ENVIRONMENT() == "prod"

0 commit comments

Comments
 (0)