From 94b253457b7c6cb5697373c162d65a587a0c7f1f Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Tue, 19 Nov 2024 09:40:34 +0100 Subject: [PATCH] :white_check_mark: [#100] Tests with required service fields and all service fields and add a test for idempotency --- pyproject.toml | 3 + .../setup_config_services_all_fields.yaml | 20 +++ ...setup_config_services_required_fields.yaml | 7 ++ tests/test_configuration_steps.py | 115 ++++++++++++++++-- 4 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 tests/files/setup_config_services_all_fields.yaml create mode 100644 tests/files/setup_config_services_required_fields.yaml diff --git a/pyproject.toml b/pyproject.toml index b1e6678..0b63ce8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,6 +102,9 @@ skip = ["env", ".tox", ".history"] [tool.pytest.ini_options] testpaths = ["tests"] DJANGO_SETTINGS_MODULE = "testapp.settings" +markers = [ + "config_path: the path to the YAML file that is loaded for setup_configuration", +] [tool.bumpversion] current_version = "0.35.1" diff --git a/tests/files/setup_config_services_all_fields.yaml b/tests/files/setup_config_services_all_fields.yaml new file mode 100644 index 0000000..1713be0 --- /dev/null +++ b/tests/files/setup_config_services_all_fields.yaml @@ -0,0 +1,20 @@ +zgw_consumers_config_enable: True +zgw_consumers: + services: + - 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 + client_id: client + secret: super-secret + nlx: http://some-outway-adress.local:8080/ + user_id: open-formulieren + user_representation: Open Formulieren + timeout: 5 + # NOT SUPPORTED YET + # client_certificatie: ... + # server_certificatie: ... diff --git a/tests/files/setup_config_services_required_fields.yaml b/tests/files/setup_config_services_required_fields.yaml new file mode 100644 index 0000000..f742ace --- /dev/null +++ b/tests/files/setup_config_services_required_fields.yaml @@ -0,0 +1,7 @@ +zgw_consumers_config_enable: True +zgw_consumers: + services: + - identifier: objecten-test + label: Objecten API test + api_root: http://objecten.local/api/v1/ + api_type: orc diff --git a/tests/test_configuration_steps.py b/tests/test_configuration_steps.py index 2f46345..463ddf1 100644 --- a/tests/test_configuration_steps.py +++ b/tests/test_configuration_steps.py @@ -1,24 +1,30 @@ import pytest from django_setup_configuration.test_utils import load_step_config_from_source +from zgw_consumers.constants import APITypes, AuthTypes 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/setup_config_services.yaml" +CONFIG_FILE_PATH_REQUIRED_FIELDS = ( + "tests/files/setup_config_services_required_fields.yaml" +) +CONFIG_FILE_PATH_ALL_FIELDS = "tests/files/setup_config_services_all_fields.yaml" @pytest.fixture() -def step_config_model(): - return load_step_config_from_source(ServiceConfigurationStep, CONFIG_FILE_PATH) +def step_config_model(request): + marker = request.node.get_closest_marker("config_step") + path = marker.kwargs["path"] + return load_step_config_from_source(ServiceConfigurationStep, path) +@pytest.mark.config_step(path=CONFIG_FILE_PATH) @pytest.mark.django_db def test_execute_configuration_step_success(step_config_model): step = ServiceConfigurationStep() - assert not step.is_configured(step_config_model) - step.execute(step_config_model) assert Service.objects.count() == 2 @@ -28,8 +34,8 @@ def test_execute_configuration_step_success(step_config_model): assert objects_service.slug == "objecten-test" assert objects_service.label == "Objecten API test" assert objects_service.api_root == "http://objecten.local/api/v1/" - assert objects_service.api_type == "orc" - assert objects_service.auth_type == "api_key" + assert objects_service.api_type == APITypes.orc + assert objects_service.auth_type == AuthTypes.api_key assert objects_service.header_key == "Authorization" assert objects_service.header_value == "Token foo" assert objects_service.timeout == 10 @@ -37,13 +43,14 @@ def test_execute_configuration_step_success(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/" - assert zaken_service.api_type == "zrc" - assert zaken_service.auth_type == "zgw" + assert zaken_service.api_type == APITypes.zrc + assert zaken_service.auth_type == AuthTypes.zgw assert zaken_service.client_id == "client" assert zaken_service.secret == "super-secret" assert zaken_service.timeout == 10 +@pytest.mark.config_step(path=CONFIG_FILE_PATH) @pytest.mark.django_db def test_execute_configuration_step_update_existing(step_config_model): ServiceFactory.create( @@ -54,8 +61,6 @@ def test_execute_configuration_step_update_existing(step_config_model): step = ServiceConfigurationStep() - assert not step.is_configured(step_config_model) - step.execute(step_config_model) assert Service.objects.count() == 2 @@ -69,3 +74,93 @@ 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.config_step(path=CONFIG_FILE_PATH_REQUIRED_FIELDS) +@pytest.mark.django_db +def test_execute_configuration_step_with_required_fields(step_config_model): + step = ServiceConfigurationStep() + + step.execute(step_config_model) + + assert Service.objects.count() == 1 + + objects_service = Service.objects.get() + + assert objects_service.slug == "objecten-test" + assert objects_service.label == "Objecten API test" + assert objects_service.api_root == "http://objecten.local/api/v1/" + assert objects_service.api_type == APITypes.orc + assert objects_service.auth_type == AuthTypes.zgw + assert objects_service.timeout == 10 + + # Not required fields + assert objects_service.api_connection_check_path == "" + assert objects_service.header_key == "" + assert objects_service.header_value == "" + assert objects_service.client_id == "" + assert objects_service.secret == "" + assert objects_service.nlx == "" + assert objects_service.user_id == "" + assert objects_service.user_representation == "" + + +@pytest.mark.config_step(path=CONFIG_FILE_PATH_ALL_FIELDS) +@pytest.mark.django_db +def test_execute_configuration_step_with_all_fields(step_config_model): + step = ServiceConfigurationStep() + + step.execute(step_config_model) + + assert Service.objects.count() == 1 + + objects_service = Service.objects.get() + + assert objects_service.slug == "objecten-test" + assert objects_service.label == "Objecten API test" + assert objects_service.api_root == "http://objecten.local/api/v1/" + assert objects_service.api_type == APITypes.orc + assert objects_service.auth_type == AuthTypes.api_key + assert objects_service.api_connection_check_path == "objects" + assert objects_service.header_key == "Authorization" + assert objects_service.header_value == "Token foo" + assert objects_service.client_id == "client" + assert objects_service.secret == "super-secret" + assert objects_service.nlx == "http://some-outway-adress.local:8080/" + assert objects_service.user_id == "open-formulieren" + assert objects_service.user_representation == "Open Formulieren" + assert objects_service.timeout == 5 + + +@pytest.mark.config_step(path=CONFIG_FILE_PATH_ALL_FIELDS) +@pytest.mark.django_db +def test_execute_configuration_step_idempotent(step_config_model): + step = ServiceConfigurationStep() + + def make_assertions(): + assert Service.objects.count() == 1 + + objects_service = Service.objects.get() + + assert objects_service.slug == "objecten-test" + assert objects_service.label == "Objecten API test" + assert objects_service.api_root == "http://objecten.local/api/v1/" + assert objects_service.api_type == APITypes.orc + assert objects_service.auth_type == AuthTypes.api_key + assert objects_service.api_connection_check_path == "objects" + assert objects_service.header_key == "Authorization" + assert objects_service.header_value == "Token foo" + assert objects_service.client_id == "client" + assert objects_service.secret == "super-secret" + assert objects_service.nlx == "http://some-outway-adress.local:8080/" + assert objects_service.user_id == "open-formulieren" + assert objects_service.user_representation == "Open Formulieren" + assert objects_service.timeout == 5 + + step.execute(step_config_model) + + make_assertions() + + step.execute(step_config_model) + + make_assertions()