1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #26823 -- Prevented update_last_login signal receiver from crashing if User model doesn't have last_login field.

This commit is contained in:
Linus Lewandowski
2017-05-27 14:35:02 +02:00
committed by Tim Graham
parent e84034b37a
commit eedc88bd4a
5 changed files with 38 additions and 5 deletions

View File

@@ -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