Skip to content

Commit

Permalink
fixing up unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Aug 7, 2019
1 parent f86223b commit ba61eb6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 44 deletions.
14 changes: 7 additions & 7 deletions aries_cloudagent/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ def execute(argv: Sequence[str] = None):
from . import available_commands, load_command

parser = ArgumentParser()
parser.add_argument(
"-v",
"--version",
action="store_true",
help="print application version and exit",
)
subparsers = parser.add_subparsers()
for cmd in available_commands():
if cmd["name"] == "help":
parser.add_argument(
"-v",
"--version",
action="store_true",
help="print application version and exit",
)
continue
module = load_command(cmd["name"])
subparser = subparsers.add_parser(cmd["name"], help=cmd["summary"])
module.init_argument_parser(subparser)
args = parser.parse_args()
args = parser.parse_args(argv)
if args.version:
print(__version__)
else:
Expand Down
6 changes: 5 additions & 1 deletion aries_cloudagent/commands/tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

class TestHelp(AsyncTestCase):
def test_exec_help(self):
command.execute([])
with async_mock.patch.object(
command.ArgumentParser, "print_help"
) as print_help:
command.execute([])
print_help.assert_called_once()
12 changes: 4 additions & 8 deletions aries_cloudagent/commands/tests/test_provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@


class TestProvision(AsyncTestCase):
def test_bad_category(self):
with async_mock.patch.object(
command.ArgumentParser, "print_usage"
) as print_usage:
with self.assertRaises(SystemExit):
command.execute([])
print_usage.assert_called_once()
def test_bad_calls(self):
with self.assertRaises(command.ProvisionError):
command.execute([])

with self.assertRaises(SystemExit):
command.execute(["bad"])

@pytest.mark.indy
def test_provision_wallet(self):
test_seed = "testseed000000000000000000000001"
command.execute(["wallet", "--wallet-type", "indy", "--seed", test_seed])
command.execute(["--wallet-type", "indy", "--seed", test_seed])
5 changes: 4 additions & 1 deletion aries_cloudagent/config/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ class LedgerGroup(ArgumentGroup):
def add_arguments(self, parser: ArgumentParser):
"""Add ledger-specific command line arguments to the parser."""
parser.add_argument(
"--ledger-pool-name", type=str, metavar="<ledger-pool-name>", help="Specify the pool name"
"--ledger-pool-name",
type=str,
metavar="<ledger-pool-name>",
help="Specify the pool name",
)
parser.add_argument(
"--genesis-transactions",
Expand Down
17 changes: 12 additions & 5 deletions aries_cloudagent/config/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,25 @@ async def accept_taa(ledger: BaseLedger, taa_info, provision: bool = False) -> b
[
(
"wallet_agreement",
"Accept the transaction author agreement and store the acceptance in the wallet",
(
"Accept the transaction author agreement and store the "
+ "acceptance in the wallet"
),
),
(
"on_file",
"Acceptance of the transaction author agreement is on file in your organization",
(
"Acceptance of the transaction author agreement is on file "
+ "in my organization"
),
),
]
)
if not provision:
allow_opts[
"for_session"
] = "Accept the transaction author agreement for the duration of the current session"
allow_opts["for_session"] = (
"Accept the transaction author agreement for the duration of "
+ "the current session"
)

found = []
for opt in allow_opts:
Expand Down
61 changes: 39 additions & 22 deletions aries_cloudagent/ledger/tests/test_indy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,40 @@

@pytest.mark.indy
class TestIndyLedger(AsyncTestCase):
@async_mock.patch("indy.pool.create_pool_ledger_config")
@async_mock.patch("builtins.open")
def test_init(self, mock_open):
async def test_init(self, mock_open, mock_create_config):
mock_open.return_value = async_mock.MagicMock()

ledger = IndyLedger("name", "wallet", "genesis_transactions")
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
ledger = IndyLedger("name", mock_wallet)

assert ledger.pool_name == "name"
assert ledger.wallet is mock_wallet

assert ledger.name == "name"
assert ledger.wallet == "wallet"
await ledger.create_pool_config("genesis_transactions")

mock_open.assert_called_once_with(GENESIS_TRANSACTION_PATH, "w")
mock_open.return_value.__enter__.return_value.write.assert_called_once_with(
"genesis_transactions"
)
mock_create_config.assert_called_once_with(
"name", json.dumps({"genesis_txn": GENESIS_TRANSACTION_PATH})
)

