def test_on_chain_payment_method_redeem_errors(): """Test redeem_payment method errors in the on-chain payment decorator.""" test_dust = 100 test_price = 8888 test_bad_price = 8887 test_db = OnChainSQLite3(':memory:', db_dir='') requests = OnChain(test_wallet, test_db) # Test that a payment less than the dust limit cannot be made with pytest.raises(PaymentBelowDustLimitError): requests.redeem_payment(test_dust, {'Bitcoin-Transaction': None}) # Test that a payment can't be made with an invalid transaction format with pytest.raises(InvalidPaymentParameterError): requests.redeem_payment(test_price, {'Bitcoin-Transaction': None}) # Test that a payment can't be made to an address that isn't the merchant's txn = _build_void_transaction(test_price) with pytest.raises(InvalidPaymentParameterError): requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()}) # Test that a payment can't be made for an incorrect amount txn = _build_void_transaction(test_bad_price, test_wallet.get_payout_address()) with pytest.raises(InsufficientPaymentError): requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()}) # Test that a payment already in the database won't be accepted txn = _build_void_transaction(test_price, test_wallet.get_payout_address()) test_db.create(str(txn.hash), test_price) with pytest.raises(DuplicatePaymentError): requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()})
def test_on_chain_payment_method_redeem_success(monkeypatch): """Test success in redeem_payment.""" test_price = 8888 test_db = OnChainSQLite3(':memory:', db_dir='') requests = OnChain(test_wallet, test_db) monkeypatch.setattr(requests.provider, 'broadcast_transaction', _mock_broadcast_success) # Test that we can redeem a proper payment txn = _build_void_transaction(test_price, test_wallet.get_payout_address()) requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()}) db_txn = test_db.lookup(str(txn.hash)) assert db_txn['txid'] == str(txn.hash) assert db_txn['amount'] == test_price # Test that we cannot re-use the same payment with pytest.raises(DuplicatePaymentError): requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()})
def test_on_chain_payment_method_redeem_broadcast(monkeypatch): """Test broadcast functionality in redeem_payment.""" test_price = 8888 test_db = OnChainSQLite3(':memory:', db_dir='') requests = OnChain(test_wallet, test_db) monkeypatch.setattr(requests.provider, 'broadcast_transaction', _mock_broadcast_failure) # Test that errors encountered during broadcast propagate with pytest.raises(TransactionBroadcastError): txn = _build_void_transaction(test_price, test_wallet.get_payout_address()) requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()}) # Test that the failed transaction doesn't persist in the database db_txn = test_db.lookup(str(txn.hash)) assert db_txn is None # Test that we can still use the same payment even after a broadcast error monkeypatch.setattr(requests.provider, 'broadcast_transaction', _mock_broadcast_success) requests.redeem_payment(test_price, {'Bitcoin-Transaction': txn.to_hex()}) db_txn = test_db.lookup(str(txn.hash)) assert db_txn['txid'] == str(txn.hash) assert db_txn['amount'] == test_price