Skip to content

Commit

Permalink
✅ Add tests for register_kanalen
Browse files Browse the repository at this point in the history
and clean up mock usage in other tests
  • Loading branch information
stevenbal committed Sep 27, 2024
1 parent d0ea73e commit 64885ff
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

DEBUG = True

SITE_ID = 1

USE_TZ = False

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -28,6 +30,7 @@
"django.contrib.admin",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
"solo",
"simple_certmanager",
"zgw_consumers",
Expand Down
16 changes: 15 additions & 1 deletion testapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
),
]
5 changes: 5 additions & 0 deletions testapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.views import View


class KanalenView(View):
pass
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
50 changes: 50 additions & 0 deletions tests/test_register_kanalen.py
Original file line number Diff line number Diff line change
@@ -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"
29 changes: 11 additions & 18 deletions tests/test_register_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@
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",
secret="secret",
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))

Expand All @@ -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))
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion tests/test_send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 64885ff

Please sign in to comment.