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

201 views
Skip to first unread message

Tim Chase

unread,
Nov 27, 2014, 9:13:16 PM11/27/14
to django...@googlegroups.com
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



Tim Chase

unread,
Nov 27, 2014, 9:30:32 PM11/27/14
to django...@googlegroups.com
On 2014-11-27 20:15, Tim Chase wrote:
> 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?

As a bit of follow-up information, if I use runserver and browse to
the view, it redirects me to /admin/common/region/add/ but it
displays as the login screen. Am I missing why this wouldn't do a
redirect to my named login URL?

(this at least demonstrates that the test-client and browser/server
behavior is consistent; I'm just unsure why I'm not getting the login
url)

Thanks,

-tim



Tim Chase

unread,
Nov 28, 2014, 7:46:05 AM11/28/14
to django...@googlegroups.com
On 2014-11-27 20:32, Tim Chase wrote:
> As a bit of follow-up information, if I use runserver and browse to
> the view, it redirects me to /admin/common/region/add/ but it
> displays as the login screen. Am I missing why this wouldn't do a
> redirect to my named login URL?

A careful reading of [1] suggests that the admin provides its own
login page via the login_template setting: "Path to a custom template
that will be used by the admin site login view." It apparently
renders this at the URL of the destination page rather than
redirecting to the regular (contrib.auth or some named auth-URL) and
providing a "next={{original_url}}"

Is there any way to get the admin to just use the contrib.auth login
page that I already have in place?

-tkc

[1]
https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#django.contrib.admin.AdminSite.login_template



Collin Anderson

unread,
Nov 30, 2014, 2:04:47 PM11/30/14
to django...@googlegroups.com, django...@tim.thechases.com
Hi,

As of django 1.7, the admin will redirect to a separate login page (whatever url reverse('admin:login') returns).


You could use a middleware to do an earlier, more straightforward require login.

Collin

Tim Chase

unread,
Dec 3, 2014, 2:02:42 PM12/3/14
to Collin Anderson, django...@googlegroups.com, django...@tim.thechases.com
On 2014-11-30 11:04, Collin Anderson wrote:
> As of django 1.7, the admin will redirect to a separate login page
> (whatever url reverse('admin:login') returns).
>
> https://github.com/django/django/commit/be0ad62994a340ad54a0b328771931932a45a899

Shouldn't this reach for reverse(settings.LOGIN_URL) instead?

-tkc


Collin Anderson

unread,
Dec 5, 2014, 8:28:54 AM12/5/14
to django...@googlegroups.com, cmawe...@gmail.com, django...@tim.thechases.com
Hi,

If you define your url before the admin, it might get your view instead. (I _think_ it will work, though I may be totally wrong.)

Collin
Reply all
Reply to author
Forward
0 new messages