/
test_qiniu.py
237 lines (180 loc) · 6.89 KB
/
test_qiniu.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
# -*- coding: utf-8 -*-
# flake8: noqa
import os
import string
import random
import tempfile
import unittest
import pytest
from qiniu import DeprecatedApi, QiniuServiceException, Auth, set_default, etag
from qiniu import Bucket, put, putfile, resumable_put, resumable_putfile
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode
from qiniu.compat import is_py2, b
from qiniu.services.storage.uploader import _put
import qiniu.config
if is_py2:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
access_key = os.getenv('QINIU_ACCESS_KEY')
secret_key = os.getenv('QINIU_SECRET_KEY')
bucket_name = os.getenv('QINIU_TEST_BUCKET')
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
class UtilsTest(unittest.TestCase):
def test_urlsafe(self):
a = '你好\x96'
u = urlsafe_base64_encode(a)
assert b(a) == urlsafe_base64_decode(u)
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('http://www.qiniu.com?go=1', 'test', '')
assert token == 'abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y='
token = dummy_auth.token_of_request('http://www.qiniu.com?go=1', 'test', 'application/x-www-form-urlencoded')
assert token == 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4='
def test_deprecatedPolicy(self):
with pytest.raises(DeprecatedApi):
dummy_auth.upload_token('1', None, policy={'asyncOps': 1})
class BucketTestCase(unittest.TestCase):
q = Auth(access_key, secret_key)
bucket = Bucket(bucket_name, q)
def test_list(self):
ret, eof = self.bucket.list(limit=4)
assert eof is False
assert len(ret.get('items')) == 4
ret, eof = self.bucket.list(limit=100)
assert eof is True
def test_buckets(self):
ret = self.bucket.buckets()
assert bucket_name in ret
def test_pefetch(self):
ret = self.bucket.prefetch('python-sdk.html')
assert ret == {}
def test_fetch(self):
ret = self.bucket.fetch('http://developer.qiniu.com/docs/v6/sdk/python-sdk.html', 'fetch.html')
assert ret == {}
def test_stat(self):
ret = self.bucket.stat('python-sdk.html')
assert 'hash' in ret
ret = self.bucket.stat(['python-sdk.html'])
assert 'hash' in ret[0]['data']
def test_delete(self):
with pytest.raises(QiniuServiceException):
ret = self.bucket.delete('del')
ret = self.bucket.delete(['del'])
assert 612 == ret[0]['code'] and ret[0]['data']['error'] == 'no such file or directory'
def test_move(self):
key = 'copyto'+rand_string(8)
self.bucket.copy({'copyfrom': key})
ret = self.bucket.move({key: key + 'move'})
assert ret[0]['code'] == 200
ret = self.bucket.delete(key + 'move')
assert ret == {}
def test_copy(self):
key = 'copyto'+rand_string(8)
ret = self.bucket.copy({'copyfrom': key})
assert ret[0]['code'] == 200
ret = self.bucket.delete(key)
assert ret == {}
class UploaderTestCase(unittest.TestCase):
mime_type = "text/plain"
params = {'x:a': 'a'}
q = Auth(access_key, secret_key)
def test_put(self):
key = 'a\\b\\c"你好'
data = 'hello bubby!'
token = self.q.upload_token(bucket_name)
ret = put(token, key, data)
assert ret['key'] == key
key = ''
data = 'hello bubby!'
token = self.q.upload_token(bucket_name, key)
ret = put(token, key, data, check_crc=True)
assert ret['key'] == key
def test_putfile(self):
localfile = __file__
key = 'test_file'
token = self.q.upload_token(bucket_name, key)
ret = putfile(token, key, localfile, mime_type=self.mime_type, check_crc=True)
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)
with pytest.raises(QiniuServiceException):
_put(token, key, data, None, None, crc32=crc32)
def test_putWithoutKey(self):
key = None
data = 'hello bubby!'
token = self.q.upload_token(bucket_name)
ret = put(token, key, data)
assert ret['hash'] == ret['key']
data = 'hello bubby!'
token = self.q.upload_token(bucket_name, 'nokey2')
with pytest.raises(QiniuServiceException):
put(token, None, data)
def test_retry(self):
key = 'retry'
data = 'hello retry!'
set_default(default_up_host='a')
token = self.q.upload_token(bucket_name)
ret = put(token, key, data)
assert ret['key'] == key
qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
class ResumableUploaderTestCase(unittest.TestCase):
mime_type = "text/plain"
params = {'x:a': 'a'}
q = Auth(access_key, secret_key)
def test_putfile(self):
localfile = __file__
key = 'test_file_r'
token = self.q.upload_token(bucket_name, key)
ret = resumable_putfile(token, key, localfile, self.params, self.mime_type)
assert ret['key'] == key
def test_big_file(self):
key = 'big'
token = self.q.upload_token(bucket_name, key)
localfile = create_temp_file(4 * 1024 * 1024 + 1)
notify = lambda progress, total: progress
ret = resumable_putfile(token, key, localfile, self.params, self.mime_type, notify=notify)
assert ret['key'] == key
remove_temp_file(localfile)
def test_retry(self):
localfile = __file__
key = 'test_file_r_retry'
set_default(default_up_host='a')
token = self.q.upload_token(bucket_name, key)
ret = resumable_putfile(token, key, localfile, self.params, self.mime_type)
assert ret['key'] == key
qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
if __name__ == '__main__':
unittest.main()