From 6ef3d95494785b89b87cf02afec1041fe2863a56 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Tue, 28 Jun 2022 10:10:28 +0200 Subject: [PATCH 1/2] :recycle: Accept 20x status codes from subscriber callbacks --- src/nrc/api/validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nrc/api/validators.py b/src/nrc/api/validators.py index 9aef2a53..41ab993f 100644 --- a/src/nrc/api/validators.py +++ b/src/nrc/api/validators.py @@ -61,7 +61,7 @@ def __call__(self, attrs): except requests.RequestException: raise serializers.ValidationError(self.message, code=self.code) - if response.status_code != 204: + if not (200 <= response.status_code <= 209): raise serializers.ValidationError(self.message, code=self.code) From e9abc69fa527bbb76a05d170cf095ad24e826248 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Tue, 28 Jun 2022 10:10:51 +0200 Subject: [PATCH 2/2] :white_check_mark: Fix/add tests for callback status validation --- src/nrc/api/tests/test_validation.py | 35 +++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/nrc/api/tests/test_validation.py b/src/nrc/api/tests/test_validation.py index 863aea65..7b9664b8 100644 --- a/src/nrc/api/tests/test_validation.py +++ b/src/nrc/api/tests/test_validation.py @@ -196,7 +196,7 @@ def test_subscriptions_invalid_sink(self): # Let callback url return 201 instead of required 204 when # sending a notification m.register_uri( - "POST", "https://endpoint.example.com/webhook", status_code=201 + "POST", "https://endpoint.example.com/webhook", status_code=302 ) response = self.client.post(subscription_create_url, data) @@ -207,6 +207,39 @@ def test_subscriptions_invalid_sink(self): error = get_validation_errors(response, "nonFieldErrors") self.assertEqual(error["code"], "invalid-callback-url") + @override_settings( + LINK_FETCHER="vng_api_common.mocks.link_fetcher_404", + ZDS_CLIENT_CLASS="vng_api_common.mocks.MockClient", + ) + def test_subscriptions_callback_url_accept_20x_status_codes(self): + DomainFactory.create(name="nl.vng.zaken") + + subscription_create_url = get_operation_url("subscription_create") + + data = { + "protocol": ProtocolChoices.HTTP, + "source": "urn:nld:oin:00000001234567890000:systeem:Zaaksysteem", + "sink": "https://endpoint.example.com/webhook", + "domain": "nl.vng.zaken", + } + + accepted_status_codes = range(200, 210) + for status_code in accepted_status_codes: + with self.subTest(callback_status_code=status_code): + with requests_mock.mock() as m: + # Let callback url return a 20x status code + m.register_uri( + "POST", + "https://endpoint.example.com/webhook", + status_code=status_code, + ) + response = self.client.post(subscription_create_url, data) + + self.assertEqual( + response.status_code, status.HTTP_201_CREATED, response.data + ) + Subscription.objects.get().delete() + @override_settings( LINK_FETCHER="vng_api_common.mocks.link_fetcher_404", ZDS_CLIENT_CLASS="vng_api_common.mocks.MockClient",