From 53fcb9aa900b13db956d44511b1cc623e2931ef3 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 09:24:33 -0500 Subject: [PATCH 1/7] chore: correct typos, more appropriately name attrs Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/did_resolver.py | 4 ++-- aries_cloudagent/resolver/did_resolver_registry.py | 2 +- aries_cloudagent/resolver/tests/test_did_resolver.py | 4 ++-- aries_cloudagent/resolver/tests/test_did_resolver_registry.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/aries_cloudagent/resolver/did_resolver.py b/aries_cloudagent/resolver/did_resolver.py index 4cf7ec958b..bac93ea416 100644 --- a/aries_cloudagent/resolver/did_resolver.py +++ b/aries_cloudagent/resolver/did_resolver.py @@ -21,7 +21,7 @@ class DIDResolver: def __init__(self, registry: DIDResolverRegistry): """Initialize a `didresolver` instance.""" - self.did_resolver_registery = registry + self.did_resolver_registry = registry async def resolve(self, did: Union[str, DID]) -> ResolvedDIDDoc: """Retrieve did doc from public registry.""" @@ -45,7 +45,7 @@ def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: valid_resolvers = list( filter( lambda resolver: resolver.supports(did.method), - self.did_resolver_registery.did_resolvers, + self.did_resolver_registry.resolvers, ) ) native_resolvers = filter(lambda resolver: resolver.native, valid_resolvers) diff --git a/aries_cloudagent/resolver/did_resolver_registry.py b/aries_cloudagent/resolver/did_resolver_registry.py index 70bb04d3ee..4da461ef0e 100644 --- a/aries_cloudagent/resolver/did_resolver_registry.py +++ b/aries_cloudagent/resolver/did_resolver_registry.py @@ -15,7 +15,7 @@ def __init__(self): LOGGER.debug("Resolvers listed") @property - def did_resolvers( + def resolvers( self, ) -> Sequence[str]: """Accessor for a list of all did resolvers.""" diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index 9f31ab4c7e..093aa387f8 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -1,4 +1,4 @@ -"""Test did resolver registery.""" +"""Test did resolver registry.""" import pytest import unittest @@ -127,7 +127,7 @@ def resolver(): def test_create_resolver(resolver): - assert len(resolver.did_resolver_registery.did_resolvers) == len(TEST_DID_METHODS) + assert len(resolver.did_resolver_registry.resolvers) == len(TEST_DID_METHODS) @pytest.mark.parametrize("did, method", zip(TEST_DIDS, TEST_DID_METHODS)) diff --git a/aries_cloudagent/resolver/tests/test_did_resolver_registry.py b/aries_cloudagent/resolver/tests/test_did_resolver_registry.py index 99b231f116..dba7afffbd 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver_registry.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver_registry.py @@ -9,4 +9,4 @@ def test_create_registry(): did_resolver_registry = DIDResolverRegistry() test_resolver = unittest.mock.MagicMock() did_resolver_registry.register(test_resolver) - assert did_resolver_registry.did_resolvers == [test_resolver] + assert did_resolver_registry.resolvers == [test_resolver] From b6dfaa42beb0fccbdf037720662f4e2b2da16d9a Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 09:25:25 -0500 Subject: [PATCH 2/7] feat: drop fully_dereference method Until this method becomes a clear need. Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/did_resolver.py | 25 ------------------- .../resolver/tests/test_did_resolver.py | 4 --- 2 files changed, 29 deletions(-) diff --git a/aries_cloudagent/resolver/did_resolver.py b/aries_cloudagent/resolver/did_resolver.py index bac93ea416..cd4862056c 100644 --- a/aries_cloudagent/resolver/did_resolver.py +++ b/aries_cloudagent/resolver/did_resolver.py @@ -62,28 +62,3 @@ async def dereference_external(self, did_url: str) -> ResolvedDIDDoc: did_url = DIDUrl.parse(did_url) doc = await self.resolve(did_url.did) return doc.dereference(did_url) - - '''async def fully_dereference(self, doc: ResolvedDIDDoc): - """Recursivly retrieve all doc service dids from public registries.""" - - async def _visit(value, doc): - if isinstance(value, dict): - doc_dict = {} - for key, value in value.items(): - doc_dict[key] = await _visit(value, doc) - return doc - elif isinstance(value, list): - return [await _visit(item, doc) for item in value] - elif isinstance(value, str): - if DID_PATTERN.match(value): # string is a did_url pattern - did_url = DIDUrl.parse(value) - did_str = "" - try: # dereference from diddoc - did_str = doc.dereference(did_url) - except ExternalResourceError: # dereference from a resolver - did_str = await self.dereference_external(did_url) - did_str = await _visit(did_str, doc) - return did_str - return value - - return await _visit(doc._doc, doc)''' diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index 093aa387f8..bf7d11e7f7 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -178,10 +178,6 @@ def test_dereference_external(resolver, did): assert result -def test_fully_dereference(resolver): - pytest.skip("need more work") - - @pytest.mark.asyncio @pytest.mark.parametrize("did", TEST_DIDS) async def test_resolve(resolver, did): From 1619dce23a82e2a2d9163555e2e9e08cd409418b Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 09:37:55 -0500 Subject: [PATCH 3/7] test: fix resolve parameters to match base class Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/did_resolver.py | 18 ++++++---- .../resolver/tests/test_did_resolver.py | 33 ++++++++++++------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/aries_cloudagent/resolver/did_resolver.py b/aries_cloudagent/resolver/did_resolver.py index cd4862056c..56153249d4 100644 --- a/aries_cloudagent/resolver/did_resolver.py +++ b/aries_cloudagent/resolver/did_resolver.py @@ -6,11 +6,13 @@ """ import logging -from typing import Union from itertools import chain -from ..resolver.diddoc import ResolvedDIDDoc # , ExternalResourceError +from typing import Union + +from ..core.profile import ProfileSession from ..resolver.base import BaseDIDResolver, DidMethodNotSupported, DidNotFound from ..resolver.did import DID, DIDUrl # , DID_PATTERN +from ..resolver.diddoc import ResolvedDIDDoc # , ExternalResourceError from .did_resolver_registry import DIDResolverRegistry LOGGER = logging.getLogger(__name__) @@ -23,14 +25,16 @@ def __init__(self, registry: DIDResolverRegistry): """Initialize a `didresolver` instance.""" self.did_resolver_registry = registry - async def resolve(self, did: Union[str, DID]) -> ResolvedDIDDoc: + async def resolve( + self, session: ProfileSession, did: Union[str, DID] + ) -> ResolvedDIDDoc: """Retrieve did doc from public registry.""" if isinstance(did, str): did = DID(did) for resolver in self._match_did_to_resolver(did): try: LOGGER.debug("Resolving DID %s with %s", did, resolver) - return await resolver.resolve(did) + return await resolver.resolve(session, did) except DidNotFound: LOGGER.debug("DID %s not found by resolver %s", did, resolver) @@ -57,8 +61,10 @@ def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: raise DidMethodNotSupported(f"{did.method} not supported") return resolvers - async def dereference_external(self, did_url: str) -> ResolvedDIDDoc: + async def dereference_external( + self, session: ProfileSession, did_url: str + ) -> ResolvedDIDDoc: """Retrieve an external did in doc service from a public registry.""" did_url = DIDUrl.parse(did_url) - doc = await self.resolve(did_url.did) + doc = await self.resolve(session, did_url.did) return doc.dereference(did_url) diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index bf7d11e7f7..bb2b9c3590 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -113,7 +113,9 @@ async def setup(self, session): def supported_methods(self): return self._supported_methods - async def resolve(self, did): + async def resolve(self, session, did): + if isinstance(self.resolved, Exception): + raise self.resolved return self.resolved @@ -126,6 +128,11 @@ def resolver(): return DIDResolver(did_resolver_registry) +@pytest.fixture +def session(): + yield async_mock.MagicMock() + + def test_create_resolver(resolver): assert len(resolver.did_resolver_registry.resolvers) == len(TEST_DID_METHODS) @@ -147,8 +154,8 @@ def test_match_did_to_resolver_native_priority(): registry = DIDResolverRegistry() native = MockResolver(["sov"], native=True) non_native = MockResolver(["sov"], native=False) - registry.register(native) registry.register(non_native) + registry.register(native) resolver = DIDResolver(registry) assert [native, non_native] == resolver._match_did_to_resolver(DID(TEST_DID0)) @@ -180,28 +187,32 @@ def test_dereference_external(resolver, did): @pytest.mark.asyncio @pytest.mark.parametrize("did", TEST_DIDS) -async def test_resolve(resolver, did): - did_doc = await resolver.resolve(did) +async def test_resolve(resolver, session, did): + did_doc = await resolver.resolve(session, did) assert isinstance(did_doc, ResolvedDIDDoc) @pytest.mark.asyncio @pytest.mark.parametrize("did", TEST_DIDS) -async def test_resolve_did(resolver, did): +async def test_resolve_did(resolver, session, did): did = DID(did) - did_doc = await resolver.resolve(did) + did_doc = await resolver.resolve(session, did) assert isinstance(did_doc, ResolvedDIDDoc) @pytest.mark.asyncio -async def test_resolve_did_x_not_supported(resolver): +async def test_resolve_did_x_not_supported(resolver, session): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") with pytest.raises(DidMethodNotSupported): - await resolver.resolve(did) + await resolver.resolve(session, did) @pytest.mark.asyncio -async def test_resolve_did_x_not_found(resolver): +async def test_resolve_did_x_not_found(session): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") - with pytest.raises(DidMethodNotSupported): - await resolver.resolve(did) + cowsay_resolver_not_found = MockResolver("cowsay", resolved=DidNotFound()) + registry = DIDResolverRegistry() + registry.register(cowsay_resolver_not_found) + resolver = DIDResolver(registry) + with pytest.raises(DidNotFound): + await resolver.resolve(session, did) From c78a75e35c152dbd3a94ef231f97cd119627f389 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 09:47:41 -0500 Subject: [PATCH 4/7] test: dereference external did url Signed-off-by: Daniel Bluhm --- .../resolver/tests/test_did_resolver.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index bb2b9c3590..67c39f923b 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -41,6 +41,7 @@ TEST_DID_METHOD2, TEST_DID_METHOD3, TEST_DID_METHOD4, + "example", ] TEST_METHOD_SPECIFIC_ID0 = "Kkyqu7CJFuQSvBp468uaDe" @@ -123,7 +124,7 @@ async def resolve(self, session, did): def resolver(): did_resolver_registry = DIDResolverRegistry() for method in TEST_DID_METHODS: - resolver = MockResolver(method, ResolvedDIDDoc(DOC)) + resolver = MockResolver([method], ResolvedDIDDoc(DOC)) did_resolver_registry.register(resolver) return DIDResolver(did_resolver_registry) @@ -176,13 +177,12 @@ def test_match_did_to_resolver_registration_order(): ) -@pytest.mark.parametrize("did", TEST_DIDS) -def test_dereference_external(resolver, did): - with async_mock.patch.object( - resolver, "resolve", async_mock.MagicMock(return_value="resolved did doc") - ) as mock_resolver: - result = mock_resolver.dereference_external(did) - assert result +@pytest.mark.asyncio +async def test_dereference_external(resolver, session): + url = "did:example:1234abcd#4" + assert DOC["verificationMethod"][1] == await resolver.dereference_external( + session, url + ) @pytest.mark.asyncio From 636a035ccc2889dd291b1bbb008697ab09e0dff7 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 09:53:46 -0500 Subject: [PATCH 5/7] chore: rename exceptions to better match DID vs Did convention Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/base.py | 4 ++-- aries_cloudagent/resolver/default/indy.py | 4 ++-- .../resolver/default/tests/test_indy.py | 4 ++-- aries_cloudagent/resolver/did_resolver.py | 8 +++---- .../resolver/did_resolver_registry.py | 1 - .../resolver/tests/test_did_resolver.py | 23 ++++++++++--------- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/aries_cloudagent/resolver/base.py b/aries_cloudagent/resolver/base.py index a85f3122e8..a70912ce3a 100644 --- a/aries_cloudagent/resolver/base.py +++ b/aries_cloudagent/resolver/base.py @@ -12,11 +12,11 @@ class ResolverError(Exception): """Base class for resolver exceptions.""" -class DidNotFound(ResolverError): +class DIDNotFound(ResolverError): """Raised when DID is not found in verifiable data registry.""" -class DidMethodNotSupported(ResolverError): +class DIDMethodNotSupported(ResolverError): """Raised when no resolver is registered for a given did method.""" diff --git a/aries_cloudagent/resolver/default/indy.py b/aries_cloudagent/resolver/default/indy.py index 7ff39d7575..8a5c515509 100644 --- a/aries_cloudagent/resolver/default/indy.py +++ b/aries_cloudagent/resolver/default/indy.py @@ -7,7 +7,7 @@ from ...core.profile import ProfileSession from ...ledger.indy import IndySdkLedger from ...ledger.error import LedgerError -from ..base import BaseDIDResolver, DidNotFound, ResolverError, ResolverType +from ..base import BaseDIDResolver, DIDNotFound, ResolverError, ResolverType from ..did import DID from ..diddoc import ResolvedDIDDoc @@ -46,7 +46,7 @@ async def resolve(self, session: ProfileSession, did: str) -> ResolvedDIDDoc: recipient_key = await ledger.get_key_for_did(str(did)) endpoint = await ledger.get_endpoint_for_did(str(did)) except LedgerError as err: - raise DidNotFound(f"DID {did} could not be resolved") from err + raise DIDNotFound(f"DID {did} could not be resolved") from err doc = ResolvedDIDDoc( { diff --git a/aries_cloudagent/resolver/default/tests/test_indy.py b/aries_cloudagent/resolver/default/tests/test_indy.py index 08066db7ad..008eb29ad8 100644 --- a/aries_cloudagent/resolver/default/tests/test_indy.py +++ b/aries_cloudagent/resolver/default/tests/test_indy.py @@ -9,7 +9,7 @@ from ....ledger.indy import IndySdkLedger from ....ledger.error import LedgerError from ...tests.test_did import TEST_DID0 -from ...base import ResolverError, DidNotFound +from ...base import ResolverError, DIDNotFound # pylint: disable=W0621 @@ -63,5 +63,5 @@ async def test_resolve_x_did_not_found( ): """Test resolve method when no did is found.""" ledger.get_key_for_did.side_effect = LedgerError - with pytest.raises(DidNotFound): + with pytest.raises(DIDNotFound): await resolver.resolve(session, TEST_DID0) diff --git a/aries_cloudagent/resolver/did_resolver.py b/aries_cloudagent/resolver/did_resolver.py index 56153249d4..c352b62b46 100644 --- a/aries_cloudagent/resolver/did_resolver.py +++ b/aries_cloudagent/resolver/did_resolver.py @@ -10,7 +10,7 @@ from typing import Union from ..core.profile import ProfileSession -from ..resolver.base import BaseDIDResolver, DidMethodNotSupported, DidNotFound +from ..resolver.base import BaseDIDResolver, DIDMethodNotSupported, DIDNotFound from ..resolver.did import DID, DIDUrl # , DID_PATTERN from ..resolver.diddoc import ResolvedDIDDoc # , ExternalResourceError from .did_resolver_registry import DIDResolverRegistry @@ -35,10 +35,10 @@ async def resolve( try: LOGGER.debug("Resolving DID %s with %s", did, resolver) return await resolver.resolve(session, did) - except DidNotFound: + except DIDNotFound: LOGGER.debug("DID %s not found by resolver %s", did, resolver) - raise DidNotFound(f"DID {did} could not be resolved.") + raise DIDNotFound(f"DID {did} could not be resolved.") def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: """Generate supported DID Resolvers. @@ -58,7 +58,7 @@ def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: ) resolvers = list(chain(native_resolvers, non_native_resolvers)) if not resolvers: - raise DidMethodNotSupported(f"{did.method} not supported") + raise DIDMethodNotSupported(f"{did.method} not supported") return resolvers async def dereference_external( diff --git a/aries_cloudagent/resolver/did_resolver_registry.py b/aries_cloudagent/resolver/did_resolver_registry.py index 4da461ef0e..5db5d28ebf 100644 --- a/aries_cloudagent/resolver/did_resolver_registry.py +++ b/aries_cloudagent/resolver/did_resolver_registry.py @@ -12,7 +12,6 @@ class DIDResolverRegistry: def __init__(self): """Initialize list for did resolvers.""" self._resolvers = [] - LOGGER.debug("Resolvers listed") @property def resolvers( diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index 67c39f923b..d9c462637f 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -1,19 +1,20 @@ """Test did resolver registry.""" -import pytest import unittest + +import pytest from asynctest import mock as async_mock -from ..did_resolver_registry import DIDResolverRegistry -from ..did_resolver import DIDResolver -from ...resolver.diddoc import ResolvedDIDDoc + from ...resolver.base import ( BaseDIDResolver, - DidMethodNotSupported, - DidNotFound, + DIDMethodNotSupported, + DIDNotFound, ResolverType, ) from ...resolver.did import DID - +from ...resolver.diddoc import ResolvedDIDDoc +from ..did_resolver import DIDResolver +from ..did_resolver_registry import DIDResolverRegistry TEST_DID0 = "did:sov:Kkyqu7CJFuQSvBp468uaDe" TEST_DID1 = "did:btcr:8kyt-fzzq-qpqq-ljsc-5l" @@ -147,7 +148,7 @@ def test_match_did_to_resolver(resolver, did, method): def test_match_did_to_resolver_x_not_supported(resolver): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") - with pytest.raises(DidMethodNotSupported): + with pytest.raises(DIDMethodNotSupported): resolver._match_did_to_resolver(did) @@ -203,16 +204,16 @@ async def test_resolve_did(resolver, session, did): @pytest.mark.asyncio async def test_resolve_did_x_not_supported(resolver, session): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") - with pytest.raises(DidMethodNotSupported): + with pytest.raises(DIDMethodNotSupported): await resolver.resolve(session, did) @pytest.mark.asyncio async def test_resolve_did_x_not_found(session): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") - cowsay_resolver_not_found = MockResolver("cowsay", resolved=DidNotFound()) + cowsay_resolver_not_found = MockResolver("cowsay", resolved=DIDNotFound()) registry = DIDResolverRegistry() registry.register(cowsay_resolver_not_found) resolver = DIDResolver(registry) - with pytest.raises(DidNotFound): + with pytest.raises(DIDNotFound): await resolver.resolve(session, did) From 28049d16c83f32892cf06a7dd549d1c6b7aada4c Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 09:58:37 -0500 Subject: [PATCH 6/7] chore: swap session for profile Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/base.py | 6 ++--- aries_cloudagent/resolver/default/indy.py | 8 +++--- .../resolver/default/tests/test_indy.py | 26 +++++++++---------- aries_cloudagent/resolver/did_resolver.py | 10 +++---- aries_cloudagent/resolver/tests/test_base.py | 4 +-- .../resolver/tests/test_did_resolver.py | 26 +++++++++---------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/aries_cloudagent/resolver/base.py b/aries_cloudagent/resolver/base.py index a70912ce3a..9166dab0a5 100644 --- a/aries_cloudagent/resolver/base.py +++ b/aries_cloudagent/resolver/base.py @@ -5,7 +5,7 @@ from typing import Sequence from .diddoc import ResolvedDIDDoc -from ..core.profile import ProfileSession +from ..core.profile import Profile class ResolverError(Exception): @@ -39,7 +39,7 @@ def __init__(self, type_: ResolverType = None): self.type = type_ or ResolverType.NON_NATIVE @abstractmethod - async def setup(self, session: ProfileSession): + async def setup(self, profile: Profile): """Do asynchronous resolver setup.""" @property @@ -57,5 +57,5 @@ def supports(self, method: str): return method in self.supported_methods @abstractmethod - async def resolve(self, session: ProfileSession, did: str) -> ResolvedDIDDoc: + async def resolve(self, profile: Profile, did: str) -> ResolvedDIDDoc: """Resolve a DID using this resolver.""" diff --git a/aries_cloudagent/resolver/default/indy.py b/aries_cloudagent/resolver/default/indy.py index 8a5c515509..fc569194df 100644 --- a/aries_cloudagent/resolver/default/indy.py +++ b/aries_cloudagent/resolver/default/indy.py @@ -4,7 +4,7 @@ """ from typing import Sequence -from ...core.profile import ProfileSession +from ...core.profile import Profile from ...ledger.indy import IndySdkLedger from ...ledger.error import LedgerError from ..base import BaseDIDResolver, DIDNotFound, ResolverError, ResolverType @@ -25,7 +25,7 @@ def __init__(self): """Initialize Indy Resolver.""" super().__init__(ResolverType.NATIVE) - async def setup(self, session: ProfileSession): + async def setup(self, profile: Profile): """Perform required setup for Indy DID resolution.""" @property @@ -33,9 +33,9 @@ def supported_methods(self) -> Sequence[str]: """Return supported methods of Indy DID Resolver.""" return ["sov"] - async def resolve(self, session: ProfileSession, did: str) -> ResolvedDIDDoc: + async def resolve(self, profile: Profile, did: str) -> ResolvedDIDDoc: """Resolve an indy DID.""" - ledger = session.inject(IndySdkLedger, required=False) + ledger = profile.inject(IndySdkLedger, required=False) if not ledger: raise NoIndyLedger("No Indy ledger isntance is configured.") diff --git a/aries_cloudagent/resolver/default/tests/test_indy.py b/aries_cloudagent/resolver/default/tests/test_indy.py index 008eb29ad8..f669a4c203 100644 --- a/aries_cloudagent/resolver/default/tests/test_indy.py +++ b/aries_cloudagent/resolver/default/tests/test_indy.py @@ -5,7 +5,7 @@ from ..indy import IndyDIDResolver from ....core.in_memory import InMemoryProfile -from ....core.profile import ProfileSession +from ....core.profile import Profile from ....ledger.indy import IndySdkLedger from ....ledger.error import LedgerError from ...tests.test_did import TEST_DID0 @@ -30,11 +30,11 @@ def ledger(): @pytest.fixture -def session(ledger): - """Session fixture.""" - session = InMemoryProfile.test_session() - session.context.injector.bind_instance(IndySdkLedger, ledger) - yield session +def profile(ledger): + """Profile fixture.""" + profile = InMemoryProfile.test_profile() + profile.context.injector.bind_instance(IndySdkLedger, ledger) + yield profile def test_supported_methods(resolver: IndyDIDResolver): @@ -44,24 +44,24 @@ def test_supported_methods(resolver: IndyDIDResolver): @pytest.mark.asyncio -async def test_resolve(resolver: IndyDIDResolver, session: ProfileSession): +async def test_resolve(resolver: IndyDIDResolver, profile: Profile): """Test resolve method.""" - assert await resolver.resolve(session, TEST_DID0) + assert await resolver.resolve(profile, TEST_DID0) @pytest.mark.asyncio -async def test_resolve_x_no_ledger(resolver: IndyDIDResolver, session: ProfileSession): +async def test_resolve_x_no_ledger(resolver: IndyDIDResolver, profile: Profile): """Test resolve method with no ledger.""" - session.context.injector.clear_binding(IndySdkLedger) + profile.context.injector.clear_binding(IndySdkLedger) with pytest.raises(ResolverError): - await resolver.resolve(session, TEST_DID0) + await resolver.resolve(profile, TEST_DID0) @pytest.mark.asyncio async def test_resolve_x_did_not_found( - resolver: IndyDIDResolver, ledger: IndySdkLedger, session: ProfileSession + resolver: IndyDIDResolver, ledger: IndySdkLedger, profile: Profile ): """Test resolve method when no did is found.""" ledger.get_key_for_did.side_effect = LedgerError with pytest.raises(DIDNotFound): - await resolver.resolve(session, TEST_DID0) + await resolver.resolve(profile, TEST_DID0) diff --git a/aries_cloudagent/resolver/did_resolver.py b/aries_cloudagent/resolver/did_resolver.py index c352b62b46..60dcdf0efe 100644 --- a/aries_cloudagent/resolver/did_resolver.py +++ b/aries_cloudagent/resolver/did_resolver.py @@ -9,7 +9,7 @@ from itertools import chain from typing import Union -from ..core.profile import ProfileSession +from ..core.profile import Profile from ..resolver.base import BaseDIDResolver, DIDMethodNotSupported, DIDNotFound from ..resolver.did import DID, DIDUrl # , DID_PATTERN from ..resolver.diddoc import ResolvedDIDDoc # , ExternalResourceError @@ -26,7 +26,7 @@ def __init__(self, registry: DIDResolverRegistry): self.did_resolver_registry = registry async def resolve( - self, session: ProfileSession, did: Union[str, DID] + self, profile: Profile, did: Union[str, DID] ) -> ResolvedDIDDoc: """Retrieve did doc from public registry.""" if isinstance(did, str): @@ -34,7 +34,7 @@ async def resolve( for resolver in self._match_did_to_resolver(did): try: LOGGER.debug("Resolving DID %s with %s", did, resolver) - return await resolver.resolve(session, did) + return await resolver.resolve(profile, did) except DIDNotFound: LOGGER.debug("DID %s not found by resolver %s", did, resolver) @@ -62,9 +62,9 @@ def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: return resolvers async def dereference_external( - self, session: ProfileSession, did_url: str + self, profile: Profile, did_url: str ) -> ResolvedDIDDoc: """Retrieve an external did in doc service from a public registry.""" did_url = DIDUrl.parse(did_url) - doc = await self.resolve(session, did_url.did) + doc = await self.resolve(profile, did_url.did) return doc.dereference(did_url) diff --git a/aries_cloudagent/resolver/tests/test_base.py b/aries_cloudagent/resolver/tests/test_base.py index 0b1b5a6cf2..664ffbfa13 100644 --- a/aries_cloudagent/resolver/tests/test_base.py +++ b/aries_cloudagent/resolver/tests/test_base.py @@ -10,14 +10,14 @@ class ExampleDIDResolver(BaseDIDResolver): def __init__(self): super().__init__() - async def setup(self, session): + async def setup(self, profile): pass @property def supported_methods(self): return ["test"] - async def resolve(self, session, did: str) -> ResolvedDIDDoc: + async def resolve(self, profile, did: str) -> ResolvedDIDDoc: return ResolvedDIDDoc({"id": "did:example:123"}) diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index d9c462637f..166bb982d1 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -108,14 +108,14 @@ def __init__(self, supported_methods, resolved=None, native: bool = False): self._supported_methods = supported_methods self.resolved = resolved - async def setup(self, session): + async def setup(self, profile): pass @property def supported_methods(self): return self._supported_methods - async def resolve(self, session, did): + async def resolve(self, profile, did): if isinstance(self.resolved, Exception): raise self.resolved return self.resolved @@ -131,7 +131,7 @@ def resolver(): @pytest.fixture -def session(): +def profile(): yield async_mock.MagicMock() @@ -179,41 +179,41 @@ def test_match_did_to_resolver_registration_order(): @pytest.mark.asyncio -async def test_dereference_external(resolver, session): +async def test_dereference_external(resolver, profile): url = "did:example:1234abcd#4" assert DOC["verificationMethod"][1] == await resolver.dereference_external( - session, url + profile, url ) @pytest.mark.asyncio @pytest.mark.parametrize("did", TEST_DIDS) -async def test_resolve(resolver, session, did): - did_doc = await resolver.resolve(session, did) +async def test_resolve(resolver, profile, did): + did_doc = await resolver.resolve(profile, did) assert isinstance(did_doc, ResolvedDIDDoc) @pytest.mark.asyncio @pytest.mark.parametrize("did", TEST_DIDS) -async def test_resolve_did(resolver, session, did): +async def test_resolve_did(resolver, profile, did): did = DID(did) - did_doc = await resolver.resolve(session, did) + did_doc = await resolver.resolve(profile, did) assert isinstance(did_doc, ResolvedDIDDoc) @pytest.mark.asyncio -async def test_resolve_did_x_not_supported(resolver, session): +async def test_resolve_did_x_not_supported(resolver, profile): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") with pytest.raises(DIDMethodNotSupported): - await resolver.resolve(session, did) + await resolver.resolve(profile, did) @pytest.mark.asyncio -async def test_resolve_did_x_not_found(session): +async def test_resolve_did_x_not_found(profile): did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A") cowsay_resolver_not_found = MockResolver("cowsay", resolved=DIDNotFound()) registry = DIDResolverRegistry() registry.register(cowsay_resolver_not_found) resolver = DIDResolver(registry) with pytest.raises(DIDNotFound): - await resolver.resolve(session, did) + await resolver.resolve(profile, did) From e4ba21fe58b8ce1a88ca779cde782477bb3f0667 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 4 Feb 2021 10:15:41 -0500 Subject: [PATCH 7/7] chore: rename dereference_external -> dereference Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/did_resolver.py | 6 ++++-- aries_cloudagent/resolver/tests/test_did_resolver.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/aries_cloudagent/resolver/did_resolver.py b/aries_cloudagent/resolver/did_resolver.py index 60dcdf0efe..7fcc4dec82 100644 --- a/aries_cloudagent/resolver/did_resolver.py +++ b/aries_cloudagent/resolver/did_resolver.py @@ -29,6 +29,7 @@ async def resolve( self, profile: Profile, did: Union[str, DID] ) -> ResolvedDIDDoc: """Retrieve did doc from public registry.""" + # TODO Cache results if isinstance(did, str): did = DID(did) for resolver in self._match_did_to_resolver(did): @@ -61,10 +62,11 @@ def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: raise DIDMethodNotSupported(f"{did.method} not supported") return resolvers - async def dereference_external( + async def dereference( self, profile: Profile, did_url: str ) -> ResolvedDIDDoc: - """Retrieve an external did in doc service from a public registry.""" + """Dereference a DID URL to its corresponding DID Doc object.""" + # TODO Use cached DID Docs when possible did_url = DIDUrl.parse(did_url) doc = await self.resolve(profile, did_url.did) return doc.dereference(did_url) diff --git a/aries_cloudagent/resolver/tests/test_did_resolver.py b/aries_cloudagent/resolver/tests/test_did_resolver.py index 166bb982d1..315cc7318f 100644 --- a/aries_cloudagent/resolver/tests/test_did_resolver.py +++ b/aries_cloudagent/resolver/tests/test_did_resolver.py @@ -179,9 +179,9 @@ def test_match_did_to_resolver_registration_order(): @pytest.mark.asyncio -async def test_dereference_external(resolver, profile): +async def test_dereference(resolver, profile): url = "did:example:1234abcd#4" - assert DOC["verificationMethod"][1] == await resolver.dereference_external( + assert DOC["verificationMethod"][1] == await resolver.dereference( profile, url )