class NoAnonymousUserBackendTest(TestCase): """ Tests that AnonymousUser does not delegate to backend if it has 'supports_anonymous_user' = False """ backend = 'hipercic.hipercore.authenticore\.tests.auth_backends.NoAnonymousUserBackend' def setUp(self): self.curr_authenticore = settings.AUTHENTICATION_BACKENDS settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,) self.user1 = AnonymousUser() def tearDown(self): settings.AUTHENTICATION_BACKENDS = self.curr_auth def test_has_perm(self): self.assertEqual(self.user1.has_perm('perm', TestObj()), False) self.assertEqual(self.user1.has_perm('anon', TestObj()), False) def test_has_perms(self): self.assertEqual(self.user1.has_perms(['anon'], TestObj()), False) def test_has_module_perms(self): self.assertEqual(self.user1.has_module_perms("app1"), False) self.assertEqual(self.user1.has_module_perms("app2"), False) def test_get_all_permissions(self): self.assertEqual(self.user1.get_all_permissions(TestObj()), set())
def test_anonymous_user(self): "Check the properties of the anonymous user" a = AnonymousUser() self.assertFalse(a.is_authenticated()) self.assertFalse(a.is_app_manager) self.assertFalse(a.is_active) self.assertFalse(a.is_admin) self.assertEqual(a.groups.all().count(), 0) self.assertEqual(a.user_permissions.all().count(), 0)
def test_custom_perms(self): user = User.objects.get(username='******') content_type=ContentType.objects.get_for_model(Group) perm = Permission.objects.create(name='test', content_type=content_type, codename='test') user.user_permissions.add(perm) user.save() # reloading user to purge the _perm_cache user = User.objects.get(username='******') self.assertEqual(user.get_all_permissions() == set([u'authenticore\.test']), True) self.assertEqual(user.get_group_permissions(), set([])) self.assertEqual(user.has_module_perms('Group'), False) self.assertEqual(user.has_module_perms('auth'), True) perm = Permission.objects.create(name='test2', content_type=content_type, codename='test2') user.user_permissions.add(perm) user.save() perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3') user.user_permissions.add(perm) user.save() user = User.objects.get(username='******') self.assertEqual(user.get_all_permissions(), set([u'authenticore\.test2', u'authenticore\.test', u'authenticore\.test3'])) self.assertEqual(user.has_perm('test'), False) self.assertEqual(user.has_perm('authenticore\.test'), True) self.assertEqual(user.has_perms(['authenticore\.test2', 'authenticore\.test3']), True) perm = Permission.objects.create(name='test_group', content_type=content_type, codename='test_group') group = Group.objects.create(name='test_group') group.permissions.add(perm) group.save() user.groups.add(group) user = User.objects.get(username='******') exp = set([u'authenticore\.test2', u'authenticore\.test', u'authenticore\.test3', u'authenticore\.test_group']) self.assertEqual(user.get_all_permissions(), exp) self.assertEqual(user.get_group_permissions(), set([u'authenticore\.test_group'])) self.assertEqual(user.has_perms(['authenticore\.test3', 'authenticore\.test_group']), True) user = AnonymousUser() self.assertEqual(user.has_perm('test'), False) self.assertEqual(user.has_perms(['authenticore\.test2', 'authenticore\.test3']), False)
def setUp(self): self.curr_authenticore = settings.AUTHENTICATION_BACKENDS settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,) self.user1 = AnonymousUser()