Skip to content

Commit

Permalink
✅[#114] use setup config management command
Browse files Browse the repository at this point in the history
  • Loading branch information
Coperh committed Aug 23, 2024
1 parent 8761799 commit 93f6c66
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
13 changes: 13 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 26 additions & 6 deletions tests/setupconfig/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.test import override_settings
from django.core.management import CommandError, call_command

import pytest
import requests
Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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},
Expand Down

0 comments on commit 93f6c66

Please sign in to comment.