diff --git a/notifications_api_common/management/commands/register_kanalen.py b/notifications_api_common/management/commands/register_kanalen.py index 4b65e36..86d3b5e 100644 --- a/notifications_api_common/management/commands/register_kanalen.py +++ b/notifications_api_common/management/commands/register_kanalen.py @@ -26,7 +26,7 @@ def create_kanaal(kanaal: str) -> None: # look up the exchange in the registry _kanaal = next(k for k in KANAAL_REGISTRY if k.label == kanaal) - kanalen = client.get("kanaal", params={"naam": kanaal}) + kanalen = client.get("kanaal", params={"naam": kanaal}).json() if kanalen: raise KanaalExists() diff --git a/testapp/settings.py b/testapp/settings.py index 98bf849..db28b97 100644 --- a/testapp/settings.py +++ b/testapp/settings.py @@ -2,6 +2,8 @@ DEBUG = True +SITE_ID = 1 + USE_TZ = False BASE_DIR = os.path.abspath(os.path.dirname(__file__)) @@ -28,6 +30,7 @@ "django.contrib.admin", "django.contrib.messages", "django.contrib.staticfiles", + "django.contrib.sites", "solo", "simple_certmanager", "zgw_consumers", diff --git a/testapp/urls.py b/testapp/urls.py index f7782af..ec5abc5 100644 --- a/testapp/urls.py +++ b/testapp/urls.py @@ -2,5 +2,19 @@ from django.urls import include, path from .api import router +from .views import KanalenView -urlpatterns = [path("admin/", admin.site.urls), path("api/", include(router.urls))] +urlpatterns = [ + path("admin/", admin.site.urls), + path("api/", include(router.urls)), + path( + "notificaties/", + include( + ( + [path("kanalen/", KanalenView.as_view(), name="kanalen")], + "notifications", + ), + namespace="notifications", + ), + ), +] diff --git a/testapp/views.py b/testapp/views.py new file mode 100644 index 0000000..699bc23 --- /dev/null +++ b/testapp/views.py @@ -0,0 +1,5 @@ +from django.views import View + + +class KanalenView(View): + pass diff --git a/tests/conftest.py b/tests/conftest.py index ac7ee5b..2d7577a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,8 @@ from notifications_api_common.models import NotificationsConfig from testapp import urls # noqa +NOTIFICATIONS_API_ROOT = "http://some-api-root/api/v1/" + def dummy_get_response(request): raise NotImplementedError() diff --git a/tests/test_register_kanalen.py b/tests/test_register_kanalen.py new file mode 100644 index 0000000..18c1225 --- /dev/null +++ b/tests/test_register_kanalen.py @@ -0,0 +1,50 @@ +from django.core.management import call_command +from django.utils.translation import gettext as _ + +import pytest +import requests_mock + +from .conftest import NOTIFICATIONS_API_ROOT + + +@pytest.mark.django_db +def test_register_kanalen(notifications_config): + with requests_mock.Mocker() as m: + m.get(f"{NOTIFICATIONS_API_ROOT}kanaal?naam=personen", json=[]) + m.post(f"{NOTIFICATIONS_API_ROOT}kanaal") + call_command("register_kanalen") + + assert len(m.request_history) == 2 + + get_request = m.request_history[0] + assert get_request.url == f"{NOTIFICATIONS_API_ROOT}kanaal?naam=personen" + + post_request = m.request_history[1] + assert post_request.url == f"{NOTIFICATIONS_API_ROOT}kanaal" + assert post_request.json() == { + "naam": "personen", + "documentatieLink": "https://example.com/notificaties/kanalen/#personen", + "filters": ["address_street"], + } + + +@pytest.mark.django_db +def test_register_kanalen_already_exists(notifications_config): + with requests_mock.Mocker() as m: + m.get( + f"{NOTIFICATIONS_API_ROOT}kanaal?naam=personen", + json=[ + { + "naam": "personen", + "documentatieLink": "https://example.com/notificaties/kanalen/#personen", + "filters": ["address_street"], + } + ], + ) + m.post(f"{NOTIFICATIONS_API_ROOT}kanaal") + call_command("register_kanalen") + + assert len(m.request_history) == 1 + + get_request = m.request_history[0] + assert get_request.url == f"{NOTIFICATIONS_API_ROOT}kanaal?naam=personen" diff --git a/tests/test_register_webhook.py b/tests/test_register_webhook.py index 346ac43..c30bf2d 100644 --- a/tests/test_register_webhook.py +++ b/tests/test_register_webhook.py @@ -4,29 +4,17 @@ from django.utils.translation import gettext as _ import pytest -from requests import Response +import requests_mock from requests.exceptions import HTTPError, RequestException from notifications_api_common.admin import register_webhook from notifications_api_common.models import Subscription +from .conftest import NOTIFICATIONS_API_ROOT -class MockResponse: - def __init__(self, data): - self.data = data - def json(self): - return self.data - - -@patch( - "ape_pie.APIClient.post", - return_value=MockResponse({"url": "https://example.com/api/v1/abonnementen/1"}), -) @pytest.mark.django_db -def test_register_webhook_success( - request_with_middleware, notifications_config, *mocks -): +def test_register_webhook_success(request_with_middleware, notifications_config): subscription = Subscription.objects.create( callback_url="https://example.com/callback", client_id="client_id", @@ -34,7 +22,12 @@ def test_register_webhook_success( channels=["zaken"], ) - register_webhook(object, request_with_middleware, Subscription.objects.all()) + with requests_mock.Mocker() as m: + m.post( + f"{NOTIFICATIONS_API_ROOT}abonnement", + json={"url": "https://example.com/api/v1/abonnementen/1"}, + ) + register_webhook(object, request_with_middleware, Subscription.objects.all()) messages = list(get_messages(request_with_middleware)) @@ -55,7 +48,7 @@ def test_register_webhook_request_exception( channels=["zaken"], ) - with patch("ape_pie.APIClient.post", side_effect=RequestException("exception")): + with patch("requests.Session.post", side_effect=RequestException("exception")): register_webhook(object, request_with_middleware, Subscription.objects.all()) messages = list(get_messages(request_with_middleware)) @@ -75,7 +68,7 @@ def test_register_webhook_http_error(request_with_middleware, notifications_conf channels=["zaken"], ) - with patch("ape_pie.APIClient.post", side_effect=HTTPError("400")): + with patch("requests.Session.post", side_effect=HTTPError("400")): register_webhook(object, request_with_middleware, Subscription.objects.all()) messages = list(get_messages(request_with_middleware)) diff --git a/tests/test_send_message.py b/tests/test_send_message.py index 2c4c380..c891ca0 100644 --- a/tests/test_send_message.py +++ b/tests/test_send_message.py @@ -11,7 +11,8 @@ from notifications_api_common.tasks import NotificationException, send_notification from testapp.models import Person -NOTIFICATIONS_API_ROOT = "http://some-api-root/api/v1/" +from .conftest import NOTIFICATIONS_API_ROOT + TESTS_DIR = Path(__file__).parent