Esempio n. 1
0
    def test_stream_file_by_sha256_raises_py42_error_when_find_file_versions_returns_204_status_code(
        self,
        mocker,
        security_client,
        storage_client_factory,
        microservice_client_factory,
        file_event_search,
        file_location,
        find_file_version,
    ):
        security_module = SecurityModule(security_client,
                                         storage_client_factory,
                                         microservice_client_factory)
        file_event_client = mocker.MagicMock(spec=FileEventClient)
        file_event_client.search.return_value = file_event_search
        file_event_client.get_file_location_detail_by_sha256.return_value = (
            file_location)
        microservice_client_factory.get_file_event_client.return_value = (
            file_event_client)

        pds_client = mocker.MagicMock(spec=PreservationDataServiceClient)
        find_file_version.status_code = 204
        pds_client.find_file_versions.return_value = find_file_version
        microservice_client_factory.get_preservation_data_service_client.return_value = (
            pds_client)

        with pytest.raises(Py42Error) as e:
            security_module.stream_file_by_sha256("shahash")

        assert e.value.args[0] == PDS_EXCEPTION_MESSAGE.format("shahash")
Esempio n. 2
0
    def test_stream_file_by_sha256_raises_py42_checksum_not_found_error_when_search_returns_empty_response(
        self,
        mocker,
        security_client,
        storage_client_factory,
        microservice_client_factory,
        file_event_search,
    ):
        security_module = SecurityModule(security_client,
                                         storage_client_factory,
                                         microservice_client_factory)
        file_event_search.text = "{}"
        file_event_client = mocker.MagicMock(spec=FileEventClient)
        file_event_client.search.return_value = file_event_search
        with pytest.raises(Py42ChecksumNotFoundError) as e:
            security_module.stream_file_by_sha256("shahash")

        assert u"No files found with SHA256 checksum" in e.value.args[0]
Esempio n. 3
0
    def test_stream_file_by_sha256_raises_file_not_found_error_when_file_location_returns_empty_response(
        self,
        mocker,
        security_client,
        storage_client_factory,
        microservice_client_factory,
        file_event_search,
        file_location,
    ):
        security_module = SecurityModule(security_client,
                                         storage_client_factory,
                                         microservice_client_factory)
        file_event_client = mocker.MagicMock(spec=FileEventClient)
        file_event_client.search.return_value = file_event_search
        file_location.text = """{"locations": []}"""
        file_event_client.get_file_location_detail_by_sha256.return_value = (
            file_location)
        microservice_client_factory.get_file_event_client.return_value = (
            file_event_client)
        with pytest.raises(Py42Error) as e:
            security_module.stream_file_by_sha256("shahash")

        assert u"PDS service can't find requested file" in e.value.args[0]
Esempio n. 4
0
    def test_stream_file_by_sha256_raises_py42_error_when_file_download_returns_failure_response(
        self,
        mocker,
        security_client,
        storage_client_factory,
        microservice_client_factory,
        file_event_search,
        file_location,
        find_file_version,
        file_download,
    ):
        security_module = SecurityModule(security_client,
                                         storage_client_factory,
                                         microservice_client_factory)
        file_event_client = mocker.MagicMock(spec=FileEventClient)
        file_event_client.search.return_value = file_event_search
        file_event_client.get_file_location_detail_by_sha256.return_value = (
            file_location)
        microservice_client_factory.get_file_event_client.return_value = (
            file_event_client)

        pds_client = mocker.MagicMock(spec=PreservationDataServiceClient)
        pds_client.find_file_versions.return_value = find_file_version
        microservice_client_factory.get_preservation_data_service_client.return_value = (
            pds_client)

        storage_node_client = mocker.MagicMock(
            spec=StoragePreservationDataClient)
        storage_node_client.get_download_token.return_value = file_download
        storage_node_client.get_file.side_effect = Py42HTTPError(HTTPError())
        microservice_client_factory.create_storage_preservation_client.return_value = (
            storage_node_client)

        with pytest.raises(Py42Error) as e:
            security_module.stream_file_by_sha256("shahash")

        assert e.value.args[0] == PDS_EXCEPTION_MESSAGE.format("shahash")
Esempio n. 5
0
    def test_stream_file_by_sha256_returns_stream_of_file(
        self,
        mocker,
        security_client,
        storage_client_factory,
        microservice_client_factory,
        file_event_search,
        file_location,
        find_file_version,
        file_download,
    ):

        security_module = SecurityModule(security_client,
                                         storage_client_factory,
                                         microservice_client_factory)
        file_event_client = mocker.MagicMock(spec=FileEventClient)
        file_event_client.search.return_value = file_event_search
        file_event_client.get_file_location_detail_by_sha256.return_value = (
            file_location)
        microservice_client_factory.get_file_event_client.return_value = (
            file_event_client)

        pds_client = mocker.MagicMock(spec=PreservationDataServiceClient)
        pds_client.find_file_versions.return_value = find_file_version
        microservice_client_factory.get_preservation_data_service_client.return_value = (
            pds_client)

        storage_node_client = mocker.MagicMock(
            spec=StoragePreservationDataClient)
        storage_node_client.get_download_token.return_value = file_download
        storage_node_client.get_file.return_value = b"stream"
        microservice_client_factory.create_storage_preservation_client.return_value = (
            storage_node_client)

        response = security_module.stream_file_by_sha256("shahash")
        assert response == b"stream"