Thursday, November 27, 2014

Test client to a redirect requiring a login doesn't set redirect_chain properly?

I have a view that can redirect to /admin/common/region/add/ but that
url (being an admin page) should require login (right?). My test that
logs-in works as expected.

However, when I try to do an assertRedirects() without logging in
first, the assertRedirects() doesn't seem to follow through to the
login URL.

def test_no_region_not_logged_in(self):
# if we have no regions and attempt to visit
# the "home" page, the site has been misconfigured so
# it should redirect to the admin page (via login)
# where a new region can be created.

url = reverse("home") # the URL that
# should redirect to /admin/common/region/add/
# via the login page

# self.client is instantiated in setUp()
response = self.client.get(url, follow=True)

# print repr(response.redirect_chain)
# the redirect_chain contains just has one entry
# rather than the two I'd expect

add_region_url = reverse("admin:common_region_add")
login_url = reverse("login")
expected_url = "%s?next=%s" % (
login_url,
quote(add_region_url)
)
self.assertRedirects(response, expected_url)

However my test fails with:

AssertionError: Response redirected to
'http://testserver/admin/common/region/add/', expected
'http://testserver/accounts/login/?next=/admin/common/region/add/'

Where am I going wrong? Do admin pages not have @login_required as I
expect? Does the test-client not fully follow when follow=True?

For what it's worth, this is Django 1.6.7 (I need something pre-1.7
because my hosting-provider's target environment only has Python 2.6,
not 2.7)

Thanks for any hints you might be able to offer.

-tkc



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20141127201514.09a0b667%40bigbox.christie.dr.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment