Skip to content
Snippets Groups Projects
Commit 7a97d2cf authored by matograine's avatar matograine
Browse files

[fix] #342 : balance command sums up many times the same pubkey

    -> return error in that case.
    -> displays an error for each wrong pubkey
* write tests for errors on `balance` command
parent 5c40d079
No related branches found
No related tags found
No related merge requests found
Pipeline #10217 passed
......@@ -25,7 +25,11 @@ from silkaj.auth import auth_method, has_auth_method
# had to import wot to prevent loop dependency. No use here.
from silkaj import wot
from silkaj.crypto_tools import check_pubkey_format, validate_checksum
from silkaj.crypto_tools import (
is_pubkey_and_check,
check_pubkey_format,
validate_checksum,
)
from silkaj.tui import display_amount, display_pubkey_and_checksum
from duniterpy.api.bma import tx, blockchain
......@@ -39,21 +43,34 @@ from duniterpy.documents.transaction import InputSource
async def cmd_amount(ctx, pubkeys):
client = ClientInstance().client
if not has_auth_method():
# check input pubkeys
if not pubkeys:
message_exit("You should specify one or many pubkeys")
pubkey_list = list()
for pubkey in pubkeys:
if check_pubkey_format(pubkey):
pubkey_list.append(validate_checksum(pubkey))
else:
pubkey_list.append(pubkey)
pubkeys_list = list()
wrong_pubkeys = False
for inputPubkey in pubkeys:
pubkey = is_pubkey_and_check(inputPubkey)
if not pubkey:
wrong_pubkeys = True
print("ERROR : pubkey {0} has a wrong format".format(inputPubkey))
elif pubkey in pubkeys_list:
message_exit(
"ERROR : pubkey {0} was specified many times".format(
display_pubkey_and_checksum(pubkey)
)
)
pubkeys_list.append(pubkey)
if wrong_pubkeys:
message_exit("Please check the pubkeys format.")
total = [0, 0]
for pubkey in pubkey_list:
for pubkey in pubkeys_list:
inputs_balance = await get_amount_from_pubkey(pubkey)
await show_amount_from_pubkey(pubkey, inputs_balance)
total[0] += inputs_balance[0]
total[1] += inputs_balance[1]
if len(pubkey_list) > 1:
if len(pubkeys_list) > 1:
await show_amount_from_pubkey("Total", total)
else:
key = auth_method()
......
"""
Copyright 2016-2020 Maël Azimi <m.a@moul.re>
Silkaj is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Silkaj is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
"""
import pytest
from click.testing import CliRunner
# had to import from wot to prevent loop dependencies
from silkaj.wot import display_pubkey_and_checksum
from silkaj.cli import cli
from silkaj.constants import FAILURE_EXIT_STATUS
def test_balance_errors():
"""
test balance command errors
"""
# twice the same pubkey
result = CliRunner().invoke(
cli,
[
"balance",
"BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
"BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
],
)
assert (
"ERROR : pubkey {0} was specified many times".format(
display_pubkey_and_checksum("BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh")
)
in result.output
)
assert result.exit_code == FAILURE_EXIT_STATUS
# wrong pubkey
result = CliRunner().invoke(
cli,
[
"balance",
"B",
],
)
assert "ERROR : pubkey {0} has a wrong format".format("B") in result.output
assert result.exit_code == FAILURE_EXIT_STATUS
# no pubkey
result = CliRunner().invoke(cli, ["balance"])
assert "You should specify one or many pubkeys" in result.output
assert result.exit_code == FAILURE_EXIT_STATUS
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