Skip to content

Commit

Permalink
Refactor tests, update reference image tests
Browse files Browse the repository at this point in the history
The previous reference image tests did not send a file,
which is now required by the API. This adds the file to
the test payloads so that they work correctly.

Also refactors the tests into separate files under the
tests/ directory, as has been done for models/, views/,
and serializers/.
  • Loading branch information
rajadain committed Nov 2, 2022
1 parent c1e1c11 commit 88f040e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 77 deletions.
2 changes: 2 additions & 0 deletions src/django/api/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# flake8: noqa: F401
from .reference_image import ReferenceImageViewTests
90 changes: 13 additions & 77 deletions src/django/api/tests.py → src/django/api/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from datetime import datetime, timezone

from django.test import TestCase
from django.utils.log import request_logger
from django.urls import reverse

from rest_framework import status
from rest_framework.test import APIClient

from api.models import Utility, User
from api.models.boundary import Boundary
from api.models.user import Roles, State
from api.models.reference_image import ReferenceImage
from api.models.submission import Approval, Submission, Review, Annotation

from api.models import (
Annotation,
Approval,
Boundary,
ReferenceImage,
Review,
Roles,
State,
Submission,
Utility,
User,
)

from .management.test_shapes import (
from .data.shapes import (
RALEIGH_FAKE_RECTANGLE,
RALEIGH_FAKE_TRIANGLE,
RALEIGH_FAKE_ZIGZAG,
Expand Down Expand Up @@ -233,70 +236,3 @@ def setUpTestData(cls):
def setUp(self):
self.client = APIClient()
self.client.force_login(self.contributor)


class ReferenceImageViewTests(BoundarySyncAPITestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.new_image = {
"filename": "test_file.jpg",
"is_visible": True,
"distortion": {},
"opacity": 100,
"uploaded_by": cls.contributor.pk,
}
cls.updated_image = {**cls.new_image, "is_visible": False}

def test_no_image_list_endpoint(self):
url = reverse("upload_image", args=[1])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.get(url)

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

# Contributor actions
def test_contributor_upload_and_update_image(self):
url = reverse("upload_image", args=[2])
response = self.client.post(url, self.new_image, format="json")

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ReferenceImage.objects.filter(boundary__pk=2).count(), 1)

update_url = reverse("update_image", args=[2, 1])
# Attempt to alter the uploaded_by field, which is illegitimate.
updated_image_illegitimate = {
**self.updated_image,
"uploaded_by": self.administrator.pk,
}

response = self.client.put(
update_url, updated_image_illegitimate, format="json"
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
ri = ReferenceImage.objects.filter(boundary__pk=2).first()
self.assertIs(ri.is_visible, False)
self.assertEqual(ri.uploaded_by.pk, self.contributor.pk)

def test_contributor_cannot_upload_for_invalid_boundary(self):
url = reverse("upload_image", args=[2400])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.post(url, self.new_image, format="json")

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

# Validator actions
def test_validator_cannot_upload_or_update_image(self):
self.client.force_login(self.validator)
url = reverse("upload_image", args=[1])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.post(url, self.new_image, format="json")

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

url = reverse("update_image", args=[1, 1])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.put(url, self.updated_image, format="json")

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Binary file added src/django/api/tests/data/raleigh_sanborn_map.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
88 changes: 88 additions & 0 deletions src/django/api/tests/reference_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from django.urls import reverse
from django.utils.log import request_logger

from rest_framework import status

from api.models import ReferenceImage

from .base import BoundarySyncAPITestCase


class ReferenceImageViewTests(BoundarySyncAPITestCase):
def setUp(self):
super().setUp()
self.test_image = open("api/tests/data/raleigh_sanborn_map.jpg", "rb")

def tearDown(self):
super().tearDown()
self.test_image.close()

def get_create_payload(self):
return {
"file": self.test_image,
"filename": self.test_image.name,
"is_visible": True,
"opacity": 100,
"uploaded_by": self.contributor.pk,
}

def get_update_payload(self):
return {
"filename": self.test_image.name,
"is_visible": False,
"distortion": {},
"opacity": 100,
}

def test_no_image_list_endpoint(self):
url = reverse("upload_image", args=[1])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.get(url)

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

# Contributor actions
def test_contributor_upload_and_update_image(self):
url = reverse("upload_image", args=[2])
response = self.client.post(url, self.get_create_payload())

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ReferenceImage.objects.filter(boundary__pk=2).count(), 1)

update_url = reverse("update_image", args=[2, 1])
# Attempt to alter the uploaded_by field, which is illegitimate.
updated_image_illegitimate = {
**self.get_update_payload(),
"uploaded_by": self.administrator.pk,
}

response = self.client.put(
update_url, updated_image_illegitimate, format="json"
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
ri = ReferenceImage.objects.filter(boundary__pk=2).first()
self.assertIs(ri.is_visible, False)
self.assertEqual(ri.uploaded_by.pk, self.contributor.pk)

def test_contributor_cannot_upload_for_invalid_boundary(self):
url = reverse("upload_image", args=[2400])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.post(url, self.get_create_payload())

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

# Validator actions
def test_validator_cannot_upload_or_update_image(self):
self.client.force_login(self.validator)
url = reverse("upload_image", args=[1])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.post(url, self.get_create_payload())

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

url = reverse("update_image", args=[1, 1])
with self.assertLogs(request_logger, "WARNING"):
response = self.client.put(url, self.get_update_payload(), format="json")

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

0 comments on commit 88f040e

Please sign in to comment.