Exemple #1
0
 def test_add_permission_to_user(self):
     content_type = ContentType.objects.get_for_model(User)
     perm = Permission.objects.create(name='test',
                                      content_type=content_type,
                                      codename='test')
     user = User.objects.get(username='******')
     self.assertEqual(user.has_perm('auth.test'), False)
     user = User.objects.get(username='******')
     add_permission_to_user(perm, user)
     self.assertEqual(UserPermissionList.objects.count(), 1)
     pl = UserPermissionList.objects.all()[0]
     self.assertEqual(pl.permissions , set([perm]))
     self.assertEqual(user.has_perm('auth.test'), True)
     self.assertEqual(user.has_perm('auth.test23x'), False)
Exemple #2
0
 def test_add_permission_to_user(self):
     content_type = ContentType.objects.get_for_model(User)
     perm = Permission.objects.create(name='test',
                                      content_type=content_type,
                                      codename='test')
     user = User.objects.get(username='******')
     self.assertEqual(user.has_perm('auth.test'), False)
     user = User.objects.get(username='******')
     add_permission_to_user(perm, user)
     self.assertEqual(UserPermissionList.objects.count(), 1)
     pl = UserPermissionList.objects.all()[0]
     self.assertEqual(pl.permissions, set([perm]))
     self.assertEqual(user.has_perm('auth.test'), True)
     self.assertEqual(user.has_perm('auth.test23x'), False)
Exemple #3
0
    def test_has_no_object_perm(self):
        """Regressiontest for #12462"""
        
        user = User.objects.get(username='******')
        content_type = ContentType.objects.get_for_model(Group)
        content_type.save()
        perm = Permission.objects.create(name='test',
                                         content_type=content_type,
                                         codename='test')
        
        # default django way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)

        self.assertEqual(user.has_perm('auth.test', 'object'), False)
        self.assertEqual(user.get_all_permissions('object'), set([]))
        self.assertEqual(user.has_perm('auth.test'), True)
        self.assertEqual(user.get_all_permissions(), set(['auth.test']))
Exemple #4
0
    def test_has_no_object_perm(self):
        """Regressiontest for #12462"""

        user = User.objects.get(username='******')
        content_type = ContentType.objects.get_for_model(Group)
        content_type.save()
        perm = Permission.objects.create(name='test',
                                         content_type=content_type,
                                         codename='test')

        # default django way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)

        self.assertEqual(user.has_perm('auth.test', 'object'), False)
        self.assertEqual(user.get_all_permissions('object'), set([]))
        self.assertEqual(user.has_perm('auth.test'), True)
        self.assertEqual(user.get_all_permissions(), set(['auth.test']))
Exemple #5
0
    def test_custom_perms(self):
        user = User.objects.get(username='******')
        content_type = ContentType.objects.get_for_model(Permission)
        perm = Permission.objects.create(name='test',
                                         content_type=content_type,
                                         codename='test')
        # default django way (ManyToManyField)
        #user.user_permissions.add(perm)      

        add_permission_to_user(perm, user)
        
        # reloading user to purge the _perm_cache
        user = User.objects.get(username='******')
        self.assertEqual(user.get_all_permissions() == set([u'auth.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')
        
        # default django way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)
        
        perm = Permission.objects.create(name='test3',
                                         content_type=content_type,
                                         codename='test3')

        # default django  way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)

        user = User.objects.get(username='******')
        self.assertEqual(user.get_all_permissions(),
                         set([u'auth.test2', u'auth.test', u'auth.test3']))
        self.assertEqual(user.has_perm('test'), False)
        self.assertEqual(user.has_perm('auth.test'), 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')
        group = Group.objects.create(name='test_group')

        # default django way (ManyToManyField)
        #group.permissions.add(perm)

        add_permission_to_group(perm, group)

        # default django way (ManyToManyField)
        #user.groups.add(group)

        add_user_to_group(user, group)
        
        user = User.objects.get(username='******')
        exp = set([u'auth.test2', u'auth.test',
                   u'auth.test3', u'auth.test_group'])
        self.assertEqual(user.get_all_permissions(), exp)
        self.assertEqual(user.get_group_permissions(),
                         set([u'auth.test_group']))
        self.assertEqual(user.has_perms(['auth.test3', 'auth.test_group']),
                         True)

        user = AnonymousUser()
        self.assertEqual(user.has_perm('test'), False)
        self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), False)
Exemple #6
0
    def test_custom_perms(self):
        user = User.objects.get(username='******')
        content_type = ContentType.objects.get_for_model(Permission)
        perm = Permission.objects.create(name='test',
                                         content_type=content_type,
                                         codename='test')
        # default django way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)

        # reloading user to purge the _perm_cache
        user = User.objects.get(username='******')
        self.assertEqual(user.get_all_permissions() == set([u'auth.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')

        # default django way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)

        perm = Permission.objects.create(name='test3',
                                         content_type=content_type,
                                         codename='test3')

        # default django  way (ManyToManyField)
        #user.user_permissions.add(perm)

        add_permission_to_user(perm, user)

        user = User.objects.get(username='******')
        self.assertEqual(user.get_all_permissions(),
                         set([u'auth.test2', u'auth.test', u'auth.test3']))
        self.assertEqual(user.has_perm('test'), False)
        self.assertEqual(user.has_perm('auth.test'), 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')
        group = Group.objects.create(name='test_group')

        # default django way (ManyToManyField)
        #group.permissions.add(perm)

        add_permission_to_group(perm, group)

        # default django way (ManyToManyField)
        #user.groups.add(group)

        add_user_to_group(user, group)

        user = User.objects.get(username='******')
        exp = set(
            [u'auth.test2', u'auth.test', u'auth.test3', u'auth.test_group'])
        self.assertEqual(user.get_all_permissions(), exp)
        self.assertEqual(user.get_group_permissions(),
                         set([u'auth.test_group']))
        self.assertEqual(user.has_perms(['auth.test3', 'auth.test_group']),
                         True)

        user = AnonymousUser()
        self.assertEqual(user.has_perm('test'), False)
        self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), False)