當前位置: 首頁>>代碼示例>>Python>>正文


Python ReadOnlyRole.user_has_role方法代碼示例

本文整理匯總了Python中onadata.libs.permissions.ReadOnlyRole.user_has_role方法的典型用法代碼示例。如果您正苦於以下問題:Python ReadOnlyRole.user_has_role方法的具體用法?Python ReadOnlyRole.user_has_role怎麽用?Python ReadOnlyRole.user_has_role使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在onadata.libs.permissions.ReadOnlyRole的用法示例。


在下文中一共展示了ReadOnlyRole.user_has_role方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_project_share_inactive_user

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_project_share_inactive_user(self):
        # create project and publish form to project
        self._publish_xls_form_to_project()
        alice_data = {'username': 'alice', 'email': '[email protected]'}
        alice_profile = self._create_user_profile(alice_data)

        # set the user inactive
        self.assertTrue(alice_profile.user.is_active)
        alice_profile.user.is_active = False
        alice_profile.user.save()

        projectid = self.project.pk

        self.assertFalse(ReadOnlyRole.user_has_role(alice_profile.user,
                                                    self.project))

        data = {'username': 'alice', 'role': ReadOnlyRole.name}
        request = self.factory.put('/', data=data, **self.extra)

        view = ProjectViewSet.as_view({
            'put': 'share'
        })
        response = view(request, pk=projectid)

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data, {'username': [u'User is not active']})

        self.assertFalse(ReadOnlyRole.user_has_role(alice_profile.user,
                                                    self.project))
        self.assertFalse(ReadOnlyRole.user_has_role(alice_profile.user,
                                                    self.xform))
開發者ID:MichaelRoethlin,項目名稱:onadata,代碼行數:33,代碼來源:test_project_viewset.py

示例2: test_project_share_readonly

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_project_share_readonly(self):
        # create project and publish form to project
        self._publish_xls_form_to_project()
        alice_data = {'username': 'alice', 'email': '[email protected]'}
        alice_profile = self._create_user_profile(alice_data)
        projectid = self.project.pk

        self.assertFalse(ReadOnlyRole.user_has_role(alice_profile.user,
                                                    self.project))

        data = {'username': 'alice', 'role': ReadOnlyRole.name}
        request = self.factory.put('/', data=data, **self.extra)

        view = ProjectViewSet.as_view({
            'put': 'share'
        })
        response = view(request, pk=projectid)

        self.assertEqual(response.status_code, 204)

        self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
                                                   self.project))
        self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
                                                   self.xform))

        perms = role.get_object_users_with_permissions(self.project)
        for p in perms:
            user = p.get('user')

            if user == alice_profile.user:
                r = p.get('role')
                self.assertEquals(r, ReadOnlyRole.name)
開發者ID:MichaelRoethlin,項目名稱:onadata,代碼行數:34,代碼來源:test_project_viewset.py

示例3: test_read_only_users_get_non_empty_formlist_using_preview_formlist

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_read_only_users_get_non_empty_formlist_using_preview_formlist(
            self, mock_send_mail):
        alice_data = {
            'username': 'alice',
            'email': '[email protected]',
            'password1': 'alice',
            'password2': 'alice'
        }
        alice_profile = self._create_user_profile(alice_data)

        self.assertFalse(
            ReadOnlyRole.user_has_role(alice_profile.user, self.project))

        # share bob's project with alice
        data = {
            'username': 'alice',
            'role': ReadOnlyRole.name,
            'email_msg': 'I have shared the project with you'
        }
        request = self.factory.post('/', data=data, **self.extra)
        share_view = ProjectViewSet.as_view({'post': 'share'})
        projectid = self.project.pk
        response = share_view(request, pk=projectid)
        self.assertEqual(response.status_code, 204)
        self.assertTrue(mock_send_mail.called)
        self.assertTrue(
            ReadOnlyRole.user_has_role(alice_profile.user, self.project))

        # check that she can authenticate successfully
        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)
        auth = DigestAuth('alice', 'alice')
        request.META.update(auth(request.META, response))
        response = self.view(request, username='bob')
        self.assertEqual(response.status_code, 200)
        # check that alice gets an empty response when requesting bob's
        # formlist
        self.assertEqual(response.data, [])

        # set endpoint to preview formList
        self.view = PreviewXFormListViewSet.as_view({"get": "list"})

        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)
        self.assertNotEqual(response.data, [])
        auth = DigestAuth('alice', 'alice')
        request.META.update(auth(request.META, response))
        response = self.view(request, username='bob')
        self.assertEqual(response.status_code, 200)
        # check that alice does NOT get an empty response when requesting bob's
        # formlist when using the preview formlist endpoint
        self.assertNotEqual(response.data, [])
