Skip to content

Commit

Permalink
✅ [#100] Tests with required service fields and all service fields
Browse files Browse the repository at this point in the history
and add a test for idempotency
  • Loading branch information
stevenbal committed Nov 21, 2024
1 parent 2c012a0 commit e749ac4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 22 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions tests/files/setup_config_services_all_fields.yaml
Original file line number Diff line number Diff line change
@@ -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: ...
7 changes: 7 additions & 0 deletions tests/files/setup_config_services_required_fields.yaml
Original file line number Diff line number Diff line change
@@ -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
117 changes: 95 additions & 22 deletions tests/test_configuration_steps.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import pytest
from django_setup_configuration.test_utils import load_step_config_from_source
from django_setup_configuration.test_utils import execute_single_step

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"


@pytest.fixture()
def step_config_model():
return load_step_config_from_source(ServiceConfigurationStep, CONFIG_FILE_PATH)
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.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)
def test_execute_configuration_step_success():
execute_single_step(ServiceConfigurationStep, CONFIG_FILE_PATH)

assert Service.objects.count() == 2

Expand All @@ -28,35 +24,31 @@ 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

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.django_db
def test_execute_configuration_step_update_existing(step_config_model):
def test_execute_configuration_step_update_existing():
ServiceFactory.create(
slug="zaken-test",
label="Objecttypen",
api_root="http://some.existing.service.local/api/v1/",
)

step = ServiceConfigurationStep()

assert not step.is_configured(step_config_model)

step.execute(step_config_model)
execute_single_step(ServiceConfigurationStep, CONFIG_FILE_PATH)

assert Service.objects.count() == 2

Expand All @@ -69,3 +61,84 @@ 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_with_required_fields():
execute_single_step(ServiceConfigurationStep, CONFIG_FILE_PATH_REQUIRED_FIELDS)

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.django_db
def test_execute_configuration_step_with_all_fields():
execute_single_step(ServiceConfigurationStep, CONFIG_FILE_PATH_ALL_FIELDS)

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.django_db
def test_execute_configuration_step_idempotent():
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

execute_single_step(ServiceConfigurationStep, CONFIG_FILE_PATH_ALL_FIELDS)

make_assertions()

execute_single_step(ServiceConfigurationStep, CONFIG_FILE_PATH_ALL_FIELDS)

make_assertions()

0 comments on commit e749ac4

Please sign in to comment.