Skip to content
Snippets Groups Projects
Commit b8872db0 authored by Moul's avatar Moul
Browse files

[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 send_doc_confirmation()
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
parent 96a82fc5
No related branches found
No related tags found
No related merge requests found
...@@ -25,13 +25,12 @@ from tabulate import tabulate ...@@ -25,13 +25,12 @@ from tabulate import tabulate
from duniterpy.api import bma from duniterpy.api import bma
from duniterpy.documents import BlockUID, block_uid, Membership 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.tools import coroutine
from silkaj.network_tools import ClientInstance from silkaj.network_tools import ClientInstance
from silkaj.blockchain_tools import BlockchainParams, HeadBlock from silkaj.blockchain_tools import BlockchainParams, HeadBlock
from silkaj.license import license_approval from silkaj.license import license_approval
from silkaj.constants import SUCCESS_EXIT_STATUS from silkaj.constants import SUCCESS_EXIT_STATUS
from silkaj.tui import display_pubkey_and_checksum
@click.command( @click.command(
...@@ -39,14 +38,9 @@ from silkaj.tui import display_pubkey_and_checksum ...@@ -39,14 +38,9 @@ from silkaj.tui import display_pubkey_and_checksum
help="Send and sign membership document: \n\ help="Send and sign membership document: \n\
for first emission and for renewal", for first emission and for renewal",
) )
@click.option( @click.pass_context
"--dry-run",
is_flag=True,
help="By-pass licence, confirmation. \
Do not send the document, but display it instead",
)
@coroutine @coroutine
async def send_membership(dry_run): async def send_membership(ctx):
# Authentication # Authentication
key = auth.auth_method() key = auth.auth_method()
...@@ -60,18 +54,13 @@ async def send_membership(dry_run): ...@@ -60,18 +54,13 @@ async def send_membership(dry_run):
# Display license and ask for confirmation # Display license and ask for confirmation
currency = head_block["currency"] currency = head_block["currency"]
if not dry_run: license_approval(currency)
license_approval(currency)
# Confirmation # Confirmation
client = ClientInstance().client client = ClientInstance().client
await display_confirmation_table(identity_uid, key.pubkey, identity_timestamp) await display_confirmation_table(identity_uid, key.pubkey, identity_timestamp)
if not dry_run: if not ctx.obj["DISPLAY_DOCUMENT"]:
if not click.confirm( await tui.send_doc_confirmation("membership document for this identity")
"Do you confirm sending a membership document for this identity?"
):
await client.close()
sys.exit(SUCCESS_EXIT_STATUS)
membership = generate_membership_document( membership = generate_membership_document(
currency, currency,
...@@ -86,10 +75,9 @@ async def send_membership(dry_run): ...@@ -86,10 +75,9 @@ async def send_membership(dry_run):
logging.debug(membership.signed_raw()) logging.debug(membership.signed_raw())
if dry_run: if ctx.obj["DISPLAY_DOCUMENT"]:
await client.close()
click.echo(membership.signed_raw()) click.echo(membership.signed_raw())
sys.exit(SUCCESS_EXIT_STATUS) await tui.send_doc_confirmation("membership document for this identity")
# Send the membership signed raw document to the node # Send the membership signed raw document to the node
response = await client(bma.blockchain.membership, membership.signed_raw()) response = await client(bma.blockchain.membership, membership.signed_raw())
...@@ -137,7 +125,7 @@ async def display_confirmation_table(identity_uid, pubkey, identity_timestamp): ...@@ -137,7 +125,7 @@ async def display_confirmation_table(identity_uid, pubkey, identity_timestamp):
table.append(["Pending membership documents will expire", expiration]) table.append(["Pending membership documents will expire", expiration])
table.append(["User Identifier (UID)", identity_uid]) 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] + ""]) table.append(["Block Identity", str(identity_timestamp)[:45] + ""])
......
...@@ -79,14 +79,14 @@ async def patched_choose_identity(pubkey): ...@@ -79,14 +79,14 @@ async def patched_choose_identity(pubkey):
@pytest.mark.parametrize( @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, False, SUCCESS_EXIT_STATUS),
(False, True, FAILURE_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 and Mock
monkeypatch.setattr(auth, "auth_method", patched_auth_method) monkeypatch.setattr(auth, "auth_method", patched_auth_method)
monkeypatch.setattr(HeadBlock, "get_head", patched_head_block) monkeypatch.setattr(HeadBlock, "get_head", patched_head_block)
...@@ -98,7 +98,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): ...@@ -98,7 +98,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
"display_confirmation_table", "display_confirmation_table",
patched_display_confirmation_table, patched_display_confirmation_table,
) )
if not dry_run: if not display:
patched_generate_membership_document = Mock() patched_generate_membership_document = Mock()
monkeypatch.setattr( monkeypatch.setattr(
membership, membership,
...@@ -107,9 +107,8 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): ...@@ -107,9 +107,8 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
) )
# Run membership command # Run membership command
command = ["membership"] command = ["--display"] if display else []
if dry_run: command += ["membership"]
command += ["--dry-run"]
confirmations = "No\nYes\nYes" if confirmation else "No\nYes\nNo" confirmations = "No\nYes\nYes" if confirmation else "No\nYes\nNo"
result = CliRunner().invoke(cli, args=command, input=confirmations) result = CliRunner().invoke(cli, args=command, input=confirmations)
...@@ -119,7 +118,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): ...@@ -119,7 +118,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
pubkey, pubkey,
identity_timestamp, identity_timestamp,
) )
if not dry_run and confirmation: if not display and confirmation:
patched_generate_membership_document.assert_called_with( patched_generate_membership_document.assert_called_with(
currency, currency,
pubkey, pubkey,
...@@ -127,7 +126,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): ...@@ -127,7 +126,7 @@ def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
identity_uid, identity_uid,
identity_timestamp, identity_timestamp,
) )
if dry_run: if display:
assert "Type: Membership" in result.output assert "Type: Membership" in result.output
assert result.exit_code == exit_code assert result.exit_code == exit_code
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment