Exemplo n.º 1
0
def test_pay_for_service_insufficient_balance(publisher_ocean_instance):
    """Tests if balance is lower than the purchased amount."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())

    token = ocn.create_data_token("DataToken1",
                                  "DT1",
                                  from_wallet=alice,
                                  blob="foo_blob")

    with pytest.raises(InsufficientBalance):
        ocn.assets.pay_for_service(
            ocn.web3,
            10000000000000,
            token.address,
            asset.did,
            0,
            ZERO_ADDRESS,
            alice,
            alice.address,
        )
Exemplo n.º 2
0
def test_ddo_address_utilities():
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample_with_credentials.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    ddo = V3Asset(json_filename=sample_ddo_path)

    assert ddo.allowed_addresses == ["0x123", "0x456a"]

    ddo.add_address_to_allow_list("0xAbc12")
    assert ddo.allowed_addresses == ["0x123", "0x456a", "0xabc12"]
    ddo.remove_address_from_allow_list("0xAbc12")
    assert ddo.allowed_addresses == ["0x123", "0x456a"]
    ddo.remove_address_from_allow_list("0x123")
    assert ddo.allowed_addresses == ["0x456a"]
    ddo.remove_address_from_allow_list("0x456a")
    assert ddo.allowed_addresses == []

    assert ddo.denied_addresses == ["0x2222", "0x333"]
    # does not exist
    ddo.remove_address_from_deny_list("0xasfaweg")
    assert ddo.denied_addresses == ["0x2222", "0x333"]
    ddo.add_address_to_deny_list("0xasfaweg")
    assert ddo.denied_addresses == ["0x2222", "0x333", "0xasfaweg"]

    ddo = V3Asset()
    assert ddo.allowed_addresses == []
    ddo.add_address_to_allow_list("0xAbc12")
    assert ddo.allowed_addresses == ["0xabc12"]
    # double adding
    ddo.add_address_to_allow_list("0xAbc12")
    assert ddo.allowed_addresses == ["0xabc12"]
Exemplo n.º 3
0
def test_ddo_dict():
    """Tests DDO creation from dictionary."""
    sample_ddo_path = get_resource_path("ddo", "ddo_sample_algorithm.json")
    assert sample_ddo_path.exists(), f"{sample_ddo_path} does not exist!"

    ddo1 = DDO(json_filename=sample_ddo_path)
    assert len(ddo1.public_keys) == 3
    assert ddo1.did == "did:op:8d1b4d73e7af4634958f071ab8dfe7ab0df14019"
Exemplo n.º 4
0
def test_account_properties_from_file(alice_account):
    key_file = get_resource_path("keys", "key_file_2.json")
    account = Account(key_file=key_file, password="******", address="0x0")
    assert json.loads(
        account.key)["id"] == "0902d04b-f26e-5c1f-e3ae-78d2c1cb16e7"
    assert account.private_key is None
    assert account.key == account._encrypted_key
    assert account.key_file == str(key_file)
Exemplo n.º 5
0
def create_asset(ocean, publisher, encrypt=False):
    """Helper function for asset creation based on ddo_sa_sample.json."""
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(
        sample_ddo_path)

    asset = V3Asset(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    return ocean.assets.create(asset.metadata, publisher, [], encrypt=encrypt)
Exemplo n.º 6
0
def test_config_from_filename():
    """Tests that a Config object can be set from a filename."""
    config_file_name = get_resource_path("config", "test_config.ini")
    config = Config(filename=config_file_name)

    assert config.aquarius_url == "https://custom-aqua.url"
    assert config.artifacts_path is not None
    assert config.metadata_store_url == config.aquarius_url
    assert config.provider_address == "0x00bd138abd70e2f00903268f3db08f2d25677c9e"
    assert isinstance(config.gas_limit, int)
Exemplo n.º 7
0
def test_config_filename_given_file_exists_wellformed_content():
    """Test creating a Config object.
    Setup: filename given, file exists, content is well-formed
    Expect: success
    """
    config_file_name = get_resource_path("config", "test_config.ini")
    config = Config(filename=config_file_name)

    assert config.metadata_cache_uri == "https://custom-aqua.url"
    assert config.provider_address == "0x00bd138abd70e2f00903268f3db08f2d25677c9e"
    assert isinstance(config.gas_limit, int)
Exemplo n.º 8
0
def create_asset(ocean, publisher):
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(
        sample_ddo_path)

    asset = DDO(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    my_secret_store = "http://myownsecretstore.com"
    auth_service = ServiceDescriptor.authorization_service_descriptor(
        my_secret_store)
    return ocean.assets.create(asset.metadata, publisher, [auth_service])
Exemplo n.º 9
0
def test_config_filename_not_given_file_exists_wellformed_content(monkeypatch):
    """Test creating a Config object.
    Setup: filename not given, default file exists, content is well-formed
    Expect: success. Uses config file at ENV_CONFIG_FILE.
    """
    config_file_name = get_resource_path("config", "test_config.ini")
    monkeypatch.setenv(ENV_CONFIG_FILE, str(config_file_name))

    config = Config()

    assert config.provider_address == "0x00bd138abd70e2f00903268f3db08f2d25677c9e"
Exemplo n.º 10
0
def test_create_asset_with_owner_address(publisher_ocean_instance):
    """Tests that an asset can be created with owner address."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())

    assert ocn.assets.create(
        asset.metadata, alice, [], owner_address=alice.address
    ), "Asset creation failed with the specified owner address."
