forked from ej2/python-quickbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.py
329 lines (249 loc) · 11.8 KB
/
test_client.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import json
import warnings
from tests.integration.test_base import QuickbooksUnitTestCase
from unittest.mock import patch, mock_open
from quickbooks.exceptions import QuickbooksException, SevereException, AuthorizationException
from quickbooks import client, mixins
from quickbooks.objects.salesreceipt import SalesReceipt
TEST_SIGNATURE = 'nfPLN16u3vMvv08ghDs+dOkLuirEVDy5wAeG/lmM2OA='
TEST_PAYLOAD = '{"stuff":"5"}'
TEST_VERIFIER_TOKEN = 'verify_me'
TEST_REFRESH_TOKEN = 'refresh'
class ClientTest(QuickbooksUnitTestCase):
def setUp(self):
super(ClientTest, self).setUp()
self.auth_client.access_token = 'ACCESS_TOKEN'
def tearDown(self):
self.qb_client = client.QuickBooks()
self.qb_client._drop()
def test_client_new(self):
self.qb_client = client.QuickBooks(
company_id="company_id",
verbose=True,
verifier_token=TEST_VERIFIER_TOKEN,
)
self.assertEqual(self.qb_client.company_id, "company_id")
def test_client_with_deprecated_minor_version(self):
with warnings.catch_warnings(record=True) as w:
self.qb_client = client.QuickBooks(
company_id="company_id",
verbose=True,
minorversion=74,
verifier_token=TEST_VERIFIER_TOKEN,
)
warnings.simplefilter("always")
self.assertEqual(self.qb_client.company_id, "company_id")
self.assertEqual(self.qb_client.minorversion, 74)
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
self.assertTrue("Minor Version 74 is no longer supported" in str(w[-1].message))
self.assertTrue("Minimum supported version is 75" in str(w[-1].message))
def test_api_url(self):
qb_client = client.QuickBooks(sandbox=False)
api_url = qb_client.api_url
self.assertFalse("sandbox" in api_url)
def test_api_url_sandbox(self):
qb_client = client.QuickBooks(
auth_client=self.auth_client,
refresh_token='REFRESH_TOKEN',
company_id='COMPANY_ID',
)
api_url = qb_client.api_url
print(api_url)
self.assertTrue("sandbox" in api_url)
def test_isvalid_object_name_valid(self):
qb_client = client.QuickBooks()
result = qb_client.isvalid_object_name("Customer")
self.assertEqual(result, True)
def test_isvalid_object_name_invalid(self):
qb_client = client.QuickBooks()
self.assertRaises(Exception, qb_client.isvalid_object_name, "invalid")
@patch('quickbooks.client.QuickBooks.make_request')
def test_batch_operation(self, make_req):
qb_client = client.QuickBooks()
qb_client.batch_operation("request_body")
self.assertTrue(make_req.called)
@patch('quickbooks.client.QuickBooks.post')
def test_misc_operation(self, post):
qb_client = client.QuickBooks(company_id='COMPANY_ID', auth_client=self.auth_client)
qb_client.misc_operation("end_point", "request_body")
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/COMPANY_ID/end_point"
post.assert_called_with(url, "request_body", 'application/json')
@patch('quickbooks.client.QuickBooks.post')
def test_create_object(self, post):
qb_client = client.QuickBooks()
qb_client.create_object("Customer", "request_body")
self.assertTrue(post.called)
@patch('quickbooks.client.QuickBooks.post')
def test_query(self, post):
qb_client = client.QuickBooks()
qb_client.query("select")
self.assertTrue(post.called)
@patch('quickbooks.client.QuickBooks.post')
def test_update_object(self, post):
qb_client = client.QuickBooks()
qb_client.update_object("Customer", "request_body")
self.assertTrue(post.called)
@patch('quickbooks.client.QuickBooks.make_request')
def test_update_object_with_request_id(self, make_req):
qb_client = client.QuickBooks(auth_client=self.auth_client)
qb_client.company_id = "1234"
qb_client.update_object("Customer", "request_body", request_id="123")
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/1234/customer"
make_req.assert_called_with("POST", url, "request_body", file_path=None, file_bytes=None, request_id="123", params={'minorversion': client.QuickBooks.MINIMUM_MINOR_VERSION})
@patch('quickbooks.client.QuickBooks.get')
def test_get_current_user(self, get):
qb_client = client.QuickBooks()
qb_client.company_id = "1234"
qb_client.get_current_user()
url = "https://appcenter.intuit.com/api/v1/user/current"
get.assert_called_with(url)
@patch('quickbooks.client.QuickBooks.make_request')
def test_get_report(self, make_req):
qb_client = client.QuickBooks(auth_client=self.auth_client)
qb_client.company_id = "1234"
qb_client.get_report("profitandloss", {1: 2})
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/1234/reports/profitandloss"
expected_params = {1: 2, 'minorversion': client.QuickBooks.MINIMUM_MINOR_VERSION}
make_req.assert_called_with("GET", url, params=expected_params)
@patch('quickbooks.client.QuickBooks.make_request')
def test_get_single_object(self, make_req):
qb_client = client.QuickBooks(auth_client=self.auth_client)
qb_client.company_id = "1234"
qb_client.get_single_object("test", 1)
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/1234/test/1"
make_req.assert_called_with("GET", url, {}, params={'minorversion': client.QuickBooks.MINIMUM_MINOR_VERSION})
@patch('quickbooks.client.QuickBooks.make_request')
def test_get_single_object_with_params(self, make_req):
qb_client = client.QuickBooks(auth_client=self.auth_client)
qb_client.company_id = "1234"
qb_client.get_single_object("test", 1, params={'param':'value'})
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/1234/test/1"
make_req.assert_called_with("GET", url, {}, params={'param':'value', 'minorversion': client.QuickBooks.MINIMUM_MINOR_VERSION})
@patch('quickbooks.client.QuickBooks.process_request')
def test_make_request(self, process_request):
process_request.return_value = MockResponseJson()
qb_client = client.QuickBooks()
qb_client.company_id = "1234"
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/1234/test/1/"
qb_client.make_request("GET", url, request_body=None, content_type='application/json')
process_request.assert_called_with(
"GET", url, data={},
headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-quickbooks V3 library'},
params={'minorversion': client.QuickBooks.MINIMUM_MINOR_VERSION})
def test_handle_exceptions(self):
qb_client = client.QuickBooks()
error_data = {
"Error": [{
"Message": "message",
"Detail": "detail",
"code": "2030",
"element": "Id"}],
"type": "ValidationFault"
}
self.assertRaises(QuickbooksException, qb_client.handle_exceptions, error_data)
def test_handle_exceptions_severe(self):
qb_client = client.QuickBooks()
error_data = {
"Error": [{
"Message": "message",
"Detail": "detail",
"code": "10001",
"element": "Id"}],
"type": "ValidationFault"
}
self.assertRaises(SevereException, qb_client.handle_exceptions, error_data)
@patch('quickbooks.client.QuickBooks.process_request')
def test_download_pdf(self, process_request):
self.qb_client.session = MockSession()
receipt = SalesReceipt()
receipt.Id = 1
process_request.return_value = MockPdfResponse()
response = receipt.download_pdf(qb=self.qb_client)
url = "https://sandbox-quickbooks.api.intuit.com/v3/company/COMPANY_ID/salesreceipt/1/pdf"
process_request.assert_called_with(
"GET", url, headers={'Content-Type': 'application/pdf', 'Accept': 'application/pdf, application/json', 'User-Agent': 'python-quickbooks V3 library'})
self.assertEqual(response, 'sample pdf content')
def test_download_nonexistent_pdf(self):
receipt = SalesReceipt()
receipt.Id = 666
self.assertRaises(QuickbooksException, receipt.download_pdf)
def test_validate_webhook_signature(self):
self.qb_client.verifier_token = TEST_VERIFIER_TOKEN
self.assertTrue(self.qb_client.validate_webhook_signature(TEST_PAYLOAD, TEST_SIGNATURE, TEST_VERIFIER_TOKEN))
def test_fail_webhook(self):
self.qb_client.verifier_token = TEST_VERIFIER_TOKEN
self.assertFalse(self.qb_client.validate_webhook_signature("", TEST_SIGNATURE, TEST_VERIFIER_TOKEN))
@patch('quickbooks.client.QuickBooks.process_request')
def test_download_pdf_not_authorized(self, process_request):
self.qb_client.session = MockSession()
receipt = SalesReceipt()
receipt.Id = 1
process_request.return_value = MockUnauthorizedResponse()
self.assertRaises(AuthorizationException, receipt.download_pdf, self.qb_client)
@patch('quickbooks.client.QuickBooks.process_request')
def test_make_request_file_closed(self, process_request):
file_path = '/path/to/file.txt'
process_request.return_value = MockResponseJson()
with patch('builtins.open', mock_open(read_data=b'file content')) as mock_file:
qb_client = client.QuickBooks(auth_client=self.auth_client)
qb_client.make_request('POST',
'https://sandbox-quickbooks.api.intuit.com/v3/company/COMPANY_ID/attachable',
request_body='{"ContentType": "text/plain"}',
file_path=file_path)
mock_file.assert_called_once_with(file_path, 'rb')
mock_file.return_value.__enter__.return_value.read.assert_called_once()
mock_file.return_value.__exit__.assert_called_once()
process_request.assert_called_once()
class MockResponse(object):
@property
def text(self):
return '{"QueryResponse": {"Department": []}}'
@property
def status_code(self):
try:
import httplib # python 2
except ImportError:
import http.client as httplib # python 3
return httplib.OK
def json(self):
return json.loads(self.text)
class MockResponseJson:
def __init__(self, json_data=None, status_code=200):
self.json_data = json_data or {}
self.status_code = status_code
@property
def text(self):
return json.dumps(self.json_data, cls=mixins.DecimalEncoder)
def json(self):
return self.json_data
class MockUnauthorizedResponse(object):
@property
def text(self):
return "UNAUTHORIZED"
@property
def status_code(self):
try:
import httplib # python 2
except ImportError:
import http.client as httplib # python 3
return httplib.UNAUTHORIZED
class MockPdfResponse(object):
@property
def status_code(self):
try:
import httplib # python 2
except ImportError:
import http.client as httplib # python 3
return httplib.OK
@property
def content(self):
return "sample pdf content"
class MockSessionManager(object):
def get_session(self):
return MockSession()
class MockSession(object):
def __init__(self):
self.access_token = "test_access_token"
def request(self, request_type, url, headers=None, params=None, data=None, **kwargs):
return MockResponse()