Skip to content

Commit

Permalink
fix amazon fps tests; make them easy to maintain
Browse files Browse the repository at this point in the history
fixed bitcoin gateway tests

fixed braintree tests; make it easier to debug

fixed google checkout tests

fixed worldpay tests; removed html fixtures

skip paylane tests if not configured
  • Loading branch information
tuxcanfly committed Apr 12, 2013
1 parent 0a8bdc4 commit 6a97caf
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 70 deletions.
1 change: 1 addition & 0 deletions billing/gateways/bitcoin_gateway.py
Expand Up @@ -51,6 +51,7 @@ def get_txns_sum(self, txns):

def purchase(self, money, address, options = None):
options = options or {}
money = Decimal(str(money))
txns = self.get_transactions_by_address(address)
received = self.get_txns_sum(txns)
response = [txn.__dict__ for txn in txns]
Expand Down
27 changes: 20 additions & 7 deletions billing/tests/amazon_fps_tests.py
@@ -1,25 +1,38 @@
import re
from urllib2 import urlparse

from django.test import TestCase
from billing import get_integration
from django.template import Template, Context
from django.conf import settings

from billing import get_integration


class AmazonFPSTestCase(TestCase):
urls = "billing.tests.test_urls"

def setUp(self):
self.fps = get_integration("amazon_fps")
fields = {
self.fields = {
"callerReference": "100",
"paymentReason": "Digital Download",
"pipelineName": "SingleUse",
"transactionAmount": 30,
"transactionAmount": '30',
"returnURL": "http://localhost/fps/fps-return-url/",
}
self.fps.add_fields(fields)
}
self.fps.add_fields(self.fields)

def testLinkGen(self):
tmpl = Template("{% load amazon_fps from amazon_fps_tags %}{% amazon_fps obj %}")
link = tmpl.render(Context({"obj": self.fps}))
pregen_link = """<a href="https://authorize.payments-sandbox.amazon.com/cobranded-ui/actions/start?callerKey=%(aws_access_key)s&callerReference=100&paymentReason=Digital%%20Download&pipelineName=SingleUse&returnURL=http%%3A%%2F%%2Flocalhost%%2Ffps%%2Ffps-return-url%%2F&signature=oSnkew7oCBPVk0IVZAjO87Ogsp4EO7jRlELaFwtqWzY%%3D&signatureMethod=HmacSHA256&signatureVersion=2&transactionAmount=30"><img src="http://g-ecx.images-amazon.com/images/G/01/cba/b/p3.gif" alt="Amazon Payments" /></a>""" % ({"aws_access_key": settings.MERCHANT_SETTINGS['amazon_fps']['AWS_ACCESS_KEY']})
self.assertEquals(pregen_link, link.strip())
# get the integration link url
url = re.search('href="(.*)">', link).groups()[0]
parsed = urlparse.urlparse(url)
query_dict = dict(urlparse.parse_qsl(parsed.query))

self.assertEquals(parsed.scheme, 'https')
self.assertEquals(parsed.netloc, 'authorize.payments-sandbox.amazon.com')
self.assertEquals(parsed.path, '/cobranded-ui/actions/start')

self.assertDictContainsSubset(self.fields, query_dict)
self.assertEquals(query_dict['callerKey'], settings.MERCHANT_SETTINGS['amazon_fps']['AWS_ACCESS_KEY'])
17 changes: 8 additions & 9 deletions billing/tests/bitcoin_tests.py
@@ -1,9 +1,8 @@
from django.test import TestCase
from billing import get_gateway
from billing.signals import *
from billing.models import EwayResponse
from billing.gateway import CardNotSupported
from billing.utils.credit_card import Visa
from billing.signals import transaction_was_successful, transaction_was_unsuccessful

TEST_AMOUNT = 0.01


class BitcoinGatewayTestCase(TestCase):
Expand All @@ -12,8 +11,8 @@ def setUp(self):
self.address = self.merchant.get_new_address()

def testPurchase(self):
self.merchant.connection.sendtoaddress(self.address, 1)
resp = self.merchant.purchase(1, self.address)
self.merchant.connection.sendtoaddress(self.address, TEST_AMOUNT)
resp = self.merchant.purchase(TEST_AMOUNT, self.address)
self.assertEquals(resp['status'], 'SUCCESS')

def testPaymentSuccessfulSignal(self):
Expand All @@ -24,8 +23,8 @@ def receive(sender, **kwargs):

