Fix redirect to only happen if the "add" action was successful. It was already breaking unit tests, but add more to be sure.

Signed-off-by: Jannis Leidel <jannis@leidel.info>
This commit is contained in:
Mathieu Pillard 2010-05-04 16:54:20 +02:00 committed by Jannis Leidel
parent aaa1b62221
commit 3be0c2ca01
2 changed files with 10 additions and 4 deletions

View file

@ -22,7 +22,7 @@ def upload_helper(o, filename):
f = open(os.path.join(o.testdatapath, filename), "rb")
response = o.client.post(reverse('avatar_add'), {
'avatar': f,
})
}, follow=True)
f.close()
return response
@ -44,6 +44,7 @@ class AvatarUploadTests(TestCase):
def testNormalImageUpload(self):
response = upload_helper(self, "test.png")
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(len(response.redirect_chain), 1)
self.failUnlessEqual(response.context['upload_avatar_form'].errors, {})
avatar = get_primary_avatar(self.user)
self.failIfEqual(avatar, None)
@ -52,18 +53,21 @@ class AvatarUploadTests(TestCase):
# use with AVATAR_ALLOWED_FILE_EXTS = ('.jpg', '.png')
response = upload_helper(self, "imagefilewithoutext")
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.failIfEqual(response.context['upload_avatar_form'].errors, {})
def testImageWithWrongExtension(self):
# use with AVATAR_ALLOWED_FILE_EXTS = ('.jpg', '.png')
response = upload_helper(self, "imagefilewithwrongext.ogg")
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.failIfEqual(response.context['upload_avatar_form'].errors, {})
def testImageTooBig(self):
# use with AVATAR_MAX_SIZE = 1024 * 1024
response = upload_helper(self, "testbig.png")
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.failIfEqual(response.context['upload_avatar_form'].errors, {})
def testDefaultUrl(self):
@ -94,8 +98,9 @@ class AvatarUploadTests(TestCase):
self.failUnlessEqual(len(avatar), 1)
response = self.client.post(reverse('avatar_delete'), {
'choices': [avatar[0].id],
})
self.failUnlessEqual(response.status_code, 302)
}, follow=True)
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(len(response.redirect_chain), 1)
count = Avatar.objects.filter(user=self.user).count()
self.failUnlessEqual(count, 0)
@ -119,6 +124,7 @@ class AvatarUploadTests(TestCase):
response = upload_helper(self, "test.png")
count_after = Avatar.objects.filter(user=self.user).count()
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.failIfEqual(response.context['upload_avatar_form'].errors, {})
self.failUnlessEqual(count_before, count_after)

View file

@ -90,7 +90,7 @@ def add(request, extra_context=None, next_override=None,
message=_("Successfully uploaded a new avatar."))
if notification:
_notification_updated(request, avatar)
return HttpResponseRedirect(next_override or _get_next(request))
return HttpResponseRedirect(next_override or _get_next(request))
return render_to_response(
'avatar/add.html',
extra_context,