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 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_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'))
def setUp(self): self.image = UserMediaImageFactory() self.image2 = UserMediaImageFactory()