class EditAndDeleteTestCaseMixin(object): """Tests that are the same for both views.""" def setUp(self): self.dummy = DummyModelFactory() self.user = self.dummy.user self.image = UserMediaImageFactory(user=self.user) self.image.content_object = self.dummy self.image.save() self.image_no_content_object = UserMediaImageFactory(user=self.user) self.other_image = UserMediaImageFactory() def test_view_with_content_object(self): self.should_be_callable_when_authenticated(self.user) resp = self.client.post(self.get_url()) self.assertRedirects(resp, self.dummy.get_absolute_url(), msg_prefix=( "If the image had a content object, view should redirect to" " that object's absolute url")) self.image = UserMediaImageFactory(user=self.user) resp = self.client.post(self.get_url(), data={'next': '/?foo=bar'}) self.assertRedirects(resp, '/?foo=bar', msg_prefix=( "If the image had a content object and ``next`` in the POST data," " view should redirect to the URL given in ``next`` and ignore" " the content object's absolute URL")) self.image = UserMediaImageFactory(user=self.user) resp = self.client.post(self.get_url() + '?next=/') self.assertRedirects(resp, '/', msg_prefix=( "If the image had a content object and ``next`` in the GET data," " view should redirect to the URL given in ``next`` and ignore" " the content object's absolute URL")) resp = self.client.post(self.get_url( view_kwargs={'pk': self.other_image.pk})) self.assertEqual(resp.status_code, 404, msg=( "Should return 404 if the user tries to manipulate another user's" " object")) resp = self.client.post(self.get_url(view_kwargs={'pk': 999})) self.assertEqual(resp.status_code, 404, msg=( 'Should return 404 if the user tries to manipulate a non existing' ' object')) def test_view_without_content_object(self): self.login(self.user) data = {'next': '/?foo=bar', } resp = self.client.post(self.get_url( view_kwargs={'pk': self.image_no_content_object.pk}), data=data) self.assertRedirects(resp, '/?foo=bar', msg_prefix=( 'If the image had no content object, view should redirect to' ' the POST data ``next`` that must be given')) self.image_no_content_object = UserMediaImageFactory(user=self.user) try: resp = self.client.post(self.get_url( view_kwargs={'pk': self.image_no_content_object.pk})) except Exception, ex: self.assertTrue('No content object' in ex.message, msg=( 'If no content object and no ``next`` parameter given,' ' view should raise an exception'))
class AJAXImageCropViewTestCase(ViewTestMixin, TestCase): """Tests for the ``AJAXImageCropView`` generic view class.""" def setUp(self): self.image = UserMediaImageFactory() self.image2 = UserMediaImageFactory() def get_view_name(self): return 'user_media_image_crop' def get_view_kwargs(self): return {'pk': self.image.pk} def test_view(self): logo_file = os.path.join( settings.DJANGO_PROJECT_ROOT, 'tests/test_media/logo.png') with open(logo_file) as f: self.image.image.save(logo_file, File(f)) self.image.save() self.is_not_callable() self.is_not_callable(user=self.image.user) data = {'x': 10, 'x2': 15, 'y': 2, 'y2': 10, 'w': 5, 'h': 8} self.is_callable(user=self.image.user, method='post', data=data, ajax=True) self.is_not_callable(user=self.image2.user, method='post', ajax=True)
class EditAndDeleteTestCaseMixin(object): """Tests that are the same for both views.""" def setUp(self): self.dummy = DummyModelFactory() self.user = self.dummy.user self.image = UserMediaImageFactory(user=self.user) self.image.content_object = self.dummy logo_file = os.path.join( settings.DJANGO_PROJECT_ROOT, 'tests/test_media/logo.png') with open(logo_file) as f: self.image.image.save(logo_file, File(f)) self.image.save() self.image_no_content_object = UserMediaImageFactory(user=self.user) self.other_image = UserMediaImageFactory() def test_view_with_content_object(self): self.should_be_callable_when_authenticated(self.user) resp = self.client.post(self.get_url()) self.assertRedirects(resp, self.dummy.get_absolute_url(), msg_prefix=( "If the image had a content object, view should redirect to" " that object's absolute url")) self.image = UserMediaImageFactory(user=self.user) resp = self.client.post(self.get_url(), data={'next': '/?foo=bar'}) self.assertRedirects(resp, '/?foo=bar', msg_prefix=( "If the image had a content object and ``next`` in the POST data," " view should redirect to the URL given in ``next`` and ignore" " the content object's absolute URL")) self.image = UserMediaImageFactory(user=self.user) resp = self.client.post(self.get_url() + '?next=/') self.assertRedirects(resp, '/', msg_prefix=( "If the image had a content object and ``next`` in the GET data," " view should redirect to the URL given in ``next`` and ignore" " the content object's absolute URL")) resp = self.client.post(self.get_url( view_kwargs={'pk': self.other_image.pk})) self.assertEqual(resp.status_code, 404, msg=( "Should return 404 if the user tries to manipulate another user's" " object")) resp = self.client.post(self.get_url(view_kwargs={'pk': 999})) self.assertEqual(resp.status_code, 404, msg=( 'Should return 404 if the user tries to manipulate a non existing' ' object')) def test_view_without_content_object(self): self.login(self.user) data = {'next': '/?foo=bar', } resp = self.client.post(self.get_url( view_kwargs={'pk': self.image_no_content_object.pk}), data=data) self.assertRedirects(resp, '/?foo=bar', msg_prefix=( 'If the image had no content object, view should redirect to' ' the POST data ``next`` that must be given')) self.image_no_content_object = UserMediaImageFactory(user=self.user) try: resp = self.client.post(self.get_url( view_kwargs={'pk': self.image_no_content_object.pk})) except Exception, ex: self.assertTrue('No content object' in ex.message, msg=( 'If no content object and no ``next`` parameter given,' ' view should raise an exception'))