This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
test_upload.py
455 lines (361 loc) · 15 KB
/
test_upload.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import base64
import json
import responses
import pytest
import mapbox
import mapbox.services.uploads
username = 'testuser'
access_token = 'pk.{0}.test'.format(
base64.b64encode(b'{"u":"testuser"}').decode('utf-8'))
upload_response_body = """
{{"progress": 0,
"modified": "date.test",
"error": null,
"tileset": "{username}.test1",
"complete": false,
"owner": "{username}",
"created": "date.test",
"id": "id.test",
"name": null}}""".format(username=username)
def test_class_attrs():
"""Get expected class attr values"""
serv = mapbox.Uploader()
assert serv.api_name == 'uploads'
assert serv.api_version == 'v1'
@responses.activate
def test_get_credentials():
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
res = mapbox.Uploader(access_token=access_token)._get_credentials()
assert res.status_code == 200
creds = res.json()
assert username in creds['url']
for k in ['key', 'bucket', 'url', 'accessKeyId',
'secretAccessKey', 'sessionToken']:
assert k in creds.keys()
@responses.activate
def test_create():
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
match_querystring=True,
body=upload_response_body, status=201,
content_type='application/json')
res = mapbox.Uploader(access_token=access_token).create(
'http://example.com/test.json', 'test1') # without username prefix
assert res.status_code == 201
job = res.json()
assert job['tileset'] == "{0}.test1".format(username)
res2 = mapbox.Uploader(access_token=access_token).create(
'http://example.com/test.json', 'testuser.test1') # also takes full tileset
assert res2.status_code == 201
job = res2.json()
assert job['tileset'] == "{0}.test1".format(username)
@responses.activate
def test_create_name():
upload_response_body = """
{"progress": 0,
"modified": "date.test",
"error": null,
"tileset": "testuser.test1",
"complete": false,
"owner": "testuser",
"created": "date.test",
"id": "id.test",
"name": "testname"}"""
def request_callback(request):
payload = json.loads(request.body.decode())
assert payload['name'] == "testname"
return (201, {}, upload_response_body)
responses.add_callback(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
match_querystring=True,
callback=request_callback)
res = mapbox.Uploader(access_token=access_token).create(
'http://example.com/test.json', 'testuser.test1', name="testname")
assert res.status_code == 201
job = res.json()
assert job['name'] == "testname"
@responses.activate
def test_list():
responses.add(
responses.GET,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
match_querystring=True,
body="[{0}]".format(upload_response_body), status=200,
content_type='application/json')
res = mapbox.Uploader(access_token=access_token).list()
assert res.status_code == 200
uploads = res.json()
assert len(uploads) == 1
assert json.loads(upload_response_body) in uploads
@responses.activate
def test_status():
job = json.loads(upload_response_body)
responses.add(
responses.GET,
'https://api.mapbox.com/uploads/v1/{0}/{1}?access_token={2}'.format(username, job['id'], access_token),
match_querystring=True,
body=upload_response_body, status=200,
content_type='application/json')
res = mapbox.Uploader(access_token=access_token).status(job)
assert res.status_code == 200
res = mapbox.Uploader(access_token=access_token).status(job['id'])
assert res.status_code == 200
status = res.json()
assert job == status
@responses.activate
def test_delete():
job = json.loads(upload_response_body)
responses.add(
responses.DELETE,
'https://api.mapbox.com/uploads/v1/{0}/{1}?access_token={2}'.format(username, job['id'], access_token),
match_querystring=True,
body=None, status=204,
content_type='application/json')
res = mapbox.Uploader(access_token=access_token).delete(job)
assert res.status_code == 204
res = mapbox.Uploader(access_token=access_token).delete(job['id'])
assert res.status_code == 204
class MockSession(object):
"""Mocks a boto3 session."""
def __init__(self, *args, **kwargs):
self.bucket = None
self.key = None
pass
def resource(self, name):
self.resource_name = name
return self
def Object(self, bucket, key):
assert self.resource_name == 's3'
self.bucket = bucket
self.key = key
return self
def put(self, Body):
assert self.bucket
assert self.key
self.body = Body
return True
def Bucket(self, bucket):
self.bucket = bucket
return self
def upload_file(self, filename, key, Callback=None):
self.filename = filename
self.key = key
self.Callback = Callback
def upload_fileobj(self, data, key, Callback=None):
self.data = data
self.key = key
self.Callback = Callback
bytes_read = data.read(8192)
if bytes_read and self.Callback:
self.Callback(len(bytes_read))
while bytes_read:
bytes_read = data.read(8192)
if bytes_read and self.Callback:
self.Callback(len(bytes_read))
def copy(self, source, key, ExtraArgs=None, Callback=None,
SourceClient=None, Config=None):
self.source = source
self.key = key
self.Callback = Callback
@responses.activate
def test_stage(monkeypatch):
monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
with open('tests/moors.json', 'rb') as src:
stage_url = mapbox.Uploader(access_token=access_token).stage(src)
assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")
@responses.activate
def test_stage_filename(monkeypatch):
"""A filename works too"""
monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
stage_url = mapbox.Uploader(access_token=access_token).stage('tests/moors.json')
assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")
@responses.activate
def test_big_stage(tmpdir, monkeypatch):
"""Files larger than 1M are multipart uploaded."""
monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
# Make a temp file larger than 1MB.
bigfile = tmpdir.join('big.txt')
bigfile.write(','.join(('num' for num in range(1000000))))
assert bigfile.size() > 1000000
with bigfile.open(mode='rb') as src:
stage_url = mapbox.Uploader(access_token=access_token).stage(src)
assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")
@responses.activate
def test_upload(monkeypatch):
"""Upload a file and create a tileset"""
monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
match_querystring=True,
body=upload_response_body, status=201,
content_type='application/json')
def print_cb(num_bytes):
print("{0} bytes uploaded".format(num_bytes))
with open('tests/moors.json', 'rb') as src:
res = mapbox.Uploader(access_token=access_token).upload(src, 'test1', callback=print_cb)
assert res.status_code == 201
job = res.json()
assert job['tileset'] == "{0}.test1".format(username)
@responses.activate
def test_upload_error(monkeypatch):
"""Upload a file and create a tileset, fails with 409"""
monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
match_querystring=True,
body="", status=409,
content_type='application/json')
with open('tests/moors.json', 'rb') as src:
res = mapbox.Uploader(access_token=access_token).upload(src, 'test1')
assert res.status_code == 409
@responses.activate
def test_upload_patch(monkeypatch):
"""Upload a file and create a tileset in patch mode"""
monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
def ensure_patch(request):
payload = json.loads(request.body.decode())
assert payload['patch']
headers = {}
return (201, headers, upload_response_body)
# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)
responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')
responses.add_callback(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
callback=ensure_patch,
match_querystring=True,
content_type='application/json')
with open('tests/moors.json', 'rb') as src:
res = mapbox.Uploader(access_token=access_token).upload(
src, 'testuser.Test1', name='test1', patch=True)
assert res.status_code == 201
job = res.json()
assert job['tileset'] == "{0}.test1".format(username)
def test_upload_tileset_validation():
with pytest.raises(mapbox.errors.ValidationError):
with open('tests/moors.json', 'rb') as src:
# limited to 32 chars, try 40
mapbox.Uploader(access_token=access_token).upload(
src, 'username.' + 'aA' * 20, name='test1')
def test_upload_tileset_validation_specialchar():
with pytest.raises(mapbox.errors.ValidationError):
with open('tests/moors.json', 'rb') as src:
# limited to a-z0-9-_ chars
mapbox.Uploader(access_token=access_token).upload(
src, 'username.&#!', name='test1')
def test_upload_tileset_validation_username():
# even with 60 chars, the addition of the
# testuser username puts it over 64 chars
with pytest.raises(mapbox.errors.ValidationError):
with open('tests/moors.json', 'rb') as src:
mapbox.Uploader(access_token=access_token).upload(
src, 'a' * 60, name='test1', patch=True)
def test_create_tileset_validation():
# even with 60 chars, the addition of the username puts it over 64 chars
with pytest.raises(mapbox.errors.ValidationError):
mapbox.Uploader(access_token=access_token).create(
'http://example.com/test.json', 'a' * 60)