Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#9868 closed (fixed)

Test client login return values should be improved

Reported by: David Sauve <dnsauve@…> Owned by: nobody
Component: Testing framework Version: 1.0
Severity: Keywords: testing test client login session
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The current implementation of the django.test.client's login method returns either True or False when trying to login a user, however, there is no way of knowing why a login failed.

The login method could probably be modified to raise an exception with further information regarding why the login failed. For example, if INSTALLED_APPS does not include 'settings'.

Attachments (0)

Change History (4)

comment:1 Changed 5 years ago by David Sauve <dnsauve@…>

  • Component changed from Uncategorized to Testing framework
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by ericholscher

  • Resolution set to wontfix
  • Status changed from new to closed

I think that changing login() to raise an exception instead of returning False would be a backwards incompatibility. Testing to see if a login worked, and then if it returns false doing something else is probably used in people's test suites.

I agree that it would be nice to know why a login might fail, but I don't know if we can change this behavior without breaking pre-existing test suites.

I'm marking this wontfix, but if you can produce a patch and tests that show that this doesn't break Django's test suite, please re-open.

I changed the return False to a raise Exception, and 2 of Django's test client tests did indeed fail.

======================================================================
ERROR: Request a page that is protected with @login, but use bad credentials
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/ericholscher/Python/django-trunk/tests/modeltests/test_client/models.py", line 303, in test_view_with_bad_login
    login = self.client.login(username='otheruser', password='nopassword')
  File "/Users/ericholscher/Python/django-trunk/django/test/client.py", line 408, in login
    raise Exception
Exception

======================================================================
ERROR: Request a page that is protected with @login, but use an inactive login
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/ericholscher/Python/django-trunk/tests/modeltests/test_client/models.py", line 309, in test_view_with_inactive_login
    login = self.client.login(username='inactive', password='password')
  File "/Users/ericholscher/Python/django-trunk/django/test/client.py", line 408, in login
    raise Exception
Exception

----------------------------------------------------------------------
Ran 34 tests in 0.933s

comment:3 Changed 5 years ago by mtredinnick

  • Resolution changed from wontfix to fixed

(In [9979]) Improved table join handling for comparisons against NULL.

This fixes a broad class of bugs involving filters that look for missing
related models and fields. Most of them don't seem to have been reported
(the added tests cover the root cause). The exception is that this has
also fixed #9868.

comment:4 Changed 5 years ago by mtredinnick

Ignore previous comment. I typo'd the bug number (should have been #9968).

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.