Skip to content

Commit

Permalink
Add unit test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <jamiehalebc@gmail.com>
  • Loading branch information
jamshale committed Jun 24, 2024
1 parent 63b61f7 commit 458ae9e
Showing 1 changed file with 214 additions and 1 deletion.
215 changes: 214 additions & 1 deletion aries_cloudagent/holder/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import json
from unittest import IsolatedAsyncioTestCase

from aries_askar import AskarErrorCode

from aries_cloudagent.tests import mock

from ...core.in_memory import InMemoryProfile
from ...admin.request_context import AdminRequestContext
from ...anoncreds.holder import AnonCredsHolder, AnonCredsHolderError
from ...askar.profile_anon import AskarAnoncredsProfile
from ...core.in_memory.profile import InMemoryProfile
from ...indy.holder import IndyHolder
from ...ledger.base import BaseLedger
from ...storage.vc_holder.base import VCHolder
Expand Down Expand Up @@ -66,6 +71,44 @@ async def test_credentials_get(self):
json_response.assert_called_once_with({"hello": "world"})
assert result is json_response.return_value

@mock.patch.object(AnonCredsHolder, "get_credential")
async def test_credentials_get_with_anoncreds(self, mock_get_credential):
self.session_inject = {}
self.profile = InMemoryProfile.test_profile(
settings={
"wallet.type": "askar-anoncreds",
"admin.admin_api_key": "secret-key",
},
profile_class=AskarAnoncredsProfile,
)
self.context = AdminRequestContext.test_context(
self.session_inject, self.profile
)
self.request_dict = {
"context": self.context,
}
self.request = mock.MagicMock(
match_info={"credential_id": "dummy"},
__getitem__=lambda _, k: self.request_dict[k],
context=self.context,
headers={"x-api-key": "secret-key"},
)
self.context.inject = mock.Mock(
return_value=mock.MagicMock(
get_credential=mock.CoroutineMock(return_value="test-credential")
)
)

mock_get_credential.return_value = json.dumps({"hello": "world"})

with mock.patch.object(
test_module.web, "json_response", mock.Mock()
) as json_response:
result = await test_module.credentials_get(self.request)
json_response.assert_called_once_with({"hello": "world"})
assert result is json_response.return_value
assert mock_get_credential.called

async def test_credentials_get_not_found(self):
self.request.match_info = {"credential_id": "dummy"}
self.profile.context.injector.bind_instance(
Expand Down Expand Up @@ -97,6 +140,43 @@ async def test_credentials_revoked(self):
json_response.assert_called_once_with({"revoked": False})
assert result is json_response.return_value

@mock.patch.object(AnonCredsHolder, "credential_revoked")
async def test_credentials_revoked_with_anoncreds(self, mock_credential_revoked):
self.session_inject = {}
self.profile = InMemoryProfile.test_profile(
settings={
"wallet.type": "askar-anoncreds",
"admin.admin_api_key": "secret-key",
},
profile_class=AskarAnoncredsProfile,
)
self.context = AdminRequestContext.test_context(
self.session_inject, self.profile
)
self.request_dict = {
"context": self.context,
}
self.request = mock.MagicMock(
match_info={"credential_id": "dummy"},
__getitem__=lambda _, k: self.request_dict[k],
context=self.context,
headers={"x-api-key": "secret-key"},
)
self.context.inject = mock.Mock(
return_value=mock.MagicMock(
get_credential=mock.CoroutineMock(return_value="test-credential")
)
)

mock_credential_revoked.return_value = False

with mock.patch.object(
test_module.web, "json_response", mock.Mock()
) as json_response:
result = await test_module.credentials_revoked(self.request)
json_response.assert_called_once_with({"revoked": False})
assert result is json_response.return_value

async def test_credentials_revoked_no_ledger(self):
self.request.match_info = {"credential_id": "dummy"}

Expand Down Expand Up @@ -159,6 +239,47 @@ async def test_attribute_mime_types_get(self):
{"results": {"a": "application/jpeg"}}
)

@mock.patch.object(AnonCredsHolder, "get_mime_type")
async def test_attribute_mime_types_get_with_anoncreds(self, mock_get_mime_type):
self.session_inject = {}
self.profile = InMemoryProfile.test_profile(
settings={
"wallet.type": "askar-anoncreds",
"admin.admin_api_key": "secret-key",
},
profile_class=AskarAnoncredsProfile,
)
self.context = AdminRequestContext.test_context(
self.session_inject, self.profile
)
self.request_dict = {
"context": self.context,
}
self.request = mock.MagicMock(
match_info={"credential_id": "dummy"},
__getitem__=lambda _, k: self.request_dict[k],
context=self.context,
headers={"x-api-key": "secret-key"},
)
self.context.inject = mock.Mock(
return_value=mock.MagicMock(
get_credential=mock.CoroutineMock(return_value="test-credential")
)
)

