Skip to content

Commit

Permalink
add some unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale committed Jan 21, 2024
1 parent ebf5336 commit 3bf2717
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
from asynctest import TestCase
from base58 import alphabet

from aries_cloudagent.anoncreds.base import AnonCredsSchemaAlreadyExists
from aries_cloudagent.anoncreds.base import (
AnonCredsRegistrationError,
AnonCredsSchemaAlreadyExists,
)
from aries_cloudagent.anoncreds.models.anoncreds_schema import (
AnonCredsSchema,
SchemaResult,
)
from aries_cloudagent.askar.profile_anon import AskarAnoncredsProfile
from aries_cloudagent.connections.models.conn_record import ConnRecord
from aries_cloudagent.core.in_memory.profile import InMemoryProfile
from aries_cloudagent.ledger.error import LedgerObjectAlreadyExistsError
from aries_cloudagent.ledger.error import LedgerError, LedgerObjectAlreadyExistsError
from aries_cloudagent.protocols.endorse_transaction.v1_0.manager import (
TransactionManager,
)
Expand Down Expand Up @@ -239,3 +242,26 @@ def serialize(self):
assert result["txn"] is not None
assert mock_create_request.called
assert mock_outbound_handler.called

async def test_txn_submit(self):
self.profile.inject = mock.MagicMock(
side_effect=[
None,
mock.CoroutineMock(
txn_submit=mock.CoroutineMock(side_effect=LedgerError("test error"))
),
mock.CoroutineMock(
txn_submit=mock.CoroutineMock(return_value="transaction response")
),
]
)

# No ledger
with self.assertRaises(LedgerError):
await self.registry.txn_submit(self.profile, "test_txn")
# Write error
with self.assertRaises(AnonCredsRegistrationError):
await self.registry.txn_submit(self.profile, "test_txn")

result = await self.registry.txn_submit(self.profile, "test_txn")
assert result == "transaction response"
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio
import json
import uuid

from unittest import IsolatedAsyncioTestCase

from aries_cloudagent.anoncreds.registry import AnonCredsRegistry
from aries_cloudagent.tests import mock

from .....admin.request_context import AdminRequestContext
Expand Down Expand Up @@ -497,6 +498,61 @@ async def test_complete_transaction(self):

assert transaction_record.state == TransactionRecord.STATE_TRANSACTION_ACKED

async def test_complete_transaction_anoncreds(self):
self.profile.settings.set_value("wallet.type", "askar-anoncreds")
self.profile.context.injector.bind_instance(
AnonCredsRegistry,
mock.MagicMock(
txn_submit=mock.CoroutineMock(
return_value=json.dumps(
{
"result": {
"txn": {"type": "101", "metadata": {"from": TEST_DID}},
"txnMetadata": {"txnId": SCHEMA_ID},
}
}
)
)
),
)
transaction_record = await self.manager.create_record(
messages_attach=self.test_messages_attach,
connection_id=self.test_connection_id,
)
future = asyncio.Future()
future.set_result(
mock.MagicMock(return_value=mock.MagicMock(add_record=mock.CoroutineMock()))
)
self.ledger.get_indy_storage = future

with mock.patch.object(
TransactionRecord, "save", autospec=True
) as save_record, mock.patch.object(
ConnRecord, "retrieve_by_id"
) as mock_conn_rec_retrieve:
mock_conn_rec_retrieve.return_value = mock.MagicMock(
metadata_get=mock.CoroutineMock(
return_value={
"transaction_their_job": (
TransactionJob.TRANSACTION_ENDORSER.name
),
"transaction_my_job": (TransactionJob.TRANSACTION_AUTHOR.name),
}
)
)

(
transaction_record,
transaction_acknowledgement_message,
) = await self.manager.complete_transaction(transaction_record, False)
save_record.assert_called_once()

assert transaction_record.state == TransactionRecord.STATE_TRANSACTION_ACKED
# txn submit called from anoncreds registry
assert self.profile.context._injector.get_provider(
AnonCredsRegistry
)._instance.txn_submit.called

async def test_create_refuse_response_bad_state(self):
transaction_record = await self.manager.create_record(
messages_attach=self.test_messages_attach,
Expand Down

0 comments on commit 3bf2717

Please sign in to comment.