-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
148 lines (133 loc) · 5.24 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import datetime
import textwrap
import unittest
from securepay.securepay import UTCTimezone, _pay_by_cc_xml, _refund_xml
class PaymentTestCase(unittest.TestCase):
def test_payment_xml_matches_example(self):
"""Check that the credit card XML output looks correct."""
# TODO: Given that XML doesn't care about spacing, our test shouldn't
# either.
xml = _pay_by_cc_xml(
timestamp=datetime.datetime(2012, 1, 1, tzinfo=UTCTimezone()),
cents='100',
purchase_order_id='1234',
cc_number='4444333322221111',
cc_expiry='11/22',
merchant_id='MERCHANT ID',
password='PASSWORD',
cc_holder='Test Person')
self.assertEqual(
xml,
textwrap.dedent("""\
<?xml version='1.0' encoding='UTF-8'?>
<SecurePayMessage>
<MessageInfo>
<messageID/>
<messageTimestamp>20120101000000000000+000</messageTimestamp>
<timeoutValue>60</timeoutValue>
<apiVersion>xml-4.2</apiVersion>
</MessageInfo>
<MerchantInfo>
<merchantID>MERCHANT ID</merchantID>
<password>PASSWORD</password>
</MerchantInfo>
<RequestType>Payment</RequestType>
<Payment>
<TxnList count="1">
<Txn ID="1">
<txnType>0</txnType>
<txnSource>23</txnSource>
<amount>100</amount>
<currency>AUD</currency>
<recurring>no</recurring>
<purchaseOrderNo>1234</purchaseOrderNo>
<CreditCardInfo>
<cardNumber>4444333322221111</cardNumber>
<expiryDate>11/22</expiryDate>
<cardHolderName>Test Person</cardHolderName>
</CreditCardInfo>
</Txn>
</TxnList>
</Payment>
</SecurePayMessage>
""").encode('utf-8'))
def test_removes_non_digits_from_cc(self):
"""Check non-digits are stripped from the credit card number."""
xml = _pay_by_cc_xml(
timestamp=datetime.datetime(2012, 1, 1, tzinfo=UTCTimezone()),
cents='100',
purchase_order_id='1234',
cc_number='4444 3333x2222-1111',
cc_expiry='11/22',
merchant_id='MERCHANT ID',
password='PASSWORD')
self.assertIn(b'<cardNumber>4444333322221111</cardNumber>', xml)
def test_xml_generation_succeeds_when_cc_holder_has_unicode_symbol(self):
"""Check XML is generated when cc_holder contains unicode symbols."""
xml = _pay_by_cc_xml(
timestamp=datetime.datetime(2012, 1, 1, tzinfo=UTCTimezone()),
cents='',
purchase_order_id='',
cc_number='',
cc_expiry='',
merchant_id='',
password='',
cc_holder='♥')
self.assertIn(b'<cardHolderName>\xe2\x99\xa5</cardHolderName>', xml)
def test_recurring_field_set(self):
"""Check that a recurring=True comes through as 'yes'."""
xml = _pay_by_cc_xml(
timestamp=datetime.datetime(2012, 1, 1, tzinfo=UTCTimezone()),
cents='100',
purchase_order_id='1234',
cc_number='4444333322221111',
cc_expiry='11/22',
merchant_id='MERCHANT ID',
password='PASSWORD',
cc_holder='Test Person',
recurring=True)
self.assertIn(b'<recurring>yes</recurring>', xml)
class RefundTestCase(unittest.TestCase):
def test_refund_xml_matches_example(self):
"""Check that the XML output looks correct."""
# TODO: Given that XML doesn't care about spacing, our test shouldn't
# either.
xml = _refund_xml(
timestamp=datetime.datetime(2012, 1, 1, tzinfo=UTCTimezone()),
cents='100',
purchase_order_id='1234',
transaction_id=4321,
merchant_id='MERCHANT ID',
password='PASSWORD',
)
self.assertEqual(
xml,
textwrap.dedent("""\
<?xml version='1.0' encoding='UTF-8'?>
<SecurePayMessage>
<MessageInfo>
<messageID/>
<messageTimestamp>20120101000000000000+000</messageTimestamp>
<timeoutValue>60</timeoutValue>
<apiVersion>xml-4.2</apiVersion>
</MessageInfo>
<MerchantInfo>
<merchantID>MERCHANT ID</merchantID>
<password>PASSWORD</password>
</MerchantInfo>
<RequestType>Payment</RequestType>
<Payment>
<TxnList count="1">
<Txn ID="1">
<txnType>4</txnType>
<txnSource>23</txnSource>
<amount>100</amount>
<purchaseOrderNo>1234</purchaseOrderNo>
<txnID>4321</txnID>
</Txn>
</TxnList>
</Payment>
</SecurePayMessage>
""").encode('utf-8'))
if __name__ == '__main__':
unittest.main()