Simplified test of contrib.auth.tests.

This commit is contained in:
Jorge C. Leitão 2014-06-14 16:55:52 +02:00 committed by Tim Graham
parent 535219e4f7
commit 0a8c0eda2a
1 changed files with 5 additions and 9 deletions

View File

@ -48,16 +48,15 @@ class BaseModelBackendTest(object):
def test_has_perm(self): def test_has_perm(self):
user = self.UserModel._default_manager.get(pk=self.user.pk) user = self.UserModel._default_manager.get(pk=self.user.pk)
self.assertEqual(user.has_perm('auth.test'), False) self.assertEqual(user.has_perm('auth.test'), False)
user.is_staff = True user.is_staff = True
user.save() user.save()
self.assertEqual(user.has_perm('auth.test'), False) self.assertEqual(user.has_perm('auth.test'), False)
user.is_superuser = True user.is_superuser = True
user.save() user.save()
self.assertEqual(user.has_perm('auth.test'), True) self.assertEqual(user.has_perm('auth.test'), True)
user.is_staff = False
user.is_superuser = False
user.save()
self.assertEqual(user.has_perm('auth.test'), False)
user.is_staff = True user.is_staff = True
user.is_superuser = True user.is_superuser = True
user.is_active = False user.is_active = False
@ -69,7 +68,6 @@ class BaseModelBackendTest(object):
content_type = ContentType.objects.get_for_model(Group) content_type = ContentType.objects.get_for_model(Group)
perm = Permission.objects.create(name='test', content_type=content_type, codename='test') perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
user.user_permissions.add(perm) user.user_permissions.add(perm)
user.save()
# reloading user to purge the _perm_cache # reloading user to purge the _perm_cache
user = self.UserModel._default_manager.get(pk=self.user.pk) user = self.UserModel._default_manager.get(pk=self.user.pk)
@ -77,21 +75,20 @@ class BaseModelBackendTest(object):
self.assertEqual(user.get_group_permissions(), set([])) self.assertEqual(user.get_group_permissions(), set([]))
self.assertEqual(user.has_module_perms('Group'), False) self.assertEqual(user.has_module_perms('Group'), False)
self.assertEqual(user.has_module_perms('auth'), True) self.assertEqual(user.has_module_perms('auth'), True)
perm = Permission.objects.create(name='test2', content_type=content_type, codename='test2') perm = Permission.objects.create(name='test2', content_type=content_type, codename='test2')
user.user_permissions.add(perm) user.user_permissions.add(perm)
user.save()
perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3') perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
user.user_permissions.add(perm) user.user_permissions.add(perm)
user.save()
user = self.UserModel._default_manager.get(pk=self.user.pk) user = self.UserModel._default_manager.get(pk=self.user.pk)
self.assertEqual(user.get_all_permissions(), set(['auth.test2', 'auth.test', 'auth.test3'])) self.assertEqual(user.get_all_permissions(), set(['auth.test2', 'auth.test', 'auth.test3']))
self.assertEqual(user.has_perm('test'), False) self.assertEqual(user.has_perm('test'), False)
self.assertEqual(user.has_perm('auth.test'), True) self.assertEqual(user.has_perm('auth.test'), True)
self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), True) self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), True)
perm = Permission.objects.create(name='test_group', content_type=content_type, codename='test_group') perm = Permission.objects.create(name='test_group', content_type=content_type, codename='test_group')
group = Group.objects.create(name='test_group') group = Group.objects.create(name='test_group')
group.permissions.add(perm) group.permissions.add(perm)
group.save()
user.groups.add(group) user.groups.add(group)
user = self.UserModel._default_manager.get(pk=self.user.pk) user = self.UserModel._default_manager.get(pk=self.user.pk)
exp = set(['auth.test2', 'auth.test', 'auth.test3', 'auth.test_group']) exp = set(['auth.test2', 'auth.test', 'auth.test3', 'auth.test_group'])
@ -109,7 +106,6 @@ class BaseModelBackendTest(object):
content_type = ContentType.objects.get_for_model(Group) content_type = ContentType.objects.get_for_model(Group)
perm = Permission.objects.create(name='test', content_type=content_type, codename='test') perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
user.user_permissions.add(perm) user.user_permissions.add(perm)
user.save()
self.assertEqual(user.has_perm('auth.test', 'object'), False) self.assertEqual(user.has_perm('auth.test', 'object'), False)
self.assertEqual(user.get_all_permissions('object'), set([])) self.assertEqual(user.get_all_permissions('object'), set([]))