Skip to content

Commit

Permalink
✅ [#207] Add tests for PUT and PATCH on Kanaal
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Dec 17, 2024
1 parent e7a40ed commit a4c6760
Showing 1 changed file with 84 additions and 14 deletions.
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

0 comments on commit a4c6760

Please sign in to comment.