Skip to content

Commit

Permalink
♻️ [#100] Change several implementation details after discussion
Browse files Browse the repository at this point in the history
* use lowercase for root level keys in YAML files (e.g. `zgw_consumers_config_enable`)
* use `identifier` instead of `slug` in config files
* add `setup_config_` prefix to test files
* remove `is_configured` and `validate_result`
  • Loading branch information
stevenbal committed Nov 21, 2024
1 parent 27ed8ed commit 2c012a0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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
api_type: orc
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
Expand Down
55 changes: 1 addition & 54 deletions tests/test_configuration_steps.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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']"
)
7 changes: 2 additions & 5 deletions zgw_consumers/contrib/setup_configuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 18 additions & 24 deletions zgw_consumers/contrib/setup_configuration/steps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django_setup_configuration.configuration import BaseConfigurationStep
from django_setup_configuration.exceptions import SelfTestFailed

from zgw_consumers.models import Service

Expand All @@ -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: ...

0 comments on commit 2c012a0

Please sign in to comment.