diff --git a/tests/files/services.yaml b/tests/files/setup_config_services.yaml similarity index 73% rename from tests/files/services.yaml rename to tests/files/setup_config_services.yaml index da8ee55..4f7a6aa 100644 --- a/tests/files/services.yaml +++ b/tests/files/setup_config_services.yaml @@ -1,7 +1,7 @@ -ZGW_CONSUMERS_CONFIG_ENABLE: True -ZGW_CONSUMERS: +zgw_consumers_config_enable: True +zgw_consumers: services: - - slug: objecten-test + - identifier: objecten-test label: Objecten API test api_root: http://objecten.local/api/v1/ api_connection_check_path: objects @@ -9,10 +9,9 @@ ZGW_CONSUMERS: auth_type: api_key header_key: Authorization header_value: Token foo - - slug: zaken-test + - identifier: zaken-test label: Zaken API test api_root: http://zaken.local/api/v1/ - connection_check_expected_status: 400 api_type: zrc auth_type: zgw client_id: client diff --git a/tests/test_configuration_steps.py b/tests/test_configuration_steps.py index 4115b46..2f46345 100644 --- a/tests/test_configuration_steps.py +++ b/tests/test_configuration_steps.py @@ -1,13 +1,11 @@ import pytest -import requests_mock -from django_setup_configuration.exceptions import SelfTestFailed from django_setup_configuration.test_utils import load_step_config_from_source from zgw_consumers.contrib.setup_configuration.steps import ServiceConfigurationStep from zgw_consumers.models import Service from zgw_consumers.test.factories import ServiceFactory -CONFIG_FILE_PATH = "tests/files/services.yaml" +CONFIG_FILE_PATH = "tests/files/setup_config_services.yaml" @pytest.fixture() @@ -46,24 +44,6 @@ def test_execute_configuration_step_success(step_config_model): assert zaken_service.timeout == 10 -@pytest.mark.django_db -def test_execute_configuration_step_already_configured(step_config_model): - ServiceFactory.create( - slug="objecten-test", - label="Objecten", - api_root="http://some.other.existing.service.local/api/v1/", - ) - ServiceFactory.create( - slug="zaken-test", - label="Zaken", - api_root="http://some.existing.service.local/api/v1/", - ) - - step = ServiceConfigurationStep() - - assert step.is_configured(step_config_model) - - @pytest.mark.django_db def test_execute_configuration_step_update_existing(step_config_model): ServiceFactory.create( @@ -89,36 +69,3 @@ def test_execute_configuration_step_update_existing(step_config_model): assert zaken_service.slug == "zaken-test" assert zaken_service.label == "Zaken API test" assert zaken_service.api_root == "http://zaken.local/api/v1/" - - -@pytest.mark.django_db -def test_execute_configuration_step_validate_result_success(step_config_model): - step = ServiceConfigurationStep() - - assert not step.is_configured(step_config_model) - - step.execute(step_config_model) - - with requests_mock.Mocker() as m: - m.get("http://objecten.local/api/v1/objects") - m.get("http://zaken.local/api/v1/", status_code=400) - step.validate_result(step_config_model) - - -@pytest.mark.django_db -def test_execute_configuration_step_validate_result_failure(step_config_model): - step = ServiceConfigurationStep() - - assert not step.is_configured(step_config_model) - - step.execute(step_config_model) - - with requests_mock.Mocker() as m: - m.get("http://objecten.local/api/v1/objects") - m.get("http://zaken.local/api/v1/", status_code=404) - with pytest.raises(SelfTestFailed) as excinfo: - step.validate_result(step_config_model) - assert ( - str(excinfo.value) - == "non-success response from the following service(s): ['zaken-test']" - ) diff --git a/zgw_consumers/contrib/setup_configuration/models.py b/zgw_consumers/contrib/setup_configuration/models.py index a364ab1..192afb7 100644 --- a/zgw_consumers/contrib/setup_configuration/models.py +++ b/zgw_consumers/contrib/setup_configuration/models.py @@ -8,16 +8,13 @@ class SingleServiceConfigurationModel(ConfigurationModel): # TODO these should probably be defined in simple_certmanager and referred to? # client_certificate: FilePath = DjangoModelRef(Service, "client_certificate") # server_certificate: FilePath = DjangoModelRef(Service, "server_certificate") - connection_check_expected_status: int | None = Field( - description="The status code that indicates that the connection check is successful", - default=200, - ) + # Identifier is mapped to slug, because slug isn't a very descriptive name for devops + identifier: str = DjangoModelRef(Service, "slug") timeout: int | None = DjangoModelRef(Service, "timeout") class Meta: django_model_refs = { Service: [ - "slug", "label", "api_type", "api_root", diff --git a/zgw_consumers/contrib/setup_configuration/steps.py b/zgw_consumers/contrib/setup_configuration/steps.py index 2e997fd..3ad2c2a 100644 --- a/zgw_consumers/contrib/setup_configuration/steps.py +++ b/zgw_consumers/contrib/setup_configuration/steps.py @@ -1,5 +1,4 @@ from django_setup_configuration.configuration import BaseConfigurationStep -from django_setup_configuration.exceptions import SelfTestFailed from zgw_consumers.models import Service @@ -13,36 +12,31 @@ class ServiceConfigurationStep(BaseConfigurationStep[ServicesConfigurationModel] verbose_name = "Configuration to connect with external services" config_model = ServicesConfigurationModel - namespace = "ZGW_CONSUMERS" - enable_setting = "ZGW_CONSUMERS_CONFIG_ENABLE" + namespace = "zgw_consumers" + enable_setting = "zgw_consumers_config_enable" def is_configured(self, model: ServicesConfigurationModel) -> bool: - slugs = [config.slug for config in model.services] - return Service.objects.filter(slug__in=slugs).count() == len(slugs) + return False def execute(self, model: ServicesConfigurationModel): - ignore_fields = ["slug", "connection_check_expected_status"] for config in model.services: Service.objects.update_or_create( - slug=config.slug, + slug=config.identifier, defaults={ - k: v - for k, v in config.model_dump().items() - if k not in ignore_fields + "label": config.label, + "api_type": config.api_type, + "api_root": config.api_root, + "api_connection_check_path": config.api_connection_check_path, + "auth_type": config.auth_type, + "client_id": config.client_id, + "secret": config.secret, + "header_key": config.header_key, + "header_value": config.header_value, + "nlx": config.nlx, + "user_id": config.user_id, + "user_representation": config.user_representation, + "timeout": config.timeout, }, ) - def validate_result(self, model: ServicesConfigurationModel) -> None: - slugs = [config.slug for config in model.services] - failed_checks = [] - ordered_models = sorted(model.services, key=lambda x: x.slug) - for service, model in zip( - Service.objects.filter(slug__in=slugs).order_by("slug"), ordered_models - ): - if service.connection_check != model.connection_check_expected_status: - failed_checks.append(service.slug) - - if failed_checks: - raise SelfTestFailed( - f"non-success response from the following service(s): {failed_checks}" - ) + def validate_result(self, model: ServicesConfigurationModel) -> None: ...