From 93f6c66f446817b5f4fd258a5c857a777e72676f Mon Sep 17 00:00:00 2001 From: Conor Holden Date: Wed, 14 Aug 2024 17:44:20 +0200 Subject: [PATCH] :white_check_mark:[#114] use setup config management command --- .../setupconfig/{boostrap.py => bootstrap.py} | 12 +++++-- pyproject.toml | 2 +- testapp/settings.py | 13 ++++++++ tests/setupconfig/test_auth.py | 32 +++++++++++++++---- 4 files changed, 50 insertions(+), 9 deletions(-) rename mozilla_django_oidc_db/setupconfig/{boostrap.py => bootstrap.py} (82%) diff --git a/mozilla_django_oidc_db/setupconfig/boostrap.py b/mozilla_django_oidc_db/setupconfig/bootstrap.py similarity index 82% rename from mozilla_django_oidc_db/setupconfig/boostrap.py rename to mozilla_django_oidc_db/setupconfig/bootstrap.py index 7c29dbd..bd9cf82 100644 --- a/mozilla_django_oidc_db/setupconfig/boostrap.py +++ b/mozilla_django_oidc_db/setupconfig/bootstrap.py @@ -1,6 +1,7 @@ from django.conf import settings from django.contrib.auth.models import Group +from django_setup_configuration.config_settings import ConfigSettings from django_setup_configuration.configuration import BaseConfigurationStep from django_setup_configuration.exceptions import ConfigurationRunFailed @@ -14,8 +15,15 @@ class AdminOIDCConfigurationStep(BaseConfigurationStep): """ verbose_name = "Configuration for admin login via OpenID Connect" - required_settings = ["OIDC_DB_SETUP_CONFIG_ADMIN_AUTH"] - enable_setting = "OIDC_DB_CONFIG_ENABLE" + + config_settings = ConfigSettings( + enable_setting="OIDC_DB_CONFIG_ENABLE", + display_name="Admin OIDC Configuration", + namespace="OIDC_DB", + models=[OpenIDConnectConfig], + update_fields=True, + required_settings=["OIDC_DB_SETUP_CONFIG_ADMIN_AUTH"], + ) def is_configured(self) -> bool: return OpenIDConnectConfig.get_solo().enabled diff --git a/pyproject.toml b/pyproject.toml index 56576f9..c785346 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ dependencies = [ "django-jsonform", "django-solo", "glom", - "mozilla-django-oidc>=3.0.0", + "mozilla-django-oidc>=2.0.0", "typing-extensions>=4.0.0", ] diff --git a/testapp/settings.py b/testapp/settings.py index 1ac9c6d..6711996 100644 --- a/testapp/settings.py +++ b/testapp/settings.py @@ -80,3 +80,16 @@ LOGIN_REDIRECT_URL = reverse_lazy("admin:index") STATIC_URL = "/static/" + +# Django setup configuration settings +try: + import django_setup_configuration + + INSTALLED_APPS += ["django_setup_configuration"] + + OIDC_DB_CONFIG_ENABLE = True + SETUP_CONFIGURATION_STEPS = [ + "mozilla_django_oidc_db.setupconfig.bootstrap.AdminOIDCConfigurationStep", + ] +except ImportError: + pass diff --git a/tests/setupconfig/test_auth.py b/tests/setupconfig/test_auth.py index 8436806..5b738d8 100644 --- a/tests/setupconfig/test_auth.py +++ b/tests/setupconfig/test_auth.py @@ -1,4 +1,4 @@ -from django.test import override_settings +from django.core.management import CommandError, call_command import pytest import requests @@ -8,14 +8,14 @@ OpenIDConnectConfig, UserInformationClaimsSources, ) -from mozilla_django_oidc_db.setupconfig.boostrap import AdminOIDCConfigurationStep +from mozilla_django_oidc_db.setupconfig.bootstrap import AdminOIDCConfigurationStep from ..conftest import KEYCLOAK_BASE_URL @pytest.mark.django_db def test_configure(setup_config_full): - AdminOIDCConfigurationStep().configure() + call_command("setup_configuration") config = OpenIDConnectConfig.get_solo() @@ -59,8 +59,29 @@ def test_configure(setup_config_full): assert config.userinfo_claims_source == UserInformationClaimsSources.id_token +@pytest.mark.django_db +def test_enable_required_setting(): + with pytest.raises(CommandError) as command_error: + call_command("setup_configuration") + + assert "OIDC_DB_SETUP_CONFIG_ADMIN_AUTH" in str(command_error.value) + + config = OpenIDConnectConfig.get_solo() + assert not config.enabled + + +@pytest.mark.django_db +def test_enable_setting(settings): + settings.OIDC_DB_CONFIG_ENABLE = False + call_command("setup_configuration") + + config = OpenIDConnectConfig.get_solo() + assert not config.enabled + + @pytest.mark.django_db def test_configure_use_defaults(setup_config_defaults): + call_command("setup_configuration") AdminOIDCConfigurationStep().configure() @@ -110,8 +131,7 @@ def test_configure_use_defaults(setup_config_defaults): @pytest.mark.vcr @pytest.mark.django_db def test_configure_use_discovery_endpoint(setup_config_discovery): - - AdminOIDCConfigurationStep().configure() + call_command("setup_configuration") config = OpenIDConnectConfig.get_solo() @@ -136,7 +156,7 @@ def test_configure_use_discovery_endpoint(setup_config_discovery): @pytest.mark.django_db -def test_configure_failure(requests_mock, setup_config_discovery): +def test_configure_discovery_failure(requests_mock, setup_config_discovery): mock_kwargs = ( {"exc": requests.ConnectTimeout}, {"exc": requests.ConnectionError},