Beispiel #1
0
def test_build_endpoint():
    """Tests that service endpoints are correctly built from URL and service name."""
    def get_service_endpoints(_provider_uri=None):
        _endpoints = TEST_SERVICE_ENDPOINTS.copy()
        _endpoints.update(
            {"newEndpoint": ["GET", "/api/v1/services/newthing"]})
        return _endpoints

    original_func = DataSP.get_service_endpoints
    DataSP.get_service_endpoints = get_service_endpoints
    config = ConfigProvider.get_config()

    endpoints = get_service_endpoints()
    uri = "http://ppp.com"
    method, endpnt = DataSP.build_endpoint("newEndpoint", provider_uri=uri)
    assert endpnt == urljoin(uri, endpoints["newEndpoint"][1])
    # config has no effect when provider_uri is set
    assert (endpnt == DataSP.build_endpoint("newEndpoint",
                                            provider_uri=uri,
                                            config=config)[1])

    method, endpnt = DataSP.build_endpoint("newEndpoint", config=config)
    assert endpnt == urljoin(DataSP.get_root_uri(config.provider_url),
                             endpoints["newEndpoint"][1])
    assert (endpnt == DataSP.build_endpoint(
        "newEndpoint", provider_uri=config.provider_url)[1])

    uri = "http://ppp.com:8030/api/v1/services/newthing"
    method, endpnt = DataSP.build_endpoint("download", provider_uri=uri)
    assert method == endpoints["download"][0]
    assert endpnt == urljoin(DataSP.get_root_uri(uri),
                             endpoints["download"][1])

    DataSP.get_service_endpoints = original_func