transaction_was_successful.connect(receive)

self.merchant.connection.sendtoaddress(self.address, 1)
resp = self.merchant.purchase(1, self.address)
self.merchant.connection.sendtoaddress(self.address, TEST_AMOUNT)
self.merchant.purchase(TEST_AMOUNT, self.address)
self.assertEquals(received_signals, [transaction_was_successful])

def testPaymentUnSuccessfulSignal(self):
Expand All @@ -36,5 +35,5 @@ def receive(sender, **kwargs):

transaction_was_unsuccessful.connect(receive)

resp = self.merchant.purchase(0.01, self.address)
self.merchant.purchase(0.001, self.address)
self.assertEquals(received_signals, [transaction_was_unsuccessful])
56 changes: 34 additions & 22 deletions billing/tests/braintree_payments_tests.py
@@ -1,9 +1,11 @@
import braintree

from django.test import TestCase

from billing import get_gateway, CreditCard
from billing.signals import *
from billing.gateway import CardNotSupported, InvalidData
from billing.utils.credit_card import Visa
import braintree


class BraintreePaymentsGatewayTestCase(TestCase):
Expand All @@ -15,6 +17,16 @@ def setUp(self):
number="4111111111111111",
verification_value="100")

def assertBraintreeResponseSuccess(self, resp, msg=None):
if resp['status'] == "FAILURE":
standardMsg = resp['response'].message
self.fail(self._formatMessage(msg, standardMsg))
else:
self.assertEquals(resp['status'], "SUCCESS")

def assertBraintreeResponseFailure(self, resp, msg=None):
self.assertEquals(resp['status'], "FAILURE")

def testCardSupported(self):
self.credit_card.number = "5019222222222222"
self.assertRaises(CardNotSupported,
Expand All @@ -25,16 +37,16 @@ def testCardType(self):
self.assertEquals(self.credit_card.card_type, Visa)

def testPurchase(self):
resp = self.merchant.purchase(1, self.credit_card)
self.assertEquals(resp["status"], "SUCCESS")
resp = self.merchant.purchase(5, self.credit_card)
self.assertBraintreeResponseSuccess(resp)

def testFailedPurchase(self):
resp = self.merchant.purchase(2001, self.credit_card)
self.assertEquals(resp["status"], "FAILURE")
self.assertBraintreeResponseFailure(resp)

def testDeclinedPurchase(self):
resp = self.merchant.purchase(2900, self.credit_card)
self.assertEquals(resp["status"], "FAILURE")
self.assertBraintreeResponseFailure(resp)

def testPaymentSuccessfulSignal(self):
received_signals = []
Expand Down Expand Up @@ -68,9 +80,9 @@ def testCreditCardExpired(self):

def testAuthorizeAndCapture(self):
resp = self.merchant.authorize(100, self.credit_card)
self.assertEquals(resp["status"], "SUCCESS")
response = self.merchant.capture(50, resp["response"].transaction.id)
self.assertEquals(response["status"], "SUCCESS")
self.assertBraintreeResponseSuccess(resp)
resp = self.merchant.capture(50, resp["response"].transaction.id)
self.assertBraintreeResponseSuccess(resp)

# Need a way to test this. Requires delaying the status to either
# "settled" or "settling"
Expand All @@ -81,10 +93,10 @@ def testAuthorizeAndCapture(self):
# self.assertEquals(response["status"], "SUCCESS")

def testAuthorizeAndVoid(self):
resp = self.merchant.authorize(100, self.credit_card)
self.assertEquals(resp["status"], "SUCCESS")
response = self.merchant.void(resp["response"].transaction.id)
self.assertEquals(response["status"], "SUCCESS")
resp = self.merchant.authorize(105, self.credit_card)
self.assertBraintreeResponseSuccess(resp)
resp = self.merchant.void(resp["response"].transaction.id)
self.assertBraintreeResponseSuccess(resp)

def testStoreMissingCustomer(self):
self.assertRaises(InvalidData,
Expand All @@ -98,7 +110,7 @@ def testStoreWithoutBillingAddress(self):
},
}
resp = self.merchant.store(self.credit_card, options=options)
self.assertEquals(resp["status"], "SUCCESS")
self.assertBraintreeResponseSuccess(resp)
self.assertEquals(resp["response"].customer.credit_cards[0].expiration_date,
"%s/%s" % (self.credit_card.month,
self.credit_card.year))
Expand All @@ -123,7 +135,7 @@ def testStoreWithBillingAddress(self):
}
}
resp = self.merchant.store(self.credit_card, options=options)
self.assertEquals(resp["status"], "SUCCESS")
self.assertBraintreeResponseSuccess(resp)
self.assertTrue(getattr(resp["response"].customer.credit_cards[0], "billing_address"))
# The tests below don't seem to work.
# billing_address = resp["response"].customer.credit_cards[0].billing_address
Expand All @@ -141,9 +153,9 @@ def testUnstore(self):
},
}
resp = self.merchant.store(self.credit_card, options=options)
self.assertEquals(resp["status"], "SUCCESS")
response = self.merchant.unstore(resp["response"].customer.credit_cards[0].token)
self.assertEquals(response["status"], "SUCCESS")
self.assertBraintreeResponseSuccess(resp)
resp = self.merchant.unstore(resp["response"].customer.credit_cards[0].token)
self.assertBraintreeResponseSuccess(resp)

