|
@@ -1,8 +1,12 @@
|
|
|
+from django.apps import apps
|
|
|
from django.contrib.auth import authenticate, signals
|
|
|
from django.contrib.auth.models import User
|
|
|
+from django.core.exceptions import FieldDoesNotExist
|
|
|
from django.test import TestCase, override_settings
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
+from .models import MinimalUser
|
|
|
+
|
|
|
|
|
|
@override_settings(ROOT_URLCONF='auth_tests.urls')
|
|
|
class SignalTestCase(TestCase):
|
|
@@ -82,3 +86,23 @@ class SignalTestCase(TestCase):
|
|
|
def test_failed_login_without_request(self):
|
|
|
authenticate(username='testclient', password='bad')
|
|
|
self.assertIsNone(self.login_failed[0]['request'])
|
|
|
+
|
|
|
+ def test_login_with_custom_user_without_last_login_field(self):
|
|
|
+ """
|
|
|
+ The user_logged_in signal is only registered if the user model has a
|
|
|
+ last_login field.
|
|
|
+ """
|
|
|
+ last_login_receivers = signals.user_logged_in.receivers
|
|
|
+ try:
|
|
|
+ signals.user_logged_in.receivers = []
|
|
|
+ with self.assertRaises(FieldDoesNotExist):
|
|
|
+ MinimalUser._meta.get_field('last_login')
|
|
|
+ with self.settings(AUTH_USER_MODEL='auth_tests.MinimalUser'):
|
|
|
+ apps.get_app_config('auth').ready()
|
|
|
+ self.assertEqual(signals.user_logged_in.receivers, [])
|
|
|
+
|
|
|
+ with self.settings(AUTH_USER_MODEL='auth.User'):
|
|
|
+ apps.get_app_config('auth').ready()
|
|
|
+ self.assertEqual(len(signals.user_logged_in.receivers), 1)
|
|
|
+ finally:
|
|
|
+ signals.user_logged_in.receivers = last_login_receivers
|