From 794ebb6b2f8955e31237eb39ad8c798c54d5f100 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sat, 13 Mar 2021 13:24:55 +0100
Subject: [PATCH] [mod] #314: membership: Convert dry-run to display option

Use global option
Add ability to send the doc after displaying the doc
Stop by-passing the license approval and the emission confirmation
Use same confirmation f()
Set one confirmation prompt after the table and the document displays
Adapt the tests

Change exit status since test_membership_cmd() is sending the doc
will be fixed in #379
---
 silkaj/membership.py     | 34 ++++++++++++++++------------------
 tests/test_membership.py | 17 ++++++++---------
 2 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/silkaj/membership.py b/silkaj/membership.py
index ea3ea1a0..2802024f 100644
--- a/silkaj/membership.py
+++ b/silkaj/membership.py
@@ -39,14 +39,9 @@ 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):
 
     # Authentication
     key = auth.auth_method()
@@ -60,18 +55,13 @@ async def send_membership(dry_run):
 
     # Display license and ask for confirmation
     currency = head_block["currency"]
-    if not dry_run:
-        license_approval(currency)
+    license_approval(currency)
 
     # 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 ctx.obj["DISPLAY_DOCUMENT"]:
+        await confirmation()
 
     membership = generate_membership_document(
         currency,
@@ -86,10 +76,9 @@ async def send_membership(dry_run):
 
     logging.debug(membership.signed_raw())
 
-    if dry_run:
-        await client.close()
+    if ctx.obj["DISPLAY_DOCUMENT"]:
         click.echo(membership.signed_raw())
-        sys.exit(SUCCESS_EXIT_STATUS)
+        await confirmation()
 
     # Send the membership signed raw document to the node
     response = await client(bma.blockchain.membership, membership.signed_raw())
@@ -102,6 +91,15 @@ async def send_membership(dry_run):
     await client.close()
 
 
+async def confirmation():
+    if not click.confirm(
+        "Do you confirm sending this membership document for this identity?"
+    ):
+        client = ClientInstance().client
+        await client.close()
+        sys.exit(SUCCESS_EXIT_STATUS)
+
+
 async def display_confirmation_table(identity_uid, pubkey, identity_timestamp):
     """
     Check whether there is pending memberships already in the mempool
diff --git a/tests/test_membership.py b/tests/test_membership.py
index 7ff1eb56..292b6f89 100644
--- a/tests/test_membership.py
+++ b/tests/test_membership.py
@@ -79,14 +79,14 @@ async def patched_choose_identity(pubkey):
 
 
 @pytest.mark.parametrize(
-    "dry_run, confirmation, exit_code",
+    "display, confirmation, exit_code",
     [
-        (True, True, SUCCESS_EXIT_STATUS),
+        (True, True, FAILURE_EXIT_STATUS),
         (False, False, SUCCESS_EXIT_STATUS),
         (False, True, FAILURE_EXIT_STATUS),
     ],
 )
-def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
+def test_membership_cmd(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 +98,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
         "display_confirmation_table",
         patched_display_confirmation_table,
     )
-    if not dry_run:
+    if not display:
         patched_generate_membership_document = Mock()
         monkeypatch.setattr(
             membership,
@@ -107,9 +107,8 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
         )
 
     # Run membership command
-    command = ["membership"]
-    if dry_run:
-        command += ["--dry-run"]
+    command = ["--display"] if display else []
+    command += ["membership"]
     confirmations = "No\nYes\nYes" if confirmation else "No\nYes\nNo"
     result = CliRunner().invoke(cli, args=command, input=confirmations)
 
@@ -119,7 +118,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
         pubkey,
         identity_timestamp,
     )
-    if not dry_run and confirmation:
+    if not display and confirmation:
         patched_generate_membership_document.assert_called_with(
             currency,
             pubkey,
@@ -127,7 +126,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
             identity_uid,
             identity_timestamp,
         )
-    if dry_run:
+    if display:
         assert "Type: Membership" in result.output
 
     assert result.exit_code == exit_code
-- 
GitLab