Gautier Hayoun committed d4eba58

Fix custom user authentication handling for django 1.5

Comments (0)

Files changed (3)


         # getting passed in the headers, then the correct user is already
         # persisted in the session and we don't need to continue.
         if request.user.is_authenticated():
-            if request.user.username == self.clean_username(username, request):
+            if request.user.get_username() == self.clean_username(username, request):
         # We are seeing this user for the first time in this session, attempt
         # to authenticate the user.


+import django
+if django.get_version() >= "1.5":
+    from django.contrib.auth.models import AbstractBaseUser
+    from django.db import models
+    class CustomUser(AbstractBaseUser):
+        email = models.EmailField(
+            max_length=255,
+            unique=True,
+            db_index=True,
+        )
+        USERNAME_FIELD = 'email'


 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals, absolute_import
+import unittest
 from webtest import AppError
+import django
 from django_webtest import WebTest
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
         user = response.context['user']
     def test_standard_auth(self):
         resp = self._login(self.user.username, '123').follow()
         user = resp.context['user']
         self.assertEqual(user, self.user)
+    @unittest.skipIf(django.get_version() < '1.5',
+            "Only matters with custom users")
+    def test_reusing_custom_user(self):
+        from testapp_tests.models import CustomUser
+        with self.settings(AUTH_USER_MODEL = 'testapp_tests.CustomUser'):
+            custom_user = CustomUser.objects.create(email="")
+            custom_user.set_password("123")
+            # Let the middleware logs the user in
+  '/template/index.html', user=custom_user)
+            # Middleware authentication check shouldn't crash
+            response ='/template/index.html', user=custom_user)
+            user = response.context['user']
+            assert user.is_authenticated()
+            self.assertEqual(user, custom_user)
 class EnvironTest(BaseAuthTest):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.