@async_mock.patch("indy.pool.set_protocol_version")
@async_mock.patch("indy.pool.create_pool_ledger_config")
@async_mock.patch("indy.pool.open_pool_ledger")
@async_mock.patch("indy.pool.close_pool_ledger")
async def test_aenter_aexit(
self, mock_close_pool, mock_open_ledger, mock_create_config, mock_set_proto
self, mock_close_pool, mock_open_ledger, mock_set_proto
):
ledger = IndyLedger("name", "wallet", "genesis_transactions")
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
ledger = IndyLedger("name", mock_wallet)

async with ledger as l:
mock_set_proto.assert_called_once_with(2)
mock_create_config.assert_called_once_with(
"name", json.dumps({"genesis_txn": GENESIS_TRANSACTION_PATH})
)
mock_open_ledger.assert_called_once_with("name", "{}")
assert l == ledger
mock_close_pool.assert_not_called()
Expand All @@ -63,7 +69,9 @@ async def test_aenter_aexit(
async def test_submit_pool_closed(
self, mock_close_pool, mock_open_ledger, mock_create_config, mock_set_proto
):
ledger = IndyLedger("name", "wallet", "genesis_transactions")
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
ledger = IndyLedger("name", mock_wallet)

with self.assertRaises(ClosedPoolError) as context:
await ledger._submit("{}")
Expand All @@ -86,8 +94,9 @@ async def test_submit_signed(
mock_sign_submit.return_value = '{"op": "REPLY"}'

mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"

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

async with ledger:
mock_wallet.get_public_did = async_mock.CoroutineMock()
Expand Down Expand Up @@ -129,9 +138,10 @@ async def test_submit_unsigned(
mock_submit.return_value = '{"op": "REPLY"}'

mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
mock_wallet.get_public_did.return_value = future

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

async with ledger:
await ledger._submit("{}", False)
Expand Down Expand Up @@ -162,9 +172,10 @@ async def test_submit_rejected(
mock_submit.return_value = '{"op": "REQNACK", "reason": "a reason"}'

mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
mock_wallet.get_public_did.return_value = future

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

async with ledger:
with self.assertRaises(LedgerTransactionError) as context:
Expand All @@ -174,9 +185,10 @@ async def test_submit_rejected(
mock_submit.return_value = '{"op": "REJECT", "reason": "another reason"}'

mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
mock_wallet.get_public_did.return_value = future

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

async with ledger:
with self.assertRaises(LedgerTransactionError) as context:
Expand All @@ -197,8 +209,9 @@ async def test_send_schema(
mock_open,
):
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"

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

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

Expand Down Expand Up @@ -227,7 +240,7 @@ async def test_send_schema(
mock_did.did, mock_create_schema.return_value[1]
)

mock_submit.assert_called_once_with(mock_build_schema_req.return_value)
mock_submit.assert_called_once_with(mock_build_schema_req.return_value, True, True)

assert schema_id == mock_create_schema.return_value[0]

Expand All @@ -250,15 +263,16 @@ async def test_send_schema_already_exists(
):
# mock_did = async_mock.CoroutineMock()

mock_wallet = async_mock.CoroutineMock()
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
mock_wallet.get_public_did = async_mock.CoroutineMock()
mock_wallet.get_public_did.return_value.did = "abc"

mock_create_schema.return_value = (1, 2)

mock_submit.side_effect = DuplicateSchemaError

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

async with ledger:
schema_id = await ledger.send_schema(
Expand All @@ -285,12 +299,13 @@ async def test_get_schema(
mock_open,
):
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
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, "{}")

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

async with ledger:
response = await ledger.get_schema("schema_id")
Expand Down Expand Up @@ -320,13 +335,14 @@ async def test_send_credential_definition(
mock_get_schema,
):
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"

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")
ledger = IndyLedger("name", mock_wallet)

schema_id = "schema_id"
tag = "tag"
Expand Down Expand Up @@ -366,12 +382,13 @@ async def test_get_credential_definition(
mock_open,
):
mock_wallet = async_mock.MagicMock()
mock_wallet.WALLET_TYPE = "indy"
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")
ledger = IndyLedger("name", mock_wallet)

async with ledger:
response = await ledger.get_credential_definition("cred_def_id")
Expand Down

0 comments on commit ba61eb6

Please sign in to comment.