# The below tests require 'test_plan' to be created in the sandbox
# console panel. This cannot be created by API at the moment
Expand All @@ -158,7 +170,7 @@ def testRecurring1(self):
},
}
resp = self.merchant.recurring(10, self.credit_card, options=options)
self.assertEquals(resp["status"], "SUCCESS")
self.assertBraintreeResponseSuccess(resp)
subscription = resp["response"].subscription
self.assertEquals(subscription.status,
braintree.Subscription.Status.Active)
Expand All @@ -174,8 +186,8 @@ def testRecurring2(self):
"price": 15
},
}
resp = self.merchant.recurring(10, self.credit_card, options=options)
self.assertEquals(resp["status"], "SUCCESS")
resp = self.merchant.recurring(15, self.credit_card, options=options)
self.assertBraintreeResponseSuccess(resp)
subscription = resp["response"].subscription
self.assertEquals(subscription.price, 15)

Expand All @@ -192,7 +204,7 @@ def testRecurring3(self):
"number_of_billing_cycles": 12,
},
}
resp = self.merchant.recurring(10, self.credit_card, options=options)
self.assertEquals(resp["status"], "SUCCESS")
resp = self.merchant.recurring(20, self.credit_card, options=options)
self.assertBraintreeResponseSuccess(resp)
subscription = resp["response"].subscription
self.assertEquals(subscription.number_of_billing_cycles, 12)
34 changes: 20 additions & 14 deletions billing/tests/google_checkout_tests.py
@@ -1,10 +1,12 @@
import re

from xml.dom.minidom import Document, parseString

from django.conf import settings
from django.test import TestCase
from django.utils.html import strip_spaces_between_tags
from billing import get_integration
from django.template import Template, Context
from django.conf import settings

from xml.dom.minidom import Document, parseString
from billing import get_integration


class GoogleCheckoutTestCase(TestCase):
Expand All @@ -25,12 +27,20 @@ def setUp(self):
def testFormGen(self):
tmpl = Template("{% load google_checkout from google_checkout_tags %}{% google_checkout obj %}")
form = tmpl.render(Context({"obj": self.gc}))
pregen_form = """<form action="https://sandbox.google.com/checkout/api/checkout/v2/checkout/Merchant/%(mid)s" method="post"><input type="hidden" name="cart" value="PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48Y2hlY2tvdXQtc2hvcHBpbmctY2FydCB4bWxucz0iaHR0cDovL2NoZWNrb3V0Lmdvb2dsZS5jb20vc2NoZW1hLzIiPjxzaG9wcGluZy1jYXJ0PjxpdGVtcz48aXRlbT48aXRlbS1uYW1lPm5hbWUgb2YgdGhlIGl0ZW08L2l0ZW0tbmFtZT48aXRlbS1kZXNjcmlwdGlvbj5JdGVtIGRlc2NyaXB0aW9uPC9pdGVtLWRlc2NyaXB0aW9uPjx1bml0LXByaWNlIGN1cnJlbmN5PSJVU0QiPjE8L3VuaXQtcHJpY2U+PHF1YW50aXR5PjE8L3F1YW50aXR5PjxtZXJjaGFudC1pdGVtLWlkPjk5OUFYWjwvbWVyY2hhbnQtaXRlbS1pZD48L2l0ZW0+PC9pdGVtcz48bWVyY2hhbnQtcHJpdmF0ZS1kYXRhPjwvbWVyY2hhbnQtcHJpdmF0ZS1kYXRhPjwvc2hvcHBpbmctY2FydD48L2NoZWNrb3V0LXNob3BwaW5nLWNhcnQ+" /><input type="hidden" name="signature" value="E+hwrsOjpJYbfy3abEcyCynzurs=" /><input type="image" name="Google Checkout" alt="Fast checkout through Google" src="http://sandbox.google.com/checkout/buttons/checkout.gif?merchant_id=%(mid)s&amp;w=180&amp;h=46&amp;style=white&amp;variant=text&amp;loc=en_US" height="46" width="180" /></form>""" % ({"mid": settings.MERCHANT_SETTINGS['google_checkout']['MERCHANT_ID']})
self.assertEquals(pregen_form, strip_spaces_between_tags(form).strip())


