Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jamie Hale <jamiehalebc@gmail.com>
  • Loading branch information
jamshale committed Sep 20, 2024
1 parent 8a6f4f2 commit a3bfe32
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 9 deletions.
4 changes: 2 additions & 2 deletions aries_cloudagent/did/manager.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Abstract class for DID manager."""

from abc import abstractmethod
from abc import ABC, abstractmethod
from typing import List, Optional

from aries_cloudagent.core.profile import Profile


class DidManager:
class DidManager(ABC):
"""Abstract class for DID manager."""

def __init__(self, profile: Profile):
Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/did/tdw/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def resolve_did_tdw(request: web.BaseRequest):
"""Resolve a DID TDW."""
context: AdminRequestContext = request["context"]
did = (await request.json()).get("did")
resolved_did_response = await DidTdwManager(context.profile).resolve_did(did)
resolved_did_response = await DidTdwManager(context.profile).resolve(did)
if resolved_did_response.get("error"):
return web.json_response(
{
Expand Down
11 changes: 5 additions & 6 deletions aries_cloudagent/did/tdw/tdw_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import logging
from typing import List, Optional

from aries_cloudagent.resolver.did_resolver import DIDResolver

from ...core.profile import Profile
from ...did.manager import DidManager
from ...resolver.did_resolver import DIDResolver

LOGGER = logging.getLogger(__name__)

Expand All @@ -18,20 +17,20 @@ def __init__(self, profile: Profile):
"""Initialize DID Rotate Manager."""
self.profile = profile

async def register_did(self, features: Optional[List[dict]] = None):
async def register(self, features: Optional[List[dict]] = None):
"""Register a DID TDW with the ledger."""
raise NotImplementedError

async def resolve_did(self, did: str):
async def resolve(self, did: str):
"""Resolve a DID TDW."""

resolver = self.profile.inject(DIDResolver)
return await resolver.resolve(self.profile, did)

async def update_did(self, did: str):
async def update(self, did: str):
"""Update a DID TDW."""
raise NotImplementedError

async def delete_did(self, did: str):
async def delete(self, did: str):
"""Delete a DID TDW."""
raise NotImplementedError
66 changes: 66 additions & 0 deletions aries_cloudagent/did/tdw/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from unittest import IsolatedAsyncioTestCase

from aries_cloudagent.admin.request_context import AdminRequestContext
from aries_cloudagent.core.in_memory.profile import InMemoryProfile
from aries_cloudagent.did.tdw.routes import resolve_did_tdw
from aries_cloudagent.resolver.did_resolver import DIDResolver
from aries_cloudagent.tests import mock

mock_response = {
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/multikey/v1",
],
"id": "did:tdw:Qma6mc1qZw3NqxwX6SB5GPQYzP4pGN2nXD15Jwi4bcDBKu:domain.example", # noqa: E501
}


class TestAnoncredsRoutes(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
self.session_inject = {}
self.profile = InMemoryProfile.test_profile(
settings={
"admin.admin_api_key": "secret-key",
},
)
self.context = AdminRequestContext.test_context(self.session_inject, self.profile)
self.request_dict = {
"context": self.context,
}
self.request = mock.MagicMock(
app={},
match_info={},
query={},
__getitem__=lambda _, k: self.request_dict[k],
context=self.context,
headers={"x-api-key": "secret-key"},
json=mock.AsyncMock(
return_value={
"did": "did:tdw:QmRHpkso6BM1jRvGee5VoiS3a6wUfPAfuPnyGRBFo4rL6k:example.com"
}
),
)

async def test_resolve_did_tdw(self):
self.profile.context.injector.bind_instance(
DIDResolver,
mock.MagicMock(resolve=mock.CoroutineMock(return_value=mock_response)),
)
response = await resolve_did_tdw(self.request)
assert response.body

async def test_resolve_did_tdw_error(self):
self.profile.context.injector.bind_instance(
DIDResolver,
mock.MagicMock(
resolve=mock.CoroutineMock(
return_value={
"error": "error",
"errorMessage": "error message",
}
)
),
)
response = await resolve_did_tdw(self.request)
assert response.status == 400
assert response.body
23 changes: 23 additions & 0 deletions aries_cloudagent/did/tdw/tests/test_tdw_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from aries_cloudagent.core.in_memory.profile import InMemoryProfile
from aries_cloudagent.did.tdw.tdw_manager import DidTdwManager
from aries_cloudagent.resolver.did_resolver import DIDResolver
from aries_cloudagent.tests import mock


class TestTdwManager:
def test_init(self):
manager = DidTdwManager(InMemoryProfile.test_profile())
assert manager

async def test_resolve(self):
profile = InMemoryProfile.test_profile()
profile.context.injector.bind_instance(
DIDResolver,
mock.MagicMock(
resolve=mock.CoroutineMock(return_value={"did_doc": "resolved"})
),
)
await DidTdwManager(profile).resolve(
"did:tdw:QmRHpkso6BM1jRvGee5VoiS3a6wUfPAfuPnyGRBFo4rL6k:example.com"
)
assert profile.context.injector.get_provider(DIDResolver)._instance.resolve.called
37 changes: 37 additions & 0 deletions aries_cloudagent/resolver/default/tests/test_tdw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest

from ....core.in_memory import InMemoryProfile
from ....core.profile import Profile
from ....messaging.valid import DIDTdw
from ..tdw import TdwDIDResolver

TEST_DID = "did:tdw:Qma6mc1qZw3NqxwX6SB5GPQYzP4pGN2nXD15Jwi4bcDBKu:domain.example"


@pytest.fixture
def resolver():
"""Resolver fixture."""
yield TdwDIDResolver()


@pytest.fixture
def profile():
"""Profile fixture."""
profile = InMemoryProfile.test_profile()
yield profile


@pytest.mark.asyncio
async def test_supported_did_regex(profile, resolver: TdwDIDResolver):
"""Test the supported_did_regex."""
assert resolver.supported_did_regex == DIDTdw.PATTERN
assert await resolver.supports(
profile,
TEST_DID,
)


@pytest.mark.asyncio
async def test_resolve(resolver: TdwDIDResolver, profile: Profile):
"""Test resolve method."""
assert await resolver.resolve(profile, TEST_DID)

0 comments on commit a3bfe32

Please sign in to comment.