From ca1cc4ed7b9c0e0efd88185fca063c06440c8bdf Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 14 Jan 2025 17:39:11 +0930 Subject: [PATCH] test(app): metrics_enabled setings checks ref:#469 fixes #470 closes #156 --- app/app/tests/unit/test_settings.py | 108 +++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/app/app/tests/unit/test_settings.py b/app/app/tests/unit/test_settings.py index cca1e7d3b..4ecb96948 100644 --- a/app/app/tests/unit/test_settings.py +++ b/app/app/tests/unit/test_settings.py @@ -1,3 +1,5 @@ +import pytest + from django.conf import settings as django_settings from django.shortcuts import reverse from django.test import TestCase, Client @@ -5,8 +7,7 @@ from app import settings -import pytest -import unittest + class SettingsDefault(TestCase): @@ -22,6 +23,16 @@ def test_setting_default_debug_off(self): assert not settings.DEBUG + def test_setting_default_debug_off_type(self): + """ Settings attribute type check + + setting `DEBUG` must be of type bool + """ + + assert type(settings.DEBUG) is bool + + + def test_setting_default_login_required(self): """ By default login should be required """ @@ -29,6 +40,35 @@ def test_setting_default_login_required(self): assert settings.LOGIN_REQUIRED + def test_setting_default_login_required_type(self): + """ Settings attribute type check + + setting `LOGIN_REQUIRED` must be of type bool + """ + + assert type(settings.LOGIN_REQUIRED) is bool + + + + def test_setting_default_metrics_off(self): + """ Ensure that metrics is off within settings by default + + Metrics is only required when user turns it on + """ + + assert not settings.METRICS_ENABLED + + + def test_setting_default_metrics_off_type(self): + """ Settings attribute type check + + setting `METRICS_ENABLED` must be of type bool + """ + + assert type(settings.METRICS_ENABLED) is bool + + + def test_setting_default_use_tz(self): """ Ensure that 'USE_TZ = True' is within settings """ @@ -36,6 +76,15 @@ def test_setting_default_use_tz(self): assert settings.USE_TZ + def test_setting_default_use_tz_type(self): + """ Settings attribute type check + + setting `USE_TZ` must be of type bool + """ + + assert type(settings.USE_TZ) is bool + + class SettingsValues(TestCase): """ Test Each setting that offers different functionality """ @@ -64,3 +113,58 @@ def test_setting_value_login_required_not(self): response = client.get(url) assert response.status_code == 200 + + + + def test_setting_value_metrics_off_middleware(self): + """ Metrics off check + + when metrics are off, its middleware must not exist in settings + """ + + assert ( + 'django_prometheus.middleware.PrometheusBeforeMiddleware' not in settings.MIDDLEWARE + and + 'django_prometheus.middleware.PrometheusAfterMiddleware' not in settings.MIDDLEWARE + ) + + + + def test_setting_value_metrics_off_installed_apps(self): + """ Metrics off check + + when metrics are off, it should not be installed. + """ + + assert 'django_prometheus' not in settings.INSTALLED_APPS + + + @pytest.mark.skip( reason = 'figure out how to test' ) + def test_setting_value_metrics_on_middleware(self): + """ Metrics off check + + logic in settings adjusts middleware when `METRICS_ENABLED=True` + + when metrics are off, its middleware must not exist in settings + """ + + assert ( + 'django_prometheus.middleware.PrometheusBeforeMiddleware' in settings.MIDDLEWARE + and + 'django_prometheus.middleware.PrometheusAfterMiddleware' in settings.MIDDLEWARE + ) + + + + @pytest.mark.skip( reason = 'figure out how to test' ) + def test_setting_value_metrics_on_installed_apps(self): + """ Metrics off check + + logic in settings adjusts installed apps when `METRICS_ENABLED=True` + + when metrics are off, it should not be installed. + """ + + settings.METRICS_ENABLED = True + + assert 'django_prometheus' in settings.INSTALLED_APPS