forked from leancloud/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engine.py
More file actions
363 lines (280 loc) · 10.6 KB
/
Copy pathtest_engine.py
File metadata and controls
363 lines (280 loc) · 10.6 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
# coding: utf-8
import os
import time
import json
import requests
from wsgi_intercept import requests_intercept, add_wsgi_intercept
import leancloud
from leancloud import Engine
from leancloud import cloudfunc
from leancloud.engine import authorization
from request_generator import generate_request
from leancloud import LeanCloudError
__author__ = 'asaka <lan@leancloud.rocks>'
env = None
TEST_APP_ID = os.environ['APP_ID']
TEST_APP_KEY = os.environ['APP_KEY']
TEST_MASTER_KEY = os.environ['MASTER_KEY']
sign_by_app_key = generate_request(TEST_APP_KEY)
sign_by_master_key = generate_request(TEST_MASTER_KEY, True)
NORMAL_HEADERS = {
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
}
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello LeanCloud']
engine = Engine(app)
def make_app():
return engine
host, port = 'localhost', 80
url = 'http://{0}:{1}/'.format(host, port)
HookObject = leancloud.Object.extend('HookObject')
def setup():
leancloud.client.USE_MASTER_KEY = None
leancloud.client.APP_ID = None
leancloud.client.APP_KEY = None
leancloud.client.MASTER_KEY = None
leancloud.init(TEST_APP_ID, TEST_APP_KEY)
authorization._ENABLE_TEST = True
authorization.APP_ID = TEST_APP_ID
authorization.APP_KEY = TEST_APP_KEY
authorization.MASTER_KEY = TEST_MASTER_KEY
requests_intercept.install()
add_wsgi_intercept(host, port, make_app)
@engine.define
def hello(**params):
return 'hello'
def teardown():
requests_intercept.uninstall()
def test_origin_response():
resp = requests.get(url)
assert resp.ok
assert resp.content == 'Hello LeanCloud'
def test_compatibility():
requests.get(url + '/1/functions/hello')
assert '_app_params' in authorization.current_environ
requests.get(url + '/1.1/functions/hello')
assert '_app_params' in authorization.current_environ
def test_app_params_1():
requests.get(url + '/__engine/1/functions/hello')
assert '_app_params' in authorization.current_environ
def test_app_params_2():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-avoscloud-application-id': 'foo',
'x-avoscloud-application-key': 'bar',
'x-avoscloud-session-token': 'baz',
})
env = authorization.current_environ
assert env['_app_params']['id'] == 'foo'
assert env['_app_params']['key'] == 'bar'
assert env['_app_params']['session_token'] == 'baz'
def test_app_params_3():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-avoscloud-request-sign': sign_by_app_key
})
env = authorization.current_environ
assert env['_app_params']['key'] == TEST_APP_KEY
def test_app_params_4():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-avoscloud-request-sign': sign_by_master_key
})
env = authorization.current_environ
assert env['_app_params']['master_key'] == TEST_MASTER_KEY
def test_app_params_5():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-avoscloud-application-id': 'foo',
'x-avoscloud-master-key': 'bar',
})
env = authorization.current_environ
assert env['_app_params']['id'] == 'foo'
assert env['_app_params']['master_key'] == 'bar'
def test_short_app_params_1():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-lc-id': 'foo',
'x-lc-key': 'bar',
'x-lc-session': 'baz',
})
env = authorization.current_environ
assert env['_app_params']['id'] == 'foo'
assert env['_app_params']['key'] == 'bar'
assert env['_app_params']['master_key'] is None
assert env['_app_params']['session_token'] == 'baz'
def test_short_app_params_2():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-lc-id': 'foo',
'x-lc-key': 'bar,master',
'x-lc-session': 'baz',
})
env = authorization.current_environ
assert env['_app_params']['id'] == 'foo'
assert env['_app_params']['key'] is None
assert env['_app_params']['master_key'] == 'bar'
assert env['_app_params']['session_token'] == 'baz'
def test_short_app_params_3():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-lc-sign': sign_by_app_key
})
env = authorization.current_environ
assert env['_app_params']['key'] == TEST_APP_KEY
assert env['_app_params']['master_key'] is None
def test_short_app_params_4():
requests.get(url + '/__engine/1/functions/hello', headers={
'x-lc-sign': sign_by_master_key
})
env = authorization.current_environ
assert env['_app_params']['key'] is None
assert env['_app_params']['master_key'] == TEST_MASTER_KEY
def test_body_params():
requests.get(url + '/__engine/1/functions/hello', headers={
'Content-Type': 'text/plain',
}, data=json.dumps({
'_ApplicationId': 'foo',
'_ApplicationKey': 'bar',
'_MasterKey': 'baz',
'_SessionToken': 'qux',
}))
env = authorization.current_environ
assert env['_app_params']['id'] == 'foo'
assert env['_app_params']['key'] == 'bar'
assert env['_app_params']['master_key'] == 'baz'
assert env['_app_params']['session_token'] == 'qux'
def test_authorization_1():
response = requests.get(url + '/__engine/1/functions/hello', headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
})
assert response.ok
assert response.json() == {u'result': u'hello'}
def test_authorization_2():
response = requests.get(url + '/__engine/1/functions/hello', headers={
'x-lc-id': TEST_APP_ID,
'x-lc-key': TEST_MASTER_KEY,
})
assert response.ok
assert response.json() == {u'result': u'hello'}
def test_authorization_3():
response = requests.get(url + '/__engine/1/functions/hello', headers={
'x-avoscloud-application-id': 'foo',
'x-avoscloud-application-key': 'bar',
})
assert response.status_code == 401
def test_register_cloud_func():
@engine.define
def ping(**params):
assert params == {"foo": ["bar", "baz"]}
return 'pong'
response = requests.post(url + '/__engine/1/functions/ping', headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
}, json={'foo': ['bar', 'baz']})
assert response.ok
assert response.json() == {u'result': u'pong'}
def test_before_save_hook():
@engine.before_save('HookObject')
def before_hook_object_save(obj):
obj.set('beforeSaveHookInserted', True)
response = requests.post(url + '/__engine/1/functions/HookObject/beforeSave', json={
'object': {'clientValue': 'blah'}
}, headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
})
assert response.ok
assert response.json() == {"beforeSaveHookInserted": True, "clientValue": "blah"}
def test_after_save_hook():
@engine.after_save('HookObject')
def after_hook_object_save(obj):
pass
response = requests.post(url + '/__engine/1/functions/HookObject/afterSave', json={
'object': {'clientValue': 'blah'}
}, headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
})
assert response.ok
assert response.json() == {'result': 'ok'}
def test_before_delete_hook():
@engine.before_delete('HookObject')
def before_hook_object_delete(obj):
pass
response = requests.post(url + '/__engine/1/functions/HookObject/beforeDelete', json={
'object': {}
}, headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
})
assert response.ok
assert response.json() == {}
def test_on_login():
@engine.on_login
def on_login(user):
assert isinstance(user, leancloud.User)
response = requests.post(url + '/__engine/1.1/functions/_User/onLogin', json={
'object': {}
}, headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
})
assert response.ok
def test_bigquery():
@engine.on_bigquery('end')
def on_bigquery_end(ok, data):
assert ok is False
assert data == {
"id": u"job id",
"status": u"OK/ERROR",
"message": u"当 status 为 ERROR 时的错误消息"
}
response = requests.post(url + '/__engine/1/functions/BigQuery/onComplete', headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
}, json={
"id": u"job id",
"status": u"OK/ERROR",
"message": u"当 status 为 ERROR 时的错误消息"
})
assert response.ok
def test_client():
leancloud.init(os.environ['APP_ID'], os.environ['APP_KEY'])
assert cloudfunc.run('add', a=1, b=2) == 3
def test_request_sms_code():
leancloud.init(os.environ['APP_ID'], master_key=os.environ['MASTER_KEY'])
try:
cloudfunc.request_sms_code('13111111111')
except LeanCloudError, e:
# 短信发送过于频繁或者欠费
if e.code in (601, 160):
pass
else:
raise e
def test_current_user():
leancloud.init(os.environ['APP_ID'], master_key=os.environ['MASTER_KEY'])
saved_user = leancloud.User()
saved_user.set('username', 'user{}'.format(int(time.time())))
saved_user.set('password', 'password')
saved_user.set_email('{}@leancloud.rocks'.format(int(time.time())))
saved_user.sign_up()
session_token = saved_user.get_session_token()
@engine.define
def current_user():
user = engine.current_user
o = leancloud.Object.extend('TestCurrentUser')()
o.set('user', user)
o.set({'yetAnotherUser': user})
o.save()
assert user.get('username') == saved_user.get('username')
response = requests.get(url + '/__engine/1/functions/current_user', headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
'x-avoscloud-session-token': session_token,
})
assert response.status_code == 200
@engine.before_save('Xxx')
def before_xxx_save(xxx):
assert engine.current_user.get('username') == saved_user.get('username')
response = requests.post(url + '/__engine/1/functions/Xxx/beforeSave', headers={
'x-avoscloud-application-id': TEST_APP_ID,
'x-avoscloud-application-key': TEST_APP_KEY,
}, json={'object': {}, 'user': {'username': saved_user.get('username')}})
assert response.status_code == 200