Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to invitation handling and performance test #68

Merged
merged 8 commits into from
Jul 15, 2019
Prev Previous commit
Next Next commit
fix swallowed exceptions in tests; add ledger tests
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Jul 13, 2019
commit 8caea32ce2c8f5577b4816945ba0add2eb4fc473
136 changes: 100 additions & 36 deletions aries_cloudagent/ledger/tests/test_indy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from aries_cloudagent.ledger.indy import (
IndyLedger,
GENESIS_TRANSACTION_PATH,
BadLedgerRequestError,
ClosedPoolError,
LedgerTransactionError,
DuplicateSchemaError,
Expand Down Expand Up @@ -80,19 +81,22 @@ async def test_submit_signed(
mock_set_proto,
):

mock_did = async_mock.MagicMock()

future = asyncio.Future()
future.set_result(mock_did)

mock_sign_submit.return_value = '{"op": "REPLY"}'

mock_wallet = async_mock.MagicMock()
mock_wallet.get_public_did.return_value = future

ledger = IndyLedger("name", mock_wallet, "genesis_transactions")

async with ledger:
mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_wallet.get_public_did.return_value = None

with self.assertRaises(BadLedgerRequestError):
await ledger._submit("{}", True)

mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_did = mock_wallet.get_public_did.return_value

await ledger._submit("{}", True)

mock_wallet.get_public_did.assert_called_once_with()
Expand Down Expand Up @@ -130,7 +134,7 @@ async def test_submit_unsigned(
async with ledger:
await ledger._submit("{}", False)

mock_wallet.get_public_did.assert_called_once_with()
mock_wallet.get_public_did.assert_not_called()

mock_submit.assert_called_once_with(ledger.pool_handle, "{}")

Expand Down Expand Up @@ -177,8 +181,8 @@ async def test_submit_rejected(
await ledger._submit("{}", False)
assert "Ledger rejected transaction request" in str(context.exception)

@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.__aenter__")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.__aexit__")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_open")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_close")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._submit")
@async_mock.patch("indy.anoncreds.issuer_create_schema")
@async_mock.patch("indy.ledger.build_schema_request")
Expand All @@ -187,17 +191,27 @@ async def test_send_schema(
mock_build_schema_req,
mock_create_schema,
mock_submit,
mock_aexit,
mock_aenter,
mock_close,
mock_open,
):
mock_did = async_mock.MagicMock()

mock_wallet = async_mock.MagicMock()
mock_wallet.get_public_did.return_value = mock_did

ledger = IndyLedger("name", mock_wallet, "genesis_transactions")

mock_create_schema.return_value = ("schema_id", "{}")

async with ledger:
mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_wallet.get_public_did.return_value = None

with self.assertRaises(BadLedgerRequestError):
schema_id = await ledger.send_schema(
"schema_name", "schema_version", [1, 2, 3]
)

mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_did = mock_wallet.get_public_did.return_value

schema_id = await ledger.send_schema(
"schema_name", "schema_version", [1, 2, 3]
)
Expand Down Expand Up @@ -253,8 +267,8 @@ async def test_send_schema_already_exists(
== f"{mock_wallet.get_public_did.return_value.did}:{2}:schema_name:schema_version"
)

@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.__aenter__")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.__aexit__")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_open")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_close")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._submit")
@async_mock.patch("indy.anoncreds.issuer_create_schema")
@async_mock.patch("indy.ledger.build_get_schema_request")
Expand All @@ -265,13 +279,12 @@ async def test_get_schema(
mock_build_get_schema_req,
mock_create_schema,
mock_submit,
mock_aexit,
mock_aenter,
mock_close,
mock_open,
):
mock_did = async_mock.MagicMock()

mock_wallet = async_mock.MagicMock()
mock_wallet.get_public_did.return_value = mock_did
mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_did = mock_wallet.get_public_did.return_value

mock_parse_get_schema_req.return_value = (None, "{}")

Expand All @@ -282,39 +295,90 @@ async def test_get_schema(

mock_wallet.get_public_did.assert_called_once_with()
mock_build_get_schema_req.assert_called_once_with(mock_did.did, "schema_id")
mock_submit.assert_called_once_with(mock_build_get_schema_req.return_value)
mock_submit.assert_called_once_with(
mock_build_get_schema_req.return_value, sign=True
)
mock_parse_get_schema_req.assert_called_once_with(mock_submit.return_value)

assert response == json.loads(mock_parse_get_schema_req.return_value[1])

@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.get_schema")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.__aenter__")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger.__aexit__")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_open")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_close")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._submit")
@async_mock.patch("indy.anoncreds.issuer_create_schema")
@async_mock.patch("indy.ledger.build_schema_request")
@async_mock.patch("indy.anoncreds.issuer_create_and_store_credential_def")
@async_mock.patch("indy.ledger.build_cred_def_request")
async def test_send_credential_definition(
self,
mock_build_schema_req,
mock_create_schema,
mock_build_cred_def,
mock_create_store_cred_def,
mock_submit,
mock_aexit,
mock_aenter,
mock_close,
mock_open,
mock_get_schema,
):
mock_did = async_mock.MagicMock()

mock_wallet = async_mock.MagicMock()
mock_wallet.get_public_did.return_value = mock_did

mock_get_schema.return_value = "{}"
cred_id = "cred_id"
cred_json = "[]"
mock_create_store_cred_def.return_value = (cred_id, cred_json)

ledger = IndyLedger("name", mock_wallet, "genesis_transactions")

schema_id = "schema_id"
tag = "tag"

async with ledger:

mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_wallet.get_public_did.return_value = None

with self.assertRaises(BadLedgerRequestError):
await ledger.send_credential_definition(schema_id, tag)

mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_did = mock_wallet.get_public_did.return_value

result_id = await ledger.send_credential_definition(schema_id, tag)
assert result_id == cred_id

mock_wallet.get_public_did.assert_called_once_with()
mock_get_schema.assert_called_once_with(schema_id)

mock_build_cred_def.assert_called_once_with(mock_did.did, cred_json)

@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_open")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._context_close")
@async_mock.patch("aries_cloudagent.ledger.indy.IndyLedger._submit")
@async_mock.patch("indy.anoncreds.issuer_create_schema")
@async_mock.patch("indy.ledger.build_get_cred_def_request")
@async_mock.patch("indy.ledger.parse_get_cred_def_response")
async def test_get_credential_definition(
self,
mock_parse_get_cred_def_req,
mock_build_get_cred_def_req,
mock_create_schema,
mock_submit,
mock_close,
mock_open,
):
mock_wallet = async_mock.MagicMock()
mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_did = mock_wallet.get_public_did.return_value

mock_parse_get_cred_def_req.return_value = (None, "{}")

ledger = IndyLedger("name", mock_wallet, "genesis_transactions")

async with ledger:
credential_definition_id = await ledger.get_schema("schema_id", "tag")
response = await ledger.get_credential_definition("cred_def_id")

mock_wallet.get_public_did.assert_called_once_with()
mock_get_schema.assert_called_once_with(
mock_wallet.get_public_did.return_value
mock_build_get_cred_def_req.assert_called_once_with(mock_did.did, "cred_def_id")
mock_submit.assert_called_once_with(
mock_build_get_cred_def_req.return_value, sign=True
)
mock_parse_get_cred_def_req.assert_called_once_with(mock_submit.return_value)

assert response == json.loads(mock_parse_get_cred_def_req.return_value[1])