相关疑难解决方法(0)

在django中的unittest期间无法更改用户权限

我最终决定对我的应用程序进行一些测试,但是如果用户可以更改另一个用户,我会坚持测试(取决于用户的类型 - 我使用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))
Run Code Online (Sandbox Code Playgroud)

即使在添加权限后,我的用户仍然没有权限.这是因为我不允许在测试期间创建任何东西(这是一种不好的做法吗?)?或者django以某种方式缓存权限?如果我在setUp添加权限,它可以工作,但我想在同一个测试中更改它(使用和不使用权限进行测试).

提前致谢!

django django-testing django-permissions

4
推荐指数
1
解决办法
2702
查看次数