-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Test Zaken endpoint that reads from the cache
- Loading branch information
1 parent
1339a9d
commit 90deadf
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import factory | ||
|
||
from ..models import Zaak | ||
|
||
|
||
def zaak_data(number: int) -> dict: | ||
return { | ||
"url": f"http://zaken-api.nl/zaken/api/v1/zaken/{number}", | ||
"identificatie": f"ZAAK-{number}", | ||
} | ||
|
||
|
||
class ZaakFactory(factory.django.DjangoModelFactory): | ||
data = factory.Sequence(zaak_data) | ||
|
||
class Meta: | ||
model = Zaak |
83 changes: 83 additions & 0 deletions
83
backend/src/openarchiefbeheer/zaken/tests/test_viewsets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from furl import furl | ||
from rest_framework import status | ||
from rest_framework.reverse import reverse | ||
from rest_framework.test import APITestCase | ||
|
||
from openarchiefbeheer.accounts.tests.factories import UserFactory | ||
from openarchiefbeheer.destruction.constants import ListItemStatus | ||
from openarchiefbeheer.destruction.tests.factories import DestructionListItemFactory | ||
|
||
from ..models import Zaak | ||
from .factories import ZaakFactory | ||
|
||
|
||
class ZakenViewSetTest(APITestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
super().setUpTestData() | ||
|
||
ZaakFactory.create_batch(5) | ||
|
||
def test_not_authenticated(self): | ||
endpoint = reverse("api:zaken-list") | ||
|
||
response = self.client.post(endpoint) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
def test_authenticated_without_permission(self): | ||
user = UserFactory.create(role__can_start_destruction=False) | ||
|
||
self.client.force_authenticate(user=user) | ||
endpoint = reverse("api:zaken-list") | ||
|
||
response = self.client.post(endpoint) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
def test_filter_out_zaken_already_in_destruction_lists(self): | ||
zaken = Zaak.objects.all() | ||
|
||
# This zaak should NOT be returned by the endpoint (it's included in a destruction list) | ||
DestructionListItemFactory.create( | ||
status=ListItemStatus.suggested, zaak=zaken[0].data["url"] | ||
) | ||
# This zaak SHOULD be returned by the endpoint (it was included in a destruction list, but was then excluded) | ||
DestructionListItemFactory.create( | ||
status=ListItemStatus.removed, zaak=zaken[1].data["url"] | ||
) | ||
|
||
user = UserFactory(username="record_manager", role__can_start_destruction=True) | ||
|
||
self.client.force_authenticate(user) | ||
response = self.client.get(reverse("api:zaken-list")) | ||
data = response.json() | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data["count"], 4) | ||
|
||
urls_zaken = [zaak["url"] for zaak in data["results"]] | ||
|
||
self.assertNotIn(zaken[0].data["url"], urls_zaken) | ||
|
||
def test_using_query_params_to_filter(self): | ||
zaken = Zaak.objects.all() | ||
|
||
filter = [zaken[0].data["identificatie"], zaken[1].data["identificatie"]] | ||
|
||
user = UserFactory(username="record_manager", role__can_start_destruction=True) | ||
|
||
endpoint = furl(reverse("api:zaken-list")) | ||
endpoint.args["identificatie__in"] = filter | ||
|
||
self.client.force_authenticate(user) | ||
response = self.client.get(endpoint.url) | ||
data = response.json() | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(data["count"], 2) | ||
|
||
urls_zaken = [zaak["url"] for zaak in data["results"]] | ||
|
||
self.assertIn(filter[0], urls_zaken) | ||
self.assertIn(filter[1], urls_zaken) |