Beispiel #2
0
def test_build_specific_endpoints():
    """Tests that a specific list of agreed endpoints is supported on the DataServiceProvider."""
    config = ConfigProvider.get_config()
    endpoints = TEST_SERVICE_ENDPOINTS

    def get_service_endpoints(_provider_uri=None):
        return TEST_SERVICE_ENDPOINTS.copy()

    original_func = DataSP.get_service_endpoints
    DataSP.get_service_endpoints = get_service_endpoints

    base_uri = DataSP.get_root_uri(config.provider_url)
    assert DataSP.build_download_endpoint()[1] == urljoin(
        base_uri, endpoints["download"][1])
    assert DataSP.build_initialize_endpoint()[1] == urljoin(
        base_uri, endpoints["initialize"][1])
    assert DataSP.build_encrypt_endpoint()[1] == urljoin(
        base_uri, endpoints["encrypt"][1])
    assert DataSP.build_fileinfo()[1] == urljoin(base_uri,
                                                 endpoints["fileinfo"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeStatus"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeStart"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeStop"][1])
    assert DataSP.build_compute_endpoint()[1] == urljoin(
        base_uri, endpoints["computeDelete"][1])

    DataSP.get_service_endpoints = original_func
    def download_service(
        did,
        service_endpoint,
        wallet,
        files,
        destination_folder,
        service_id,
        token_address,
        order_tx_id,
        index=None,
    ):

        indexes = range(len(files))
        if index is not None:
            assert isinstance(index,
                              int), logger.error("index has to be an integer.")
            assert index >= 0, logger.error(
                "index has to be 0 or a positive integer.")
            assert index < len(files), logger.error(
                "index can not be bigger than the number of files")
            indexes = [index]

        base_url = (f"{service_endpoint}"
                    f"?documentId={did}"
                    f"&serviceId={service_id}"
                    f"&serviceType={ServiceTypes.ASSET_ACCESS}"
                    f"&dataToken={token_address}"
                    f"&transferTxId={order_tx_id}"
                    f"&consumerAddress={wallet.address}")
        provider_uri = DataServiceProvider.get_root_uri(service_endpoint)
        for i in indexes:
            signature = DataServiceProvider.sign_message(
                wallet, did, provider_uri=provider_uri)
            download_url = base_url + f"&signature={signature}&fileIndex={i}"
            logger.info(
                f"invoke consume endpoint with this url: {download_url}")
            response = DataServiceProvider.get_http_client().get(download_url,
                                                                 stream=True)
            file_name = DataProviderMock._get_file_name(response)
            DataServiceProvider.write_file(response, destination_folder,
                                           file_name or f"file-{i}")

        return True
Beispiel #4
0
    def is_consumable(
        self,
        credential: Optional[dict] = None,
        with_connectivity_check: bool = True,
        provider_uri: Optional[str] = None,
    ) -> bool:
        """Checks whether an asset is consumable and returns a ConsumableCode."""
        if self.is_disabled or self.is_retired:
            return ConsumableCodes.ASSET_DISABLED

        if (with_connectivity_check and provider_uri
                and not DataServiceProvider.check_asset_file_info(
                    self.did, DataServiceProvider.get_root_uri(provider_uri))):
            return ConsumableCodes.CONNECTIVITY_FAIL

        # to be parameterized in the future, can implement other credential classes
        manager = AddressCredential(self)

        if manager.requires_credential():
            return manager.validate_access(credential)

        return ConsumableCodes.OK
def test_build_endpoint():
    """Tests that service endpoints are correctly built from URL and service name."""
    def get_service_endpoints(_provider_uri=None):
        _endpoints = TEST_SERVICE_ENDPOINTS.copy()
        _endpoints.update(
            {"newEndpoint": ["GET", "/api/v1/services/newthing"]})
        return _endpoints

    original_func = DataSP.get_service_endpoints
    DataSP.get_service_endpoints = get_service_endpoints

    endpoints = get_service_endpoints()
    uri = "http://localhost:8030"
    method, endpnt = DataSP.build_endpoint("newEndpoint", provider_uri=uri)
    assert endpnt == urljoin(uri, endpoints["newEndpoint"][1])

    uri = "http://localhost:8030/api/v1/services/newthing"
    method, endpnt = DataSP.build_endpoint("download", provider_uri=uri)
    assert method == endpoints["download"][0]
    assert endpnt == urljoin(DataSP.get_root_uri(uri),
                             endpoints["download"][1])

    DataSP.get_service_endpoints = original_func
Beispiel #6
0
def test_get_root_uri():
    """Tests extraction of base URLs from various inputs."""
    uri = "http://ppp.com"
    assert DataSP.get_root_uri(uri) == uri
    assert DataSP.get_root_uri("http://ppp.com:8000") == "http://ppp.com:8000"
    assert (DataSP.get_root_uri("http://ppp.com:8000/api/v1/services/") ==
            "http://ppp.com:8000")
    assert DataSP.get_root_uri(
        "http://ppp.com:8000/api/v1") == "http://ppp.com:8000"
    assert (DataSP.get_root_uri("http://ppp.com:8000/services") ==
            "http://ppp.com:8000/services")
    assert (DataSP.get_root_uri("http://ppp.com:8000/services/download") ==
            "http://ppp.com:8000")
    assert (DataSP.get_root_uri("http://ppp.com:8000/api/v2/services") ==
            "http://ppp.com:8000/api/v2/services")
    assert (DataSP.get_root_uri("http://ppp.com:8000/api/v2/services/") ==
            "http://ppp.com:8000/api/v2")

    with pytest.raises(InvalidURL):
        DataSP.get_root_uri("thisIsNotAnURL")

    with pytest.raises(InvalidURL):
        DataSP.get_root_uri("//")
def test_get_root_uri():
    """Tests extraction of base URLs from various inputs."""
    uri = "https://provider.mainnet.oceanprotocol.com"
    assert DataSP.is_valid_provider(uri)
    assert DataSP.get_root_uri(uri) == uri
    assert DataSP.get_root_uri(
        "http://localhost:8030") == "http://localhost:8030"
    assert (DataSP.get_root_uri("http://localhost:8030/api/v1/services/") ==
            "http://localhost:8030")
    assert (DataSP.get_root_uri("http://localhost:8030/api/v1") ==
            "http://localhost:8030")
    assert (DataSP.get_root_uri("http://localhost:8030/services") ==
            "http://localhost:8030/services")
    assert (DataSP.get_root_uri("http://localhost:8030/services/download") ==
            "http://localhost:8030")
    assert (DataSP.get_root_uri("http://localhost:8030/api/v2/services") ==
            "http://localhost:8030/api/v2/services")
    assert (DataSP.get_root_uri("http://localhost:8030/api/v2/services/") ==
            "http://localhost:8030/api/v2")

    assert not DataSP.is_valid_provider("thisIsNotAnURL")
    with pytest.raises(InvalidURL):
        DataSP.get_root_uri("thisIsNotAnURL")

    with pytest.raises(InvalidURL):
        # URL is of correct format but unreachable
        DataSP.get_root_uri("http://thisisaurl.but/itshouldnt")

    with pytest.raises(InvalidURL):
        # valid URL, but no provider address
        DataSP.get_root_uri("http://oceanprotocol.com")

    with pytest.raises(InvalidURL):
        DataSP.get_root_uri("//")