開發者ID:onaio,項目名稱:onadata,代碼行數:56,代碼來源:test_xform_list_viewset.py

示例4: test_project_users_get_readonly_role_on_add_form

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
 def test_project_users_get_readonly_role_on_add_form(self):
     self._project_create()
     alice_data = {'username': 'alice', 'email': '[email protected]'}
     alice_profile = self._create_user_profile(alice_data)
     ReadOnlyRole.add(alice_profile.user, self.project)
     self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
                                                self.project))
     self._publish_xls_form_to_project()
     self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
                                                self.xform))
     self.assertFalse(OwnerRole.user_has_role(alice_profile.user,
                                              self.xform))
開發者ID:CharaD7,項目名稱:kobocat,代碼行數:14,代碼來源:test_project_viewset.py

示例5: test_get_xform_list_other_user_with_readonly_role

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_get_xform_list_other_user_with_readonly_role(self):
        request = self.factory.get('/')
        response = self.view(request)
        alice_data = {'username': 'alice', 'email': '[email protected]'}
        alice_profile = self._create_user_profile(alice_data)

        ReadOnlyRole.add(alice_profile.user, self.xform)
        self.assertTrue(
            ReadOnlyRole.user_has_role(alice_profile.user, self.xform)
        )

        auth = DigestAuth('alice', 'bobbob')
        request.META.update(auth(request.META, response))
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
        content = response.render().content
        self.assertNotIn(self.xform.id_string, content)
        self.assertEqual(
            content, '<?xml version="1.0" encoding="utf-8"?>\n<xforms '
            'xmlns="http://openrosa.org/xforms/xformsList"></xforms>')
        self.assertTrue(response.has_header('X-OpenRosa-Version'))
        self.assertTrue(
            response.has_header('X-OpenRosa-Accept-Content-Length'))
        self.assertTrue(response.has_header('Date'))
        self.assertEqual(response['Content-Type'], 'text/xml; charset=utf-8')
開發者ID:ghelobytes,項目名稱:onadata,代碼行數:27,代碼來源:test_xform_list_api.py

示例6: test_project_share_endpoint

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_project_share_endpoint(self, mock_send_mail):
        # create project and publish form to project
        self._publish_xls_form_to_project()
        alice_data = {'username': 'alice', 'email': '[email protected]'}
        alice_profile = self._create_user_profile(alice_data)
        projectid = self.project.pk

        ROLES = [ReadOnlyRole,
                 DataEntryRole,
                 EditorRole,
                 ManagerRole,
                 OwnerRole]
        for role_class in ROLES:
            self.assertFalse(role_class.user_has_role(alice_profile.user,
                                                      self.project))

            data = {'username': 'alice', 'role': role_class.name,
                    'email_msg': 'I have shared the project with you'}
            request = self.factory.post('/', data=data, **self.extra)

            view = ProjectViewSet.as_view({
                'post': 'share'
            })
            response = view(request, pk=projectid)

            self.assertEqual(response.status_code, 204)
            self.assertTrue(mock_send_mail.called)

            self.assertTrue(role_class.user_has_role(alice_profile.user,
                                                     self.project))
            self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
                                                       self.xform))
            # Reset the mock called value to False
            mock_send_mail.called = False

            data = {'username': 'alice', 'role': ''}
            request = self.factory.post('/', data=data, **self.extra)
            response = view(request, pk=projectid)

            self.assertEqual(response.status_code, 400)
            self.assertEqual(response.get('Last-Modified'), None)
            self.assertFalse(mock_send_mail.called)

            role_class._remove_obj_permissions(alice_profile.user,
                                               self.project)