Exemplo n.º 11
0
def test_create_asset_without_dt_address(publisher_ocean_instance):
    """Tests creation of the asset which has not the data token address."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())

    assert ocn.assets.create(
        asset.metadata, alice, [], data_token_address=None
    ), "Asset creation failed with the specified datatoken address."
Exemplo n.º 12
0
def test_ddo_unlisting():
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    ddo = V3Asset(json_filename=sample_ddo_path)
    assert ddo.is_listed

    ddo.unlist()
    assert not ddo.is_listed

    ddo.list()
    assert ddo.is_listed
Exemplo n.º 13
0
def test_ddo_retiring():
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    ddo = V3Asset(json_filename=sample_ddo_path)
    assert not ddo.is_retired

    ddo.retire()
    assert ddo.is_retired
    assert ddo.is_consumable() == ConsumableCodes.ASSET_DISABLED

    ddo.unretire()
    assert not ddo.is_retired
Exemplo n.º 14
0
def test_ddo_credentials_disabled():
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample_disabled.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    ddo = V3Asset(json_filename=sample_ddo_path)
    assert ddo.is_disabled
    assert not ddo.is_enabled

    ddo.enable()
    assert not ddo.is_disabled
    assert ddo.is_enabled

    ddo.disable()
    assert ddo.is_consumable() == ConsumableCodes.ASSET_DISABLED
Exemplo n.º 15
0
def test_create_asset_without_dt_address(publisher_ocean_instance):
    """Tests creation of the asset which has not the data token address."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = DDO(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    my_secret_store = "http://myownsecretstore.com"
    auth_service = ServiceDescriptor.authorization_service_descriptor(
        my_secret_store)

    assert ocn.assets.create(asset.metadata,
                             alice, [auth_service],
                             data_token_address=None)
Exemplo n.º 16
0
def get_ddo_sample(datatoken_address):
    did = f"did:op:{remove_0x_prefix(datatoken_address)}"
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(
        sample_ddo_path)

    asset = DDO(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())

    checksum_dict = dict()
    for service in asset.services:
        checksum_dict[str(service.index)] = checksum(service.main)

    asset.add_proof(checksum_dict, get_publisher_wallet())
    asset._did = did
    return asset
