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.
* 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>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:2>
Comment (by xafer):
cf https://github.com/django/django/pull/2769
--
Ticket URL: <https://code.djangoproject.com/ticket/22771#comment:3>
* 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>
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>
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>