|
| 1 | +import pytest |
| 2 | + |
| 3 | +from backend.wallet.transaction import Transaction |
| 4 | +from backend.wallet.wallet import Wallet |
| 5 | + |
| 6 | +def test_transaction(): |
| 7 | + sender_wallet = Wallet() |
| 8 | + recipient = 'recipient' |
| 9 | + amount = 50 |
| 10 | + transaction = Transaction(sender_wallet, recipient, amount) |
| 11 | + |
| 12 | + assert transaction.output[recipient] == amount |
| 13 | + assert transaction.output[sender_wallet.address] == sender_wallet.balance - amount |
| 14 | + |
| 15 | + assert 'timestamp' in transaction.input |
| 16 | + assert transaction.input['amount'] == sender_wallet.balance |
| 17 | + assert transaction.input['address'] == sender_wallet.address |
| 18 | + assert transaction.input['public_key'] == sender_wallet.public_key |
| 19 | + assert Wallet.verify( |
| 20 | + transaction.input['public_key'], |
| 21 | + transaction.output, |
| 22 | + transaction.input['signature'] |
| 23 | + ) |
| 24 | + |
| 25 | +def test_transaction_exceeds_balance(): |
| 26 | + with pytest.raises(Exception, match='Amount exceeds balance'): |
| 27 | + Transaction(Wallet(), 'recipient', 9001) |
| 28 | + |
| 29 | +def test_transaction_update_exceeds_balance(): |
| 30 | + sender_wallet = Wallet() |
| 31 | + transaction = Transaction(sender_wallet, 'recipient', 50) |
| 32 | + |
| 33 | + with pytest.raises(Exception, match='Amount exceeds balance'): |
| 34 | + transaction.update(sender_wallet, 'new_recipient', 9001) |
| 35 | + |
| 36 | +def test_transaction_update(): |
| 37 | + sender_wallet = Wallet() |
| 38 | + first_recipient = 'first_recipient' |
| 39 | + first_amount = 50 |
| 40 | + transaction = Transaction(sender_wallet, first_recipient, first_amount) |
| 41 | + |
| 42 | + next_recipient = 'next_recipient' |
| 43 | + next_amount = 75 |
| 44 | + transaction.update(sender_wallet, next_recipient, next_amount) |
| 45 | + |
| 46 | + assert transaction.output[next_recipient] == next_amount |
| 47 | + assert transaction.output[sender_wallet.address] ==\ |
| 48 | + sender_wallet.balance - first_amount - next_amount |
| 49 | + assert Wallet.verify( |
| 50 | + transaction.input['public_key'], |
| 51 | + transaction.output, |
| 52 | + transaction.input['signature'] |
| 53 | + ) |
| 54 | + |
| 55 | + to_first_again_amount = 25 |
| 56 | + transaction.update(sender_wallet, first_recipient, to_first_again_amount) |
| 57 | + |
| 58 | + assert transaction.output[first_recipient] == \ |
| 59 | + first_amount + to_first_again_amount |
| 60 | + assert transaction.output[sender_wallet.address] ==\ |
| 61 | + sender_wallet.balance - first_amount - next_amount - to_first_again_amount |
| 62 | + assert Wallet.verify( |
| 63 | + transaction.input['public_key'], |
| 64 | + transaction.output, |
| 65 | + transaction.input['signature'] |
| 66 | + ) |
| 67 | + |
| 68 | +def test_valid_transaction(): |
| 69 | + Transaction.is_valid_transaction(Transaction(Wallet(), 'recipient', 50)) |
| 70 | + |
| 71 | +def test_valid_transaction_with_invalid_outputs(): |
| 72 | + sender_wallet = Wallet() |
| 73 | + transaction = Transaction(sender_wallet, 'recipient', 50) |
| 74 | + transaction.output[sender_wallet.address] = 9001 |
| 75 | + |
| 76 | + with pytest.raises(Exception, match='Invalid transaction output values'): |
| 77 | + Transaction.is_valid_transaction(transaction) |
| 78 | + |
| 79 | +def test_valid_transaction_with_invalid_signature(): |
| 80 | + transaction = Transaction(Wallet(), 'recipient', 50) |
| 81 | + transaction.input['signature'] = Wallet().sign(transaction.output) |
| 82 | + |
| 83 | + with pytest.raises(Exception, match='Invalid signature'): |
| 84 | + Transaction.is_valid_transaction(transaction) |
0 commit comments