-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add isolated tests explicitly for Good permission classes
- Loading branch information
1 parent
44046b8
commit de1db7e
Showing
3 changed files
with
155 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,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) |
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,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", | ||
), | ||
] |
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,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 |