Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [#207] Add experimental PUT and PATCH for Kanaal #208

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/nrc/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from djangorestframework_camel_case.util import camelize, underscoreize
from notifications_api_common.api.serializers import NotificatieSerializer
from rest_framework import fields, serializers
from vng_api_common.validators import URLValidator
from rest_framework.validators import UniqueValidator
from vng_api_common.validators import IsImmutableValidator, URLValidator

from nrc.api.tasks import deliver_message
from nrc.datamodel.models import Abonnement, Filter, FilterGroup, Kanaal, Notificatie
Expand Down Expand Up @@ -39,6 +40,12 @@ class Meta:
fields = ("url", "naam", "documentatie_link", "filters")
extra_kwargs = {
"url": {"lookup_field": "uuid"},
"naam": {
"validators": [
UniqueValidator(queryset=Kanaal.objects.all()),
IsImmutableValidator(),
]
},
"documentatie_link": {"required": False, "validators": [URLValidator()]},
"filters": {"required": False},
}
Expand Down
98 changes: 84 additions & 14 deletions src/nrc/api/tests/test_kanaal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

from rest_framework import status
from rest_framework.test import APITestCase
from vng_api_common.tests import JWTAuthMixin, get_operation_url
from vng_api_common.tests import (
JWTAuthMixin,
get_operation_url,
get_validation_errors,
reverse,
)

from nrc.datamodel.models import Kanaal
from nrc.datamodel.tests.factories import KanaalFactory
Expand Down Expand Up @@ -46,25 +51,90 @@ def test_kanaal_create_nonunique(self):
response.status_code, status.HTTP_400_BAD_REQUEST, response.data
)

def test_kanaal_update_delete(self):
def test_kanaal_update(self):
kanaal = KanaalFactory.create(
naam="zaken",
documentatie_link="https://example.com/doc",
filters=["zaaktype"],
)
kanaal_url = reverse(kanaal)
data = {
"naam": "zaken",
"documentatie_link": "https://example.com/updated",
"filters": ["zaaktype", "zaaktype.catalogus"],
}

response = self.client.put(kanaal_url, data)

self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

# check parsing to model
data = response.json()
kanaal = Kanaal.objects.get()
self.assertEqual(kanaal.naam, "zaken")
self.assertEqual(kanaal.documentatie_link, "https://example.com/updated")
self.assertEqual(kanaal.filters, ["zaaktype", "zaaktype.catalogus"])

def test_kanaal_partial_update(self):
kanaal = KanaalFactory.create(
naam="zaken",
documentatie_link="https://example.com/doc",
filters=["zaaktype"],
)
kanaal_url = reverse(kanaal)
data = {"filters": ["zaaktype", "zaaktype.catalogus"]}

response = self.client.patch(kanaal_url, data)

self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

# check parsing to model
data = response.json()
kanaal = Kanaal.objects.get()
self.assertEqual(kanaal.naam, "zaken")
self.assertEqual(kanaal.documentatie_link, "https://example.com/doc")
self.assertEqual(kanaal.filters, ["zaaktype", "zaaktype.catalogus"])

def test_kanaal_cannot_update_naam(self):
kanaal = KanaalFactory.create(
naam="zaken",
documentatie_link="https://example.com/doc",
filters=["zaaktype"],
)
kanaal_url = reverse(kanaal)
data = {
"naam": "modified",
"documentatie_link": "https://example.com/updated",
"filters": ["zaaktype", "zaaktype.catalogus"],
}

response = self.client.put(kanaal_url, data)

self.assertEqual(
response.status_code, status.HTTP_400_BAD_REQUEST, response.data
)

# check parsing to model
data = response.json()

error = get_validation_errors(response, "naam")
self.assertEqual(error["code"], "wijzigen-niet-toegelaten")

kanaal = Kanaal.objects.get()
self.assertEqual(kanaal.naam, "zaken")
self.assertEqual(kanaal.documentatie_link, "https://example.com/doc")
self.assertEqual(kanaal.filters, ["zaaktype"])

def test_kanaal_delete(self):
"""
test /kanaal PUT, DELETE:
attempt to update and destroy kanaal via request
test /kanaal DELETE:
attempt to destroy kanaal via request
check if response contents status 405
"""
kanaal = Kanaal.objects.create(naam="zaken")
kanaal_url = get_operation_url("kanaal_read", uuid=kanaal.uuid)
data = {"documentatie_link": "https://example.com/doc"}

response_put = self.client.put(kanaal_url, data)

self.assertEqual(
response_put.status_code,
status.HTTP_405_METHOD_NOT_ALLOWED,
response_put.data,
)

response_delete = self.client.delete(kanaal_url, data)
response_delete = self.client.delete(kanaal_url)

self.assertEqual(
response_delete.status_code,
Expand Down
10 changes: 9 additions & 1 deletion src/nrc/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from vng_api_common.viewsets import CheckQueryParamsMixin

from nrc.datamodel.models import Abonnement, Kanaal
from nrc.utils.help_text import mark_experimental

from .filters import KanaalFilter
from .scopes import SCOPE_NOTIFICATIES_CONSUMEREN, SCOPE_NOTIFICATIES_PUBLICEREN
Expand Down Expand Up @@ -54,17 +55,22 @@ def perform_create(self, serializer):
@extend_schema_view(
list=extend_schema(summary="Alle KANAALen opvragen."),
retrieve=extend_schema(summary="Een specifiek KANAAL opvragen."),
update=extend_schema(summary=mark_experimental("Een specifiek KANAAL bewerken.")),
partial_update=extend_schema(
summary=mark_experimental("Een specifiek KANAAL deels bewerken.")
),
create=extend_schema(summary="Maak een KANAAL aan."),
)
class KanaalViewSet(
CheckQueryParamsMixin,
mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.UpdateModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet,
):
"""
Opvragen en aanmaken van KANAALen.
Opvragen, aanmaken en bewerken van KANAALen.

Op een KANAAL publiceren componenten (bronnen) hun NOTIFICATIEs. Alleen
componenten die NOTIFICATIEs willen publiceren dienen een KANAAL aan te
Expand All @@ -80,6 +86,8 @@ class KanaalViewSet(
"list": SCOPE_NOTIFICATIES_PUBLICEREN | SCOPE_NOTIFICATIES_CONSUMEREN,
"retrieve": SCOPE_NOTIFICATIES_PUBLICEREN | SCOPE_NOTIFICATIES_CONSUMEREN,
"create": SCOPE_NOTIFICATIES_PUBLICEREN,
"update": SCOPE_NOTIFICATIES_PUBLICEREN,
"partial_update": SCOPE_NOTIFICATIES_PUBLICEREN,
}


Expand Down
5 changes: 5 additions & 0 deletions src/nrc/utils/help_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.utils.translation import gettext_lazy as _


def mark_experimental(text):
return _("**EXPERIMENTEEL** {}").format(text)
Loading
Loading