Skip to content

Commit

Permalink
✅ [open-zaak/open-notificaties#156] Add/fix tests for generate_notifi…
Browse files Browse the repository at this point in the history
…caties
  • Loading branch information
stevenbal committed Dec 13, 2024
1 parent 5f04d30 commit 48b9571
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 30 deletions.
8 changes: 6 additions & 2 deletions testapp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ class PersonViewSet(NotificationViewSetMixin, viewsets.ModelViewSet):
serializer_class = PersonSerializer
notifications_kanaal = Kanaal(
"personen",
main_resource=Person,
kenmerken=("address_street",),
main_resource=Person(),
kenmerken=("name", "address_street"),
extra_kwargs={"address_street": {"help_text": "custom help text"}},
)

def get_notification_main_object_url(self, data: dict, kanaal: Kanaal):
return data["url"]


# URL routing
router = routers.DefaultRouter()
Expand Down
3 changes: 3 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

SECRET_KEY = "so-secret-i-cant-believe-you-are-looking-at-this"

PROJECT_NAME = "Notifications API Common"
SITE_TITLE = "Notifications API Common"

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
Expand Down
51 changes: 51 additions & 0 deletions tests/test_generate_notificaties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from unittest.mock import mock_open, patch

from django.test.testcases import call_command

import pytest

EXPECTED_OUTPUT = """## Notificaties
## Berichtkenmerken voor Notifications API Common API
Kanalen worden typisch per component gedefinieerd. Producers versturen berichten op bepaalde kanalen,
consumers ontvangen deze. Consumers abonneren zich via een notificatiecomponent (zoals <a href="https://notificaties-api.vng.cloud/api/v1/schema/" rel="nofollow">https://notificaties-api.vng.cloud/api/v1/schema/</a>) op berichten.
Hieronder staan de kanalen beschreven die door deze component gebruikt worden, met de kenmerken bij elk bericht.
De architectuur van de notificaties staat beschreven op <a href="https://github.com/VNG-Realisatie/notificaties-api" rel="nofollow">https://github.com/VNG-Realisatie/notificaties-api</a>.
### personen
**Kanaal**
`personen`
**Main resource**
`person`
**Kenmerken**
* `name`: The name of the person
* `address_street`: custom help text
**Resources en acties**
* <code>person</code>: create, update, destroy
"""


@pytest.mark.django_db
@patch(
"notifications_api_common.management.commands.generate_notificaties.open",
new_callable=mock_open,
)
def test_generate_notificaties(mock_file):
call_command("generate_notificaties", output_file=["foobar"])

mock_file().write.assert_called_once_with(EXPECTED_OUTPUT)
77 changes: 50 additions & 27 deletions tests/test_register_kanalen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@

from notifications_api_common.kanalen import KANAAL_REGISTRY, Kanaal

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

@pytest.fixture
def override_kanalen():

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

KANAAL_REGISTRY.clear()
KANAAL_REGISTRY.update(kanalen)
KANAAL_REGISTRY.clear()
KANAAL_REGISTRY.update(kanalen)


@pytest.mark.django_db
def test_register_kanalen_success(notifications_config, requests_mock):
def test_register_kanalen_success(
notifications_config, requests_mock, override_kanalen
):
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal"
params = urlencode(dict(naam="foobar"))

Expand Down Expand Up @@ -58,27 +64,38 @@ def test_register_kanalen_success(notifications_config, requests_mock):


@pytest.mark.django_db
def test_register_kanalen_from_registry_success(notifications_config, requests_mock):
def test_register_kanalen_from_registry_success(
notifications_config, requests_mock, override_kanalen
):
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal"

url_mapping = {
kanaal.label: f"{kanaal_url}?{urlencode(dict(naam=kanaal.label))}"
for kanaal in kanalen
"foobar": f"{kanaal_url}?{urlencode(dict(naam='foobar'))}",
"boofar": f"{kanaal_url}?{urlencode(dict(naam='boofar'))}",
}

for kanaal in kanalen:
requests_mock.get(url_mapping[kanaal.label], json=[])

requests_mock.post(
kanaal_url,
json={
"url": "http://example.com",
"naam": kanaal.label,
"documentatieLink": "http://example.com",
"filters": ["string"],
},
status_code=201,
)
requests_mock.get(url_mapping["foobar"], json=[])
requests_mock.post(
kanaal_url,
json={
"url": "http://example.com",
"naam": "foobar",
"documentatieLink": "http://example.com",
"filters": ["string"],
},
status_code=201,
)
requests_mock.get(url_mapping["boofar"], json=[])
requests_mock.post(
kanaal_url,
json={
"url": "http://example.com",
"naam": "boofar",
"documentatieLink": "http://example.com",
"filters": ["string"],
},
status_code=201,
)

reverse_patch = (
"notifications_api_common.management.commands.register_kanalen.reverse"
Expand Down Expand Up @@ -106,7 +123,9 @@ def test_register_kanalen_from_registry_success(notifications_config, requests_m


@pytest.mark.django_db
def test_register_kanalen_existing_kanalen(notifications_config, requests_mock):
def test_register_kanalen_existing_kanalen(
notifications_config, requests_mock, override_kanalen
):
"""
Test that already registered kanalen does not cause issues
"""
Expand Down Expand Up @@ -135,7 +154,9 @@ def test_register_kanalen_existing_kanalen(notifications_config, requests_mock):


@pytest.mark.django_db
def test_register_kanalen_unknown_url(notifications_config, requests_mock):
def test_register_kanalen_unknown_url(
notifications_config, requests_mock, override_kanalen
):
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal"
params = urlencode(dict(naam="foobar"))

Expand All @@ -157,7 +178,9 @@ def test_register_kanalen_unknown_url(notifications_config, requests_mock):


@pytest.mark.django_db
def test_register_kanalen_incorrect_post(notifications_config, requests_mock):
def test_register_kanalen_incorrect_post(
notifications_config, requests_mock, override_kanalen
):
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal"
params = urlencode(dict(naam="foobar"))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_api_create_person(api_client, notifications_config):
"resourceUrl": f"http://testserver{person_url}",
"actie": "create",
"aanmaakdatum": "2022-01-01T00:00:00",
"kenmerken": {"addressStreet": "Grotestraat"},
"kenmerken": {"name": "John", "addressStreet": "Grotestraat"},
}
)

Expand Down

0 comments on commit 48b9571

Please sign in to comment.