[Django] #22771: TestCase.client.logout fails when using custom authentication backend not based on AUTH_USER_MODEL

29 views
Skip to first unread message

Django

unread,
Jun 5, 2014, 1:14:10 PM6/5/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
---------------------------------+-----------------------------------------
Reporter: xafer | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing | Version: 1.6
framework | Keywords: logout test AUTH_USER_MODEL
Severity: Normal | Has patch: 0
Triage Stage: Unreviewed | UI/UX: 0
Easy pickings: 0 |
---------------------------------+-----------------------------------------
Hello !

I'm not sure if it's a bug or a new feature:

TestCase.client.login calls django.contrib.auth.login(request, user)
which stores the user id and its backend in the request's session:
request.session[SESSION_KEY] = user.pk
request.session[BACKEND_SESSION_KEY] = user.backend

But in TestCase.client.logout calls, Django tries to get the authenticated
user with:
uid = self.session.get(SESSION_KEY)
request.user = UserModel._default_manager.get(pk=uid)

assuming user is a AUTH_USER_MODEL and without checking its
BACKEND_SESSION_KEY...

It would seem cleaner to actually rly on django.contrib.auth.get_user to
find the correct user (whatever its type) and allow the concurrent use of
multiple user models (typically user model and customer model...).

See https://github.com/xavfernandez/django_test_client_bugreport for a
failing example.

--
Ticket URL: <https://code.djangoproject.com/ticket/22771>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 6, 2014, 4:53:12 AM6/6/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
-------------------------------------+-------------------------------------

Reporter: xafer | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:
Keywords: logout test | Triage Stage:
AUTH_USER_MODEL | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by xafer):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

To backup the change, using django.contrib.auth.get_user is also what's
done by the AuthenticationMiddleware (cf
https://github.com/django/django/blob/1.6.5/django/contrib/auth/middleware.py#L18)

--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:1>

Django

unread,
Jun 6, 2014, 5:24:26 AM6/6/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
-------------------------------------+-------------------------------------

Reporter: xafer | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:
Keywords: logout test | Triage Stage:
AUTH_USER_MODEL | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by xafer):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:2>

Django

unread,
Jun 10, 2014, 4:56:58 AM6/10/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
-------------------------------------+-------------------------------------

Reporter: xafer | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:
Keywords: logout test | Triage Stage:
AUTH_USER_MODEL | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by xafer):

cf https://github.com/django/django/pull/2769

--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:3>

Django

unread,
Jun 13, 2014, 1:04:41 PM6/13/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: xafer | Owner: nobody
Type: Uncategorized | Status: closed

Component: Testing framework | Version: 1.6
Severity: Normal | Resolution: fixed

Keywords: logout test | Triage Stage:
AUTH_USER_MODEL | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"50b9313e0a0739dfe1ac699449b66ed3e62add51"]:
{{{
#!CommitTicketReference repository=""
revision="50b9313e0a0739dfe1ac699449b66ed3e62add51"
Fixed #22771 -- Fixed test.Client.logout when using custom auth backend.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:4>

Django

unread,
Jun 16, 2014, 4:49:41 AM6/16/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: xafer | Owner: nobody

Type: Uncategorized | Status: closed
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution: fixed
Keywords: logout test | Triage Stage:
AUTH_USER_MODEL | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by xafer):

Thanks for the quick fix !
Any chance this could make it into 1.7 ?

--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:5>

Django

unread,
Jun 16, 2014, 8:35:27 AM6/16/14
to django-...@googlegroups.com
#22771: TestCase.client.logout fails when using custom authentication backend not
based on AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: xafer | Owner: nobody

Type: Uncategorized | Status: closed
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution: fixed
Keywords: logout test | Triage Stage:
AUTH_USER_MODEL | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"183e9d20294dec28df8ae5ea8ed7acc712adcd2b"]:
{{{
#!CommitTicketReference repository=""
revision="183e9d20294dec28df8ae5ea8ed7acc712adcd2b"
[1.7.x] Fixed #22771 -- Fixed test.Client.logout when using custom auth
backend.

Backport of 50b9313e0a from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:6>

Reply all
Reply to author
Forward
0 new messages