forked from qiniu/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_qiniu.py
More file actions
1113 lines (975 loc) · 39.9 KB
/
Copy pathtest_qiniu.py
File metadata and controls
1113 lines (975 loc) · 39.9 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# flake8: noqa
import os
import string
import random
import tempfile
import functools
import requests
import unittest
import pytest
from freezegun import freeze_time
from qiniu import Auth, set_default, etag, PersistentFop, build_op, op_save, Zone, QiniuMacAuth
from qiniu import put_data, put_file, put_stream
from qiniu import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, build_batch_stat, \
build_batch_delete, DomainManager
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode, canonical_mime_header_key
from qiniu.compat import is_py2, is_py3, b, json
from qiniu.services.storage.uploader import _form_put
from qiniu.http import __return_wrapper as return_wrapper, qn_http_client
from qiniu.http.middleware import Middleware, RetryDomainsMiddleware
import qiniu.config
if is_py2:
import sys
import StringIO
import urllib
from imp import reload
reload(sys)
sys.setdefaultencoding('utf-8')
StringIO = StringIO.StringIO
urlopen = urllib.urlopen
elif is_py3:
import io
import urllib
StringIO = io.StringIO
urlopen = urllib.request.urlopen
if hasattr(functools, 'cache'):
cache_decorator = functools.cache
else:
def cache_decorator(func):
cache = {}
@functools.wraps(func)
def wrapper(*args, **kwargs):
key = (args, frozenset(kwargs.items()))
if key in cache:
return cache[key]
result = func(*args, **kwargs)
cache[key] = result
return result
return wrapper
access_key = os.getenv('QINIU_ACCESS_KEY')
secret_key = os.getenv('QINIU_SECRET_KEY')
bucket_name = os.getenv('QINIU_TEST_BUCKET')
hostscache_dir = None
dummy_access_key = 'abcdefghklmnopq'
dummy_secret_key = '1234567890'
dummy_auth = Auth(dummy_access_key, dummy_secret_key)
def rand_string(length):
lib = string.ascii_uppercase
return ''.join([random.choice(lib) for i in range(0, length)])
def create_temp_file(size):
t = tempfile.mktemp()
f = open(t, 'wb')
f.seek(size - 1)
f.write(b('0'))
f.close()
return t
def remove_temp_file(file):
try:
os.remove(file)
except OSError:
pass
def is_travis():
return os.environ['QINIU_TEST_ENV'] == 'travis'
class MiddlewareRecorder(Middleware):
def __init__(self, rec, label):
self.rec = rec
self.label = label
def __call__(self, request, nxt):
self.rec.append(
'bef_{0}{1}'.format(self.label, len(self.rec))
)
resp = nxt(request)
self.rec.append(
'aft_{0}{1}'.format(self.label, len(self.rec))
)
return resp
class HttpTest(unittest.TestCase):
def test_response_need_retry(self):
_ret, resp_info = qn_http_client.get('https://qiniu.com/index.html')
resp_info.req_id = 'mocked-req-id'
resp_info.error = None
def gen_case(code):
if 0 < code < 500:
return code, False
if code in [
501, 509, 573, 579, 608, 612, 614, 616, 618, 630, 631, 632, 640, 701
]:
return code, False
return code, True
cases = [
gen_case(i) for i in range(-1, 800)
]
for test_code, should_retry in cases:
resp_info.status_code = test_code
assert_msg = '{0} should{1} retry'.format(test_code, '' if should_retry else ' NOT')
assert resp_info.need_retry() == should_retry, assert_msg
def test_middlewares(self):
rec_ls = []
mw_a = MiddlewareRecorder(rec_ls, 'A')
mw_b = MiddlewareRecorder(rec_ls, 'B')
qn_http_client.get(
'https://qiniu.com/index.html',
middlewares=[
mw_a,
mw_b
]
)
assert rec_ls == ['bef_A0', 'bef_B1', 'aft_B2', 'aft_A3']
def test_retry_domains(self):
rec_ls = []
mw_rec = MiddlewareRecorder(rec_ls, 'rec')
ret, resp = qn_http_client.get(
'https://fake.pysdk.qiniu.com/index.html',
middlewares=[
RetryDomainsMiddleware(
backup_domains=[
'unavailable.pysdk.qiniu.com',
'qiniu.com'
],
max_retry_times=3
),
mw_rec
]
)
# ['bef_rec0', 'bef_rec1', 'bef_rec2'] are 'fake.pysdk.qiniu.com' with retried 3 times
# ['bef_rec3', 'bef_rec4', 'bef_rec5'] are 'unavailable.pysdk.qiniu.com' with retried 3 times
# ['bef_rec6', 'aft_rec7'] are 'qiniu.com' and it's success
assert rec_ls == [
'bef_rec0', 'bef_rec1', 'bef_rec2',
'bef_rec3', 'bef_rec4', 'bef_rec5',
'bef_rec6', 'aft_rec7'
]
assert ret == {}
assert resp.status_code == 200
def test_retry_domains_fail_fast(self):
rec_ls = []
mw_rec = MiddlewareRecorder(rec_ls, 'rec')
ret, resp = qn_http_client.get(
'https://fake.pysdk.qiniu.com/index.html',
middlewares=[
RetryDomainsMiddleware(
backup_domains=[
'unavailable.pysdk.qiniu.com',
'qiniu.com'
],
retry_condition=lambda _resp, _req: False
),
mw_rec
]
)
# ['bef_rec0'] are 'fake.pysdk.qiniu.com' with fail fast
assert rec_ls == ['bef_rec0']
assert ret is None
assert resp.status_code == -1
def test_json_decode_error(self):
def mock_res():
r = requests.Response()
r.status_code = 200
r.headers.__setitem__('X-Reqid', 'mockedReqid')
def json_func():
raise ValueError('%s: line %d column %d (char %d)' % ('Expecting value', 0, 0, 0))
r.json = json_func
return r
mocked_res = mock_res()
ret, _ = return_wrapper(mocked_res)
assert ret == {}
class UtilsTest(unittest.TestCase):
def test_urlsafe(self):
a = 'hello\x96'
u = urlsafe_base64_encode(a)
assert b(a) == urlsafe_base64_decode(u)
def test_canonical_mime_header_key(self):
field_names = [
":status",
":x-test-1",
":x-Test-2",
"content-type",
"CONTENT-LENGTH",
"oRiGin",
"ReFer",
"Last-Modified",
"acCePt-ChArsEt",
"x-test-3",
"cache-control",
]
expect_canonical_field_names = [
":status",
":x-test-1",
":x-Test-2",
"Content-Type",
"Content-Length",
"Origin",
"Refer",
"Last-Modified",
"Accept-Charset",
"X-Test-3",
"Cache-Control",
]
assert len(field_names) == len(expect_canonical_field_names)
for i in range(len(field_names)):
assert canonical_mime_header_key(field_names[i]) == expect_canonical_field_names[i]
class AuthTestCase(unittest.TestCase):
def test_token(self):
token = dummy_auth.token('test')
assert token == 'abcdefghklmnopq:mSNBTR7uS2crJsyFr2Amwv1LaYg='
def test_token_with_data(self):
token = dummy_auth.token_with_data('test')
assert token == 'abcdefghklmnopq:-jP8eEV9v48MkYiBGs81aDxl60E=:dGVzdA=='
def test_noKey(self):
with pytest.raises(ValueError):
Auth(None, None).token('nokey')
with pytest.raises(ValueError):
Auth('', '').token('nokey')
def test_token_of_request(self):
token = dummy_auth.token_of_request('https://www.qiniu.com?go=1', 'test', '')
assert token == 'abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y='
token = dummy_auth.token_of_request('https://www.qiniu.com?go=1', 'test', 'application/x-www-form-urlencoded')
assert token == 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4='
def test_QiniuMacRequestsAuth(self):
auth = QiniuMacAuth("ak", "sk")
test_cases = [
{
"method": "GET",
"host": None,
"url": "",
"qheaders": {
"X-Qiniu-": "a",
"X-Qiniu": "b",
"Content-Type": "application/x-www-form-urlencoded",
},
"content_type": "application/x-www-form-urlencoded",
"body": "{\"name\": \"test\"}",
"except_sign_token": "ak:0i1vKClRDWFyNkcTFzwcE7PzX74=",
},
{
"method": "GET",
"host": None,
"url": "",
"qheaders": {
"Content-Type": "application/json",
},
"content_type": "application/json",
"body": "{\"name\": \"test\"}",
"except_sign_token": "ak:K1DI0goT05yhGizDFE5FiPJxAj4=",
},
{
"method": "POST",
"host": None,
"url": "",
"qheaders": {
"Content-Type": "application/json",
"X-Qiniu": "b",
},
"content_type": "application/json",
"body": "{\"name\": \"test\"}",
"except_sign_token": "ak:0ujEjW_vLRZxebsveBgqa3JyQ-w=",
},
{
"method": "GET",
"host": "upload.qiniup.com",
"url": "http://upload.qiniup.com",
"qheaders": {
"X-Qiniu-": "a",
"X-Qiniu": "b",
"Content-Type": "application/x-www-form-urlencoded",
},
"content_type": "application/x-www-form-urlencoded",
"body": "{\"name\": \"test\"}",
"except_sign_token": "ak:GShw5NitGmd5TLoo38nDkGUofRw=",
},
{
"method": "GET",
"host": "upload.qiniup.com",
"url": "http://upload.qiniup.com",
"qheaders": {
"Content-Type": "application/json",
"X-Qiniu-Bbb": "BBB",
"X-Qiniu-Aaa": "DDD",
"X-Qiniu-": "a",
"X-Qiniu": "b",
},
"content_type": "application/json",
"body": "{\"name\": \"test\"}",
"except_sign_token": "ak:DhNA1UCaBqSHCsQjMOLRfVn63GQ=",
},
{
"method": "GET",
"host": "upload.qiniup.com",
"url": "http://upload.qiniup.com",
"qheaders": {
"Content-Type": "application/x-www-form-urlencoded",
"X-Qiniu-Bbb": "BBB",
"X-Qiniu-Aaa": "DDD",
"X-Qiniu-": "a",
"X-Qiniu": "b",
},
"content_type": "application/x-www-form-urlencoded",
"body": "name=test&language=go",
"except_sign_token": "ak:KUAhrYh32P9bv0COD8ugZjDCmII=",
},
{
"method": "GET",
"host": "upload.qiniup.com",
"url": "http://upload.qiniup.com",
"qheaders": {
"Content-Type": "application/x-www-form-urlencoded",
"X-Qiniu-Bbb": "BBB",
"X-Qiniu-Aaa": "DDD",
},
"content_type": "application/x-www-form-urlencoded",
"body": "name=test&language=go",
"except_sign_token": "ak:KUAhrYh32P9bv0COD8ugZjDCmII=",
},
{
"method": "GET",
"host": "upload.qiniup.com",
"url": "http://upload.qiniup.com/mkfile/sdf.jpg",
"qheaders": {
"Content-Type": "application/x-www-form-urlencoded",
"X-Qiniu-Bbb": "BBB",
"X-Qiniu-Aaa": "DDD",
"X-Qiniu-": "a",
"X-Qiniu": "b",
},
"content_type": "application/x-www-form-urlencoded",
"body": "name=test&language=go",
"except_sign_token": "ak:fkRck5_LeyfwdkyyLk-hyNwGKac=",
},
{
"method": "GET",
"host": "upload.qiniup.com",
"url": "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df",
"qheaders": {
"Content-Type": "application/x-www-form-urlencoded",
"X-Qiniu-Bbb": "BBB",
"X-Qiniu-Aaa": "DDD",
"X-Qiniu-": "a",
"X-Qiniu": "b",
},
"content_type": "application/x-www-form-urlencoded",
"body": "name=test&language=go",
"except_sign_token": "ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4=",
},
]
for test_case in test_cases:
sign_token = auth.token_of_request(
method=test_case["method"],
host=test_case["host"],
url=test_case["url"],
qheaders=auth.qiniu_headers(test_case["qheaders"]),
content_type=test_case["content_type"],
body=test_case["body"],
)
assert sign_token == test_case["except_sign_token"]
def test_verify_callback(self):
body = 'name=sunflower.jpg&hash=Fn6qeQi4VDLQ347NiRm-RlQx_4O2&location=Shanghai&price=1500.00&uid=123'
url = 'test.qiniu.com/callback'
ok = dummy_auth.verify_callback('QBox abcdefghklmnopq:ZWyeM5ljWMRFwuPTPOwQ4RwSto4=', url, body)
assert ok
class BucketTestCase(unittest.TestCase):
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
def test_list(self):
ret, eof, info = self.bucket.list(bucket_name, limit=4)
assert eof is False
assert len(ret.get('items')) == 4
ret, eof, info = self.bucket.list(bucket_name, limit=1000)
print(ret, eof, info)
assert info.status_code == 200
def test_buckets(self):
ret, info = self.bucket.buckets()
print(info)
assert bucket_name in ret
def test_prefetch(self):
ret, info = self.bucket.prefetch(bucket_name, 'python-sdk.html', hostscache_dir=hostscache_dir)
print(info)
assert ret['key'] == 'python-sdk.html'
def test_fetch(self):
ret, info = self.bucket.fetch('https://developer.qiniu.com/kodo/sdk/python', bucket_name,
'fetch.html', hostscache_dir=hostscache_dir)
print(info)
assert ret['key'] == 'fetch.html'
assert 'hash' in ret
def test_fetch_without_key(self):
ret, info = self.bucket.fetch('https://developer.qiniu.com/kodo/sdk/python', bucket_name,
hostscache_dir=hostscache_dir)
print(info)
assert ret['key'] == ret['hash']
assert 'hash' in ret
def test_stat(self):
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
print(info)
assert 'hash' in ret
def test_delete(self):
ret, info = self.bucket.delete(bucket_name, 'del')
print(info)
assert ret is None
assert info.status_code == 612
def test_rename(self):
key = 'renameto' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
key2 = key + 'move'
ret, info = self.bucket.rename(bucket_name, key, key2)
print(info)
assert ret == {}
ret, info = self.bucket.delete(bucket_name, key2)
print(info)
assert ret == {}
def test_copy(self):
key = 'copyto' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
print(info)
assert ret == {}
ret, info = self.bucket.delete(bucket_name, key)
print(info)
assert ret == {}
def test_change_mime(self):
ret, info = self.bucket.change_mime(bucket_name, 'python-sdk.html', 'text/html')
print(info)
assert ret == {}
def test_change_type(self):
target_key = 'copyto' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, target_key)
ret, info = self.bucket.change_type(bucket_name, target_key, 1)
print(info)
assert ret == {}
ret, info = self.bucket.stat(bucket_name, target_key)
print(info)
assert 'type' in ret
self.bucket.delete(bucket_name, target_key)
def test_copy_force(self):
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, 'copyfrom', force='true')
print(info)
assert info.status_code == 200
def test_batch_copy(self):
key = 'copyto' + rand_string(8)
ops = build_batch_copy(bucket_name, {'copyfrom': key}, bucket_name)
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
ops = build_batch_delete(bucket_name, [key])
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_copy_force(self):
ops = build_batch_copy(bucket_name, {'copyfrom': 'copyfrom'}, bucket_name, force='true')
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_move(self):
key = 'moveto' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
key2 = key + 'move'
ops = build_batch_move(bucket_name, {key: key2}, bucket_name)
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
ret, info = self.bucket.delete(bucket_name, key2)
print(info)
assert ret == {}
def test_batch_move_force(self):
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, 'copyfrom', force='true')
print(info)
assert info.status_code == 200
ops = build_batch_move(bucket_name, {'copyfrom': 'copyfrom'}, bucket_name, force='true')
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_rename(self):
key = 'rename' + rand_string(8)
self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
key2 = key + 'rename'
ops = build_batch_move(bucket_name, {key: key2}, bucket_name)
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
ret, info = self.bucket.delete(bucket_name, key2)
print(info)
assert ret == {}
def test_batch_rename_force(self):
ret, info = self.bucket.rename(bucket_name, 'copyfrom', 'copyfrom', force='true')
print(info)
assert info.status_code == 200
ops = build_batch_rename(bucket_name, {'copyfrom': 'copyfrom'}, force='true')
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_batch_stat(self):
ops = build_batch_stat(bucket_name, ['python-sdk.html'])
ret, info = self.bucket.batch(ops)
print(info)
assert ret[0]['code'] == 200
def test_delete_after_days(self):
days = '5'
ret, info = self.bucket.delete_after_days(bucket_name, 'invaild.html', days)
assert info.status_code == 612
key = 'copyto' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
ret, info = self.bucket.delete_after_days(bucket_name, key, days)
assert info.status_code == 200
def test_set_object_lifecycle(self):
key = 'test_set_object_lifecycle' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
assert info.status_code == 200
ret, info = self.bucket.set_object_lifecycle(
bucket=bucket_name,
key=key,
to_line_after_days=10,
to_archive_after_days=20,
to_deep_archive_after_days=30,
delete_after_days=40
)
assert info.status_code == 200
def test_set_object_lifecycle_with_cond(self):
key = 'test_set_object_lifecycle_cond' + rand_string(8)
ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
assert info.status_code == 200
ret, info = self.bucket.stat(bucket_name, key)
assert info.status_code == 200
key_hash = ret['hash']
ret, info = self.bucket.set_object_lifecycle(
bucket=bucket_name,
key=key,
to_line_after_days=10,
to_archive_after_days=20,
to_deep_archive_after_days=30,
delete_after_days=40,
cond={
'hash': key_hash
}
)
assert info.status_code == 200
def test_list_domains(self):
ret, info = self.bucket.list_domains(bucket_name)
print(info)
assert info.status_code == 200
assert isinstance(ret, list)
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date(self):
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
assert ret is None
assert info.status_code == 403
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date_with_disable_date_sign(self):
q = Auth(access_key, secret_key, disable_qiniu_timestamp_signature=True)
bucket = BucketManager(q)
ret, info = bucket.stat(bucket_name, 'python-sdk.html')
assert 'hash' in ret
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date_env(self):
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = 'True'
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
os.unsetenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE')
assert 'hash' in ret
@freeze_time("1970-01-01")
def test_invalid_x_qiniu_date_env_be_ignored(self):
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = 'True'
q = Auth(access_key, secret_key, disable_qiniu_timestamp_signature=False)
bucket = BucketManager(q)
ret, info = bucket.stat(bucket_name, 'python-sdk.html')
os.unsetenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE')
assert ret is None
assert info.status_code == 403
@cache_decorator
def get_valid_up_host():
zone = Zone()
try:
hosts = json.loads(
zone.bucket_hosts(access_key, bucket_name)
).get('hosts')
up_host = 'https://' + hosts[0].get('up', {}).get('domains')[0]
except IndexError:
up_host = 'https://upload.qiniup.com'
return up_host
class UploaderTestCase(unittest.TestCase):
mime_type = "text/plain"
params = {'x:a': 'a'}
metadata = {
'x-qn-meta-name': 'qiniu',
'x-qn-meta-age': '18'
}
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
def test_put(self):
key = 'a\\b\\c"hello'
data = 'hello bubby!'
token = self.q.upload_token(bucket_name)
ret, info = put_data(token, key, data)
print(info)
assert ret['key'] == key
def test_put_crc(self):
key = ''
data = 'hello bubby!'
token = self.q.upload_token(bucket_name, key)
ret, info = put_data(token, key, data, check_crc=True)
print(info)
assert ret['key'] == key
def test_putfile(self):
localfile = __file__
key = 'test_file'
token = self.q.upload_token(bucket_name, key)
ret, info = put_file(token, key, localfile, mime_type=self.mime_type, check_crc=True)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
def test_putInvalidCrc(self):
key = 'test_invalid'
data = 'hello bubby!'
crc32 = 'wrong crc32'
token = self.q.upload_token(bucket_name)
ret, info = _form_put(token, key, data, None, None, crc=crc32)
print(info)
assert ret is None
assert info.status_code == 400
def test_putWithoutKey(self):
key = None
data = 'hello bubby!'
token = self.q.upload_token(bucket_name)
ret, info = put_data(token, key, data)
print(info)
assert ret['hash'] == ret['key']
data = 'hello bubby!'
token = self.q.upload_token(bucket_name, 'nokey2')
ret, info = put_data(token, None, data)
print(info)
assert ret is None
assert info.status_code == 403 # key not match
def test_withoutRead_withoutSeek_retry(self):
try:
key = 'retry'
data = 'hello retry!'
up_host_backup = get_valid_up_host()
set_default(default_zone=Zone('http://a', up_host_backup))
token = self.q.upload_token(bucket_name)
ret, info = put_data(token, key, data)
print(info)
assert ret['key'] == key
assert ret['hash'] == 'FlYu0iBR1WpvYi4whKXiBuQpyLLk'
finally:
set_default(default_zone=Zone())
qiniu.config._is_customized_default['default_zone'] = False
def test_putData_without_fname(self):
if is_travis():
return
localfile = create_temp_file(30 * 1024 * 1024)
key = 'test_putData_without_fname'
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name)
ret, info = put_data(token, key, input_stream)
print(info)
assert ret is not None
def test_putData_without_fname1(self):
if is_travis():
return
localfile = create_temp_file(30 * 1024 * 1024)
key = 'test_putData_without_fname1'
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name)
ret, info = put_data(token, key, input_stream, self.params, self.mime_type, False, None, "")
print(info)
assert ret is not None
def test_putData_without_fname2(self):
if is_travis():
return
localfile = create_temp_file(30 * 1024 * 1024)
key = 'test_putData_without_fname2'
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name)
ret, info = put_data(token, key, input_stream, self.params, self.mime_type, False, None, " ")
print(info)
assert ret is not None
def test_put_file_with_metadata(self):
localfile = __file__
key = 'test_file_with_metadata'
token = self.q.upload_token(bucket_name, key)
ret, info = put_file(token, key, localfile, metadata=self.metadata)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
ret, info = self.bucket.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'
def test_put_data_with_metadata(self):
key = 'put_data_with_metadata'
data = 'hello metadata!'
token = self.q.upload_token(bucket_name, key)
ret, info = put_data(token, key, data, metadata=self.metadata)
assert ret['key'] == key
ret, info = self.bucket.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'
class ResumableUploaderTestCase(unittest.TestCase):
mime_type = "text/plain"
params = {'x:a': 'a'}
metadata = {
'x-qn-meta-name': 'qiniu',
'x-qn-meta-age': '18'
}
q = Auth(access_key, secret_key)
bucket = BucketManager(q)
def test_put_stream(self):
localfile = __file__
key = 'test_file_r'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(__file__), size, hostscache_dir,
self.params,
self.mime_type, part_size=None, version=None, bucket_name=None)
assert ret['key'] == key
def test_put_stream_v2_without_bucket_name(self):
localfile = __file__
key = 'test_file_r'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(__file__), size, hostscache_dir,
self.params,
self.mime_type, part_size=1024 * 1024 * 10, version='v2')
assert ret['key'] == key
def test_put_2m_stream_v2(self):
localfile = create_temp_file(2 * 1024 * 1024 + 1)
key = 'test_file_r'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir,
self.params,
self.mime_type, part_size=1024 * 1024 * 4, version='v2', bucket_name=bucket_name)
assert ret['key'] == key
remove_temp_file(localfile)
def test_put_4m_stream_v2(self):
localfile = create_temp_file(4 * 1024 * 1024)
key = 'test_file_r'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir,
self.params,
self.mime_type, part_size=1024 * 1024 * 4, version='v2', bucket_name=bucket_name)
assert ret['key'] == key
remove_temp_file(localfile)
def test_put_10m_stream_v2(self):
localfile = create_temp_file(10 * 1024 * 1024 + 1)
key = 'test_file_r'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir,
self.params,
self.mime_type, part_size=1024 * 1024 * 4, version='v2', bucket_name=bucket_name)
assert ret['key'] == key
remove_temp_file(localfile)
def test_put_stream_v2_without_key(self):
part_size = 1024 * 1024 * 4
localfile = create_temp_file(part_size + 1)
key = None
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir,
self.params,
self.mime_type, part_size=part_size, version='v2', bucket_name=bucket_name)
assert ret['key'] == ret['hash']
remove_temp_file(localfile)
def test_put_stream_v2_with_empty_return_body(self):
part_size = 1024 * 1024 * 4
localfile = create_temp_file(part_size + 1)
key = 'test_file_empty_return_body'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key, policy={'returnBody': ' '})
ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir,
self.params,
self.mime_type, part_size=part_size, version='v2', bucket_name=bucket_name)
assert info.status_code == 200
assert ret == {}
remove_temp_file(localfile)
def test_big_file(self):
key = 'big'
token = self.q.upload_token(bucket_name, key)
localfile = create_temp_file(4 * 1024 * 1024 + 1)
progress_handler = lambda progress, total: progress
ret, info = put_file(token, key, localfile, self.params, self.mime_type, progress_handler=progress_handler)
print(info)
assert ret['key'] == key
remove_temp_file(localfile)
def test_retry(self):
try:
localfile = __file__
key = 'test_file_r_retry'
token = self.q.upload_token(bucket_name, key)
up_host_backup = get_valid_up_host()
set_default(default_zone=Zone('http://a', up_host_backup))
ret, info = put_file(token, key, localfile, self.params, self.mime_type)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
finally:
set_default(default_zone=Zone())
qiniu.config._is_customized_default['default_zone'] = False
def test_put_stream_with_key_limits(self):
localfile = __file__
key = 'test_file_r'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key, policy={'keylimit': ['test_file_d']})
ret, info = put_stream(token, key, input_stream, os.path.basename(__file__), size, hostscache_dir,
self.params,
self.mime_type)
assert info.status_code == 403
token = self.q.upload_token(bucket_name, key, policy={'keylimit': ['test_file_d', 'test_file_r']})
ret, info = put_stream(token, key, input_stream, os.path.basename(__file__), size, hostscache_dir,
self.params,
self.mime_type)
assert info.status_code == 200
def test_put_stream_with_metadata(self):
localfile = __file__
key = 'test_put_stream_with_metadata'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(__file__), size, hostscache_dir,
self.params, self.mime_type,
part_size=None, version=None, bucket_name=None, metadata=self.metadata)
assert ret['key'] == key
ret, info = self.bucket.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'
def test_put_stream_v2_with_metadata(self):
part_size = 1024 * 1024 * 4
localfile = create_temp_file(part_size + 1)
key = 'test_put_stream_v2_with_metadata'
size = os.stat(localfile).st_size
with open(localfile, 'rb') as input_stream:
token = self.q.upload_token(bucket_name, key)
ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir,
self.params, self.mime_type,
part_size=part_size, version='v2', bucket_name=bucket_name, metadata=self.metadata)
assert ret['key'] == key
remove_temp_file(localfile)
ret, info = self.bucket.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'
class DownloadTestCase(unittest.TestCase):
q = Auth(access_key, secret_key)
def test_private_url(self):
private_bucket_domain = 'private-sdk.peterpy.cn'
private_key = 'gogopher.jpg'
base_url = 'http://%s/%s' % (private_bucket_domain, private_key)
private_url = self.q.private_download_url(base_url, expires=3600)
print(private_url)
r = requests.get(private_url)
assert r.status_code == 200
class MediaTestCase(unittest.TestCase):
def test_pfop(self):
q = Auth(access_key, secret_key)
pfop = PersistentFop(q, 'testres', 'sdktest')
op = op_save('avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240', 'pythonsdk', 'pfoptest')
ops = []
ops.append(op)
ret, info = pfop.execute('sintel_trailer.mp4', ops, 1)
print(info)
assert ret['persistentId'] is not None
class EtagTestCase(unittest.TestCase):
def test_zero_size(self):