1
|
|
|
from mock import patch |
2
|
|
|
import nose.tools |
3
|
|
|
|
4
|
|
|
import audit.paypal.TrrFile |
5
|
|
|
|
6
|
|
|
# weird thing we have to do to get better assert_equals feedback |
7
|
|
|
nose.tools.assert_equals.im_class.maxDiff = None |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def get_base_row(): |
11
|
|
|
|
12
|
|
|
return { |
13
|
|
|
"Column Type": "SB", |
14
|
|
|
"Transaction ID": "AS7D98AS7D9A8S7D9AS", |
15
|
|
|
"Invoice ID": "", |
16
|
|
|
"PayPal Reference ID": "", |
17
|
|
|
"PayPal Reference ID Type": "", |
18
|
|
|
"Transaction Event Code": "", |
19
|
|
|
"Transaction Initiation Date": "2016/09/24 11:55:01 -0700", |
20
|
|
|
"Transaction Completion Date": "2016/09/24 11:55:01 -0700", |
21
|
|
|
"Transaction Debit or Credit": "", |
22
|
|
|
"Gross Transaction Amount": "1000", |
23
|
|
|
"Gross Transaction Currency": "USD", |
24
|
|
|
"Fee Debit or Credit": "", |
25
|
|
|
"Fee Amount": "55", |
26
|
|
|
"Fee Currency": "USD", |
27
|
|
|
"Transactional Status": "S", |
28
|
|
|
"Insurance Amount": "", |
29
|
|
|
"Sales Tax Amount": "0", |
30
|
|
|
"Shipping Amount": "0", |
31
|
|
|
"Transaction Subject": "", |
32
|
|
|
"Transaction Note": "", |
33
|
|
|
"Payer's Account ID": "[email protected]", |
34
|
|
|
"Payer Address Status": "N", |
35
|
|
|
"Item Name": "Generous benificence", |
36
|
|
|
"Item ID": "DONATE", |
37
|
|
|
"Option 1 Name": "", |
38
|
|
|
"Option 1 Value": "", |
39
|
|
|
"Option 2 Name": "", |
40
|
|
|
"Option 2 Value": "", |
41
|
|
|
"Auction Site": "", |
42
|
|
|
"Auction Buyer ID": "", |
43
|
|
|
"Auction Closing Date": "", |
44
|
|
|
"Shipping Address Line1": "", |
45
|
|
|
"Shipping Address Line2": "", |
46
|
|
|
"Shipping Address City": "", |
47
|
|
|
"Shipping Address State": "", |
48
|
|
|
"Shipping Address Zip": "", |
49
|
|
|
"Shipping Address Country": "", |
50
|
|
|
"Shipping Method": "", |
51
|
|
|
"Custom Field": "1234567", |
52
|
|
|
"Billing Address Line1": "", |
53
|
|
|
"Billing Address Line2": "", |
54
|
|
|
"Billing Address City": "", |
55
|
|
|
"Billing Address State": "", |
56
|
|
|
"Billing Address Zip": "", |
57
|
|
|
"Billing Address Country": "", |
58
|
|
|
"Consumer ID": "", |
59
|
|
|
"First Name": "Banana", |
60
|
|
|
"Last Name": "Man", |
61
|
|
|
"Consumer Business Name": "", |
62
|
|
|
"Card Type": "", |
63
|
|
|
"Payment Source": "", |
64
|
|
|
"Shipping Name": "", |
65
|
|
|
"Authorization Review Status": "", |
66
|
|
|
"Protection Eligibility": "", |
67
|
|
|
"Payment Tracking ID": "" |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def get_refund_row(): |
72
|
|
|
|
73
|
|
|
row = get_base_row() |
74
|
|
|
row.update({ |
75
|
|
|
"PayPal Reference ID": "3GJH3GJ3334214812", |
76
|
|
|
"PayPal Reference ID Type": "TXN", |
77
|
|
|
"Transaction Event Code": "T1107", |
78
|
|
|
"Transaction Debit or Credit": "DR", |
79
|
|
|
"Fee Debit or Credit": "CR", |
80
|
|
|
"Fee Amount": "55", |
81
|
|
|
"Transaction Note": "refund", |
82
|
|
|
}) |
83
|
|
|
return row |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def get_ec_refund_row(): |
87
|
|
|
|
88
|
|
|
row = get_base_row() |
89
|
|
|
row.update({ |
90
|
|
|
"Invoice ID": "4123422", |
91
|
|
|
"PayPal Reference ID": "3GJH3GJ3334214812", |
92
|
|
|
"PayPal Reference ID Type": "TXN", |
93
|
|
|
"Transaction Event Code": "T1107", |
94
|
|
|
"Transaction Debit or Credit": "DR", |
95
|
|
|
"Fee Debit or Credit": "CR", |
96
|
|
|
"Transaction Note": "refund", |
97
|
|
|
"Custom Field": "4123422", |
98
|
|
|
"Item ID": "", |
99
|
|
|
}) |
100
|
|
|
return row |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def get_ec_recurring_refund_row(): |
104
|
|
|
|
105
|
|
|
row = get_base_row() |
106
|
|
|
row.update({ |
107
|
|
|
"Invoice ID": "4123422", |
108
|
|
|
"PayPal Reference ID": "3GJH3GJ3334214812", |
109
|
|
|
"PayPal Reference ID Type": "TXN", |
110
|
|
|
"Transaction Event Code": "T1107", |
111
|
|
|
"Transaction Debit or Credit": "DR", |
112
|
|
|
"Fee Debit or Credit": "CR", |
113
|
|
|
"Transaction Note": "refund", |
114
|
|
|
"Custom Field": "", |
115
|
|
|
"Item ID": "", |
116
|
|
|
}) |
117
|
|
|
return row |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def get_recurring_row(): |
121
|
|
|
row = get_base_row() |
122
|
|
|
row.update({ |
123
|
|
|
"PayPal Reference ID": "3GJH3GJ3334214812", |
124
|
|
|
"PayPal Reference ID Type": "SUB", |
125
|
|
|
"Transaction Event Code": "T0002", |
126
|
|
|
"Gross Transaction Amount": "10.00", |
127
|
|
|
}) |
128
|
|
|
return row |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
@patch("queue.redis_wrap.Redis") |
132
|
|
|
@patch("civicrm.civicrm.Civicrm") |
133
|
|
|
@patch("process.globals") |
134
|
|
|
@patch("audit.paypal.paypal_api.PaypalApiClassic") |
135
|
|
|
def test_recurring_charge_without_subscription(MockPaypalApi, MockGlobals, MockCivicrm, MockRedis): |
136
|
|
|
''' |
137
|
|
|
Regression test for T143903 |
138
|
|
|
''' |
139
|
|
|
row = get_recurring_row() |
140
|
|
|
row["Transaction ID"] = "" |
141
|
|
|
row["PayPal Reference ID"] = "" |
142
|
|
|
|
143
|
|
|
MockCivicrm().transaction_exists.return_value = False |
144
|
|
|
|
145
|
|
|
parser = audit.paypal.TrrFile.TrrFile("dummy_path") |
146
|
|
|
with nose.tools.assert_raises(Exception) as cm: |
147
|
|
|
parser.parse_line(row) |
148
|
|
|
|
149
|
|
|
# Should have failed with a specific missing field error. |
150
|
|
|
# FIXME: Annoyingly, this masks any other, unexpected exception. |
151
|
|
|
assert cm.exception.message == "Missing field subscr_id" |
152
|
|
|
|
153
|
|
|
# Make sure we didn't try to send anything to the queue. |
154
|
|
|
MockRedis().send.assert_has_calls([]) |
155
|
|
|
|
156
|
|
|
|
157
|
|
View Code Duplication |
@patch("queue.redis_wrap.Redis") |
|
|
|
|
158
|
|
|
@patch("civicrm.civicrm.Civicrm") |
159
|
|
|
@patch("process.globals") |
160
|
|
|
def test_refund_send(MockGlobals, MockCivicrm, MockRedis): |
161
|
|
|
''' |
162
|
|
|
Test that we send a refund for a donation that isn't yet refunded |
163
|
|
|
''' |
164
|
|
|
row = get_refund_row() |
165
|
|
|
MockCivicrm().transaction_refunded.return_value = False |
166
|
|
|
|
167
|
|
|
parser = audit.paypal.TrrFile.TrrFile("dummy_path") |
168
|
|
|
|
169
|
|
|
parser.parse_line(row) |
170
|
|
|
|
171
|
|
|
# Did we send it? |
172
|
|
|
args = MockRedis().send.call_args |
173
|
|
|
assert args[0][0] == 'refund' |
174
|
|
|
expected = {'last_name': 'Man', 'thankyou_date': 0, 'city': '', 'payment_method': '', 'gateway_status': 'S', 'currency': 'USD', 'postal_code': '', 'date': 1474743301, 'gateway_refund_id': 'AS7D98AS7D9A8S7D9AS', 'gateway': 'paypal', 'state_province': '', 'gross': 10.0, 'first_name': 'Banana', 'fee': 0.55, 'gateway_txn_id': 'AS7D98AS7D9A8S7D9AS', 'gross_currency': 'USD', 'country': '', 'payment_submethod': '', 'note': 'refund', 'supplemental_address_1': '', 'settled_date': 1474743301, 'gateway_parent_id': '3GJH3GJ3334214812', 'type': 'refund', 'email': '[email protected]', 'street_address': '', 'contribution_tracking_id': '1234567', 'order_id': '1234567'} |
175
|
|
|
actual = args[0][1] |
176
|
|
|
nose.tools.assert_equals(expected, actual) |
177
|
|
|
|
178
|
|
|
|
179
|
|
View Code Duplication |
@patch("queue.redis_wrap.Redis") |
|
|
|
|
180
|
|
|
@patch("civicrm.civicrm.Civicrm") |
181
|
|
|
@patch("process.globals") |
182
|
|
|
def test_ec_refund_send(MockGlobals, MockCivicrm, MockRedis): |
183
|
|
|
''' |
184
|
|
|
Test that express checkout refunds are marked as such |
185
|
|
|
''' |
186
|
|
|
row = get_ec_refund_row() |
187
|
|
|
|
188
|
|
|
MockCivicrm().transaction_refunded.return_value = False |
189
|
|
|
|
190
|
|
|
parser = audit.paypal.TrrFile.TrrFile("dummy_path") |
191
|
|
|
|
192
|
|
|
parser.parse_line(row) |
193
|
|
|
|
194
|
|
|
# Did we send it? |
195
|
|
|
args = MockRedis().send.call_args |
196
|
|
|
expected = {'last_name': 'Man', 'thankyou_date': 0, 'city': '', 'payment_method': '', 'gateway_status': 'S', 'currency': 'USD', 'postal_code': '', 'date': 1474743301, 'gateway_refund_id': 'AS7D98AS7D9A8S7D9AS', 'gateway': 'paypal_ec', 'state_province': '', 'gross': 10.0, 'first_name': 'Banana', 'fee': 0.55, 'gateway_txn_id': 'AS7D98AS7D9A8S7D9AS', 'gross_currency': 'USD', 'country': '', 'payment_submethod': '', 'note': 'refund', 'supplemental_address_1': '', 'settled_date': 1474743301, 'gateway_parent_id': '3GJH3GJ3334214812', 'type': 'refund', 'email': '[email protected]', 'street_address': '', 'contribution_tracking_id': '4123422', 'order_id': '4123422'} |
197
|
|
|
assert args[0][0] == 'refund' |
198
|
|
|
actual = args[0][1] |
199
|
|
|
nose.tools.assert_equals(expected, actual) |
200
|
|
|
|
201
|
|
|
|
202
|
|
View Code Duplication |
@patch("queue.redis_wrap.Redis") |
|
|
|
|
203
|
|
|
@patch("civicrm.civicrm.Civicrm") |
204
|
|
|
@patch("process.globals") |
205
|
|
|
def test_ec_recurring_refund_send(MockGlobals, MockCivicrm, MockRedis): |
206
|
|
|
''' |
207
|
|
|
Test that express checkout recurring refunds are marked as ec too |
208
|
|
|
''' |
209
|
|
|
row = get_ec_recurring_refund_row() |
210
|
|
|
|
211
|
|
|
MockCivicrm().transaction_refunded.return_value = False |
212
|
|
|
|
213
|
|
|
parser = audit.paypal.TrrFile.TrrFile("dummy_path") |
214
|
|
|
|
215
|
|
|
parser.parse_line(row) |
216
|
|
|
|
217
|
|
|
# Did we send it? |
218
|
|
|
args = MockRedis().send.call_args |
219
|
|
|
expected = {'last_name': 'Man', 'thankyou_date': 0, 'city': '', 'payment_method': '', 'gateway_status': 'S', 'currency': 'USD', 'postal_code': '', 'date': 1474743301, 'gateway_refund_id': 'AS7D98AS7D9A8S7D9AS', 'gateway': 'paypal_ec', 'state_province': '', 'gross': 10.0, 'first_name': 'Banana', 'fee': 0.55, 'gateway_txn_id': 'AS7D98AS7D9A8S7D9AS', 'gross_currency': 'USD', 'country': '', 'payment_submethod': '', 'note': 'refund', 'supplemental_address_1': '', 'settled_date': 1474743301, 'gateway_parent_id': '3GJH3GJ3334214812', 'type': 'refund', 'email': '[email protected]', 'street_address': '', 'contribution_tracking_id': '4123422', 'order_id': '4123422'} |
220
|
|
|
assert args[0][0] == 'refund' |
221
|
|
|
actual = args[0][1] |
222
|
|
|
nose.tools.assert_equals(expected, actual) |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
@patch("queue.redis_wrap.Redis") |
226
|
|
|
@patch("civicrm.civicrm.Civicrm") |
227
|
|
|
@patch("process.globals") |
228
|
|
|
def test_refund_duplicate(MockGlobals, MockCivicrm, MockRedis): |
229
|
|
|
''' |
230
|
|
|
Test that we do not send a refund for a donation that is refunded |
231
|
|
|
''' |
232
|
|
|
row = get_refund_row() |
233
|
|
|
MockCivicrm().transaction_refunded.return_value = True |
234
|
|
|
|
235
|
|
|
parser = audit.paypal.TrrFile.TrrFile("dummy_path") |
236
|
|
|
|
237
|
|
|
parser.parse_line(row) |
238
|
|
|
|
239
|
|
|
# Did we send it? |
240
|
|
|
MockRedis().send.assert_has_calls([]) |
241
|
|
|
|
242
|
|
|
|
243
|
|
View Code Duplication |
@patch("queue.redis_wrap.Redis") |
|
|
|
|
244
|
|
|
@patch("civicrm.civicrm.Civicrm") |
245
|
|
|
@patch("process.globals") |
246
|
|
|
def test_recurring(MockGlobals, MockCivicrm, MockRedis): |
247
|
|
|
''' |
248
|
|
|
Test that we send a normalized message for recurring donations |
249
|
|
|
''' |
250
|
|
|
MockCivicrm().transaction_exists.return_value = False |
251
|
|
|
|
252
|
|
|
row = get_recurring_row() |
253
|
|
|
parser = audit.paypal.TrrFile.TrrFile("dummy_path") |
254
|
|
|
|
255
|
|
|
parser.parse_line(row) |
256
|
|
|
|
257
|
|
|
# Did we send it? |
258
|
|
|
args = MockRedis().send.call_args |
259
|
|
|
assert args[0][0] == 'recurring' |
260
|
|
|
expected = {'last_name': 'Man', 'txn_type': 'subscr_payment', 'thankyou_date': 0, 'city': '', 'payment_method': '', 'gateway_status': 'S', 'currency': 'USD', 'postal_code': '', 'date': 1474743301, 'subscr_id': '3GJH3GJ3334214812', 'gateway': 'paypal', 'state_province': '', 'gross': 0.1, 'first_name': 'Banana', 'fee': 0.55, 'gateway_txn_id': 'AS7D98AS7D9A8S7D9AS', 'country': '', 'payment_submethod': '', 'note': '', 'supplemental_address_1': '', 'settled_date': 1474743301, 'email': '[email protected]', 'street_address': '', 'contribution_tracking_id': '1234567', 'order_id': '1234567'} |
261
|
|
|
actual = args[0][1] |
262
|
|
|
nose.tools.assert_equals(expected, actual) |
263
|
|
|
|