Beispiel #1
0
    def test_require_permissions__have_no_permission(self):
        view = Mock(return_value="fake response")
        required_permissions = ["test_permission1", "test_permission2"]
        self.request.user.has_perm.return_value = False

        decorator = require_permissions(required_permissions)
        decorated = decorator(view)
        with self.assertRaises(Forbidden):
            decorated(self, self.request)
Beispiel #2
0
    def test_require_permissions__have_no_permission(self):
        view = Mock(return_value='fake response')
        required_permissions = ['test_permission1', 'test_permission2']
        self.request.user.has_perm.return_value = False

        decorator = require_permissions(required_permissions)
        decorated = decorator(view)
        with self.assertRaises(Forbidden):
            decorated(self, self.request)
Beispiel #3
0
    def test_require_permissions__have_all_required_permissions(self):
        view = Mock(return_value="fake response")
        required_permissions = ["test_permission1", "test_permission2"]
        self.request.user.has_perm.return_value = True

        decorator = require_permissions(required_permissions)
        decorated = decorator(view)
        decorated(self, self.request)

        # It will check each permission in the list.
        self.assertEqual(self.request.user.has_perm.call_count, len(required_permissions))
        self.request.user.has_perm.assert_any_call(required_permissions[0])
        self.request.user.has_perm.assert_any_call(required_permissions[1])

        # After the permissions are checked, it will call the view function.
        view.assert_called_once_with(self, self.request)
Beispiel #4
0
    def test_require_permissions__have_all_required_permissions(self):
        view = Mock(return_value='fake response')
        required_permissions = ['test_permission1', 'test_permission2']
        self.request.user.has_perm.return_value = True

        decorator = require_permissions(required_permissions)
        decorated = decorator(view)
        decorated(self, self.request)

        # It will check each permission in the list.
        self.assertEqual(self.request.user.has_perm.call_count, len(required_permissions))
        self.request.user.has_perm.assert_any_call(required_permissions[0])
        self.request.user.has_perm.assert_any_call(required_permissions[1])

        # After the permissions are checked, it will call the view function.
        view.assert_called_once_with(self, self.request)