Skip to content

Commit

Permalink
✅ [#100] Add tests for ServiceConfigurationStep
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Nov 12, 2024
1 parent c440a96 commit 286fcbe
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"simple_certmanager",
"solo",
"testapp",
"django_setup_configuration",
]

MIDDLEWARE = [
Expand Down
17 changes: 17 additions & 0 deletions tests/files/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ZGW_CONSUMERS_CONFIG_ENABLE: True
ZGW_CONSUMERS:
services:
- slug: objecten-test
label: Objecten API test
api_root: http://objecten.local/api/v1/
api_type: orc
auth_type: api_key
header_key: Authorization
header_value: Token foo
- slug: zaken-test
label: Zaken API test
api_root: http://zaken.local/api/v1/
api_type: zrc
auth_type: zgw
client_id: client
secret: super-secret
124 changes: 124 additions & 0 deletions tests/test_configuration_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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"


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

objects_service, zaken_service = Service.objects.all()

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.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.client_id == "client"
assert zaken_service.secret == "super-secret"
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(
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)

assert Service.objects.count() == 2

objects_service, zaken_service = Service.objects.all()

assert objects_service.slug == "objecten-test"
assert objects_service.label == "Objecten API test"
assert objects_service.api_root == "http://objecten.local/api/v1/"

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/")
m.get("http://zaken.local/api/v1/")
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/")
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 configured service(s)', [HTTPError('404 Client Error: None for url: http://zaken.local/api/v1/')])"
)

0 comments on commit 286fcbe

Please sign in to comment.