Exemplo n.º 17
0
def test_ddo_credentials_addresses_both():
    """Tests DDO credentials when both deny and allow lists exist on the asset."""
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample_with_credentials.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    ddo = V3Asset(json_filename=sample_ddo_path)
    address_credential = AddressCredential(ddo)
    assert address_credential.get_addresses_of_class("allow") == ["0x123", "0x456a"]
    assert address_credential.get_addresses_of_class("deny") == ["0x2222", "0x333"]
    assert (
        address_credential.validate_access({"type": "address", "value": "0x111"})
        == ConsumableCodes.CREDENTIAL_NOT_IN_ALLOW_LIST
    )
    assert (
        address_credential.validate_access({"type": "address", "value": "0x456A"})
        == ConsumableCodes.OK
    )
Exemplo n.º 18
0
def test_create_asset_with_address(publisher_ocean_instance):
    """Tests that an asset can be created with specific DT address."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())

    token = ocn.create_data_token("DataToken1",
                                  "DT1",
                                  from_wallet=alice,
                                  blob="foo_blob")

    assert ocn.assets.create(
        asset.metadata, alice, [], data_token_address=token.address
    ), "Asset creation failed with the specified datatoken address."
Exemplo n.º 19
0
def test_verify_order_tx(alice_address, bob_address, alice_ocean, alice_wallet):
    """Tests verify_order_tx function."""
    alice_w3 = alice_ocean.web3.eth.blockNumber

    token = alice_ocean.create_data_token(
        "DataToken1", "DT1", from_wallet=alice_wallet, blob="foo_blob"
    )

    token.mint(alice_address, to_base_18(100.0), from_wallet=alice_wallet)
    token.approve(bob_address, to_base_18(1.0), from_wallet=alice_wallet)
    transfer_tx_id = token.transfer(
        bob_address, to_base_18(5.0), from_wallet=alice_wallet
    )

    with pytest.raises(AssertionError):
        # dummy tx id
        token.verify_order_tx(
            alice_w3, "0x0", "some_did", "some_index", "some_amount", alice_address
        )

    transfer_tx_id = token.transfer(
        bob_address, to_base_18(5.0), from_wallet=alice_wallet
    )
    with pytest.raises(AssertionError):
        # tx id is from transfer, not order
        token.verify_order_tx(
            alice_w3,
            transfer_tx_id,
            "some_did",
            "some_index",
            "some_amount",
            alice_address,
        )

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = DDO(json_filename=sample_ddo_path)
    order_tx_id = token.startOrder(
        alice_address, to_base_18(1.0), 1, ZERO_ADDRESS, alice_wallet
    )

    with pytest.raises(AssertionError):
        # the wrong asset did, this is a sample
        token.verify_order_tx(
            alice_w3, order_tx_id, asset.did, "some_index", "some_amount", alice_address
        )
Exemplo n.º 20
0
def test_ddo_dict():
    """Tests DDO creation from dictionary."""
    sample_ddo_path = get_resource_path("ddo", "ddo_sample_algorithm.json")
    assert sample_ddo_path.exists(), f"{sample_ddo_path} does not exist!"

    ddo1 = V3Asset(json_filename=sample_ddo_path)
    assert ddo1.did == "did:op:8d1b4d73e7af4634958f071ab8dfe7ab0df14019"

    ddo1.add_proof({"checksum": "test"}, get_publisher_wallet())

    ddo_dict = ddo1.as_dictionary()
    assert ddo_dict["publicKey"][0]["id"] == ddo1.did
    assert ddo_dict["publicKey"][0]["owner"] == get_publisher_wallet().address
    assert ddo_dict["publicKey"][0]["type"] == "EthereumECDSAKey"

    assert ddo_dict["authentication"][0] == {
        "type": "RsaSignatureAuthentication2018",
        "publicKey": ddo1.did,
    }
Exemplo n.º 21
0
def test_create_asset_with_address(publisher_ocean_instance):
    """Tests that an asset can be created with specific DT address."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = DDO(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    my_secret_store = "http://myownsecretstore.com"
    auth_service = ServiceDescriptor.authorization_service_descriptor(
        my_secret_store)

    token = ocn.create_data_token("DataToken1",
                                  "DT1",
                                  from_wallet=alice,
                                  blob="foo_blob")

    assert ocn.assets.create(asset.metadata,
                             alice, [auth_service],
                             data_token_address=token.address)
