diff --git a/silkaj/membership.py b/silkaj/membership.py index ea3ea1a01e4d0e89f7022b01125cb8c94d5f458e..a9b63ffb7622ce8e3b3150ab8a225f5565d1f7ff 100644 --- a/silkaj/membership.py +++ b/silkaj/membership.py @@ -25,13 +25,12 @@ from tabulate import tabulate from duniterpy.api import bma from duniterpy.documents import BlockUID, block_uid, Membership -from silkaj import auth, wot +from silkaj import auth, wot, tui from silkaj.tools import coroutine from silkaj.network_tools import ClientInstance from silkaj.blockchain_tools import BlockchainParams, HeadBlock from silkaj.license import license_approval from silkaj.constants import SUCCESS_EXIT_STATUS -from silkaj.tui import display_pubkey_and_checksum @click.command( @@ -39,14 +38,10 @@ from silkaj.tui import display_pubkey_and_checksum help="Send and sign membership document: \n\ for first emission and for renewal", ) -@click.option( - "--dry-run", - is_flag=True, - help="By-pass licence, confirmation. \ -Do not send the document, but display it instead", -) +@click.pass_context @coroutine -async def send_membership(dry_run): +async def send_membership(ctx): + dry_run = ctx.obj["DRY_RUN"] # Authentication key = auth.auth_method() @@ -66,12 +61,8 @@ async def send_membership(dry_run): # Confirmation client = ClientInstance().client await display_confirmation_table(identity_uid, key.pubkey, identity_timestamp) - if not dry_run: - if not click.confirm( - "Do you confirm sending a membership document for this identity?" - ): - await client.close() - sys.exit(SUCCESS_EXIT_STATUS) + if not dry_run and not ctx.obj["DISPLAY_DOCUMENT"]: + await tui.send_doc_confirmation("membership document for this identity") membership = generate_membership_document( currency, @@ -87,10 +78,14 @@ async def send_membership(dry_run): logging.debug(membership.signed_raw()) if dry_run: - await client.close() click.echo(membership.signed_raw()) + await client.close() sys.exit(SUCCESS_EXIT_STATUS) + if ctx.obj["DISPLAY_DOCUMENT"]: + click.echo(membership.signed_raw()) + await tui.send_doc_confirmation("membership document for this identity") + # Send the membership signed raw document to the node response = await client(bma.blockchain.membership, membership.signed_raw()) @@ -137,7 +132,7 @@ async def display_confirmation_table(identity_uid, pubkey, identity_timestamp): table.append(["Pending membership documents will expire", expiration]) table.append(["User Identifier (UID)", identity_uid]) - table.append(["Public Key", display_pubkey_and_checksum(pubkey)]) + table.append(["Public Key", tui.display_pubkey_and_checksum(pubkey)]) table.append(["Block Identity", str(identity_timestamp)[:45] + "…"]) diff --git a/tests/test_membership.py b/tests/test_membership.py index 7ff1eb56fc6398b23c7815198c63e9df54e130f0..359c738b31da6a950fad7a42cb6520224b12fa97 100644 --- a/tests/test_membership.py +++ b/tests/test_membership.py @@ -79,14 +79,16 @@ async def patched_choose_identity(pubkey): @pytest.mark.parametrize( - "dry_run, confirmation, exit_code", + "dry_run, display, confirmation, exit_code", [ - (True, True, SUCCESS_EXIT_STATUS), - (False, False, SUCCESS_EXIT_STATUS), - (False, True, FAILURE_EXIT_STATUS), + (True, True, False, FAILURE_EXIT_STATUS), + (True, False, False, SUCCESS_EXIT_STATUS), + (False, True, False, SUCCESS_EXIT_STATUS), + (False, True, True, FAILURE_EXIT_STATUS), + (False, False, True, FAILURE_EXIT_STATUS), ], ) -def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): +def test_membership_cmd(dry_run, display, confirmation, exit_code, monkeypatch): # Monkeypatch and Mock monkeypatch.setattr(auth, "auth_method", patched_auth_method) monkeypatch.setattr(HeadBlock, "get_head", patched_head_block) @@ -98,7 +100,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): "display_confirmation_table", patched_display_confirmation_table, ) - if not dry_run: + if not dry_run and not display: patched_generate_membership_document = Mock() monkeypatch.setattr( membership, @@ -107,28 +109,39 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): ) # Run membership command - command = ["membership"] + command = [] if dry_run: command += ["--dry-run"] - confirmations = "No\nYes\nYes" if confirmation else "No\nYes\nNo" + if display: + command += ["--display"] + command += ["membership"] + pass_license = "No\nYes\n" + confirmations = pass_license + ("Yes" if confirmation else "No") result = CliRunner().invoke(cli, args=command, input=confirmations) - # Assert functions are called - patched_display_confirmation_table.assert_awaited_once_with( - identity_uid, - pubkey, - identity_timestamp, - ) - if not dry_run and confirmation: - patched_generate_membership_document.assert_called_with( - currency, - pubkey, - membership_timestamp, + if dry_run and display: + assert ( + "ERROR: display and dry-run options can not be used together\n" + == result.output + ) + + else: + # Assert functions are called + patched_display_confirmation_table.assert_awaited_once_with( identity_uid, + pubkey, identity_timestamp, ) - if dry_run: - assert "Type: Membership" in result.output + if dry_run or display: + assert "Type: Membership" in result.output + else: + patched_generate_membership_document.assert_called_with( + currency, + pubkey, + membership_timestamp, + identity_uid, + identity_timestamp, + ) assert result.exit_code == exit_code