Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests for the new Area update view.

  • Loading branch information...
commit 40351f81ca29e8ed77b655921994fdd53f086436 1 parent ffed3d3
@melinath melinath authored
View
26 daguerre/tests/base.py
@@ -1,5 +1,6 @@
import os
+from django.contrib.auth.models import User, Permission
from django.test import TestCase
try:
from PIL import ImageChops, Image
@@ -8,6 +9,7 @@
import ImageChops
import daguerre
+from daguerre.models import Area
from daguerre.utils import save_image
@@ -32,3 +34,27 @@ def assertImageEqual(self, im1, im2):
def create_image(self, test_path):
image = Image.open(self._data_path(test_path))
return save_image(image, 'daguerre/test/{0}'.format(test_path))
+
+ def create_area(self, test_path='100x100.png', x1=0, y1=0, x2=100, y2=100, **kwargs):
+ if 'storage_path' not in kwargs:
+ kwargs['storage_path'] = self.create_image(test_path)
+ kwargs.update({
+ 'x1': x1,
+ 'y1': y1,
+ 'x2': x2,
+ 'y2': y2
+ })
+ return Area.objects.create(**kwargs)
+
+ def create_user(self, username='test', password='test', permissions=None, **kwargs):
+ user = User(username=username, **kwargs)
+ user.set_password(password)
+ user.save()
+
+ if permissions:
+ for permission in permissions:
+ app_label, codename = permission.split('.')
+ permission = Permission.objects.get(content_type__app_label=app_label, codename=codename)
+ user.user_permissions.add(permission)
+
+ return user
View
280 daguerre/tests/unit/test_views.py
@@ -1,9 +1,13 @@
+import json
+
+from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.test import RequestFactory
+from daguerre.models import Area
from daguerre.tests.base import BaseTestCase
from daguerre.utils.adjustments import AdjustmentHelper
-from daguerre.views import AdjustedImageRedirectView, AjaxAdjustmentInfoView
+from daguerre.views import AdjustedImageRedirectView, AjaxAdjustmentInfoView, AjaxUpdateAreaView
class AdjustedImageRedirectViewTestCase(BaseTestCase):
@@ -58,5 +62,277 @@ def test_nonexistant(self):
self.view.kwargs = {'storage_path': storage_path}
get_params = helper.to_querydict()
self.view.request = factory.get('/', get_params,
- HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertRaises(Http404, self.view.get, self.view.request)
+
+
+class AjaxUpdateAreaViewTestCase(BaseTestCase):
+ def setUp(self):
+ self.factory = RequestFactory()
+ super(AjaxUpdateAreaViewTestCase, self).setUp()
+
+ def test_not_ajax(self):
+ request = self.factory.get('/')
+ view = AjaxUpdateAreaView()
+ self.assertRaises(Http404, view.get, request)
+ self.assertRaises(Http404, view.post, request)
+ self.assertRaises(Http404, view.delete, request)
+
+ def test_get__pk(self):
+ area = self.create_area(x2=50, y2=50)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk,
+ }
+ request = self.factory.get('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ with self.assertNumQueries(1):
+ response = view.get(request)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response['Content-Type'], "application/json")
+ data = json.loads(response.content)
+ self.assertEqual(data, area.serialize())
+
+ def test_get__pk__wrong(self):
+ area = self.create_area(x2=50, y2=50)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk + 1,
+ }
+ request = self.factory.get('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ with self.assertNumQueries(1):
+ self.assertRaises(Http404, view.get, request)
+
+ def test_get__no_pk(self):
+ area1 = self.create_area(x2=50, y2=50)
+ area2 = self.create_area(x2=50, y2=50, storage_path=area1.storage_path)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area1.storage_path,
+ 'pk': None
+ }
+ request = self.factory.get('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ with self.assertNumQueries(1):
+ response = view.get(request)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response['Content-Type'], "application/json")
+ data = json.loads(response.content)
+ self.assertEqual(data, [area1.serialize(), area2.serialize()])
+
+ def test_post__no_change_perms(self):
+ view = AjaxUpdateAreaView()
+ request = self.factory.post('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = AnonymousUser()
+ self.assertFalse(request.user.has_perm('daguerre.change_area'))
+ with self.assertNumQueries(0):
+ response = view.post(request)
+
+ self.assertEqual(response.status_code, 403)
+ self.assertEqual(response.content, '')
+
+ def test_post__invalid_params(self):
+ area = self.create_area(x2=50, y2=50)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk,
+ }
+
+ params = {
+ 'x1': 0,
+ 'y1': 0,
+ 'x2': 50,
+ 'y2': 50,
+ 'priority': 3,
+ }
+ user = self.create_user(permissions=['daguerre.change_area'])
+ self.assertTrue(user.has_perm('daguerre.change_area'))
+ for key in params:
+ params_copy = params.copy()
+ params_copy[key] = 'hi'
+ request = self.factory.post('/', params_copy, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = user
+ with self.assertNumQueries(0):
+ self.assertRaises(Http404, view.post, request)
+
+ def test_post__update(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ old_serialize = area.serialize()
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk,
+ }
+
+ params = {
+ 'x1': 50,
+ 'y1': 50,
+ 'x2': 100,
+ 'y2': 100,
+ 'priority': 1,
+ 'name': 'fun'
+ }
+ request = self.factory.post('/', params, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = self.create_user(permissions=['daguerre.change_area'])
+ self.assertTrue(request.user.has_perm('daguerre.change_area'))
+ with self.assertNumQueries(3):
+ response = view.post(request)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response['Content-Type'], "application/json")
+ self.assertEqual(Area.objects.count(), 1)
+ data = json.loads(response.content)
+ new_area = Area.objects.get(pk=area.pk, storage_path=area.storage_path)
+ self.assertEqual(data, new_area.serialize())
+ self.assertNotEqual(data, old_serialize)
+ del data['storage_path']
+ del data['id']
+ self.assertEqual(data, params)
+
+ def test_post__update__invalid(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk,
+ }
+
+ params = {
+ 'x1': 100,
+ 'y1': 50,
+ 'x2': 50,
+ 'y2': 100,
+ 'priority': 1,
+ 'name': 'fun'
+ }
+ request = self.factory.post('/', params, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = self.create_user(permissions=['daguerre.change_area'])
+ self.assertTrue(request.user.has_perm('daguerre.change_area'))
+ with self.assertNumQueries(1):
+ response = view.post(request)
+
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(Area.objects.count(), 1)
+ data = json.loads(response.content)
+ self.assertEqual(data.keys(), ['error'])
+
+ def test_post__add(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': None,
+ }
+
+ params = {
+ 'x1': 50,
+ 'y1': 50,
+ 'x2': 100,
+ 'y2': 100,
+ 'priority': 1,
+ 'name': 'fun'
+ }
+ request = self.factory.post('/', params, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = self.create_user(permissions=['daguerre.change_area', 'daguerre.add_area'])
+ self.assertTrue(request.user.has_perm('daguerre.change_area'))
+ self.assertTrue(request.user.has_perm('daguerre.add_area'))
+ with self.assertNumQueries(2):
+ response = view.post(request)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response['Content-Type'], "application/json")
+ self.assertEqual(Area.objects.count(), 2)
+ data = json.loads(response.content)
+ new_area = Area.objects.exclude(pk=area.pk).get()
+ self.assertEqual(data, new_area.serialize())
+ del data['storage_path']
+ del data['id']
+ self.assertEqual(data, params)
+
+ def test_post__add__no_perms(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': None,
+ }
+
+ params = {
+ 'x1': 50,
+ 'y1': 50,
+ 'x2': 100,
+ 'y2': 100,
+ 'priority': 1,
+ 'name': 'fun'
+ }
+ request = self.factory.post('/', params, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = self.create_user(permissions=['daguerre.change_area'])
+ self.assertTrue(request.user.has_perm('daguerre.change_area'))
+ with self.assertNumQueries(1):
+ response = view.post(request)
+
+ self.assertEqual(response.status_code, 403)
+ self.assertEqual(Area.objects.count(), 1)
+ self.assertEqual(response.content, '')
+
+ def test_delete__no_perms(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk,
+ }
+
+ request = self.factory.delete('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = AnonymousUser()
+ self.assertFalse(request.user.has_perm('daguerre.delete_area'))
+ with self.assertNumQueries(0):
+ response = view.delete(request)
+
+ self.assertEqual(response.status_code, 403)
+ self.assertEqual(Area.objects.count(), 1)
+ self.assertEqual(response.content, '')
+
+ def test_delete__no_pk(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': None,
+ }
+
+ request = self.factory.delete('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = self.create_user(permissions=['daguerre.delete_area'])
+ self.assertTrue(request.user.has_perm('daguerre.delete_area'))
+ with self.assertNumQueries(0):
+ self.assertRaises(Http404, view.delete, request)
+
+ self.assertEqual(Area.objects.count(), 1)
+
+ def test_delete(self):
+ area = self.create_area(x2=50, y2=50)
+ self.assertEqual(Area.objects.count(), 1)
+ view = AjaxUpdateAreaView()
+ view.kwargs = {
+ 'storage_path': area.storage_path,
+ 'pk': area.pk,
+ }
+
+ request = self.factory.delete('/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ request.user = self.create_user(permissions=['daguerre.delete_area'])
+ self.assertTrue(request.user.has_perm('daguerre.delete_area'))
+ with self.assertNumQueries(3):
+ response = view.delete(request)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.content, '')
+ self.assertEqual(Area.objects.count(), 0)
View
6 daguerre/views.py
@@ -47,7 +47,7 @@ def get(self, request, *args, **kwargs):
# Something went wrong. The image probably doesn't exist.
raise Http404
- return HttpResponse(json.dumps(info_dict), mimetype="application/json")
+ return HttpResponse(json.dumps(info_dict), content_type="application/json")
class AjaxUpdateAreaView(View):
@@ -80,7 +80,7 @@ def get(self, request, *args, **kwargs):
else:
areas = Area.objects.filter(storage_path=storage_path)
data = [area.serialize() for area in areas]
- return HttpResponse(json.dumps(data), mimetype="application/json")
+ return HttpResponse(json.dumps(data), content_type="application/json")
def post(self, request, *args, **kwargs):
if not request.is_ajax():
@@ -121,7 +121,7 @@ def post(self, request, *args, **kwargs):
data = area.serialize()
return HttpResponse(json.dumps(data),
- mimetype="application/json",
+ content_type="application/json",
status=status)
def delete(self, request, *args, **kwargs):
Please sign in to comment.
Something went wrong with that request. Please try again.