Skip to content

Commit

Permalink
Fix redirect to only happen if the "add" action was successful. It wa…
Browse files Browse the repository at this point in the history
…s already breaking unit tests, but add more to be sure.

Signed-off-by: Jannis Leidel <jannis@leidel.info>
  • Loading branch information
diox authored and jezdez committed May 5, 2010
1 parent aaa1b62 commit 3be0c2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions avatar/tests.py
Expand Up @@ -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

Expand All @@ -44,6 +44,7 @@ def testNonImageUpload(self):
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)
Expand All @@ -52,18 +53,21 @@ def testImageWithoutExtension(self):
# 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):
Expand Down Expand Up @@ -94,8 +98,9 @@ def testDeleteAvatar(self):
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)

Expand All @@ -119,6 +124,7 @@ def testTooManyAvatars(self):
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)

Expand Down
2 changes: 1 addition & 1 deletion avatar/views.py
Expand Up @@ -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,
Expand Down

0 comments on commit 3be0c2c

Please sign in to comment.