From a962bc7c45d868523a18e5139b96b275b3df2589 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 24 Nov 2012 15:56:31 +0100 Subject: [PATCH] Updated User manager when testing custom AUTH_USER_MODEL This is giving more real test conditions when AUTH_USER_MODEL is set with override_settings. --- django/contrib/auth/tests/__init__.py | 13 +++++++++++++ django/contrib/auth/tests/basic.py | 2 ++ 2 files changed, 15 insertions(+) diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index b3007ea484..038c8980d9 100644 --- a/django/contrib/auth/tests/__init__.py +++ b/django/contrib/auth/tests/__init__.py @@ -14,3 +14,16 @@ from django.contrib.auth.tests.tokens import * from django.contrib.auth.tests.views import * # The password for the fixture data users is 'password' + +from django.dispatch import receiver +from django.test.signals import setting_changed + + +@receiver(setting_changed) +def user_model_swapped(**kwargs): + if kwargs['setting'] == 'AUTH_USER_MODEL': + from django.db.models.manager import ensure_default_manager + from django.contrib.auth.models import User + # Reset User manager + setattr(User, 'objects', User._default_manager) + ensure_default_manager(User) diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py index bc7344f753..2c807cca51 100644 --- a/django/contrib/auth/tests/basic.py +++ b/django/contrib/auth/tests/basic.py @@ -162,6 +162,8 @@ class BasicTestCase(TestCase): def test_swappable_user(self): "The current user model can be swapped out for another" self.assertEqual(get_user_model(), CustomUser) + with self.assertRaises(AttributeError): + User.objects.all() @override_settings(AUTH_USER_MODEL='badsetting') def test_swappable_user_bad_setting(self):