Beispiel #1
0
def test_on_chain_payment_method_headers():
    """Test general header methods in the on-chain payment decorator."""
    test_price = 8888
    test_address = '100MY0000FAKE0000ADDRESS0000'
    test_db = OnChainSQLite3(':memory:', db_dir='')
    requests = OnChain(test_wallet, test_db)

    # Test that it returns a list of payment headers
    payment_headers = requests.payment_headers
    assert isinstance(payment_headers, list)
    assert OnChain.http_payment_data in payment_headers

    # Test that it returns a dict of 402 headers given a price and address
    http_402_headers = requests.get_402_headers(test_price, address=test_address)
    assert isinstance(http_402_headers, dict)
    assert http_402_headers[OnChain.http_402_price] == test_price
    assert http_402_headers[OnChain.http_402_address] == test_address

    # Test that it returns a dict of 402 headers given a price only
    http_402_headers = requests.get_402_headers(test_price)
    assert isinstance(http_402_headers, dict)
    assert http_402_headers[OnChain.http_402_price] == test_price
    assert http_402_headers[OnChain.http_402_address] == test_wallet.get_payout_address()
Beispiel #2
0
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()})
Beispiel #3
0
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
Beispiel #4
0
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()})