def test_get_all_accessible_by_hash_as_staff_returns_global_template(self):
     mock_request = create_mock_request(user=self.staff_user1)
     templates = template_api.get_all_accessible_by_hash(
         self.fixture.global_template.hash, request=mock_request)
     self.assertTrue(self.fixture.user1_template not in list(templates))
     self.assertTrue(self.fixture.user2_template not in list(templates))
     self.assertTrue(self.fixture.global_template in list(templates))
Пример #2
0
 def test_template_get_all_by_hash_contains_only_template(
         self, mock_get_all_by_hash):
     mock_user = create_mock_user("1", is_superuser=True)
     mock_request = create_mock_request(user=mock_user)
     _generic_get_all_test(
         self,
         mock_get_all_by_hash,
         template_api.get_all_accessible_by_hash("fake_hash",
                                                 request=mock_request),
     )
Пример #3
0
def init_schema_info(oai_harvester_metadata_format, request=None):
    """Init schema information for an OaiHarvesterMetadataFormat.

    Args:
        oai_harvester_metadata_format: The OaiHarvesterMetadataFormat to init.
        request:

    Returns:
        Init OaiHarvesterMetadataFormat.

    """
    # TODO: refactor send request with cookies (same code in other apps)
    try:
        session_id = request.session.session_key
    except:
        session_id = None
    http_response = send_get_request(
        oai_harvester_metadata_format.schema, cookies={"sessionid": session_id}
    )
    if http_response.status_code == status.HTTP_200_OK:
        string_xml = http_response.text
        oai_harvester_metadata_format.xml_schema = string_xml
        try:
            oai_harvester_metadata_format.hash = get_hash(string_xml)
        except exceptions.XSDError:
            raise exceptions.ApiError(
                "Impossible to hash the schema for the following "
                "metadata format: {0}."
            )
        list_template = api_template.get_all_accessible_by_hash(
            oai_harvester_metadata_format.hash, request=request
        )
        # FIXME: What to do if several templates with the same hash.
        if len(list_template) == 1:
            oai_harvester_metadata_format.template = list_template[0]
        elif len(list_template) > 1:
            raise exceptions.ApiError(
                "Several templates have the same hash. "
                "Impossible to determine a template for the following "
                "metadata format: {0}.".format(
                    oai_harvester_metadata_format.metadata_prefix
                )
            )
    else:
        raise exceptions.ApiError(
            "Impossible to init schema information for the following "
            "metadata format: {0}.".format(
                oai_harvester_metadata_format.metadata_prefix
            )
        )

    return oai_harvester_metadata_format
Пример #4
0
def _update_query_builder(query_builder, templates, request=None):
    """Update the query criteria with a list of templates.

    Args:
        query_builder:
        templates:

    Returns:

    """
    if len(templates) > 0:
        template_id_list = []
        for template in templates:
            template_id_list.extend(
                template_api.get_all_accessible_by_hash(
                    template["hash"], request=request).values_list("id"))

        # Even if the list is empty, we add it to the query
        # empty list means there is no equal template with the hash given
        query_builder.add_list_criteria("template", template_id_list)
 def test_get_all_accessible_by_hash_as_user_does_not_return_other_user_template(
     self, ):
     mock_request = create_mock_request(user=self.user1)
     templates = template_api.get_all_accessible_by_hash(
         self.fixture.user2_template.hash, request=mock_request)
     self.assertTrue(templates.count() == 0)
 def test_get_all_accessible_by_hash_as_anonymous_does_not_return_global(
         self):
     mock_request = create_mock_request(user=self.anonymous_user)
     templates = template_api.get_all_accessible_by_hash(
         self.fixture.global_template.hash, request=mock_request)
     self.assertTrue(templates.count() == 0)