From d5044240fcc005a03e16ef8e342e4b76272a227c Mon Sep 17 00:00:00 2001
From: matograine <tom.ngr@zaclys.net>
Date: Thu, 24 Sep 2020 18:36:30 +0200
Subject: [PATCH] [fix] #342 : balance command sums up many times the same
 pubkey     -> return error in that case.

* write tests for errors on `balance` command
---
 silkaj/money.py     |  6 ++++++
 tests/test_money.py | 52 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
 create mode 100644 tests/test_money.py

diff --git a/silkaj/money.py b/silkaj/money.py
index 3734ce5e..2828411a 100644
--- a/silkaj/money.py
+++ b/silkaj/money.py
@@ -49,6 +49,12 @@ async def cmd_amount(ctx, pubkeys):
         checked_pubkeys = list()
         for pubkey in pubkeys:
             pubkey = check_public_key(pubkey, True)
+            if pubkey in checked_pubkeys:
+                message_exit(
+                    "Error : pubkey {0} was specified many times".format(
+                        pubkey_with_checksum(pubkey)
+                    )
+                )
             checked_pubkeys.append(pubkey)
             if not pubkey:
                 return
diff --git a/tests/test_money.py b/tests/test_money.py
new file mode 100644
index 00000000..969a3a68
--- /dev/null
+++ b/tests/test_money.py
@@ -0,0 +1,52 @@
+"""
+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 pubkey_with_checksum
+from silkaj.cli import cli
+from silkaj.constants import FAILURE_EXIT_STATUS
+
+
+def test_balance_errors():
+    """
+    test balance command errors
+    """
+
+    # twice the samepubkey
+    result = CliRunner().invoke(
+        cli,
+        [
+            "balance",
+            "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
+            "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
+        ],
+    )
+    assert (
+        "Error : pubkey {0} was specified many times".format(
+            pubkey_with_checksum("BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh")
+        )
+        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
-- 
GitLab