Skip to content

Commit

Permalink
✅ [open-zaak/open-notificaties#207] Add tests for register_kanalen up…
Browse files Browse the repository at this point in the history
…date
  • Loading branch information
stevenbal committed Dec 17, 2024
1 parent f8d9358 commit 06d335c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 39 deletions.
14 changes: 13 additions & 1 deletion testapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@

from .api import router

urlpatterns = [path("admin/", admin.site.urls), path("api/", include(router.urls))]
notifications_patterns = [
path("kanalen/", lambda request: request, name="kanalen"),
]


urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(router.urls)),
path(
"notifications/",
include((notifications_patterns, "notifications"), namespace="notifications"),
),
]
114 changes: 76 additions & 38 deletions tests/test_register_kanalen.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from io import StringIO
from unittest.mock import Mock, patch
from unittest.mock import Mock
from urllib.parse import urlencode

from django.test.testcases import call_command

import pytest
from furl import furl

from notifications_api_common.kanalen import KANAAL_REGISTRY, Kanaal

from .conftest import NOTIFICATIONS_API_ROOT


@pytest.fixture
def override_kanalen():

kanalen = set(
(
Kanaal(label="foobar", main_resource=Mock()),
Kanaal(label="boofar", main_resource=Mock()),
Kanaal(
label="foobar", main_resource=Mock(), kenmerken=("kenmerk1", "kenmerk2")
),
Kanaal(label="boofar", main_resource=Mock(), kenmerken=("kenmerk1",)),
)
)

Expand Down Expand Up @@ -43,14 +48,7 @@ def test_register_kanalen_success(
status_code=201,
)

reverse_patch = (
"notifications_api_common.management.commands.register_kanalen.reverse"
)

with patch(reverse_patch) as mocked_reverse:
mocked_reverse.return_value = "/notifications/kanalen"

call_command("register_kanalen", kanalen=["foobar"])
call_command("register_kanalen", kanalen=["foobar"])

assert len(requests_mock.request_history) == 2

Expand Down Expand Up @@ -78,7 +76,7 @@ def test_register_kanalen_from_registry_success(
requests_mock.post(
kanaal_url,
json={
"url": "http://example.com",
"url": f"{NOTIFICATIONS_API_ROOT}kanalen/1",
"naam": "foobar",
"documentatieLink": "http://example.com",
"filters": ["string"],
Expand All @@ -89,22 +87,15 @@ def test_register_kanalen_from_registry_success(
requests_mock.post(
kanaal_url,
json={
"url": "http://example.com",
"url": f"{NOTIFICATIONS_API_ROOT}kanalen/2",
"naam": "boofar",
"documentatieLink": "http://example.com",
"filters": ["string"],
},
status_code=201,
)

reverse_patch = (
"notifications_api_common.management.commands.register_kanalen.reverse"
)

with patch(reverse_patch) as mocked_reverse:
mocked_reverse.return_value = "/notifications/kanalen"

call_command("register_kanalen")
call_command("register_kanalen")

assert len(requests_mock.request_history) == 4

Expand All @@ -127,30 +118,84 @@ def test_register_kanalen_existing_kanalen(
notifications_config, requests_mock, override_kanalen
):
"""
Test that already registered kanalen does not cause issues
Test that already registered kanalen are updated
"""
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal"
params = urlencode(dict(naam="foobar"))

requests_mock.get(
f"{kanaal_url}?{params}",
furl(kanaal_url).set({"naam": "foobar"}).url,
json=[
{
"url": "http://example.com",
"url": f"{NOTIFICATIONS_API_ROOT}kanalen/1",
"naam": "foobar",
"documentatieLink": "http://example.com",
"documentatieLink": "http://old.example.com",
"filters": ["string"],
}
],
)
requests_mock.put(
f"{NOTIFICATIONS_API_ROOT}kanalen/1",
json=[
{
"url": f"{NOTIFICATIONS_API_ROOT}kanalen/1",
"naam": "foobar",
"documentatieLink": "http://old.example.com",
"filters": ["string"],
}
],
)

call_command("register_kanalen", kanalen=["foobar"])
requests_mock.get(
furl(kanaal_url).set({"naam": "boofar"}).url,
json=[
{
"url": f"{NOTIFICATIONS_API_ROOT}kanalen/2",
"naam": "foobar",
"documentatieLink": "http://old.example.com",
"filters": [],
}
],
)
requests_mock.put(
f"{NOTIFICATIONS_API_ROOT}kanalen/2",
json=[
{
"url": f"{NOTIFICATIONS_API_ROOT}kanalen/2",
"naam": "foobar",
"documentatieLink": "http://old.example.com",
"filters": [],
}
],
)

assert len(requests_mock.request_history) == 1
call_command("register_kanalen")

request = requests_mock.request_history[0]
assert len(requests_mock.request_history) == 4

assert request._request.url == f"{kanaal_url}?{params}"
get_request1, put_request1, get_request2, put_request2 = (
requests_mock.request_history
)
assert get_request1.method == "GET"
assert get_request1.url == furl(kanaal_url).set({"naam": "boofar"}).url

assert put_request1.method == "PUT"
assert put_request1.url == f"{NOTIFICATIONS_API_ROOT}kanalen/2"
assert put_request1.json() == {
"naam": "boofar",
"documentatieLink": "https://example.com/notifications/kanalen/#boofar",
"filters": ["kenmerk1"],
}

assert get_request2.method == "GET"
assert get_request2.url == furl(kanaal_url).set({"naam": "foobar"}).url

assert put_request2.method == "PUT"
assert put_request2.url == f"{NOTIFICATIONS_API_ROOT}kanalen/1"
assert put_request2.json() == {
"naam": "foobar",
"documentatieLink": "https://example.com/notifications/kanalen/#foobar",
"filters": ["kenmerk1", "kenmerk2"],
}


@pytest.mark.django_db
Expand Down Expand Up @@ -190,14 +235,7 @@ def test_register_kanalen_incorrect_post(

stderr = StringIO()

reverse_patch = (
"notifications_api_common.management.commands.register_kanalen.reverse"
)

with patch(reverse_patch) as mocked_reverse:
mocked_reverse.return_value = "/notifications/kanalen"

call_command("register_kanalen", kanalen=["foobar"], stderr=stderr)
call_command("register_kanalen", kanalen=["foobar"], stderr=stderr)

partial_failure_message = "Unable to create kanaal foobar"

Expand Down

0 comments on commit 06d335c

Please sign in to comment.