Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed May 1, 2024
1 parent f3cb99f commit 722b9d9
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
89 changes: 89 additions & 0 deletions backend/src/openarchiefbeheer/api/tests/test_zaken_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from furl import furl
from requests_mock import Mocker
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
from zgw_consumers.constants import APITypes
from zgw_consumers.test.factories import ServiceFactory

from openarchiefbeheer.accounts.tests.factories import UserFactory

RESPONSE_LIST = {
"results": [{"identificatie": "ZAAK-01"}, {"identificatie": "ZAAK-02"}],
"count": 2,
"previous": None,
"next": None,
}


@Mocker()
class ZakenEndpointsTestCase(APITestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()

ServiceFactory.create(
api_type=APITypes.zrc,
api_root="http://localhost:8003/zaken/api/v1",
client_id="vcr-local-test",
secret="vcr-local-test",
)

def test_not_authenticated(self, m):
api_url = reverse("api:zaken:zaken-list")

response = self.client.get(api_url)

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_successful_response(self, m):
user = UserFactory.create(username="test", password="password")
api_url = reverse("api:zaken:zaken-list")

m.get("http://localhost:8003/zaken/api/v1/zaken", json=RESPONSE_LIST)

self.client.force_authenticate(user=user)
response = self.client.get(api_url)

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

data = response.json()

self.assertEqual(data, RESPONSE_LIST)

def test_with_filters(self, m):
user = UserFactory.create(username="test", password="password")
api_url = furl(reverse("api:zaken:zaken-list"))
api_url.args["bronorganisatie"] = "000000"
api_url.args["archiefstatus"] = "nog_te_archiveren"

m.get("http://localhost:8003/zaken/api/v1/zaken", json=RESPONSE_LIST)

self.client.force_authenticate(user=user)
response = self.client.get(api_url.url)

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

query_params = m.request_history[0].qs

self.assertIn("bronorganisatie", query_params)
self.assertIn("archiefstatus", query_params)

def test_error_response(self, m):
user = UserFactory.create(username="test", password="password")
api_url = reverse("api:zaken:zaken-list")

m.get(
"http://localhost:8003/zaken/api/v1/zaken",
json={"invalid": "Something went wrong"},
status_code=status.HTTP_400_BAD_REQUEST,
)

self.client.force_authenticate(user=user)
response = self.client.get(api_url)

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

data = response.json()

self.assertEqual(data, {"error": {"invalid": "Something went wrong"}})
5 changes: 5 additions & 0 deletions backend/src/openarchiefbeheer/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@
"openarchiefbeheer.api.authentication.urls", namespace="authentication"
),
),
# Actual endpoints
path(
"v1/zaken/",
include("openarchiefbeheer.api.zaken.urls", namespace="zaken"),
),
]
Empty file.
10 changes: 10 additions & 0 deletions backend/src/openarchiefbeheer/api/zaken/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework.routers import DefaultRouter

from .views import ZakenViewSet

app_name = "zaken"

router = DefaultRouter()
router.register("", ZakenViewSet, basename="zaken")

urlpatterns = router.urls
44 changes: 44 additions & 0 deletions backend/src/openarchiefbeheer/api/zaken/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.utils.translation import gettext_lazy as _

from drf_spectacular.utils import extend_schema, extend_schema_view
from requests.exceptions import RequestException
from rest_framework import viewsets
from rest_framework.renderers import JSONRenderer
from rest_framework.request import Request
from rest_framework.response import Response
from zgw_consumers.client import build_client
from zgw_consumers.constants import APITypes
from zgw_consumers.models import Service


@extend_schema_view(
list=extend_schema(
summary=_("List zaken"),
description=_(
"Retrieve zaken using the configured ZRC service. "
"For information over the query parameters accepted and the schema of the response, look at the "
"'/zaken/api/v1/zaken' list endpoint of Open Zaak."
),
),
)
class ZakenViewSet(viewsets.ViewSet):
# We don't want the CamelCase renderer to alter the responses from Open Zaak
renderer_classes = (JSONRenderer,)

def list(self, request: Request) -> Response:
zrc_service = Service.objects.get(api_type=APITypes.zrc)
zrc_client = build_client(zrc_service)

with zrc_client:
response = zrc_client.get(
"zaken",
headers={"Accept-Crs": "EPSG:4326"},
params=request.query_params,
)

try:
response.raise_for_status()
except RequestException:
return Response({"error": response.json()}, status=response.status_code)

return Response(response.json(), status=response.status_code)

0 comments on commit 722b9d9

Please sign in to comment.