Permalink
Browse files

mock bitcoin RPC

  • Loading branch information...
1 parent 10cdaa3 commit 7dac49a496c90461c082ddae8e15ad07752a8b88 @tuxcanfly tuxcanfly committed Jun 10, 2014
Showing with 71 additions and 45 deletions.
  1. +29 −19 billing/tests/bitcoin_tests.py
  2. +42 −26 example/app/views.py
@@ -1,3 +1,8 @@
+import mock
+import decimal
+
+from bitcoinrpc.data import TransactionInfo
+
from django.conf import settings
from django.test import TestCase
from django.utils.unittest.case import skipIf
@@ -6,39 +11,44 @@
from billing.signals import transaction_was_successful, transaction_was_unsuccessful
-TEST_AMOUNT = 0.01
+TEST_AMOUNT = decimal.Decimal('0.01')
+TEST_ADDRESS = 'n2RL9NRRGvKNqovb14qacSfbz6zQBkzDbU'
+TEST_SUCCESSFUL_TXNS = [TransactionInfo(address=TEST_ADDRESS, amount=TEST_AMOUNT)]
@skipIf(not settings.MERCHANT_SETTINGS.get("bitcoin", None), "gateway not configured")
class BitcoinGatewayTestCase(TestCase):
+
def setUp(self):
- self.merchant = get_gateway("bitcoin")
- self.address = self.merchant.get_new_address()
+ with mock.patch('bitcoinrpc.connection.BitcoinConnection') as MockBitcoinConnection:
+ connection = MockBitcoinConnection()
+ connection.getnewaddress.return_value = TEST_ADDRESS
+ connection.listtransactions.return_value = TEST_SUCCESSFUL_TXNS
+ self.merchant = get_gateway("bitcoin")
+ self.address = self.merchant.get_new_address()
def testPurchase(self):
- self.merchant.connection.sendtoaddress(self.address, TEST_AMOUNT)
- resp = self.merchant.purchase(TEST_AMOUNT, self.address)
- self.assertEquals(resp['status'], 'SUCCESS')
+ resp = self.merchant.purchase(TEST_AMOUNT, self.address)
+ self.assertEquals(resp['status'], 'SUCCESS')
def testPaymentSuccessfulSignal(self):
- received_signals = []
+ received_signals = []
- def receive(sender, **kwargs):
- received_signals.append(kwargs.get("signal"))
+ def receive(sender, **kwargs):
+ received_signals.append(kwargs.get("signal"))
- transaction_was_successful.connect(receive)
+ transaction_was_successful.connect(receive)
- self.merchant.connection.sendtoaddress(self.address, TEST_AMOUNT)
- self.merchant.purchase(TEST_AMOUNT, self.address)
- self.assertEquals(received_signals, [transaction_was_successful])
+ self.merchant.purchase(TEST_AMOUNT, self.address)
+ self.assertEquals(received_signals, [transaction_was_successful])
def testPaymentUnSuccessfulSignal(self):
- received_signals = []
+ received_signals = []
- def receive(sender, **kwargs):
- received_signals.append(kwargs.get("signal"))
+ def receive(sender, **kwargs):
+ received_signals.append(kwargs.get("signal"))
- transaction_was_unsuccessful.connect(receive)
+ transaction_was_unsuccessful.connect(receive)
- self.merchant.purchase(0.001, self.address)
- self.assertEquals(received_signals, [transaction_was_unsuccessful])
+ self.merchant.purchase(TEST_AMOUNT/2, self.address)
+ self.assertEquals(received_signals, [transaction_was_unsuccessful])
View
@@ -1,4 +1,8 @@
+import mock
import datetime
+import decimal
+
+from bitcoinrpc.data import TransactionInfo
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
@@ -20,6 +24,10 @@
from app.conf import GATEWAY_INITIAL, INTEGRATION_INITIAL
+BTC_TEST_AMOUNT = decimal.Decimal('0.01')
+BTC_TEST_ADDRESS = 'n2RL9NRRGvKNqovb14qacSfbz6zQBkzDbU'
+BTC_TEST_SUCCESSFUL_TXNS = [TransactionInfo(address=BTC_TEST_ADDRESS, amount=BTC_TEST_AMOUNT)]
+
def render(request, template, template_vars={}):
return render_to_response(template, template_vars, RequestContext(request))
@@ -412,34 +420,42 @@ def offsite_eway_done(request):
def bitcoin(request):
- amount = 0.01
- bitcoin_obj = get_gateway("bitcoin")
- address = request.session.get("bitcoin_address", None)
- if not address:
- address = bitcoin_obj.get_new_address()
- request.session["bitcoin_address"] = address
- return render(request, "app/bitcoin.html", {
- "title": "Bitcoin",
- "amount": amount,
- "address": address,
- "settings": settings
- })
+ with mock.patch('bitcoinrpc.connection.BitcoinConnection') as MockBitcoinConnection:
+ connection = MockBitcoinConnection()
+ connection.getnewaddress.return_value = BTC_TEST_ADDRESS
+ connection.listtransactions.return_value = BTC_TEST_SUCCESSFUL_TXNS
+ amount = 0.01
+ bitcoin_obj = get_gateway("bitcoin")
+ address = request.session.get("bitcoin_address", None)
+ if not address:
+ address = bitcoin_obj.get_new_address()
+ request.session["bitcoin_address"] = address
+ return render(request, "app/bitcoin.html", {
+ "title": "Bitcoin",
+ "amount": amount,
+ "address": address,
+ "settings": settings
+ })
def bitcoin_done(request):
- amount = 0.01
- bitcoin_obj = get_gateway("bitcoin")
- address = request.session.get("bitcoin_address", None)
- if not address:
- return HttpResponseRedirect(reverse("app_bitcoin"))
- result = bitcoin_obj.purchase(amount, address)
- if result['status'] == 'SUCCESS':
- del request.session["bitcoin_address"]
- return render(request, "app/bitcoin_done.html", {
- "title": "Bitcoin",
- "amount": amount,
- "address": address,
- "result": result
- })
+ with mock.patch('bitcoinrpc.connection.BitcoinConnection') as MockBitcoinConnection:
+ connection = MockBitcoinConnection()
+ connection.getnewaddress.return_value = BTC_TEST_ADDRESS
+ connection.listtransactions.return_value = BTC_TEST_SUCCESSFUL_TXNS
+ amount = 0.01
+ bitcoin_obj = get_gateway("bitcoin")
+ address = request.session.get("bitcoin_address", None)
+ if not address:
+ return HttpResponseRedirect(reverse("app_bitcoin"))
+ result = bitcoin_obj.purchase(amount, address)
+ if result['status'] == 'SUCCESS':
+ del request.session["bitcoin_address"]
+ return render(request, "app/bitcoin_done.html", {
+ "title": "Bitcoin",
+ "amount": amount,
+ "address": address,
+ "result": result
+ })
def offsite_ogone(request):

0 comments on commit 7dac49a

Please sign in to comment.