|
1 | 1 | from unittest.mock import patch
|
2 | 2 |
|
3 |
| -from django.test import override_settings |
| 3 | +from django.test import TestCase, override_settings |
4 | 4 | from django.utils.timezone import now
|
5 | 5 |
|
| 6 | +import requests_mock |
| 7 | +from celery.exceptions import Retry |
| 8 | +from notifications_api_common.models import NotificationsConfig |
6 | 9 | from rest_framework import status
|
7 | 10 | from rest_framework.reverse import reverse
|
8 | 11 | from rest_framework.test import APITestCase
|
9 | 12 | from vng_api_common.conf.api import BASE_REST_FRAMEWORK
|
10 | 13 | from vng_api_common.tests import JWTAuthMixin
|
11 | 14 |
|
| 15 | +from nrc.api.tasks import deliver_message |
12 | 16 | from nrc.datamodel.models import Notificatie
|
13 | 17 | from nrc.datamodel.tests.factories import (
|
14 | 18 | AbonnementFactory,
|
@@ -142,3 +146,56 @@ def test_notificatie_send_empty_kenmerk_value(self, mock_task):
|
142 | 146 | mock_task.assert_called_once_with(
|
143 | 147 | abon.id, msg, notificatie_id=Notificatie.objects.get().id, attempt=1
|
144 | 148 | )
|
| 149 | + |
| 150 | + |
| 151 | +@patch("notifications_api_common.autoretry.get_exponential_backoff_interval") |
| 152 | +@patch("notifications_api_common.autoretry.NotificationsConfig.get_solo") |
| 153 | +@patch("nrc.api.serializers.deliver_message.retry") |
| 154 | +class NotificatieRetryTests(TestCase): |
| 155 | + def test_notificatie_retry_use_global_config( |
| 156 | + self, mock_retry, mock_config, mock_get_exponential_backoff |
| 157 | + ): |
| 158 | + """ |
| 159 | + Verify that retry variables configured on `NotificationsConfig` override the |
| 160 | + variables from the settings |
| 161 | + """ |
| 162 | + mock_config.return_value = NotificationsConfig( |
| 163 | + notification_delivery_max_retries=4, |
| 164 | + notification_delivery_retry_backoff=4, |
| 165 | + notification_delivery_retry_backoff_max=28, |
| 166 | + ) |
| 167 | + kanaal = KanaalFactory.create( |
| 168 | + naam="zaken", filters=["bron", "zaaktype", "vertrouwelijkheidaanduiding"] |
| 169 | + ) |
| 170 | + abon = AbonnementFactory.create(callback_url="https://example.com/callback") |
| 171 | + filter_group = FilterGroupFactory.create(kanaal=kanaal, abonnement=abon) |
| 172 | + FilterFactory.create( |
| 173 | + filter_group=filter_group, key="bron", value="082096752011" |
| 174 | + ) |
| 175 | + msg = { |
| 176 | + "kanaal": "zaken", |
| 177 | + "hoofdObject": "https://ref.tst.vng.cloud/zrc/api/v1/zaken/d7a22", |
| 178 | + "resource": "status", |
| 179 | + "resourceUrl": "https://ref.tst.vng.cloud/zrc/api/v1/statussen/d7a22/721c9", |
| 180 | + "actie": "create", |
| 181 | + "aanmaakdatum": now(), |
| 182 | + "kenmerken": { |
| 183 | + "bron": "082096752011", |
| 184 | + "zaaktype": "example.com/api/v1/zaaktypen/5aa5c", |
| 185 | + "vertrouwelijkheidaanduiding": "openbaar", |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + mock_retry.side_effect = Retry() |
| 190 | + with requests_mock.Mocker() as m: |
| 191 | + m.post(abon.callback_url, status_code=404) |
| 192 | + with self.assertRaises(Retry): |
| 193 | + deliver_message(abon.id, msg) |
| 194 | + |
| 195 | + mock_get_exponential_backoff.assert_called_once_with( |
| 196 | + factor=4, |
| 197 | + retries=0, |
| 198 | + maximum=28, |
| 199 | + full_jitter=False, |
| 200 | + ) |
| 201 | + self.assertEqual(deliver_message.max_retries, 4) |
0 commit comments