Skip to content

Commit

Permalink
Add isolated tests explicitly for Good permission classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincarrogan committed Feb 12, 2024
1 parent 44046b8 commit de1db7e
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
116 changes: 116 additions & 0 deletions api/goods/tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from django.test import override_settings
from django.urls import reverse

from parameterized import parameterized

from test_helpers.clients import DataTestClient

from api.goods.enums import GoodStatus
from api.goods.tests.factories import GoodFactory


@override_settings(
ROOT_URLCONF="api.goods.tests.urls",
)
class TestIsDocumentInOrganisation(DataTestClient):
def setUp(self):
super().setUp()
self.good = GoodFactory(organisation=self.organisation)

def test_document_is_in_organisation(self):
good_document = self.create_good_document(
good=self.good,
user=self.exporter_user,
organisation=self.organisation,
s3_key="thisisakey",
name="doc1.pdf",
)

url = reverse(
"test-is-document-in-organisation",
kwargs={
"pk": str(good_document.pk),
},
)

response = self.client.get(url, **self.exporter_headers)
self.assertEqual(response.status_code, 200)

def test_document_in_other_organisation(self):
other_organisation = self.create_organisation_with_exporter_user()[0]
self.good.organisation = other_organisation
self.good.save()
good_document = self.create_good_document(
good=self.good,
user=self.exporter_user,
organisation=other_organisation,
s3_key="thisisakey",
name="doc1.pdf",
)

url = reverse(
"test-is-document-in-organisation",
kwargs={
"pk": good_document.pk,
},
)
response = self.client.get(url, **self.exporter_headers)
self.assertEqual(response.status_code, 403)


@override_settings(
ROOT_URLCONF="api.goods.tests.urls",
)
class TestIsGoodDraft(DataTestClient):
def setUp(self):
super().setUp()
self.good = GoodFactory(
organisation=self.organisation,
status=GoodStatus.DRAFT,
)

def test_good_is_in_draft(self):
good_document = self.create_good_document(
good=self.good,
user=self.exporter_user,
organisation=self.organisation,
s3_key="thisisakey",
name="doc1.pdf",
)

url = reverse(
"test-is-good-draft",
kwargs={
"pk": str(good_document.pk),
},
)

response = self.client.get(url, **self.exporter_headers)
self.assertEqual(response.status_code, 200)

@parameterized.expand(
[
GoodStatus.SUBMITTED,
GoodStatus.QUERY,
GoodStatus.VERIFIED,
],
)
def test_document_is_on_other_status(self, status):
self.good.status = status
self.good.save()
good_document = self.create_good_document(
good=self.good,
user=self.exporter_user,
organisation=self.organisation,
s3_key="thisisakey",
name="doc1.pdf",
)

url = reverse(
"test-is-good-draft",
kwargs={
"pk": good_document.pk,
},
)
response = self.client.get(url, **self.exporter_headers)
self.assertEqual(response.status_code, 403)
19 changes: 19 additions & 0 deletions api/goods/tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.urls import path

from .views import (
IsDocumentInOrganisationView,
IsGoodDraftView,
)

urlpatterns = [
path(
"test-is-document-in-organisation/<uuid:pk>/",
IsDocumentInOrganisationView.as_view(),
name="test-is-document-in-organisation",
),
path(
"test-is-good-draft/<uuid:pk>/",
IsGoodDraftView.as_view(),
name="test-is-good-draft",
),
]
20 changes: 20 additions & 0 deletions api/goods/tests/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rest_framework.generics import RetrieveAPIView

from api.goods.models import GoodDocument
from api.goods.permissions import (
IsDocumentInOrganisation,
IsGoodDraft,
)
from api.goods.serializers import GoodDocumentViewSerializer


class IsDocumentInOrganisationView(RetrieveAPIView):
permission_classes = (IsDocumentInOrganisation,)
queryset = GoodDocument.objects.all()
serializer_class = GoodDocumentViewSerializer


class IsGoodDraftView(RetrieveAPIView):
permission_classes = (IsGoodDraft,)
queryset = GoodDocument.objects.all()
serializer_class = GoodDocumentViewSerializer

0 comments on commit de1db7e

Please sign in to comment.