Code

#20114 closed Bug (fixed)

Failures in tests in django.contrib.auth when using custom LOGIN_URL/LOGOUT_URL

Reported by: jriley@… Owned by: matiasb
Component: contrib.auth Version: 1.5
Severity: Normal Keywords: tests
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

My LOGIN_URL on one project is set to /accounts/signin/, and upon running tests, contrib.auth fails testLoginRequired. If I move the LOGIN_URL back to /login/ this test passes just fine. Looking at contrib.auth.tests.decorators, it appears the login_url is hard-coded to /login/ in the test suite - it does allow overriding of the login_url as a function argument, but that override isn't actually happening when the tests are run.

When I attached a debugger and ran through the tests to this point, as I predicted, I saw these two values:

response['Location'] = 'http://testserver/accounts/signin/?next=/login_required/'
login_url = '/login/'

While this case would have worked if my URL was /accounts/login/, it ultimately fails as tests fail if your LOGIN_URL doesn't contain /login/, despite the documentation indicating you can use any URL you wish. These test failures are new in 1.5; all tests for contrib.auth passed just fine on the same codebase before upgrading to 1.5 from 1.4.5, so I presume this was introduced with the major overhaul of contrib.auth for 1.5.

I was able to replicate the testLoginRequired failure with a fresh Django 1.5 project using the following steps:

  1. Add the following URLs to project urls.py:
    url(r'^signin/$', 'django.contrib.auth.views.login'),
    url(r'^signout/$', 'django.contrib.auth.views.logout'),
    
  2. Configure LOGIN_URL and LOGOUT_URL:
    LOGIN_URL = '/signin/'
    LOGOUT_URL = '/signout/'
    
  3. Run tests
    ./manage.py test auth
    

Result:

$ python manage.py test auth
Creating test database for alias 'default'...
..............................................................F.................................................................................s...................................
======================================================================
FAIL: testLoginRequired (django.contrib.auth.tests.decorators.LoginRequiredTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jriley/.virtualenvs/test/local/lib/python2.7/site-packages/django/contrib/auth/tests/decorators.py", line 37, in testLoginRequired
    self.assertTrue(login_url in response['Location'])
AssertionError: False is not true

----------------------------------------------------------------------
Ran 180 tests in 8.206s

FAILED (failures=1, skipped=1)
Destroying test database for alias 'default'...

It's certainly not severe; I just switched my URLs back to /login/ and /logout/ respectively, but it would be nice to have full configurability again. :)

Thank you for taking the time to look at this!

Attachments (0)

Change History (4)

comment:1 Changed 16 months ago by matiasb

  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Just reproduced with 1.5 and master.

comment:2 Changed 16 months ago by matiasb

  • Owner changed from nobody to matiasb
  • Status changed from new to assigned

comment:3 Changed 16 months ago by matiasb

  • Has patch set

Proposed pull request: https://github.com/django/django/pull/952
Tests passing with SQLite.

comment:4 Changed 15 months ago by Preston Holmes <preston@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In a49e7dd2a34882fc68244e024eb2876b21c7e8a8:

Fixed #20114 -- support custom project login_url in tests

Thanks to Matias Bordese for the patch

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.