-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathhelperpe.py
More file actions
278 lines (221 loc) · 6.94 KB
/
helperpe.py
File metadata and controls
278 lines (221 loc) · 6.94 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import os
import random
import re
import sys
import time
import unittest
from future.builtins import str
from future.builtins import super
from mock import patch, Mock
import openpay
def generate_order_id():
order_id = 'oid-test-{0}-{1}'.format(
random.randint(1, 3000), str(time.time())[7:])
if len(order_id) > 20:
order_id = order_id[:20]
return order_id
NOW = datetime.datetime.now()
DUMMY_CUSTOMER = {
"name": "Cliente Peru",
"last_name": "Vazquez Juarez",
"phone_number": "4448936475",
"email": "juan.vazquez@empresa.pe"
}
DUMMY_CARD = {
'holder_name': 'Juan Lopez',
'card_number': '4111111111111111',
"cvv2": "110",
'expiration_month': NOW.month,
'expiration_year': str(NOW.year + 4)[2:]
}
DUMMY_CHARGE = {
'method': 'card',
'amount': 100,
'currency': 'PEN',
'source_id': '',
'description': 'Dummy Charge',
'device_session_id': ''
}
DUMMY_CHARGE_MERCHANT = {
'method': 'card',
'amount': 100,
'currency': 'PEN',
'description': 'Dummy Charge',
'confirm': 'false',
'redirect_url': 'http://testing-openpay-pe.com',
'customer': DUMMY_CUSTOMER
}
DUMMY_CHARGE_STORE = {
'method': 'store',
'amount': 100,
'currency': 'PEN',
'description': 'Store Charge'
}
DUMMY_CHARGE_STORE_MERCHANT = {
'method': 'store',
'amount': 100,
'currency': 'PEN',
'description': 'Store Charge',
'customer': DUMMY_CUSTOMER
}
DUMMY_PLAN = {
'name': 'Amazing Gold Plan',
'amount': 2000,
'repeat_every': 1,
'repeat_unit': 'month',
'retry_times': 2,
'status_after_retry': 'cancelled',
'trial_days': 0
}
DUMMY_TRANSFER = {
'amount': 400,
'customer_id': 'acuqxruyv0hi1wfdwmym',
'description': 'Dummy Transfer',
'order_id': 'oid-00099',
}
DUMMY_ADDRESS = {
"city": "Lima",
"country_code": "PE",
"postal_code": "02002",
"line1": "Av 5 de Febrero",
"line2": "Roble 207",
"line3": "col carrillo",
"state": "Lima"
}
DUMMY_CHECKOUT = {
"amount": 250,
"currency": "PEN",
"description": "Cargo cobro con link",
"redirect_url": "https://misitioempresa.pe",
"order_id": "oid-12331",
"expiration_date": "2021-08-31 12:50",
"send_email": True,
"customer": DUMMY_CUSTOMER
}
DUMMY_CHECKOUT_WITHOUT_CUSTOMER = {
"amount": 250,
"currency": "PEN",
"description": "Cargo cobro con link",
"redirect_url": "https://misitioempresa.pe",
"order_id": "oid-12331",
"expiration_date": "2021-08-31 12:50",
"send_email": True
}
DUMMY_WEBHOOK = {
"url": "https://webhook.site/99e29beb-fd9e-4c3f-9b49-ad28bac3daa5",
"user": "juanito",
"password": "passjuanito",
"event_types":
[
"charge.refunded",
"charge.failed",
"charge.cancelled",
"charge.created",
"chargeback.accepted"
]
}
DUMMY_TOKEN = {
"card_number": "4111111111111111",
"holder_name": "Juan Perez Ramirez",
"expiration_year": "29",
"expiration_month": "12",
"cvv2": "110",
"address": {
"city": "Lima",
"country_code": "PE",
"postal_code": "110511",
"line1": "Av 5 de Febrero",
"line2": "Roble 207",
"line3": "col carrillo",
"state": "Lima"
}
}
class OpenpayTestCase(unittest.TestCase):
RESTORE_ATTRIBUTES = ('api_version', 'api_key')
def setUp(self):
super(OpenpayTestCase, self).setUp()
self._openpay_original_attributes = {}
for attr in self.RESTORE_ATTRIBUTES:
self._openpay_original_attributes[attr] = getattr(openpay, attr)
api_base = os.environ.get('OPENPAY_API_BASE')
if api_base:
openpay.api_base = api_base
# Sandbox
openpay.api_key = os.environ.get(
'OPENPAY_API_KEY', 'sk_f934dfe51645483e82106301d985a4f6')
openpay.merchant_id = "m3cji4ughukthjcsglv0"
openpay.country = "pe"
openpay.public_ip = '138.84.62.226'
# Dev
# openpay.api_key = os.environ.get(
# 'OPENPAY_API_KEY', '68df281c16184d47bb773d70abd4191b')
# openpay.merchant_id = "m4se8bd4fef1mkzk6d1b"
openpay.verify_ssl_certs = False
def tearDown(self):
super(OpenpayTestCase, self).tearDown()
for attr in self.RESTORE_ATTRIBUTES:
setattr(openpay, attr, self._openpay_original_attributes[attr])
# Python < 2.7 compatibility
def assertRaisesRegexp(self, exception, regexp, callable, *args, **kwargs):
try:
callable(*args, **kwargs)
except exception as err:
if regexp is None:
return True
if isinstance(regexp, str):
regexp = re.compile(regexp)
if not regexp.search(str(err)):
raise self.failureException('"%s" does not match "%s"' %
(regexp.pattern, str(err)))
else:
raise self.failureException(
'%s was not raised' % (exception.__name__,))
class OpenpayUnitTestCase(OpenpayTestCase):
REQUEST_LIBRARIES = ['urlfetch', 'requests', 'pycurl']
if sys.version_info >= (3, 0):
REQUEST_LIBRARIES.append('urllib.request')
else:
REQUEST_LIBRARIES.append('urllib2')
def setUp(self):
super(OpenpayUnitTestCase, self).setUp()
self.request_patchers = {}
self.request_mocks = {}
for lib in self.REQUEST_LIBRARIES:
patcher = patch("openpay.http_client.%s" % (lib,))
self.request_mocks[lib] = patcher.start()
self.request_patchers[lib] = patcher
def tearDown(self):
super(OpenpayUnitTestCase, self).tearDown()
for patcher in list(self.request_patchers.values()):
patcher.stop()
class OpenpayApiTestCase(OpenpayTestCase):
def setUp(self):
super(OpenpayApiTestCase, self).setUp()
self.requestor_patcher = patch('openpay.api.APIClient')
requestor_class_mock = self.requestor_patcher.start()
self.requestor_mock = requestor_class_mock.return_value
def tearDown(self):
super(OpenpayApiTestCase, self).tearDown()
self.requestor_patcher.stop()
def mock_response(self, res):
self.requestor_mock.request = Mock(return_value=(res, 'reskey'))
class MyResource(openpay.resource.APIResource):
pass
class MySingleton(openpay.resource.SingletonAPIResource):
pass
class MyListable(openpay.resource.ListableAPIResource):
pass
class MyCreatable(openpay.resource.CreateableAPIResource):
pass
class MyUpdateable(openpay.resource.UpdateableAPIResource):
pass
class MyDeletable(openpay.resource.DeletableAPIResource):
pass
class MyComposite(openpay.resource.ListableAPIResource,
openpay.resource.CreateableAPIResource,
openpay.resource.UpdateableAPIResource,
openpay.resource.DeletableAPIResource):
pass