Skip to content

Commit 301dceb

Browse files
committed
Added some negative testing and error catching for custom colour
1 parent 1a0b67a commit 301dceb

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = django-sys-indicator
3-
version = 0.3.0
3+
version = 0.4.0
44
url = https://github.com/marksweb/django-sys-indicator
55
description = A system/environment indicator for django
66
long_description = file: README.md

src/django_sys_indicator/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def SYSTEM_INDICATOR_LABEL(self) -> str: # noqa: N802
3030
return getattr(dj_settings, "SYSTEM_INDICATOR_LABEL", 'localhost')
3131

3232
@property
33-
def SYSTEM_INDICATOR_COLORS(self) -> str: # noqa: N802
33+
def SYSTEM_INDICATOR_COLORS(self) -> dict: # noqa: N802
3434
return getattr(dj_settings, "SYSTEM_INDICATOR_COLORS", DEFAULT_COLOURS)
3535

3636
@property

src/django_sys_indicator/utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77

88
def django_sys_indicator_tag() -> str:
9-
if not settings.SYSTEM_INDICATOR_ENABLED:
10-
return ""
11-
129
template_name = 'django_sys_indicator/system_indicator.html'
13-
color, border_color = settings.SYSTEM_INDICATOR_COLORS[
14-
settings.SYSTEM_INDICATOR_COLOR
15-
]
10+
11+
try:
12+
color, border_color = settings.SYSTEM_INDICATOR_COLORS[
13+
settings.SYSTEM_INDICATOR_COLOR
14+
]
15+
except KeyError:
16+
# Invalid colour chosen
17+
color, border_color = settings.SYSTEM_INDICATOR_COLORS['red']
1618
return render_to_string(
1719
template_name,
1820
{

tests/test_middleware.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,27 @@ def test_enabled_orange(self):
5656
assert settings.SYSTEM_INDICATOR_LABEL in content
5757
assert border_color in content
5858
assert color in content
59+
60+
@override_settings(SYSTEM_INDICATOR_ENABLED=True, SYSTEM_INDICATOR_COLOR='black')
61+
def test_invalid_colour(self):
62+
response = self.middleware(self.request)
63+
content = response.content.decode(response.charset)
64+
# Invalid colour results in red as a default
65+
color, border_color = settings.SYSTEM_INDICATOR_COLORS['red']
66+
67+
assert settings.SYSTEM_INDICATOR_LABEL in content
68+
assert border_color in content
69+
assert color in content
70+
71+
@override_settings(SYSTEM_INDICATOR_ENABLED=True, SYSTEM_INDICATOR_LABEL=123)
72+
def test_int_label(self):
73+
response = self.middleware(self.request)
74+
content = response.content.decode(response.charset)
75+
color, border_color = settings.SYSTEM_INDICATOR_COLORS[
76+
settings.SYSTEM_INDICATOR_COLOR
77+
]
78+
79+
# cast to string here because of the assert against the string content
80+
assert str(settings.SYSTEM_INDICATOR_LABEL) in content
81+
assert border_color in content
82+
assert color in content

0 commit comments

Comments
 (0)