From 8c38c0244f8c530bc7a4d67d7f70bb59af942a63 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 28 Apr 2020 17:41:06 +0200 Subject: prevent crashes when money should be transfered to the issuer --- geldschieberbot.py | 22 +++++++++++++++------- test.py | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/geldschieberbot.py b/geldschieberbot.py index c249dd2..44327e1 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -72,9 +72,11 @@ def record(recipient, donor, amount): """Apply changes to the balance""" # Only change anything if this is not a dry run - if not dry_run: - balance[donor][recipient] += amount - balance[recipient][donor] -= amount + if dry_run: + return + + balance[donor][recipient] += amount + balance[recipient][donor] -= amount def to_cent(euro): if '.' in euro: @@ -275,11 +277,14 @@ def split(sender, args, msg): output = f"Split {to_euro(amount)} between {persons} -> {to_euro(amount_per_person)} each\n" change = [args] for p in args[2:]: - if not p in name2num: - output += p + " not known. Please take care manually\n" + if p in name2num: + if p == recipient: + output += f"{p}, you will be charged multiple times. This may not be what you want\n" + else: + record(recipient, p, amount_per_person) + change.append([recipient, p, amount_per_person]) else: - record(recipient, p, amount_per_person) - change.append([recipient, p, amount_per_person]) + output += f"{p} not known. Please take care manually\n" if record_changes and not dry_run: changes[recipient].append(change) @@ -307,6 +312,9 @@ def transaction(sender, args, msg): else: return None, 'recipient not known' + if sender == recipient: + return None, 'you can not transfere money to or from yourself' + try: amount = to_cent(amount) except: diff --git a/test.py b/test.py index 94235e1..929605c 100755 --- a/test.py +++ b/test.py @@ -197,6 +197,13 @@ class TestTransactionCmd(unittest.TestCase): compare_state("test/state.json_transactions1") + def test_transactions_with_myself(self): + res = run_bot(self, num[alice], f"!schieb {alice} 1,1") + self.assertEqual(res.stdout, 'ERROR: you can not transfere money to or from yourself') + + res = run_bot(self, num[alice], f"!zieh {alice} 2.1") + self.assertEqual(res.stdout, 'ERROR: you can not transfere money to or from yourself') + class TestSumCmd(unittest.TestCase): def test_summary_single_user(self): reset_state("test/state.json_transactions1") @@ -297,6 +304,17 @@ foo not known. Please take care manually New Balance: alice: \t<- charlie 10.00 +\tBalance: 10.00""" + self.assertEqual(res.stdout, msg) + + def test_split_with_sender_in_user_list(self): + res = run_bot(self, num[alice], f"!split 30 {charlie} {alice}") + msg = \ +"""Split 30.00 between 3 -> 10.00 each +alice, you will be charged multiple times. This may not be what you want +New Balance: +alice: +\t<- charlie 10.00 \tBalance: 10.00""" self.assertEqual(res.stdout, msg) -- cgit v1.2.3