English 中文(简体)
能够在 d戈进行单位测试时改变用户许可。
原标题:can t change user permissions during unittest in django

最后,我决定对我的照片进行一些测试,但如果用户能够改变另一个用户(取决于用户的类型——我使用django-rules能够进行逻辑的许可检查,但这不是重要的)我就坐在测试上。

在这方面,我迄今为止已经制定了法典。

class RulesAndPermissionsTests(TestCase):
    fixtures = [ auth_no_permissions.json ,  profiles.json ,  rules.json ]

    def setUp(self):
        self.c = Client()
        self.user = User.objects.get(username="estagiario")
        self.non_staff = User.objects.get(username="fisica")
        self.admin = User.objects.get(username="admin")
        login = self.c.login(username= estagiario , password= estagiario )

    def test_can_change_non_staff_users(self):
        self.assertFalse(self.user.has_perm( logical_change_user , self.non_staff.profile)) # can t change non staff users without permission

        # now add the permission and test it again
        self.user.user_permissions.add(Permission.objects.get(codename= change_user ))
        print self.user.get_all_permissions() # prints set([])
        self.assertTrue(self.user.has_perm( logical_change_user , self.non_staff.profile))

即使在增加了许可之后,我的使用者仍然没有许可。 这是否是因为我不允许在试验中制造任何东西(这是一种坏的做法?)? 或者,是否允许 d? 如果我增加在规定的许可 当它工作时,我希望在同样的试验中加以改变(在未经许可的情况下进行测试)。

提前感谢!

最佳回答

如果看https://github.com/django/django/blob/master/django/contrib/auth/backends.py” rel=“noreferer”>ModelBackend,请看Django在用户物体上的许可。

You could try wiping the cache, but that could break your tests if the caching mechanism changes in future. The easiest thing to do is to refetch the user from the database in your test.

from django.contrib.auth.models import Permission

def test_can_change_non_staff_users(self):
    self.assertFalse(self.user.has_perm( logical_change_user , self.non_staff.profile)) # can t change non staff users without permission

    # now add the permission and test it again
    self.user.user_permissions.add(Permission.objects.get(codename= change_user ))

    # refetch user from the database
    self.user = User.objects.get(pk=self.user.pk)
    print self.user.get_all_permissions() # should now include new permission
    self.assertTrue(self.user.has_perm( logical_change_user , self.non_staff.profile))
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签