-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_file.py
More file actions
274 lines (214 loc) · 7.28 KB
/
Copy pathtest_file.py
File metadata and controls
274 lines (214 loc) · 7.28 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
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import io
import six
if six.PY2:
from urlparse import urlparse
if six.PY3:
from urllib.parse import urlparse
import requests
from nose.tools import with_setup # type: ignore
from nose.tools import assert_raises # type: ignore
from nose.tools import raises # type: ignore
import leancloud
from leancloud import File, LeanCloudError
from leancloud import ACL
__author__ = "asaka"
def setup_func():
leancloud.init(os.environ["APP_ID"], master_key=os.environ["MASTER_KEY"])
def setup_without_master_key():
leancloud.init(os.environ["APP_ID"], os.environ["APP_KEY"])
def test_basic(): # type: () -> None
def fn(s):
f = File("Blah", s, mime_type="text/plain")
assert f.name == "Blah"
assert f._metadata["size"] == 14
assert f.size == 14
assert f._metadata["_checksum"] == "55e562bfee2bde4f9e71b8885eb5e303"
b = b"blah blah blah"
fn(io.BytesIO(b))
fn(memoryview(b))
import tempfile
f = tempfile.SpooledTemporaryFile()
f.write(b)
fn(f)
if six.PY2:
import StringIO
import cStringIO
fn(StringIO.StringIO(b))
fn(cStringIO.StringIO(b))
fn(buffer(b)) # noqa: F821
if six.PY3:
fn(b)
def test_create_with_url(): # type: () -> None
f = File.create_with_url(
"xxx",
u"http://i1.wp.com/leancloud.cn/images/static/default-avatar.png",
meta_data={},
)
assert f._url == "http://i1.wp.com/leancloud.cn/images/static/default-avatar.png"
assert f.url is None
def test_create_without_data(): # type: () -> None
f = File.create_without_data("a123")
assert f.id == "a123"
def test_acl(): # type: () -> None
acl_ = ACL()
f = File("Blah", io.BytesIO(b"xxx"))
assert_raises(TypeError, f.set_acl, "a")
f.set_acl(acl_)
assert f.get_acl() == acl_
@with_setup(setup_func)
def test_save(): # type: () -> None
user = leancloud.User()
name = "user1_name"
passwd = "password"
try:
user.login(name, passwd)
except LeanCloudError as e:
if e.code == 211:
user.set_username(name)
user.set_password(passwd)
user.sign_up()
user.login(name, passwd)
f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
f.save()
assert f.owner_id == user.id
assert f.id
assert f.name == "Blah.txt"
assert f.mime_type == "text/plain"
assert not f.url.endswith(".")
assert f.created_at == f.updated_at
@with_setup(setup_func)
def test_save_with_specified_key(): # type: () -> None
f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
user_specified_key = "abc"
f.key = user_specified_key
f.save()
assert f.id
assert f.created_at == f.updated_at
assert f.name == "Blah.txt"
assert f.mime_type == "text/plain"
path = urlparse(f.url).path
if path.startswith("/avos-cloud-"): # old school aws s3 file url
assert path.split("/")[2] == user_specified_key
elif f.url.startswith("https://lc-gluttony"): # new aws s3 gluttony bucket
gluttony_path = "/" + os.environ["APP_ID"][0:12] + "/" + user_specified_key
assert path == gluttony_path
else:
assert path == "/" + user_specified_key
@with_setup(setup_without_master_key)
def test_save_with_specified_key_but_without_master_key(): # type: () -> None
f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
f.key = "abc"
try:
f.save()
except LeanCloudError as e:
if e.code == 1 and e.error.startswith("Unsupported file key"):
pass
@with_setup(setup_func)
def test_query(): # type: () -> None
files = leancloud.Query("File").find()
for f in files:
assert isinstance(f, File)
assert f.id
assert f.url
assert f.name
assert f.metadata
assert f.created_at == f.updated_at
if f.metadata.get("__source") == 'external':
assert f.url
else:
assert f.key
assert isinstance(leancloud.File.query.first(), File)
@with_setup(setup_func)
def test_scan(): # type: () -> None
files = leancloud.Query("File").scan()
for f in files:
assert isinstance(f, File)
assert f.created_at == f.updated_at
assert f.name
assert f.metadata
if f.metadata.get("__source") == 'external':
assert f.url
else:
assert f.key
@with_setup(setup_func)
def test_save_external(): # type: () -> None
file_name = "lenna.jpg"
file_url = "http://i1.wp.com/leancloud.cn/images/static/default-avatar.png"
f = File.create_with_url(file_name, file_url)
f.save()
assert f.id
assert f.created_at == f.updated_at
file_on_cloud = File.create_without_data(f.id)
file_on_cloud.fetch()
assert file_on_cloud.name == file_name
assert file_on_cloud.url == file_url
@raises(ValueError)
def test_thumbnail_url_erorr(): # type: () -> None
f = File.create_with_url("xx", "")
f.get_thumbnail_url(100, 100)
@with_setup(setup_func)
@raises(ValueError)
def test_thumbnail_size_erorr(): # type: () -> None
r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
b = io.BytesIO(r.content)
f = File("Lenna2.jpg", b)
f.save()
assert f.id
f.get_thumbnail_url(-1, -1)
f.get_thumbnail_url(1, 1, quality=110)
@with_setup(setup_func)
def test_thumbnail(): # type: () -> None
r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
b = io.BytesIO(r.content)
f = File("Lenna2.jpg", b)
f.save()
assert f.id
url = f.get_thumbnail_url(100, 100)
assert url.endswith("?imageView/2/w/100/h/100/q/100/format/png")
@with_setup(setup_func)
def test_destroy(): # type: () -> None
r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
b = io.BytesIO(r.content)
f = File("Lenna2.jpg", b)
f.save()
assert f.id
f.destroy()
@with_setup(setup_func)
def test_file_callback(): # type: () -> None
d = {}
def noop(token, *args, **kwargs):
d["token"] = token
f = File("xxx", io.BytesIO(b"xxx"))
f._save_to_s3 = noop
f._save_to_qiniu = noop
f._save_to_qcloud = noop
f.save()
f._save_callback(d["token"], False)
# time.sleep(3)
# File should be deleted by API server
# assert_raises(leancloud.LeanCloudError, File.query().get, f.id)
@with_setup(setup_func)
def test_fetch(): # type: () -> None
r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
b = io.BytesIO(r.content)
f = File("Lenna2.jpg", b)
f.metadata["foo"] = "bar"
f.save()
fetched = File.create_without_data(f.id)
fetched.fetch()
normalized_f_url = f.url.split("/")[-1]
normalized_fetched_url = f.url.split("/")[-1]
assert fetched.id == f.id
assert fetched.metadata == f.metadata
assert fetched.name == f.name
assert fetched.size == f.size
assert fetched.url == f.url or normalized_fetched_url == normalized_f_url
f.destroy()
def test_checksum(): # type: () -> None
f = File("Blah", open("tests/sample_text.txt", "rb"))
assert f._metadata["_checksum"] == "d0588d95e45eed70745ffabdf0b18acd"