Exemplo n.º 22
0
def test_ddo_credentials_addresses_no_access_list():
    """Tests DDO credentials when neither deny, nor allow lists exist on the asset."""
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample_with_credentials.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    # if "allow" OR "deny" exist, we need a credential,
    # so remove both to test the behaviour of no credential supplied
    ddo = V3Asset(json_filename=sample_ddo_path)
    address_credential = AddressCredential(ddo)
    ddo.credentials.pop("allow")
    ddo.credentials.pop("deny")

    assert address_credential.validate_access() == ConsumableCodes.OK

    # test that we can use another credential if address is not required
    assert (
        ddo.is_consumable(
            {"type": "somethingelse", "value": "test"}, with_connectivity_check=False
        )
        == ConsumableCodes.OK
    )
Exemplo n.º 23
0
def test_verify_order_tx(alice_address, bob_address, alice_ocean,
                         alice_wallet):
    """Tests verify_order_tx function."""
    token = alice_ocean.create_data_token("DataToken1",
                                          "DT1",
                                          from_wallet=alice_wallet,
                                          blob="foo_blob")

    token.mint(alice_address, to_wei(100), from_wallet=alice_wallet)
    token.approve(bob_address, to_wei(1), from_wallet=alice_wallet)
    transfer_tx_id = token.transfer(bob_address,
                                    to_wei(5),
                                    from_wallet=alice_wallet)

    with pytest.raises(TimeExhausted):
        with patch("ocean_lib.models.data_token.DataToken.get_tx_receipt"
                   ) as mock:
            # dummy tx id which is not found in the chain
            # catches TimeExhausted exception from web3
            mock.side_effect = TimeExhausted()
            token.verify_order_tx("0x0", "some_did", "some_index",
                                  "some_amount", alice_address)

    transfer_tx_id = token.transfer(bob_address,
                                    to_wei(5),
                                    from_wallet=alice_wallet)
    with pytest.raises(AssertionError):
        # tx id is from transfer, not order
        token.verify_order_tx(transfer_tx_id, "some_did", "some_index",
                              "some_amount", alice_address)

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    order_tx_id = token.startOrder(alice_address, to_wei(1), 1, ZERO_ADDRESS,
                                   alice_wallet)

    with pytest.raises(AssertionError):
        # the wrong asset did, this is a sample
        token.verify_order_tx(order_tx_id, asset.did, "some_index",
                              "some_amount", alice_address)
Exemplo n.º 24
0
def test_InsufficientBalance(publisher_ocean_instance):
    alice = get_publisher_wallet()
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())

    token = publisher_ocean_instance.create_data_token("DataToken1",
                                                       "DT1",
                                                       from_wallet=alice,
                                                       blob="foo_blob")

    with pytest.raises(InsufficientBalance):
        publisher_ocean_instance.assets.pay_for_service(
            publisher_ocean_instance.web3,
            to_wei("12345678999999.9"),
            token.address,
            asset.did,
            0,
            ZERO_ADDRESS,
            alice,
            alice.address,
        )
Exemplo n.º 25
0
def test_ddo_credentials_addresses_only_deny():
    """Tests DDO credentials when only the deny list exists on the asset."""
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample_with_credentials.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)
    # remove allow to test the behaviour of deny
    ddo = V3Asset(json_filename=sample_ddo_path)
    ddo.credentials.pop("allow")

    address_credential = AddressCredential(ddo)
    assert address_credential.get_addresses_of_class("allow") == []
    assert address_credential.get_addresses_of_class("deny") == ["0x2222", "0x333"]
    assert (
        address_credential.validate_access({"type": "address", "value": "0x111"})
        == ConsumableCodes.OK
    )
    assert (
        address_credential.validate_access({"type": "address", "value": "0x333"})
        == ConsumableCodes.CREDENTIAL_IN_DENY_LIST
    )

    credential = {"type": "address", "value": ""}
    with pytest.raises(MalformedCredential):
        address_credential.validate_access(credential)