-
Notifications
You must be signed in to change notification settings - Fork 10
/
tests.py
430 lines (358 loc) · 15.3 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import six
import json
import datetime
import unittest
from mock import patch
from urlparse import urljoin
from requests.models import Response
from requests.sessions import Session
from boxview.boxview import BoxView, BoxViewError, RetryAfter, API_URL
from boxview.utils import format_date, get_mimetype_from_headers
TEST_URL = 'https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf'
TEST_DOCUMENT = {
"type": "document",
"id": "2da6cf9261824fb0a4fe532f94d14625",
"status": "done",
"name": "Leaves of Grass",
"created_at": "2013-08-30T00:17:37Z",
"modified_at": "2013-08-30T00:17:37Z"
}
TEST_DOCUMENT_LIST = {
"document_collection": {
"total_count": 1,
"entries": [TEST_DOCUMENT],
}
}
TEST_SESSION = {
'type': 'session',
'id': '4fba9eda0dd745d491ad0b98e224aa25',
'expires_at': '3915-10-29T01:31:48.677Z',
'urls': {
'view': 'https://view-api.box.com/1/sessions/4fba9eda0dd745d491ad0b98e224aa25/view',
'assets': 'https://view-api.box.com/1/sessions/4fba9eda0dd745d491ad0b98e224aa25/assets/',
'realtime': 'https://view-api.box.com/sse/4fba9eda0dd745d491ad0b98e224aa25',
},
}
TEST_STORAGE_PROFILE = {
'provider': 'S3',
's3_bucket_name': 'super-awesome-bucket',
's3_access_key_id': 'AKIAIOSFODNN7EXAMPLE',
's3_secret_access_key': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY',
}
TEST_WEBHOOK = {
'url': 'https://wow.such.io/view-api-callback',
}
class BoxViewTestCase(unittest.TestCase):
def setUp(self):
self.api = BoxView('<box view api key>')
def test_initials(self):
# api key is required
self.assertRaises(ValueError, BoxView)
now = datetime.datetime.utcnow().replace(microsecond=0)
dtfiso = now.isoformat()
dfiso = now.date().isoformat()
self.assertEqual(dtfiso, format_date(dtfiso))
self.assertEqual(dtfiso, format_date(now))
self.assertEqual(dfiso, format_date(now.date()))
headers = {'Content-Type': 'text/plain'}
self.assertEqual('text/plain', get_mimetype_from_headers(headers))
headers = {'Content-Type': 'text/plain; charset=utf-8'}
self.assertEqual('text/plain', get_mimetype_from_headers(headers))
@patch.object(Session, 'request')
def test_crate_document_from_url(self, mock_request):
response = Response()
response.status_code = 201
response._content = json.dumps(TEST_DOCUMENT)
mock_request.return_value = response
result = self.api.create_document(url=TEST_URL,
name='Test Document',
thumbnails='100x100,200x200',
non_svg=False)
self.assertIsNotNone(result)
self.assertEqual(result, TEST_DOCUMENT)
data = {
'url': TEST_URL,
'name': 'Test Document',
'thumbnails': '100x100,200x200',
'non_svg': False,
}
headers = {'Content-Type': 'application/json'}
url = urljoin(API_URL, 'documents')
mock_request.assert_called_with('POST', url,
data=json.dumps(data),
headers=headers)
# url of file param is required
self.assertRaises(ValueError, self.api.create_document)
@patch.object(Session, 'request')
def test_create_document_from_file(self, mock_request):
response = Response()
response.status_code = 201
response._content = json.dumps(TEST_DOCUMENT)
mock_request.return_value = response
stream = six.BytesIO()
result = self.api.create_document_from_file(stream,
name='Test Document')
self.assertEqual(result, TEST_DOCUMENT)
result = self.api.create_document_from_file(__file__,
name='Test Document')
self.assertEqual(result, TEST_DOCUMENT)
@patch.object(Session, 'request')
def test_get_document(self, mock_request):
response = Response()
response.status_code = 200
response._content = json.dumps(TEST_DOCUMENT)
mock_request.return_value = response
result = self.api.get_document(TEST_DOCUMENT['id'])
self.assertIsNotNone(result)
self.assertEqual(result, TEST_DOCUMENT)
@patch.object(Session, 'request')
def test_get_documents(self, mock_request):
response = Response()
response.status_code = 200
response._content = json.dumps(TEST_DOCUMENT_LIST)
mock_request.return_value = response
now = datetime.datetime.utcnow()
result = self.api.get_documents(limit=10, created_before=now)
self.assertIsNotNone(result)
self.assertEqual(result, TEST_DOCUMENT_LIST)
@patch.object(Session, 'request')
def test_update_document(self, mock_request):
response = Response()
response.status_code = 200
response._content = json.dumps(TEST_DOCUMENT)
mock_request.return_value = response
result = self.api.update_document(TEST_DOCUMENT['id'],
name='TestDocument')
self.assertIsNotNone(result)
self.assertEqual(result, TEST_DOCUMENT)
@patch.object(Session, 'request')
def test_delete_document(self, mock_request):
response = Response()
response.status_code = 204
mock_request.return_value = response
self.api.delete_document(TEST_DOCUMENT['id'])
@patch.object(Session, 'request')
def test_get_document_content(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'text/plain'
response._content = 'test'
response.raw = six.BytesIO('test')
mock_request.return_value = response
stream = six.BytesIO()
mimetype = self.api.get_document_content(stream, TEST_DOCUMENT['id'])
self.assertEqual(stream.getvalue(), response._content)
self.assertEqual(mimetype, response.headers['Content-Type'])
stream = six.BytesIO()
self.api.get_document_content(stream,
TEST_DOCUMENT['id'],
extension='.pdf')
self.assertEqual(stream.getvalue(), response._content)
stream = six.BytesIO()
self.api.get_document_content(stream,
TEST_DOCUMENT['id'],
extension='.zip')
self.assertEqual(stream.getvalue(), response._content)
stream = six.BytesIO()
# allowed only .zip and .pdf extensions
self.assertRaises(ValueError,
self.api.get_document_content,
stream,
TEST_DOCUMENT['id'],
extension='.docx')
@patch.object(Session, 'request')
def test_get_document_content_to_string(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'text/plain'
response._content = 'test'
response.raw = six.BytesIO('test')
mock_request.return_value = response
doc_id = TEST_DOCUMENT['id']
result, mimetype = self.api.get_document_content_to_string(doc_id)
self.assertIsNotNone(result)
self.assertEqual(result, response._content)
self.assertEqual(mimetype, response.headers['Content-Type'])
@patch.object(Session, 'request')
def test_get_document_content_to_file(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'text/plain'
response._content = 'test'
response.raw = six.BytesIO('test')
mock_request.return_value = response
filename = 'boxview.txt'
mimetype = self.api.get_document_content_to_file(filename,
TEST_DOCUMENT['id'])
self.assertEqual(mimetype, response.headers['Content-Type'])
self.assertTrue(os.path.exists(filename))
try:
os.remove(filename)
except OSError:
pass
@patch.object(Session, 'request')
def test_get_document_content_mimetype(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'text/plain'
mock_request.return_value = response
mimetype = self.api.get_document_content_mimetype(TEST_DOCUMENT['id'])
self.assertEqual(mimetype, response.headers['Content-Type'])
@patch.object(Session, 'request')
def test_create_session(self, mock_request):
response = Response()
response.status_code = 201
response._content = json.dumps(TEST_SESSION)
mock_request.return_value = response
expires_at = datetime.datetime.utcnow()
doc_id = TEST_DOCUMENT['id']
result = self.api.create_session(doc_id,
duration=600,
expires_at=expires_at,
is_downloadable=True,
is_text_selectable=True)
self.assertIsNotNone(result)
self.assertEqual(result['id'], TEST_SESSION['id'])
data = {
'document_id': doc_id,
'duration': 600,
'expires_at': expires_at.replace(microsecond=0).isoformat(),
'is_downloadable': True,
'is_text_selectable': True
}
headers = {'Content-Type': 'application/json'}
url = urljoin(API_URL, 'sessions')
mock_request.assert_called_with('POST', url,
data=json.dumps(data),
headers=headers)
@patch.object(Session, 'request')
def test_ready_to_view(self, mock_request):
response = Response()
response.status_code = 200
response._content = json.dumps(TEST_DOCUMENT)
mock_request.return_value = response
result = self.api.ready_to_view(TEST_DOCUMENT['id'])
self.assertIsNotNone(result)
self.assertTrue(bool(result))
response._content = json.dumps(dict(TEST_DOCUMENT, status='error'))
result = self.api.ready_to_view(TEST_DOCUMENT['id'])
self.assertFalse(bool(result))
@patch.object(Session, 'request')
def test_request_error(self, mock_request):
response = Response()
response.status_code = 401
response._content = 'Unauthorized'
response.reason = 'Unauthorized'
mock_request.return_value = response
self.assertRaises(BoxViewError,
self.api.get_document,
TEST_DOCUMENT['id'])
@patch.object(Session, 'request')
def test_request_retry_after(self, mock_request):
response = Response()
response.status_code = 202
response.headers['Retry-After'] = '100.0'
mock_request.return_value = response
try:
self.api.get_thumbnail_to_string(TEST_DOCUMENT['id'], 100, 100)
except RetryAfter as e:
self.assertEqual(e.seconds, 100.0)
else:
self.assertTrue(False)
@patch.object(Session, 'request')
def test_get_thumbnail(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'image/png'
response._content = 'test'
response.raw = six.BytesIO('test')
mock_request.return_value = response
stream = six.BytesIO()
mimetype = self.api.get_thumbnail(stream, TEST_DOCUMENT['id'], 100, 100)
self.assertEqual(stream.getvalue(), response._content)
self.assertEqual(mimetype, response.headers['Content-Type'])
@patch.object(Session, 'request')
def test_get_thumbnail_to_string(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'image/png'
response._content = 'test'
response.raw = six.BytesIO('test')
mock_request.return_value = response
doc_id = TEST_DOCUMENT['id']
result, mimetype = self.api.get_thumbnail_to_string(doc_id, 100, 100)
self.assertIsNotNone(result)
self.assertEqual(result, response._content)
self.assertEqual(mimetype, response.headers['Content-Type'])
@patch.object(Session, 'request')
def test_get_thumbnail_to_file(self, mock_request):
response = Response()
response.status_code = 200
response.headers['Content-Type'] = 'image/png'
response._content = 'test'
response.raw = six.BytesIO('test')
mock_request.return_value = response
filename = 'boxview.png'
mimetype = self.api.get_thumbnail_to_file(filename,
TEST_DOCUMENT['id'],
100, 100)
self.assertEqual(mimetype, response.headers['Content-Type'])
self.assertTrue(os.path.exists(filename))
try:
os.remove(filename)
except OSError:
pass
@patch.object(Session, 'request')
def test_create_storage_profile(self, mock_request):
response = Response()
response.status_code = 201
response._content = json.dumps(TEST_STORAGE_PROFILE)
mock_request.return_value = response
result = self.api.create_storage_profile(
'S3', 'bucket', 'key_id', 'secret_key')
self.assertIsNotNone(result)
self.assertEqual(result, TEST_STORAGE_PROFILE)
@patch.object(Session, 'request')
def test_get_storage_profile(self, mock_request):
response = Response()
response.status_code = 200
response._content = json.dumps(TEST_STORAGE_PROFILE)
mock_request.return_value = response
result = self.api.get_storage_profile()
self.assertIsNotNone(result)
self.assertEqual(result, TEST_STORAGE_PROFILE)
@patch.object(Session, 'request')
def test_delete_storage_profile(self, mock_request):
response = Response()
response.status_code = 204
mock_request.return_value = response
self.api.delete_storage_profile()
@patch.object(Session, 'request')
def test_create_webhook(self, mock_request):
response = Response()
response.status_code = 201
response._content = json.dumps(TEST_WEBHOOK)
mock_request.return_value = response
result = self.api.create_webhook('http://example.com/webhook')
self.assertIsNotNone(result)
self.assertEqual(result, TEST_WEBHOOK)
@patch.object(Session, 'request')
def test_get_webhook(self, mock_request):
response = Response()
response.status_code = 200
response._content = json.dumps(TEST_WEBHOOK)
mock_request.return_value = response
result = self.api.get_webhook()
self.assertIsNotNone(result)
self.assertEqual(result, TEST_WEBHOOK)
@patch.object(Session, 'request')
def test_delete_webhook(self, mock_request):
response = Response()
response.status_code = 204
mock_request.return_value = response
self.api.delete_webhook()
if __name__ == '__main__':
unittest.main()