|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import Mock, patch |
| 3 | + |
| 4 | +from django.core.cache import cache |
| 5 | +from django.test import TestCase |
| 6 | + |
| 7 | +from django_setup_configuration.test_utils import execute_single_step |
| 8 | +from zgw_consumers.constants import APITypes |
| 9 | +from zgw_consumers.models import Service |
| 10 | +from zgw_consumers.test.factories import ServiceFactory |
| 11 | + |
| 12 | +from ...models import APIConfig |
| 13 | +from ..steps import APIConfigConfigurationStep |
| 14 | + |
| 15 | +TEST_FILES = (Path(__file__).parent / "files").resolve() |
| 16 | +CONFIG_FILE_PATH_1 = str(TEST_FILES / "setup_config_api.yaml") |
| 17 | +CONFIG_FILE_PATH_2 = str(TEST_FILES / "setup_config_api_different_service.yaml") |
| 18 | + |
| 19 | + |
| 20 | +class APIConfigConfigurationStepTests(TestCase): |
| 21 | + def setUp(self): |
| 22 | + super().setUp() |
| 23 | + |
| 24 | + self.addCleanup(cache.clear) |
| 25 | + |
| 26 | + def test_configure_api_config_create_new(self): |
| 27 | + service = ServiceFactory( |
| 28 | + slug="selectielijst", |
| 29 | + api_type=APITypes.orc, |
| 30 | + api_root="https://selectielijst.openzaak.nl/api/v1/", |
| 31 | + ) |
| 32 | + |
| 33 | + execute_single_step(APIConfigConfigurationStep, yaml_source=CONFIG_FILE_PATH_1) |
| 34 | + |
| 35 | + config = APIConfig.get_solo() |
| 36 | + |
| 37 | + self.assertEqual(service.pk, config.selectielijst_api_service.pk) |
| 38 | + |
| 39 | + def test_configure_api_config_update_existing(self): |
| 40 | + service1 = ServiceFactory( |
| 41 | + slug="selectielijst", |
| 42 | + ) |
| 43 | + service2 = ServiceFactory( |
| 44 | + slug="selectielijst-new", |
| 45 | + ) |
| 46 | + |
| 47 | + config = APIConfig.get_solo() |
| 48 | + config.selectielijst_api_service = service1 |
| 49 | + config.save() |
| 50 | + |
| 51 | + execute_single_step(APIConfigConfigurationStep, yaml_source=CONFIG_FILE_PATH_2) |
| 52 | + |
| 53 | + config.refresh_from_db() |
| 54 | + |
| 55 | + self.assertEqual(service2.pk, config.selectielijst_api_service.pk) |
| 56 | + |
| 57 | + def test_configure_api_config_missing_service(self): |
| 58 | + with self.assertRaises(Service.DoesNotExist): |
| 59 | + execute_single_step( |
| 60 | + APIConfigConfigurationStep, yaml_source=CONFIG_FILE_PATH_1 |
| 61 | + ) |
| 62 | + |
| 63 | + def test_idempotency(self): |
| 64 | + service = ServiceFactory( |
| 65 | + slug="selectielijst", |
| 66 | + api_type=APITypes.orc, |
| 67 | + api_root="https://selectielijst.openzaak.nl/api/v1/", |
| 68 | + ) |
| 69 | + |
| 70 | + execute_single_step(APIConfigConfigurationStep, yaml_source=CONFIG_FILE_PATH_1) |
| 71 | + |
| 72 | + mock = Mock() |
| 73 | + config = APIConfig(selectielijst_api_service=service) |
| 74 | + with patch.object(config, "save", new=mock.method): |
| 75 | + with patch( |
| 76 | + "openarchiefbeheer.config.setup_configuration.steps.APIConfig.get_solo", |
| 77 | + return_value=config, |
| 78 | + ): |
| 79 | + execute_single_step( |
| 80 | + APIConfigConfigurationStep, yaml_source=CONFIG_FILE_PATH_1 |
| 81 | + ) |
| 82 | + |
| 83 | + mock.method.assert_not_called() |
0 commit comments