Skip to content

Commit

Permalink
✅ [#125] Add/update tests for changed setup config format
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Dec 9, 2024
1 parent 5243c3f commit 38c6eb1
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 73 deletions.
10 changes: 10 additions & 0 deletions tests/setupconfig/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def sync_groups_config_yml():
return "tests/setupconfig/files/sync_groups.yml"


@pytest.fixture()
def multiple_configs_yml():
return "tests/setupconfig/files/multiple_configs.yml"


@pytest.fixture()
def missing_identifier_yml():
return "tests/setupconfig/files/missing_identifier.yml"


@pytest.fixture
def set_config_to_non_default_values():
"""
Expand Down
14 changes: 8 additions & 6 deletions tests/setupconfig/files/defaults.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
configurations:
- identifier: admin-oidc
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
10 changes: 6 additions & 4 deletions tests/setupconfig/files/discovery.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
oidc_rp_client_id: testid
oidc_rp_client_secret: 7DB3KUAAizYCcmZufpHRVOcD0TOkNO3I
endpoint_config:
oidc_op_discovery_endpoint: http://localhost:8080/realms/test/
configurations:
- identifier: admin-oidc
oidc_rp_client_id: testid
oidc_rp_client_secret: 7DB3KUAAizYCcmZufpHRVOcD0TOkNO3I
endpoint_config:
oidc_op_discovery_endpoint: http://localhost:8080/realms/test/
70 changes: 36 additions & 34 deletions tests/setupconfig/files/full_setup.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
enabled: False
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
oidc_rp_scopes_list:
- open_id
- email
- profile
- extra_scope
oidc_rp_sign_algo: RS256
oidc_rp_idp_sign_key: key
oidc_op_jwks_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/certs
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
username_claim:
- claim_name
groups_claim:
- groups_claim_name
claim_mapping:
first_name:
- given_name
sync_groups: false
sync_groups_glob_pattern: local.groups.*
default_groups:
- local.groups.Admins
- local.groups.Read-only
make_users_staff: true
superuser_group_names:
- superuser
oidc_use_nonce: false
oidc_nonce_size: 48
oidc_state_size: 48
userinfo_claims_source: id_token
configurations:
- identifier: admin-oidc
enabled: False
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
oidc_rp_scopes_list:
- open_id
- email
- profile
- extra_scope
oidc_rp_sign_algo: RS256
oidc_rp_idp_sign_key: key
oidc_op_jwks_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/certs
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
username_claim:
- claim_name
groups_claim:
- groups_claim_name
claim_mapping:
first_name:
- given_name
sync_groups: false
sync_groups_glob_pattern: local.groups.*
default_groups:
- local.groups.Admins
- local.groups.Read-only
make_users_staff: true
superuser_group_names:
- superuser
oidc_use_nonce: false
oidc_nonce_size: 48
oidc_state_size: 48
userinfo_claims_source: id_token

9 changes: 9 additions & 0 deletions tests/setupconfig/files/missing_identifier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
configurations:
- oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
17 changes: 17 additions & 0 deletions tests/setupconfig/files/multiple_configs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
configurations:
- identifier: admin-oidc
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
- identifier: admin-oidc
oidc_rp_client_id: client-id2
oidc_rp_client_secret: secret2
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
22 changes: 12 additions & 10 deletions tests/setupconfig/files/no_sync_groups.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
sync_groups: false
default_groups:
- SuperAdmins
- NormalUsers
configurations:
- identifier: admin-oidc
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
sync_groups: false
default_groups:
- SuperAdmins
- NormalUsers
28 changes: 15 additions & 13 deletions tests/setupconfig/files/sync_groups.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
oidc_db_config_enable: True
oidc_db_config_admin_auth:
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
sync_groups: true
sync_groups_glob_pattern: local.groups.*
default_groups:
- local.groups.SuperAdmins
- local.WeirdAdmins
- local.groups.NormalUsers
- local.WeirdUsers
configurations:
- identifier: admin-oidc
oidc_rp_client_id: client-id
oidc_rp_client_secret: secret
endpoint_config:
oidc_op_authorization_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/auth
oidc_op_token_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/token
oidc_op_user_endpoint: http://localhost:8080/realms/test/protocol/openid-connect/userinfo
sync_groups: true
sync_groups_glob_pattern: local.groups.*
default_groups:
- local.groups.SuperAdmins
- local.WeirdAdmins
- local.groups.NormalUsers
- local.WeirdUsers
46 changes: 40 additions & 6 deletions tests/setupconfig/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

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 (
Expand Down Expand Up @@ -244,6 +247,33 @@ def test_configure_discovery_failure(
assert config.oidc_op_discovery_endpoint == ""


@pytest.mark.django_db
def test_configure_fails_with_multiple_configs(multiple_configs_yml):
with pytest.raises(ConfigurationRunFailed) as excinfo:
execute_single_step(
AdminOIDCConfigurationStep, yaml_source=multiple_configs_yml
)
assert (
str(excinfo.value)
== "Currently it is only possible to specify a single OIDC configuration"
)

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


@pytest.mark.django_db
def test_configure_fails_without_identifier(missing_identifier_yml):
with pytest.raises(PrerequisiteFailed) as excinfo:
execute_single_step(
AdminOIDCConfigurationStep, yaml_source=missing_identifier_yml
)
assert "oidc_db_config_admin_auth.configurations.0.identifier" in str(excinfo.value)

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


@pytest.mark.django_db
def test_sync_groups_is_false(no_sync_groups_config_yml):
# create groups so they can be found
Expand All @@ -253,8 +283,10 @@ def test_sync_groups_is_false(no_sync_groups_config_yml):
AdminOIDCConfigurationStep, yaml_source=no_sync_groups_config_yml
)

assert not result.config_model.sync_groups
assert result.config_model.default_groups == ["SuperAdmins", "NormalUsers"]
config_model = result.config_model.configurations[0]

assert not config_model.sync_groups
assert config_model.default_groups == ["SuperAdmins", "NormalUsers"]

config = OpenIDConnectConfig.get_solo()
assert config.default_groups.all().count() == 1
Expand All @@ -271,14 +303,16 @@ def test_sync_groups_is_true(sync_groups_config_yml):
AdminOIDCConfigurationStep, yaml_source=sync_groups_config_yml
)

assert result.config_model.sync_groups
assert result.config_model.default_groups == [
config_model = result.config_model.configurations[0]

assert config_model.sync_groups
assert config_model.default_groups == [
"local.groups.SuperAdmins",
"local.WeirdAdmins",
"local.groups.NormalUsers",
"local.WeirdUsers",
]
assert result.config_model.sync_groups_glob_pattern == "local.groups.*"
assert config_model.sync_groups_glob_pattern == "local.groups.*"

config = OpenIDConnectConfig.get_solo()
assert config.default_groups.all().count() == 3
Expand Down

0 comments on commit 38c6eb1

Please sign in to comment.