Skip to content

Commit

Permalink
♻️[#114] streamline setup config tests
Browse files Browse the repository at this point in the history
* Set non default values in model in test_configure_use_defaults
* Remove model fixtures in favour of file yml fixtures
* Move from build_step_config_from_sources in fixture to execute_single_step in test
* Remove model validaiton tests
  • Loading branch information
Coperh committed Nov 29, 2024
1 parent 1d0bdbf commit da8600b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 118 deletions.
73 changes: 57 additions & 16 deletions tests/setupconfig/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from django_setup_configuration.test_utils import build_step_config_from_sources

from mozilla_django_oidc_db.setup_configuration.steps import AdminOIDCConfigurationStep
from mozilla_django_oidc_db.models import (
OpenIDConnectConfig,
UserInformationClaimsSources,
)

"""
Key cloak credentials are setup for the keycloak docker-compose.yml.
Expand All @@ -14,22 +16,61 @@
"""


@pytest.fixture
def setup_config_discovery_model(settings):
return build_step_config_from_sources(
AdminOIDCConfigurationStep, "tests/setupconfig/files/discovery.yml"
)
# Test files
@pytest.fixture()
def full_config_yml():
return "tests/setupconfig/files/full_setup.yml"


@pytest.fixture
def setup_config_defaults_model(settings):
return build_step_config_from_sources(
AdminOIDCConfigurationStep, "tests/setupconfig/files/defaults.yml"
)
@pytest.fixture()
def default_config_yml():
return "tests/setupconfig/files/defaults.yml"


@pytest.fixture()
def discovery_endpoint_config_yml():
return "tests/setupconfig/files/discovery.yml"


@pytest.fixture
def setup_config_full_model():
return build_step_config_from_sources(
AdminOIDCConfigurationStep, "tests/setupconfig/files/full_setup.yml"
)
def set_config_to_non_default_values():
"""
Set the current config to non-default values.
"""

config = OpenIDConnectConfig.get_solo()

# Will be always overwritten
config.oidc_rp_client_id = "client-id"
config.oidc_rp_client_secret = "secret"
config.oidc_op_authorization_endpoint = "http://localhost:8080/whatever"
config.oidc_op_token_endpoint = "http://localhost:8080/whatever"
config.oidc_op_user_endpoint = "http://localhost:8080/whatever"

# Set some non-default values
config.oidc_op_discovery_endpoint = "http://localhost:8080/whatever"
config.enabled = False

config.oidc_rp_scopes_list = [
"not_open_id",
"not_email",
"not_profile",
"not_extra_scope",
]
config.oidc_rp_sign_algo = "M1911"
config.oidc_rp_idp_sign_key = "name"
config.oidc_op_jwks_endpoint = "http://localhost:8080/whatever"
config.username_claim = ["claim_title"]
config.groups_claim = ["groups_claim_title"]
config.claim_mapping = {"first_title": ["given_title"]}
config.sync_groups = True
config.sync_groups_glob_pattern = "not_local.groups.*"

config.make_users_staff = False
config.superuser_group_names = ["poweruser"]
config.oidc_use_nonce = True
config.oidc_nonce_size = 64
config.oidc_state_size = 64
config.userinfo_claims_source = UserInformationClaimsSources.userinfo_endpoint

config.save()
6 changes: 0 additions & 6 deletions tests/setupconfig/files/discovery_disabled.yml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/setupconfig/files/empty.yml

This file was deleted.

8 changes: 0 additions & 8 deletions tests/setupconfig/files/partial_endpoints.yml

This file was deleted.

106 changes: 20 additions & 86 deletions tests/setupconfig/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import pytest
import requests
from django_setup_configuration.exceptions import ConfigurationRunFailed
from django_setup_configuration.exceptions import (
ConfigurationRunFailed,
PrerequisiteFailed,
)
from django_setup_configuration.test_utils import execute_single_step

from mozilla_django_oidc_db.models import (
OpenIDConnectConfig,
Expand All @@ -22,9 +26,8 @@ def clear_solo_cache():


@pytest.mark.django_db
def test_configure(setup_config_full_model):
step = AdminOIDCConfigurationStep()
step.execute(setup_config_full_model)
def test_configure(full_config_yml):
execute_single_step(AdminOIDCConfigurationStep, yaml_source=full_config_yml)

config = OpenIDConnectConfig.get_solo()

Expand Down Expand Up @@ -69,82 +72,8 @@ def test_configure(setup_config_full_model):


@pytest.mark.django_db
def test_required_settings():
output = StringIO()
err = StringIO()
with pytest.raises(CommandError) as command_error:
call_command(
"setup_configuration",
yaml_file="tests/setupconfig/files/empty.yml",
stdout=output,
stderr=err,
)

assert "Prerequisites for configuration are not fulfilled:" in str(
command_error.value
)

assert "oidc_db_config_admin_auth.oidc_rp_client_id" in str(command_error.value)
assert "oidc_db_config_admin_auth.oidc_rp_client_secret" in str(command_error.value)
assert "oidc_db_config_admin_auth.endpoint_config" in str(command_error.value)

config = OpenIDConnectConfig.get_solo()
assert not config.enabled


@pytest.mark.django_db
def test_partial_endpoints_provided():
"""
Test what if only one endpoint (not discovery) is provided
"""
output = StringIO()
err = StringIO()
with pytest.raises(CommandError) as command_error:
call_command(
"setup_configuration",
yaml_file="tests/setupconfig/files/partial_endpoints.yml",
stdout=output,
stderr=err,
)

assert "Prerequisites for configuration are not fulfilled:" in str(
command_error.value
)

assert (
"oidc_db_config_admin_auth.endpoint_config.all.oidc_op_token_endpoint"
in str(command_error.value)
)
assert "oidc_db_config_admin_auth.endpoint_config.all.oidc_op_user_endpoint" in str(
command_error.value
)

config = OpenIDConnectConfig.get_solo()
assert not config.enabled


@pytest.mark.django_db
def test_enable_setting():
output = StringIO()
err = StringIO()
with pytest.raises(CommandError) as command_error:
call_command(
"setup_configuration",
yaml_file="tests/setupconfig/files/discovery_disabled.yml",
stdout=output,
stderr=err,
)

assert "No steps enabled, aborting." in str(command_error.value)

config = OpenIDConnectConfig.get_solo()
assert not config.enabled


@pytest.mark.django_db
def test_configure_use_defaults(setup_config_defaults_model):
step = AdminOIDCConfigurationStep()
step.execute(setup_config_defaults_model)
def test_configure_use_defaults(set_config_to_non_default_values, default_config_yml):
execute_single_step(AdminOIDCConfigurationStep, yaml_source=default_config_yml)

config = OpenIDConnectConfig.get_solo()

Expand Down Expand Up @@ -191,9 +120,10 @@ def test_configure_use_defaults(setup_config_defaults_model):

@pytest.mark.vcr
@pytest.mark.django_db
def test_configure_use_discovery_endpoint(setup_config_discovery_model):
step = AdminOIDCConfigurationStep()
step.execute(setup_config_discovery_model)
def test_configure_use_discovery_endpoint(discovery_endpoint_config_yml):
execute_single_step(
AdminOIDCConfigurationStep, yaml_source=discovery_endpoint_config_yml
)

config = OpenIDConnectConfig.get_solo()

Expand All @@ -218,7 +148,7 @@ def test_configure_use_discovery_endpoint(setup_config_discovery_model):


@pytest.mark.django_db
def test_configure_discovery_failure(requests_mock, setup_config_discovery_model):
def test_configure_discovery_failure(requests_mock, discovery_endpoint_config_yml):
mock_kwargs = (
{"exc": requests.ConnectTimeout},
{"exc": requests.ConnectionError},
Expand All @@ -233,6 +163,10 @@ def test_configure_discovery_failure(requests_mock, setup_config_discovery_model
)

with pytest.raises(ConfigurationRunFailed):
AdminOIDCConfigurationStep().execute(setup_config_discovery_model)
execute_single_step(
AdminOIDCConfigurationStep, yaml_source=discovery_endpoint_config_yml
)

assert not OpenIDConnectConfig.get_solo().enabled
config = OpenIDConnectConfig.get_solo()
assert not config.enabled
assert config.oidc_op_discovery_endpoint == ""

0 comments on commit da8600b

Please sign in to comment.