開發者ID:199212151746,項目名稱:onadata,代碼行數:47,代碼來源:test_project_viewset.py

示例7: test_reassign_role

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_reassign_role(self):
        self._publish_transportation_form()
        alice = self._create_user('alice', 'alice')

        self.assertFalse(ManagerRole.user_has_role(alice, self.xform))

        ManagerRole.add(alice, self.xform)

        self.assertTrue(ManagerRole.user_has_role(alice, self.xform))
        self.assertTrue(ManagerRole.has_role(
            perms_for(alice, self.xform), self.xform))

        ReadOnlyRole.add(alice, self.xform)

        self.assertFalse(ManagerRole.user_has_role(alice, self.xform))
        self.assertTrue(ReadOnlyRole.user_has_role(alice, self.xform))
        self.assertFalse(ManagerRole.has_role(
            perms_for(alice, self.xform), self.xform))
        self.assertTrue(ReadOnlyRole.has_role(
            perms_for(alice, self.xform), self.xform))
開發者ID:199212151746,項目名稱:onadata,代碼行數:22,代碼來源:test_permissions.py

示例8: test_get_xform_list_with_shared_forms

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_get_xform_list_with_shared_forms(self, mock_send_mail):
        # create user alice
        alice_data = {
            'username': 'alice',
            'email': '[email protected]',
            'password1': 'alice',
            'password2': 'alice'
        }
        alice_profile = self._create_user_profile(alice_data)

        # check that she can authenticate successfully
        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)
        auth = DigestAuth('alice', 'alice')
        request.META.update(auth(request.META, response))
        response = self.view(request)
        self.assertEqual(response.status_code, 200)

        self.assertFalse(
            ReadOnlyRole.user_has_role(alice_profile.user, self.project))
        # share bob's project with her
        data = {
            'username': 'alice',
            'role': ReadOnlyRole.name,
            'email_msg': 'I have shared the project with you'
        }
        request = self.factory.post('/', data=data, **self.extra)
        share_view = ProjectViewSet.as_view({'post': 'share'})
        projectid = self.project.pk
        response = share_view(request, pk=projectid)
        self.assertEqual(response.status_code, 204)
        self.assertTrue(mock_send_mail.called)
        self.assertTrue(
            ReadOnlyRole.user_has_role(alice_profile.user, self.project))

        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)
        auth = DigestAuth('alice', 'alice')
        request.META.update(auth(request.META, response))
        response = self.view(request, username='alice')
        self.assertEqual(response.status_code, 200)

        path = os.path.join(
            os.path.dirname(__file__), '..', 'fixtures', 'formList.xml')

        with open(path, encoding='utf-8') as f:
            form_list_xml = f.read().strip()
            data = {"hash": self.xform.hash, "pk": self.xform.pk}
            content = response.render().content.decode('utf-8')
            self.assertEqual(content, form_list_xml % data)
            download_url = ('<downloadUrl>http://testserver/%s/'
                            'forms/%s/form.xml</downloadUrl>') % (
                                self.user.username, self.xform.id)
            # check that bob's form exists in alice's formList
            self.assertTrue(download_url in content)
            self.assertTrue(response.has_header('X-OpenRosa-Version'))
            self.assertTrue(
                response.has_header('X-OpenRosa-Accept-Content-Length'))
            self.assertTrue(response.has_header('Date'))
            self.assertEqual(response['Content-Type'],
                             'text/xml; charset=utf-8')
開發者ID:onaio,項目名稱:onadata,代碼行數:65,代碼來源:test_xform_list_viewset.py

