Пример #1
0
    def test_get_relation_types_definition_index_variable(self):
        book = BookFixture(Book, generate_m2m={'authors': (2, 2)}).create_one()
        get_nodeset_for_queryset(Store.objects.filter(pk=book.pk), sync=True)

        user = User.objects.filter(
            pk__in=book.authors.values('user')).latest('pk')
        perm = Permission.objects.get(content_type__app_label='auth',
                                      codename='change_user')

        access_rule = AccessRule.objects.create(
            ctype_source=utils.get_content_type(User),
            ctype_target=utils.get_content_type(User),
            relation_types=[
                {
                    'AUTHOR': None
                },
                {
                    'BOOK': None
                },
                {
                    '{0:AUTHORS}': {
                        'pk': '{source}.pk'
                    }
                },  # '{source}.pk' will be ignored
                {
                    'USER': None
                }
            ])
        access_rule.permissions.add(perm)
        user.user_permissions.add(perm)

        queryset = utils.get_users_with_perms(user, 'auth.change_user')
        self.assertEqual({user}, set(queryset))
Пример #2
0
    def test_no_perms_with_superusers(self):
        self.user1.is_superuser = True
        self.user1.save()

        queryset = utils.get_users_with_perms(self.book,
                                              permissions=[],
                                              with_superusers=True)
        self.assertEqual(set(queryset), {self.user1})
Пример #3
0
    def test_single_perm(self):
        access_rule = AccessRule.objects.create(
            ctype_source=utils.get_content_type(User),
            ctype_target=utils.get_content_type(Book),
            relation_types=[{
                'AUTHOR': None
            }, {
                'BOOK': None
            }])
        perm = Permission.objects.get(content_type__app_label='testapp',
                                      codename='view_book')
        access_rule.permissions.add(perm)
        self.user1.user_permissions.add(perm)

        users = utils.get_users_with_perms(obj=self.book,
                                           permissions='view_book')
        self.assertEqual(set(users), {self.user1})
Пример #4
0
    def test_multiple_perms(self):
        access_rule = AccessRule.objects.create(
            ctype_source=utils.get_content_type(User),
            ctype_target=utils.get_content_type(Book),
            relation_types=[{
                'AUTHOR': None
            }, {
                'BOOK': None
            }])
        perms = Permission.objects.filter(
            content_type__app_label='testapp',
            codename__in=['view_book', 'change_book'])
        access_rule.permissions.add(*perms)
        self.user1.user_permissions.add(*perms)
        self.user2.user_permissions.add(
            perms.get(codename='change_book'))  # Should not be in result set

        users = utils.get_users_with_perms(
            obj=self.book, permissions=['change_book', 'view_book'])
        self.assertEqual(set(users), {self.user1})
Пример #5
0
 def test_no_perms(self):
     queryset = utils.get_users_with_perms(self.book, permissions=[])
     self.assertEqual(set(queryset), set())