diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2020-04-28 17:41:06 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2020-04-28 17:41:06 +0200 |
| commit | 8c38c0244f8c530bc7a4d67d7f70bb59af942a63 (patch) | |
| tree | 608958c83029db242d6d546a3aee3ba8931355cf | |
| parent | 7d2b5de4552834628c68228e12a68c2e29e52791 (diff) | |
| download | geldschieberbot-8c38c0244f8c530bc7a4d67d7f70bb59af942a63.tar.gz geldschieberbot-8c38c0244f8c530bc7a4d67d7f70bb59af942a63.zip | |
prevent crashes when money should be transfered to the issuer
| -rw-r--r-- | geldschieberbot.py | 22 | ||||
| -rwxr-xr-x | 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: @@ -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") @@ -299,6 +306,17 @@ 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) def test_split(self): res = run_bot(self, num[alice], "!split 30 " + bob + " " + charlie) |