示例9: test_form_id_filter_for_require_auth_account

# 需要導入模塊: from onadata.libs.permissions import ReadOnlyRole [as 別名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import user_has_role [as 別名]
    def test_form_id_filter_for_require_auth_account(self):
        """
        Test formList formID filter for account that requires authentication
        """
        # Bob submit forms
        xls_path = os.path.join(settings.PROJECT_ROOT, "apps", "main", "tests",
                                "fixtures", "tutorial.xls")
        self._publish_xls_form_to_project(xlsform_path=xls_path)

        xls_file_path = os.path.join(settings.PROJECT_ROOT, "apps", "logger",
                                     "fixtures",
                                     "external_choice_form_v1.xlsx")
        self._publish_xls_form_to_project(xlsform_path=xls_file_path)

        # Set require auth to true
        self.user.profile.require_auth = True
        self.user.profile.save()
        request = self.factory.get('/', {'formID': self.xform.id_string})
        response = self.view(request, username=self.user.username)
        self.assertEqual(response.status_code, 401)

        # Test for authenticated user but unrecognized formID
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/', {'formID': 'unrecognizedID'})
        request.META.update(auth(request.META, response))
        response = self.view(request, username=self.user.username)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])

        # Test for authenticated user and valid formID
        request = self.factory.get('/', {'formID': self.xform.id_string})
        self.assertTrue(self.user.profile.require_auth)
        response = self.view(request, username=self.user.username)
        self.assertEqual(response.status_code, 401)
        auth = DigestAuth('bob', 'bobbob')
        request.META.update(auth(request.META, response))
        response = self.view(request, username=self.user.username)
        self.assertEqual(response.status_code, 200)

        path = os.path.join(
            os.path.dirname(__file__), '..', 'fixtures', 'formList2.xml')

        with open(path, encoding='utf-8') as f:
            form_list = f.read().strip()
            data = {"hash": self.xform.hash, "pk": self.xform.pk,
                    'version': self.xform.version}
            content = response.render().content.decode('utf-8')
            self.assertEqual(content, form_list % data)

        # Test for shared forms
        # Create user Alice
        alice_data = {
            'username': 'alice',
            'email': '[email protected]',
            'password1': 'alice',
            'password2': 'alice'
        }
        alice_profile = self._create_user_profile(alice_data)

        # check that she can authenticate successfully
        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)
        auth = DigestAuth('alice', 'alice')
        request.META.update(auth(request.META, response))
        response = self.view(request)
        self.assertEqual(response.status_code, 200)

        self.assertFalse(
            ReadOnlyRole.user_has_role(alice_profile.user, self.project))

        # share Bob's project with Alice
        data = {
            'username': 'alice',
            'role': ReadOnlyRole.name
        }
        request = self.factory.post('/', data=data, **self.extra)
        share_view = ProjectViewSet.as_view({'post': 'share'})
        project_id = self.project.pk
        response = share_view(request, pk=project_id)
        self.assertEqual(response.status_code, 204)
        self.assertTrue(
            ReadOnlyRole.user_has_role(alice_profile.user, self.project))

        request = self.factory.get('/', {'formID': self.xform.id_string})
        response = self.view(request)
        self.assertEqual(response.status_code, 401)
        auth = DigestAuth('alice', 'alice')
        request.META.update(auth(request.META, response))
        response = self.view(request, username='alice')
        self.assertEqual(response.status_code, 200)

        path = os.path.join(
            os.path.dirname(__file__), '..', 'fixtures', 'formList2.xml')

        with open(path, encoding='utf-8') as f:
            form_list = f.read().strip()
            data = {"hash": self.xform.hash, "pk": self.xform.pk,
                    "version": self.xform.version}
            content = response.render().content.decode('utf-8')
#.........這裏部分代碼省略.........
開發者ID:onaio,項目名稱:onadata,代碼行數:103,代碼來源:test_xform_list_viewset.py


注:本文中的onadata.libs.permissions.ReadOnlyRole.user_has_role方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。