def test_create_auth_from_json(): """Tests create_authentication_from_json function in DDOs.""" auth = {"publicKey": "0x00000", "type": "auth-type", "nothing": ""} assert DDO.create_authentication_from_json(auth) == { "publicKey": "0x00000", "type": "auth-type", } with pytest.raises(ValueError): DDO.create_authentication_from_json({"type": "auth-type"})
def create_asset(ocean, publisher): """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 = 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])
def test_create_public_key_from_json(): """Tests create_public_key_from_json function in DDOs.""" pkey = {"id": "pkeyid", "type": "keytype", "owner": "0x00009"} pub_key_inst = DDO.create_public_key_from_json(pkey) assert isinstance(pub_key_inst, PublicKeyBase) assert pub_key_inst.get_id() == pkey["id"] assert pub_key_inst.get_type() == PUBLIC_KEY_TYPE_ETHEREUM_ECDSA assert pub_key_inst.get_owner() == pkey["owner"] pub_key_inst = DDO.create_public_key_from_json({"type": PUBLIC_KEY_TYPE_RSA}) assert pub_key_inst.get_id() == "" assert pub_key_inst.get_type() == PUBLIC_KEY_TYPE_RSA assert pub_key_inst.get_owner() is None
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)
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"
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 = DDO(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(10000000000000.0, token.address, asset.did, 0, ZERO_ADDRESS, alice)
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)
def get_asset_ddo(self, did): """ Retrieve asset ddo for a given did. :param did: Asset DID string :return: DDO instance """ response = self.requests_session.get(f"{self.url}/{did}").content parsed_response = _parse_response(response, None) if not parsed_response: return {} return DDO(dictionary=parsed_response)
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)
def get_ddo_sample(datatoken_address): """Helper function to get a sample ddo for testing.""" 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
def test_creating_ddo_from_scratch(): """Tests creating a DDO from scratch.""" # create an empty ddo ddo = DDO() assert ddo.did is None assert ddo.asset_id is None assert ddo.created is not None did = DID.did({"0": "0x99999999999999999"}) ddo.assign_did(did) assert ddo.did == did ddo.add_service(TEST_SERVICE_TYPE, TEST_SERVICE_URL) pub_acc = get_publisher_wallet() # add a proof to the first public_key/authentication ddo.add_proof("checksum", pub_acc) ddo_text_proof = ddo.as_text() assert ddo_text_proof assert not ddo.public_keys ddo.add_public_key(did, pub_acc.address) assert len(ddo.public_keys) == 1 assert ddo.get_public_key(0) == ddo.public_keys[0] with pytest.raises(IndexError): ddo.get_public_key(1) assert ddo.get_public_key(did) == ddo.public_keys[0] assert ddo.get_public_key("0x32233") is None assert not ddo.authentications ddo.add_authentication(did, "") assert len(ddo.authentications) == 1