Browse Source

Merge pull request #2321 from django-oscar/fix/auth_backend_signature

Fix EmailBackend.authenticate() signature for Django 1.11.
master
Alexander Gaevsky 8 years ago
parent
commit
f87bc62cd9

+ 2
- 0
docs/source/releases/v1.5.rst View File

@@ -44,6 +44,8 @@ These methods/modules have been removed:
44 44
    alternative.
45 45
  - ``oscar.test.decorators`` module.
46 46
  - ``oscar.core.utils.compose`` function.
47
+ - ``oscar.apps.customer.auth_backends.Emailbackend``. Use
48
+   ``oscar.apps.customer.auth_backends.EmailBackend`` instead.
47 49
 
48 50
 
49 51
 .. _silk: https://github.com/django-silk/silk

+ 10
- 12
src/oscar/apps/customer/auth_backends.py View File

@@ -1,5 +1,6 @@
1 1
 import warnings
2 2
 
3
+import django
3 4
 from django.contrib.auth.backends import ModelBackend
4 5
 from django.core.exceptions import ImproperlyConfigured
5 6
 
@@ -22,7 +23,7 @@ class EmailBackend(ModelBackend):
22 23
     For this to work, the User model must have an 'email' field
23 24
     """
24 25
 
25
-    def authenticate(self, email=None, password=None, *args, **kwargs):
26
+    def _authenticate(self, request, email=None, password=None, *args, **kwargs):
26 27
         if email is None:
27 28
             if 'username' not in kwargs or kwargs['username'] is None:
28 29
                 return None
@@ -55,14 +56,11 @@ class EmailBackend(ModelBackend):
55 56
                 "password")
56 57
         return None
57 58
 
58
-
59
-# Deprecated since Oscar 1.0 because of the spelling.
60
-class Emailbackend(EmailBackend):
61
-
62
-    def __init__(self):
63
-        warnings.warn(
64
-            "Oscar's auth backend EmailBackend has been renamed in Oscar 1.0 "
65
-            " and you're using the old name of Emailbackend. Please rename "
66
-            " all references; most likely in the AUTH_BACKENDS setting.",
67
-            DeprecationWarning)
68
-        super(Emailbackend, self).__init__()
59
+    # The signature of the authenticate method changed in Django 1.11 to include
60
+    # a mandatory `request` argument.
61
+    if django.VERSION < (1, 11):
62
+        def authenticate(self, email=None, password=None, *args, **kwargs):
63
+            return self._authenticate(None, email, password, *args, **kwargs)
64
+    else:
65
+        def authenticate(self, *args, **kwargs):
66
+            return self._authenticate(*args, **kwargs)

+ 26
- 0
tests/integration/customer/test_auth_backend.py View File

@@ -0,0 +1,26 @@
1
+import unittest
2
+
3
+import django
4
+from django.test import TestCase
5
+
6
+from oscar.apps.customer.auth_backends import EmailBackend
7
+from oscar.test.factories import UserFactory
8
+
9
+
10
+class AuthBackendTestCase(TestCase):
11
+
12
+    def setUp(self):
13
+        self.user = UserFactory(email='foo@example.com', is_staff=True)
14
+        self.user.set_password('letmein')
15
+        self.user.save()
16
+        self.backend = EmailBackend()
17
+
18
+    @unittest.skipUnless(django.VERSION < (1, 11), "for Django <1.11 only")
19
+    def test_authentication_method_signature_pre_django_1_11(self):
20
+        auth_result = self.backend.authenticate('foo@example.com', 'letmein')
21
+        self.assertEqual(auth_result, self.user)
22
+
23
+    @unittest.skipUnless(django.VERSION >= (1, 11), "for Django >=1.11 only")
24
+    def test_authentication_method_signature_post_django_1_11(self):
25
+        auth_result = self.backend.authenticate(None, 'foo@example.com', 'letmein')
26
+        self.assertEqual(auth_result, self.user)

Loading…
Cancel
Save