Exemple #1
0
    def __init__(self, user, list_current_workspace=[], is_administration=False, show_global_workspace=False):
        self.WORKSPACES_OPTIONS = []
        self.WORKSPACES_OPTIONS.append(('', '-----------'))

        # We retrieve all workspaces with write access, or all workspaces if administration
        if is_administration:
            all_workspaces = workspace_api.get_all()
        else:
            all_workspaces = list(workspace_api.get_all_workspaces_with_write_access_by_user(user))
            if show_global_workspace:
                workspace_global = workspace_api.get_global_workspace()
                if workspace_global not in all_workspaces:
                    all_workspaces.append(workspace_global)

        if len(all_workspaces) == 0:
            raise DoesNotExist("You don't have access to any workspaces with sufficient rights to assign a " + get_data_label() + ".")

        # We sort by title, case insensitive
        sort_workspaces = sorted(all_workspaces, key=lambda s: s.title.lower())

        # We add them
        for workspace in sort_workspaces:
            is_workspace_global = workspace_api.is_workspace_global(workspace)
            if (list_current_workspace == [] or\
                    (len(list_current_workspace) > 0 and workspace not in list_current_workspace)) \
                            and ((show_global_workspace and is_workspace_global) or not is_workspace_global):

                self.WORKSPACES_OPTIONS.append((workspace.id,
                                                workspace.title + " (" + ("GLOBAL" if is_workspace_global else user_api.get_user_by_id(workspace.owner).username) + ")"))

        super(ChangeWorkspaceForm, self).__init__()
        self.fields['workspaces'].choices = []
        self.fields['workspaces'].choices = self.WORKSPACES_OPTIONS
Exemple #2
0
    def test_patch_returns_http_404_when_data_not_found(self, mock_get_by_id):
        # Arrange
        mock_user = create_mock_user('1')
        mock_get_by_id.side_effect = DoesNotExist("error")

        # Mock
        response = RequestMock.do_request_patch(
            data_rest_views.DataDetail.as_view(), mock_user, param={'pk': '1'})

        # Assert
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Exemple #3
0
    def test_get_returns_http_404_when_data_not_found(self, mock_get_by_id):
        # Arrange
        mock_user = create_mock_user("1")
        mock_get_by_id.side_effect = DoesNotExist("error")

        # Mock
        response = RequestMock.do_request_get(
            data_rest_views.DataPermissions.as_view(), mock_user, data={"ids": '["1"]'}
        )

        # Assert
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Exemple #4
0
    def test_get_returns_http_404_when_data_not_found(self,
                                                      mock_template_api_get):
        # Arrange
        mock_user = create_mock_user("1")
        mock_template_api_get.side_effect = DoesNotExist("error")

        # Mock
        response = RequestMock.do_request_get(
            template_rest_views.TemplateDownload.as_view(),
            mock_user,
            param={"pk": "1"})

        # Assert
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
    def test_get_returns_http_404_when_data_not_found(self, mock_get_by_id):
        # Arrange
        mock_user = create_mock_user("1")
        mock_get_by_id.side_effect = DoesNotExist("error")

        # Mock
        response = RequestMock.do_request_get(
            views.TemplateVersionManagerDetail.as_view(),
            mock_user,
            param={"pk": "invalid"},
        )

        # Assert
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def get_data_by_pid(pid):
    """Return data object with the given pid.

    Parameters:
        pid:

    Returns: data object
    """
    json_pid_path = "dict_content.%s" % PID_XPATH
    query_result = Data.execute_query({json_pid_path: pid}, order_by_field=[])
    query_result_length = len(query_result)

    if query_result_length == 0:
        raise DoesNotExist("PID is not attached to any data.")
    elif query_result_length != 1:
        raise ApiError("PID must be unique.")
    else:
        return query_result[0]
    def test_url_recognized_template_does_not_exist(
            self, mock_get_dependency_content, mock_get):
        # Arrange
        url_template_download = reverse('core_main_app_rest_template_download',
                                        kwargs={'pk': 'pk'})
        xml_string = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' \
                     '<xs:include schemaLocation="http://dummy.com{0}?id=1234"/>' \
                     '</xs:schema>'.format(url_template_download)
        dependency = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' \
                     '<xs:element name="test"/></xs:schema>'
        mock_get_dependency_content.return_value = dependency
        mock_get.side_effect = DoesNotExist("Error")

        # Act
        flattener = XSDFlattenerDatabaseOrURL(xml_string)
        flat_string = flattener.get_flat()

        # Assert
        self.assertTrue('<xs:element name="test"/>' in flat_string)
    def __init__(self, user, list_current_workspace=[]):
        self.WORKSPACES_OPTIONS = []
        self.WORKSPACES_OPTIONS.append(('', '-----------'))

        # We retrieve all workspaces with write access
        all_workspaces = workspace_api.get_all_workspaces_with_write_access_by_user(user)

        if len(all_workspaces) == 0:
            raise DoesNotExist("You don't have access to any workspaces with sufficient rights to assign a document.")

        # We sort by title, case insensitive
        sort_workspaces = sorted(all_workspaces, key=lambda s: s.title.lower())

        # We add them
        for workspace in sort_workspaces:
            if list_current_workspace == [] or\
                    (len(list_current_workspace) > 0 and workspace not in list_current_workspace):
                self.WORKSPACES_OPTIONS.append((workspace.id, workspace.title))

        super(ChangeWorkspaceForm, self).__init__()
        self.fields['workspaces'].choices = []
        self.fields['workspaces'].choices = self.WORKSPACES_OPTIONS
Exemple #9
0
    def test_url_recognized_template_does_not_exist(
            self, mock_get_dependency_content, mock_get):
        # Arrange
        mock_user = create_mock_user("1", is_superuser=True)
        mock_request = create_mock_request(user=mock_user)
        url_template_download = reverse("core_main_app_rest_template_download",
                                        kwargs={"pk": "pk"})
        xml_string = (
            '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">'
            '<xs:include schemaLocation="http://dummy.com{0}?id=1234"/>'
            "</xs:schema>".format(url_template_download))
        dependency = ('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">'
                      '<xs:element name="test"/></xs:schema>')
        mock_get_dependency_content.return_value = dependency
        mock_get.side_effect = DoesNotExist("Error")

        # Act
        flattener = XSDFlattenerDatabaseOrURL(xml_string, request=mock_request)
        flat_string = flattener.get_flat()

        # Assert
        self.assertTrue('<xs:element name="test"/>' in flat_string)