-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ [#200] Add/fix tests for setup config steps
- Loading branch information
Showing
7 changed files
with
156 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
zgw_consumers_config_enable: True | ||
zgw_consumers: | ||
services: | ||
- identifier: autorisaties-api | ||
label: Objecttypen API test | ||
api_root: http://localhost:8001/autorisaties/api/v1/ | ||
api_type: ac | ||
auth_type: zgw | ||
client_id: open-notificaties | ||
secret: oPMsHCEuoP9Qh8vP06D7 | ||
user_id: open-notificaties | ||
user_representation: Open Notificaties Demodam | ||
|
||
autorisaties_api_config_enable: True | ||
autorisaties_api: | ||
# Configure Open Notificaties to make use of Open Zaak's Autorisaties API | ||
authorizations_api_service_identifier: autorisaties-api | ||
|
||
vng_api_common_credentials_config_enable: True | ||
vng_api_common_credentials: | ||
items: | ||
# Credentials for Open Zaak to be able to make requests to Open Notificaties | ||
- identifier: open-zaak | ||
secret: G2LIVfXal1J93puQkV3O | ||
|
||
notifications_config_enable: True | ||
notifications_config: | ||
# No notifications_api_service necessary, because Open Notificaties doesn't send | ||
# notifications to itself | ||
notification_delivery_max_retries: 5 | ||
notification_delivery_retry_backoff: 3 | ||
notification_delivery_retry_backoff_max: 30 | ||
|
||
site_config_enable: True | ||
site_config: | ||
domain: opennotificaties.local:8000 | ||
organization: Demodam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
autorisaties_api_config_enable: True | ||
autorisaties_api: | ||
authorizations_api_service_identifier: autorisaties-api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
site_config_enable: True | ||
site_config: | ||
domain: opennotificaties.local:8000 | ||
organization: Demodam |
145 changes: 37 additions & 108 deletions
145
src/nrc/tests/config/test_authorization_configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,58 @@ | ||
from unittest.mock import patch | ||
from django.test import TestCase | ||
|
||
from django.test import TestCase, override_settings | ||
|
||
import requests | ||
import requests_mock | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
from django_setup_configuration.test_utils import execute_single_step | ||
from vng_api_common.authorizations.models import AuthorizationsConfig, ComponentTypes | ||
from vng_api_common.models import JWTSecret | ||
|
||
from nrc.config.authorization import AuthorizationStep, OpenZaakAuthStep | ||
from zgw_consumers.test.factories import ServiceFactory | ||
|
||
from nrc.config.authorization import AuthorizationStep | ||
|
||
@override_settings( | ||
AUTORISATIES_API_ROOT="https://oz.example.com/autorisaties/api/v1/", | ||
NOTIF_OPENZAAK_CLIENT_ID="notif-client-id", | ||
NOTIF_OPENZAAK_SECRET="notif-secret", | ||
) | ||
class AuthorizationConfigurationTests(TestCase): | ||
def test_configure(self): | ||
configuration = AuthorizationStep() | ||
CONFIG_FILE_PATH = "src/nrc/tests/config/files/setup_config_auth_config.yaml" | ||
|
||
configuration.configure() | ||
|
||
config = AuthorizationsConfig.get_solo() | ||
service = config.authorizations_api_service | ||
class AuthorizationConfigurationTests(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
super().setUpTestData() | ||
|
||
self.assertEqual(config.component, ComponentTypes.nrc) | ||
self.assertEqual( | ||
service.api_root, "https://oz.example.com/autorisaties/api/v1/" | ||
cls.service = ServiceFactory.create( | ||
slug="autorisaties-api", | ||
api_root="http://openzaak.local/autorisaties/api/v1/", | ||
) | ||
|
||
self.assertEqual(service.client_id, "notif-client-id") | ||
self.assertEqual(service.secret, "notif-secret") | ||
def test_execute_configuration_step_success(self): | ||
execute_single_step(AuthorizationStep, yaml_source=CONFIG_FILE_PATH) | ||
|
||
@requests_mock.Mocker() | ||
def test_selftest_ok(self, m): | ||
configuration = AuthorizationStep() | ||
configuration.configure() | ||
|
||
m.get("https://oz.example.com/autorisaties/api/v1/applicaties", json=[]) | ||
config = AuthorizationsConfig.get_solo() | ||
|
||
configuration.test_configuration() | ||
self.assertEqual(config.component, ComponentTypes.nrc) | ||
self.assertEqual(config.authorizations_api_service, self.service) | ||
|
||
self.assertEqual( | ||
m.last_request.url, "https://oz.example.com/autorisaties/api/v1/applicaties" | ||
) | ||
def test_execute_configuration_step_update_existing(self): | ||
config = AuthorizationsConfig.get_solo() | ||
config.component = ComponentTypes.zrc | ||
config.authorizations_api_service = ServiceFactory.create(slug="other-api") | ||
config.save() | ||
|
||
@requests_mock.Mocker() | ||
def test_selftest_fail(self, m): | ||
configuration = AuthorizationStep() | ||
configuration.configure() | ||
execute_single_step(AuthorizationStep, yaml_source=CONFIG_FILE_PATH) | ||
|
||
m.get("https://oz.example.com/autorisaties/api/v1/applicaties", status_code=403) | ||
config = AuthorizationsConfig.get_solo() | ||
service = config.authorizations_api_service | ||
|
||
with self.assertRaises(SelfTestFailed): | ||
configuration.test_configuration() | ||
self.assertEqual(config.component, ComponentTypes.nrc) | ||
self.assertEqual(service, self.service) | ||
|
||
self.assertEqual( | ||
m.last_request.url, "https://oz.example.com/autorisaties/api/v1/applicaties" | ||
) | ||
def test_execute_configuration_step_idempotent(self): | ||
def make_assertions(): | ||
config = AuthorizationsConfig.get_solo() | ||
service = config.authorizations_api_service | ||
|
||
def test_is_configured(self): | ||
configuration = AuthorizationStep() | ||
self.assertFalse(configuration.is_configured()) | ||
|
||
configuration.configure() | ||
|
||
self.assertTrue(configuration.is_configured()) | ||
|
||
|
||
@override_settings( | ||
OPENZAAK_NOTIF_CLIENT_ID="oz-client-id", | ||
OPENZAAK_NOTIF_SECRET="oz-secret", | ||
) | ||
class OpenZaakConfigurationTests(TestCase): | ||
def test_configure(self): | ||
configuration = OpenZaakAuthStep() | ||
|
||
configuration.configure() | ||
|
||
jwt_secret = JWTSecret.objects.get(identifier="oz-client-id") | ||
self.assertEqual(jwt_secret.secret, "oz-secret") | ||
|
||
@requests_mock.Mocker() | ||
@patch( | ||
"nrc.config.authorization.build_absolute_url", | ||
return_value="http://testserver/kanaal", | ||
) | ||
def test_selftest_ok(self, m, *mocks): | ||
configuration = OpenZaakAuthStep() | ||
configuration.configure() | ||
m.get("http://testserver/kanaal", json=[]) | ||
|
||
configuration.test_configuration() | ||
|
||
self.assertEqual(m.last_request.url, "http://testserver/kanaal") | ||
self.assertEqual(m.last_request.method, "GET") | ||
|
||
@requests_mock.Mocker() | ||
@patch( | ||
"nrc.config.authorization.build_absolute_url", | ||
return_value="http://testserver/kanaal", | ||
) | ||
def test_selftest_fail(self, m, *mocks): | ||
configuration = OpenZaakAuthStep() | ||
configuration.configure() | ||
|
||
mock_kwargs = ( | ||
{"exc": requests.ConnectTimeout}, | ||
{"exc": requests.ConnectionError}, | ||
{"status_code": 404}, | ||
{"status_code": 403}, | ||
{"status_code": 500}, | ||
) | ||
for mock_config in mock_kwargs: | ||
with self.subTest(mock=mock_config): | ||
m.get("http://testserver/kanaal", **mock_config) | ||
self.assertEqual(config.component, ComponentTypes.nrc) | ||
self.assertEqual(service, self.service) | ||
|
||
with self.assertRaises(SelfTestFailed): | ||
configuration.test_configuration() | ||
execute_single_step(AuthorizationStep, yaml_source=CONFIG_FILE_PATH) | ||
|
||
def test_is_configured(self): | ||
configuration = OpenZaakAuthStep() | ||
self.assertFalse(configuration.is_configured()) | ||
make_assertions() | ||
|
||
configuration.configure() | ||
execute_single_step(AuthorizationStep, yaml_source=CONFIG_FILE_PATH) | ||
|
||
self.assertTrue(configuration.is_configured()) | ||
make_assertions() |
Oops, something went wrong.