def test_ddo_import_from_text(): test_url = 'http://localhost' ddo = DDO.create(test_url, service_list=['meta', 'trust']) assert(ddo) ddo_text = json.dumps(json.loads(ddo.as_text), sort_keys=True, indent=4) import_ddo = DDO.import_from_text(ddo_text) assert(import_ddo) assert(import_ddo.as_text == ddo.as_text)
def test_ddo_get_did_from_ddo(): did = did_generate_random() test_url = 'http://localhost' ddo = DDO.create(test_url, service_list=['meta', 'trust'], did=did) assert(ddo) assert(ddo.did == did) assert(DDO.get_did_from_ddo(ddo) == did) assert(DDO.get_did_from_ddo(ddo.as_text) == did)
def execute(self, args: Any, output: Any) -> Any: network = self.get_network( args.url, contract_names={'DIDContract': args.contract_name}) key_pair = KeyPair.import_from_file(args.keyfile, args.password) if not key_pair: output.add_error( 'unable to load keyfile using the provided password') return register_account = ConvexAccount(key_pair, args.address) service_list = None if args.service_list: name_list = re.split(r'[^a-z]+', args.service_list, re.IGNORECASE) service_list = [] for name in name_list: service_name = name.strip() if service_name not in SUPPORTED_SERVICES: output.add_error(f'{service_name} is not a valid service') return service_list.append(service_name) ddo = DDO.create(args.agent_url, service_list=service_list) if network.register_did(register_account, ddo.did, ddo.as_text): output.add_line( f'{args.agent_url} has been registered with the did {ddo.did}') output.add_line(ddo.as_text) output.set_value('did', ddo.did) output.set_value('ddo_text', ddo.as_text) else: output.add_error(f'unable to register {args.agent_url}')
def remote_agent_invokable(config): ddo_options = None local_agent = config['agents']['invokable'] ddo = DDO.create(local_agent['url']) authentication = AuthenticationBasic(local_agent['username'], local_agent['password']) return RemoteAgent(ddo, authentication=authentication)
def __init__(self, name, ddo_text=None, did=None, url=None, authentication=None, http_client=None): """ Create an agent access record. This record tries to obtain all the nesseray information needed to resolve an agent. :param str name: Name of the agent, this can be used to get the agent information. :param str ddo_text: DDO text of the agent if available, if not this class will request for it later. :param str did: DID of the agent, similar to ddo_text, the did wil requested later if not available. :param str url: URL of the agent. :param dict authentication: authentication object used for agent access """ ddo = None if ddo_text: ddo = DDO.import_from_text(ddo_text) if did is None: did = ddo.did self._name = name self._url = url self._ddo = ddo self._did = did self._authentication = authentication self._agent_cache = {} self._http_client = http_client
def test_ddo_create_from_service_list_version(): test_url = 'http://localhost' ddo = DDO.create(test_url, service_list=['meta', 'trust'], version='v99') assert(ddo) #ddo_text = json.dumps(json.loads(ddo.as_text), sort_keys=True, indent=4) #print(ddo_text) assert('meta' in ddo.service) assert(ddo.service['meta']['serviceEndpoint'] == 'http://localhost/api/v99/meta')
def test_remote_agent_set_http_client(): ddo = DDO.create('http://localhost:3030') agent = RemoteAgent(ddo) assert (agent.http_client) new_client = object() agent.http_client = new_client assert (agent.http_client) assert (isinstance(agent.http_client, object))
def remote_agent_surfer(config): ddo_options = None local_agent = config['agents']['surfer'] ddo = DDO.create(local_agent['url'], ['meta', 'storage', 'invoke', 'market', 'trust', 'auth']) authentication = AuthenticationBasic(local_agent['username'], local_agent['password']) return RemoteAgent(ddo, authentication=authentication)
def test_agent_manager_load_ddo(): manager = AgentManager() ddo = DDO.create('http://test.com') manager.register_agent('test_agent', ddo_text=ddo.as_text) result_ddo = manager.load_ddo('test_agent') assert(result_ddo) assert(result_ddo.as_text == ddo.as_text)
def test_agent_manager_is_agent(): manager = AgentManager() ddo = DDO.create('http://test.com') manager.register_agent('test_agent', ddo=ddo) assert(manager.is_agent('test_agent')) assert(not manager.is_agent('invalid_test_agent'))
def __init__(self, ddo: DDO = None) -> None: if ddo is None: did = did_generate_random() ddo = DDO(did) AgentBase.__init__(self, ddo=ddo) self._memory = {'listing': {}, 'asset': {}, 'purchase': {}}
def test_agent_manager_register_agent(): manager = AgentManager() authentication = AuthenticationBasic('user_test', 'password_test') ddo = DDO.create('http://test.com') manager.register_local_agent(ddo, authentication=authentication) assert(manager.local_agent) with pytest.raises(ValueError): manager.register_local_agent('failed')
def test_ddo_create_from_service_list(): test_url = 'http://localhost' ddo = DDO.create(test_url, service_list=['meta', 'trust']) assert(ddo) #ddo_text = json.dumps(json.loads(ddo.as_text), sort_keys=True, indent=4) #print(ddo_text) assert('meta' in ddo.service) assert(ddo.service['meta']['serviceEndpoint'] == 'http://localhost/api/v1/meta') with pytest.raises(TypeError, match=r'Service list must be a list or tuple of service names'): ddo = DDO.create(test_url, service_list={ 'bad_name':'meta', 'invalid': 'trust', }) with pytest.raises(TypeError, match=r'Service list must be a list of type string names'): ddo = DDO.create(test_url, service_list=[ {'type':'meta'}, {'type': 'trust'}, ])
def test_05_agent_endpoint_update(config, remote_agent_surfer): endpoint = remote_agent_surfer.get_endpoint('DEP.Meta.v1') assert (endpoint) assert (re.search('meta', endpoint)) local_agent = config['agents']['surfer'] ddo = DDO.create(local_agent['url'], service_list=['meta'], version='v99') new_endpoint_uri = '/app/v99/meta/test' new_agent = RemoteAgent(ddo=ddo) with pytest.raises(ValueError): new_endpoint = new_agent.get_endpoint('DEP.Meta.v99')
def test_network_resolve_agent(config, ethereum_network, ethereum_accounts): local_agent = config['agents']['surfer'] ddo = ethereum_network.resolve_agent(local_agent['url']) did = did_generate_random() ddo = DDO.create(local_agent['url']) ddo_text = ddo.as_text register_account = ethereum_accounts[0] ethereum_network.register_did(register_account, did, ddo_text) ddo = ethereum_network.resolve_agent(did) assert (ddo)
def test_04_agent_register_and_resolve(convex_network, config, convex_accounts): local_agent = config['agents']['surfer'] ddo = DDO.create(local_agent['url']) authentication = AuthenticationBasic(local_agent['username'], local_agent['password']) register_account = convex_accounts[0] did = ddo.did remote_agent = RemoteAgent.register(convex_network, register_account, ddo, authentication) assert (remote_agent) found_ddo = DDO.import_from_text(convex_network.resolve_did(did)) assert (found_ddo.as_text == ddo.as_text) resolved_agent = RemoteAgent.load(ddo.did, convex_network, authentication=authentication) assert (resolved_agent) assert (resolved_agent.ddo) assert (resolved_agent.ddo.as_text == ddo.as_text)
def test_agent_manager_unregister_agent(): manager = AgentManager() ddo = DDO.create('http://test.com') manager.register_agent('test_agent', ddo=ddo) access = manager.find_agent_access('test_agent') assert(access) assert(access.ddo.as_text == ddo.as_text) manager.unregister_agent('test_agent') access = manager.find_agent_access('test_agent') assert(not access)
def resolve_agent(self, agent_url_did: str, username: str = None, password: str = None, authentication: Authentication = None) -> DDO: # stop circular references on import from starfish.agent.remote_agent import RemoteAgent ddo = None if is_did(agent_url_did): ddo_text = self.resolve_did(agent_url_did) if ddo_text: ddo = DDO.import_from_text(ddo_text) return ddo if not authentication: if username or password: authentication = AuthenticationBasic(username, password) ddo_text = RemoteAgent.resolve_url(agent_url_did, authentication) if ddo_text: ddo = DDO.import_from_text(ddo_text) return ddo
def resolve_agent_url(url, authentication=None, http_client=None): """ Resolve an agent based on it's url. :param str url: URL of the agent :param dict authentication: Authentication of the agent. :param object http_client: HTTP client to use to access the agent api :returns: DDO object of the remote agent """ logger.debug(f'resolving remote agent did from {url}') try: ddo_text = RemoteAgent.resolve_url(url, authentication=authentication, http_client=http_client) if ddo_text: return DDO.import_from_text(ddo_text) # ignore connetion errors to remote agents except StarfishConnectionError: pass
def __init__(self, ddo: DDO, authentication: Authentication = None, http_client: Any = None) -> None: if authentication and not isinstance(authentication, Authentication): raise TypeError( 'authentication parameter must be a child of Authentication class' ) self._authentication = authentication if isinstance(ddo, str): ddo = DDO.import_from_text(ddo) elif isinstance(ddo, DDO): pass else: raise ValueError(f'Unknown ddo type {ddo}') AgentBase.__init__(self, ddo) self._adapter = RemoteAgentAdapter(http_client)
def test_did_registry_contract(ethereum_network, config, ethereum_accounts): """ Register and find a ddo based on a did """ did_registry_contract = ethereum_network.get_contract('DIDRegistry') did = did_generate_random() local_agent = config['agents']['surfer'] ddo = DDO.create('http://localhost', did=did) ddo_text = ddo.as_text register_account = ethereum_accounts[0] tx_hash = did_registry_contract.register(register_account, did, ddo_text) receipt = did_registry_contract.wait_for_receipt(tx_hash) block_number = did_registry_contract.get_block_number(did) assert (receipt.blockNumber == block_number) saved_ddo_text = did_registry_contract.get_value(did) assert (ddo_text == saved_ddo_text)
def test_ddo_is_supported_service(): assert(DDO.is_supported_service('meta')) assert(not DDO.is_supported_service('bad-service-name'))
def test_remote_agent_get_adapter(): ddo = DDO.create('http://localhost:3030') agent = RemoteAgent(ddo) assert (agent.adapter) assert (isinstance(agent.adapter, RemoteAgentAdapter))
def test_ddo_basic_init(): did = did_generate_random() ddo = DDO(did) assert(ddo) assert(ddo.did == did)
def test_ddo_create_all_services(): test_url = 'http://localhost' ddo = DDO.create(test_url) assert(ddo) assert(len(ddo.service_list) > 5)
def test_remote_agent_get_meta_list(): ddo = DDO.create('http://localhost') agent = RemoteAgent(ddo) with pytest.raises(StarfishConnectionError): result = agent.get_metadata_list()
def test_ddo_add_services(): ddo = DDO() ddo.add_service('meta', endpoint='http://localhost/api/v98/meta') ddo.add_service('trust', url='http://localhost') ddo.add_service('DID.Meta.v1', endpoint='http://localhost/api/v1/meta_did') assert('meta' in ddo.service) assert(ddo.service['meta']['serviceEndpoint'] == 'http://localhost/api/v98/meta') assert('trust' in ddo.service) assert(ddo.service['trust']['serviceEndpoint'] == 'http://localhost/api/v1/trust') assert('DID.Meta.v1' in ddo.service) assert(ddo.service['DID.Meta.v1']['serviceEndpoint'] == 'http://localhost/api/v1/meta_did') with pytest.raises(ValueError, match=r'Invalid service name'): ddo.add_service('test') with pytest.raises(ValueError, match=r'No endpoint or url provided to add a service to the ddo'): ddo.add_service('auth')
def test_ddo_checksum(): test_url = 'http://localhost' ddo = DDO.create(test_url, service_list=['meta', 'trust']) assert(ddo.checksum == '5da48d1d8b9ad1fdb27400e336dc007dc90a86957dfe1cc179566118d79d9166')