form_action_url = re.search('form action="(.*)" method="post"', form).groups()[0]
input_image_src = re.search('<input type="image" .* src="(.*)" height', form).groups()[0]

expected_form_action_url = "https://sandbox.google.com/checkout/api/checkout/v2/checkout/Merchant/%s" % settings.MERCHANT_SETTINGS['google_checkout']['MERCHANT_ID']
expected_input_image_src = "http://sandbox.google.com/checkout/buttons/checkout.gif?merchant_id=%s&amp;w=180&amp;h=46&amp;style=white&amp;variant=text&amp;loc=en_US" % settings.MERCHANT_SETTINGS['google_checkout']['MERCHANT_ID']

self.assertEquals(form_action_url, expected_form_action_url)
self.assertEquals(input_image_src, expected_input_image_src)


def testBuildXML(self):
xml = self.gc.build_xml()
good_xml = """<?xml version="1.0" encoding="utf-8"?><checkout-shopping-cart xmlns="http://checkout.google.com/schema/2"><shopping-cart><items><item><item-name>name of the item</item-name><item-description>Item description</item-description><unit-price currency="USD">1</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id></item></items><merchant-private-data></merchant-private-data></shopping-cart><checkout-flow-support><merchant-checkout-flow-support><continue-shopping-url>http://127.0.0.1:8000/offsite/google-checkout/</continue-shopping-url></merchant-checkout-flow-support></checkout-flow-support></checkout-shopping-cart>"""
good_xml = """<?xml version="1.0" encoding="utf-8"?><checkout-shopping-cart xmlns="http://checkout.google.com/schema/2"><shopping-cart><items><item><item-name>name of the item</item-name><item-description>Item description</item-description><unit-price currency="USD">1</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id></item></items><merchant-private-data></merchant-private-data></shopping-cart><checkout-flow-support><merchant-checkout-flow-support><continue-shopping-url>http://127.0.0.1:8000/offsite/google-checkout/</continue-shopping-url></merchant-checkout-flow-support></checkout-flow-support></checkout-shopping-cart>"""
self.assertEquals(xml, good_xml)


Expand Down Expand Up @@ -180,7 +190,7 @@ def testFullCartXML(self):
self.gc.add_fields(fields)

xml = self.gc.build_xml()
good_xml = """<?xml version="1.0" encoding="utf-8"?><checkout-shopping-cart xmlns="http://checkout.google.com/schema/2"><shopping-cart><items><item><item-name>name of the item</item-name><item-description>Item description</item-description><unit-price currency="USD">1</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id></item></items><merchant-private-data></merchant-private-data></shopping-cart><checkout-flow-support><merchant-checkout-flow-support><continue-shopping-url>http://127.0.0.1:8000/offsite/google-checkout/</continue-shopping-url><shipping-methods><flat-rate-shipping name="UPS Next Day Air"><price currency="USD">20.0</price><shipping-restrictions><allow-us-po-box>false</allow-us-po-box><excluded-areas><us-state-area><state>AK</state></us-state-area><us-state-area><state>HI</state></us-state-area></excluded-areas></shipping-restrictions></flat-rate-shipping><flat-rate-shipping name="UPS Ground"><price currency="USD">15.0</price><shipping-restrictions><allow-us-po-box>false</allow-us-po-box></shipping-restrictions></flat-rate-shipping></shipping-methods></merchant-checkout-flow-support></checkout-flow-support></checkout-shopping-cart>"""
good_xml = """<?xml version="1.0" encoding="utf-8"?><checkout-shopping-cart xmlns="http://checkout.google.com/schema/2"><shopping-cart><items><item><item-name>name of the item</item-name><item-description>Item description</item-description><unit-price currency="USD">1</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id></item></items><merchant-private-data></merchant-private-data></shopping-cart><checkout-flow-support><merchant-checkout-flow-support><continue-shopping-url>http://127.0.0.1:8000/offsite/google-checkout/</continue-shopping-url><shipping-methods><flat-rate-shipping name="UPS Next Day Air"><price currency="USD">20.0</price><shipping-restrictions><allow-us-po-box>false</allow-us-po-box><excluded-areas><us-state-area><state>AK</state></us-state-area><us-state-area><state>HI</state></us-state-area></excluded-areas></shipping-restrictions></flat-rate-shipping><flat-rate-shipping name="UPS Ground"><price currency="USD">15.0</price><shipping-restrictions><allow-us-po-box>false</allow-us-po-box></shipping-restrictions></flat-rate-shipping></shipping-methods></merchant-checkout-flow-support></checkout-flow-support></checkout-shopping-cart>"""
self.assertEquals(xml, good_xml)


