Ticket #12776: 12776.patch

File 12776.patch, 3.8 KB (added by Bruno Renié, 15 years ago)

Throws more SiteProfileNotAvailable, adds some verbosity

  • django/contrib/auth/tests/__init__.py

     
    66        import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
    77from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
    88from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS
     9from django.contrib.auth.tests.models import ProfileTestCase
    910
    1011# The password for the fixture data users is 'password'
    1112
  • django/contrib/auth/tests/models.py

     
     1from django.conf import settings
     2from django.test import TestCase
     3from django.contrib.auth.models import User, SiteProfileNotAvailable
     4
     5class ProfileTestCase(TestCase):
     6    fixtures = ['authtestdata.json']
     7    def setUp(self):
     8        """Backs up the AUTH_PROFILE_MODULE"""
     9        self.old_AUTH_PROFILE_MODULE = getattr(settings,
     10                                               'AUTH_PROFILE_MODULE', None)
     11
     12    def tearDown(self):
     13        """Restores the AUTH_PROFILE_MODULE -- if it was not set it is deleted,
     14        otherwise the old value is restored"""
     15        if self.old_AUTH_PROFILE_MODULE is None and \
     16                hasattr(settings, 'AUTH_PROFILE_MODULE'):
     17            del settings.AUTH_PROFILE_MODULE
     18
     19        if self.old_AUTH_PROFILE_MODULE is not None:
     20            settings.AUTH_PROFILE_MODULE = self.old_AUTH_PROFILE_MODULE
     21
     22    def test_site_profile_not_available(self):
     23        # calling get_profile without AUTH_PROFILE_MODULE set
     24        if hasattr(settings, 'AUTH_PROFILE_MODULE'):
     25            del settings.AUTH_PROFILE_MODULE
     26        user = User.objects.get(username='testclient')
     27        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
     28
     29        # Bad syntax in AUTH_PROFILE_MODULE:
     30        settings.AUTH_PROFILE_MODULE = 'foobar'
     31        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
     32
     33        # module that doesn't exist
     34        settings.AUTH_PROFILE_MODULE = 'foo.bar'
     35        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
  • django/contrib/auth/models.py

     
    336336        if not hasattr(self, '_profile_cache'):
    337337            from django.conf import settings
    338338            if not getattr(settings, 'AUTH_PROFILE_MODULE', False):
    339                 raise SiteProfileNotAvailable
     339                raise SiteProfileNotAvailable('You need to set AUTH_PROFILE_MO'
     340                                              'DULE in your project settings')
    340341            try:
    341342                app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
     343            except ValueError:
     344                raise SiteProfileNotAvailable('app_label and model_name should'
     345                        ' be separated by a dot in the AUTH_PROFILE_MODULE set'
     346                        'ting')
     347
     348            try:
    342349                model = models.get_model(app_label, model_name)
     350                if model is None:
     351                    raise SiteProfileNotAvailable('Unable to load the profile '
     352                        'model, check AUTH_PROFILE_MODULE in your project sett'
     353                        'ings')
    343354                self._profile_cache = model._default_manager.using(self._state.db).get(user__id__exact=self.id)
    344355                self._profile_cache.user = self
    345356            except (ImportError, ImproperlyConfigured):
Back to Top