mock_get_mime_type.side_effect = [None, {"a": "application/jpeg"}]

with mock.patch.object(test_module.web, "json_response") as mock_response:
await test_module.credentials_attr_mime_types_get(self.request)
mock_response.assert_called_once_with({"results": None})

with mock.patch.object(test_module.web, "json_response") as mock_response:
await test_module.credentials_attr_mime_types_get(self.request)
mock_response.assert_called_once_with(
{"results": {"a": "application/jpeg"}}
)
assert mock_get_mime_type.called

async def test_credentials_remove(self):
self.request.match_info = {"credential_id": "dummy"}
self.profile.context.injector.bind_instance(
Expand All @@ -173,6 +294,57 @@ async def test_credentials_remove(self):
json_response.assert_called_once_with({})
assert result is json_response.return_value

@mock.patch.object(AnonCredsHolder, "delete_credential")
async def test_credentials_remove_with_anoncreds(self, mock_delete_credential):
self.session_inject = {}
self.profile = InMemoryProfile.test_profile(
settings={
"wallet.type": "askar-anoncreds",
"admin.admin_api_key": "secret-key",
},
profile_class=AskarAnoncredsProfile,
)
self.context = AdminRequestContext.test_context(
self.session_inject, self.profile
)
self.request_dict = {
"context": self.context,
}
self.request = mock.MagicMock(
match_info={"credential_id": "dummy"},
__getitem__=lambda _, k: self.request_dict[k],
context=self.context,
headers={"x-api-key": "secret-key"},
)
self.context.inject = mock.Mock(
return_value=mock.MagicMock(
get_credential=mock.CoroutineMock(return_value="test-credential")
)
)

mock_delete_credential.side_effect = [
None,
AnonCredsHolderError(
"anoncreds error", error_code=AskarErrorCode.NOT_FOUND
),
AnonCredsHolderError(
"anoncreds error", error_code=AskarErrorCode.UNEXPECTED
),
]

with mock.patch.object(
test_module.web, "json_response", mock.Mock()
) as json_response:
result = await test_module.credentials_remove(self.request)
json_response.assert_called_once_with({})
assert result is json_response.return_value
assert mock_delete_credential.called

with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.credentials_remove(self.request)
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.credentials_remove(self.request)

async def test_credentials_remove_not_found(self):
self.request.match_info = {"credential_id": "dummy"}
self.profile.context.injector.bind_instance(
Expand Down Expand Up @@ -202,6 +374,47 @@ async def test_credentials_list(self):
json_response.assert_called_once_with({"results": [{"hello": "world"}]})
assert result is json_response.return_value

@mock.patch.object(AnonCredsHolder, "get_credentials")
async def test_credentials_list_with_anoncreds(self, mock_get_credentials):
self.session_inject = {}
self.profile = InMemoryProfile.test_profile(
settings={
"wallet.type": "askar-anoncreds",
"admin.admin_api_key": "secret-key",
},
profile_class=AskarAnoncredsProfile,
)
self.context = AdminRequestContext.test_context(
self.session_inject, self.profile
)
self.request_dict = {
"context": self.context,
}
self.request = mock.MagicMock(
match_info={"credential_id": "dummy"},
query={
"start": "0",
"count": "10",
},
__getitem__=lambda _, k: self.request_dict[k],
context=self.context,
headers={"x-api-key": "secret-key"},
)
self.context.inject = mock.Mock(
return_value=mock.MagicMock(
get_credential=mock.CoroutineMock(return_value="test-credential")
)
)

mock_get_credentials.return_value = [{"hello": "world"}]

with mock.patch.object(
test_module.web, "json_response", mock.Mock()
) as json_response:
result = await test_module.credentials_list(self.request)
json_response.assert_called_once_with({"results": [{"hello": "world"}]})
assert result is json_response.return_value

async def test_credentials_list_x_holder(self):
self.request.query = {"start": "0", "count": "10"}
self.profile.context.injector.bind_instance(
Expand Down

0 comments on commit 458ae9e

Please sign in to comment.