Expand Down Expand Up @@ -551,9 +561,5 @@ def testFullCartXML(self):
self.gc.add_fields(fields)

xml = self.gc.build_xml()
good_xml = """<?xml version="1.0" encoding="utf-8"?><checkout-shopping-cart xmlns="http://checkout.google.com/schema/2"><shopping-cart><items><item><item-name>name of the item</item-name><item-description>Item description</item-description><unit-price currency="USD">1</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id></item><item><item-name>tax free item</item-name><item-description>Item description</item-description><unit-price currency="USD">2</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id><tax-table-selector>tax_exempt</tax-table-selector></item></items><merchant-private-data></merchant-private-data></shopping-cart><checkout-flow-support><merchant-checkout-flow-support><continue-shopping-url>http://127.0.0.1:8000/offsite/google-checkout/</continue-shopping-url><tax-tables><default-tax-table><tax-rules><default-tax-rule><shipping-taxed>false</shipping-taxed><rate>0.08375</rate><tax-area><us-zip-area><zip-pattern>100*</zip-pattern></us-zip-area></tax-area></default-tax-rule><default-tax-rule><shipping-taxed>true</shipping-taxed><rate>0.04</rate><tax-area><us-state-area><state>NY</state></us-state-area></tax-area></default-tax-rule></tax-rules></default-tax-table><alternate-tax-tables><alternate-tax-table name="tax_exempt" standalone="true"><alternate-tax-rules/></alternate-tax-table></alternate-tax-tables></tax-tables></merchant-checkout-flow-support></checkout-flow-support></checkout-shopping-cart>"""
good_xml = """<?xml version="1.0" encoding="utf-8"?><checkout-shopping-cart xmlns="http://checkout.google.com/schema/2"><shopping-cart><items><item><item-name>name of the item</item-name><item-description>Item description</item-description><unit-price currency="USD">1</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id></item><item><item-name>tax free item</item-name><item-description>Item description</item-description><unit-price currency="USD">2</unit-price><quantity>1</quantity><merchant-item-id>999AXZ</merchant-item-id><tax-table-selector>tax_exempt</tax-table-selector></item></items><merchant-private-data></merchant-private-data></shopping-cart><checkout-flow-support><merchant-checkout-flow-support><continue-shopping-url>http://127.0.0.1:8000/offsite/google-checkout/</continue-shopping-url><tax-tables><default-tax-table><tax-rules><default-tax-rule><shipping-taxed>false</shipping-taxed><rate>0.08375</rate><tax-area><us-zip-area><zip-pattern>100*</zip-pattern></us-zip-area></tax-area></default-tax-rule><default-tax-rule><shipping-taxed>true</shipping-taxed><rate>0.04</rate><tax-area><us-state-area><state>NY</state></us-state-area></tax-area></default-tax-rule></tax-rules></default-tax-table><alternate-tax-tables><alternate-tax-table name="tax_exempt" standalone="true"><alternate-tax-rules/></alternate-tax-table></alternate-tax-tables></tax-tables></merchant-checkout-flow-support></checkout-flow-support></checkout-shopping-cart>"""
self.assertEquals(xml, good_xml)




0 comments on commit 6a97caf

Please sign in to comment.