forked from cenobites/flask-jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_jsonrpc_tests.py
executable file
·354 lines (293 loc) · 15.3 KB
/
flask_jsonrpc_tests.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012-2013, Cenobit Technologies, Inc. http://cenobit.es/
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the Cenobit Technologies nor the names of
# its contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import time
import uuid
import unittest
import tempfile
import subprocess
from optparse import OptionParser
from flask import json
from flask_jsonrpc.proxy import ServiceProxy
from flask_jsonrpc.site import validate_params
from flask_jsonrpc import _parse_sig, OrderedDict, JSONRPC
from flask_jsonrpc.types import Any, Object, Number, Boolean, String, Array, Nil
from flask_jsonrpc.exceptions import (Error, ParseError, InvalidRequestError,
MethodNotFoundError, InvalidParamsError,
ServerError, RequestPostError,
InvalidCredentialsError, OtherError)
from run import app, jsonrpc
def check_auth(username, password):
return True
@jsonrpc.method('jsonrpc.echo')
def echo(name='Flask JSON-RPC'):
return 'Hello {}'.format(name)
@jsonrpc.method('jsonrpc.echoMyStr')
def echoMyStr(string):
return string
@jsonrpc.method('jsonrpc.echoAuth', authenticated=check_auth)
def echoAuth(string):
return string
@jsonrpc.method('jsonrpc.echoAuthChecked(string=str) -> str', authenticated=check_auth, validate=True)
def echoAuthChecked(string):
return string
@jsonrpc.method('jsonrpc.notify')
def notify(string):
pass
@jsonrpc.method('jsonrpc.fails')
def fails(string):
raise IndexError
@jsonrpc.method('jsonrpc.strangeEcho')
def strangeEcho(string, omg, wtf, nowai, yeswai='Default'):
return [string, omg, wtf, nowai, yeswai]
@jsonrpc.method('jsonrpc.safeEcho', safe=True)
def safeEcho(string):
return string
@jsonrpc.method('jsonrpc.strangeSafeEcho', safe=True)
def strangeSafeEcho(*args, **kwargs):
return strangeEcho(*args, **kwargs)
@jsonrpc.method('jsonrpc.checkedEcho(string=str, string2=str) -> str', safe=True, validate=True)
def protectedEcho(string, string2):
return string + string2
@jsonrpc.method('jsonrpc.checkedArgsEcho(string=str, string2=str)', validate=True)
def protectedArgsEcho(string, string2):
return string + string2
@jsonrpc.method('jsonrpc.checkedReturnEcho() -> String', validate=True)
def protectedReturnEcho():
return 'this is a string'
@jsonrpc.method('jsonrpc.authCheckedEcho(Object, Array) -> Object', validate=True)
def authCheckedEcho(obj1, arr1):
return {'obj1': obj1, 'arr1': arr1}
@jsonrpc.method('jsonrpc.varArgs(String, String, str3=String) -> Array', validate=True)
def checkedVarArgsEcho(*args, **kw):
return list(args) + kw.values()
class JSONRPCFunctionalTests(unittest.TestCase):
def test_method_parser(self):
working_sigs = [
('jsonrpc', 'jsonrpc', OrderedDict(), Any),
('jsonrpc.methodName', 'jsonrpc.methodName', OrderedDict(), Any),
('jsonrpc.methodName() -> list', 'jsonrpc.methodName', OrderedDict(), list),
('jsonrpc.methodName(str, str, str ) ', 'jsonrpc.methodName', OrderedDict([('a', str), ('b', str), ('c', str)]), Any),
('jsonrpc.methodName(str, b=str, c=str)', 'jsonrpc.methodName', OrderedDict([('a', str), ('b', str), ('c', str)]), Any),
('jsonrpc.methodName(str, b=str) -> dict', 'jsonrpc.methodName', OrderedDict([('a', str), ('b', str)]), dict),
('jsonrpc.methodName(str, str, c=Any) -> Any', 'jsonrpc.methodName', OrderedDict([('a', str), ('b', str), ('c', Any)]), Any),
('jsonrpc(Any ) -> Any', 'jsonrpc', OrderedDict([('a', Any)]), Any),
]
error_sigs = [
('jsonrpc(str) -> nowai', ValueError),
('jsonrpc(nowai) -> Any', ValueError),
('jsonrpc(nowai=str, str)', ValueError),
('jsonrpc.methodName(nowai*str) -> Any', ValueError)
]
for sig in working_sigs:
ret = _parse_sig(sig[0], list(iter(sig[2])))
self.assertEquals(ret[0], sig[1])
self.assertEquals(ret[1], sig[2])
self.assertEquals(ret[2], sig[3])
for sig in error_sigs:
e = None
try:
_parse_sig(sig[0], ['a'])
except Exception, exc:
e = exc
self.assert_(type(e) is sig[1])
def test_validate_args(self):
sig = 'jsonrpc(String, String) -> String'
M = jsonrpc.method(sig, validate=True)(lambda s1, s2: s1+s2)
self.assert_(validate_params(M, {'params': ['omg', u'wtf']}) is None)
E = None
try:
validate_params(M, {'params': [['omg'], ['wtf']]})
except Exception, e:
E = e
self.assert_(type(E) is InvalidParamsError)
def test_validate_args_any(self):
sig = 'jsonrpc(s1=Any, s2=Any)'
M = jsonrpc.method(sig, validate=True)(lambda s1, s2: s1+s2)
self.assert_(validate_params(M, {'params': ['omg', 'wtf']}) is None)
self.assert_(validate_params(M, {'params': [['omg'], ['wtf']]}) is None)
self.assert_(validate_params(M, {'params': {'s1': 'omg', 's2': 'wtf'}}) is None)
def test_types(self):
assert type(u'') == String
assert type('') == String
assert not type('') == Object
assert not type([]) == Object
assert type([]) == Array
assert type('') == Any
assert Any.kind('') == String
assert Any.decode('str') == String
assert Any.kind({}) == Object
assert Any.kind(None) == Nil
class ServiceProxyTestCase(unittest.TestCase):
def setUp(self):
self.service_url = 'http://127.0.0.1:5000/api'
def tearDown(self):
pass
def test_positional_args(self):
proxy = ServiceProxy(self.service_url)
self.assert_(proxy.jsonrpc.echo()[u'result'] == 'Hello Flask JSON-RPC')
try:
proxy.jsonrpc.echo(name='Hello')
except Exception, e:
self.assert_(e.args[0] == 'Unsupported arg type for JSON-RPC 1.0 '
'(the default version for this client, '
'pass version="2.0" to use keyword arguments)')
def test_keyword_args(self):
proxy = ServiceProxy(self.service_url, version='2.0')
self.assert_(proxy.jsonrpc.echo(name='Flask')[u'result'] == 'Hello Flask')
self.assert_(proxy.jsonrpc.echo('JSON-RPC')[u'result'] == 'Hello JSON-RPC')
class FlaskJSONRPCTestCase(unittest.TestCase):
def setUp(self):
app.config['DEBUG'] = True
app.config['TESTING'] = True
self.app = app.test_client()
self.service_url = jsonrpc.service_url
def tearDown(self):
pass
def _make_payload(self, method, params=None, version='1.0', is_notify=False):
return json.dumps({
'jsonrpc': version,
'method': method,
'params': params if params else [],
'id': None if is_notify else str(uuid.uuid1())
})
def _call(self, req):
return json.loads((self.app.post(self.service_url, data=req)).data)
def _assert_equals(self, resp, st):
assert st == resp, '{} != {}'.format(st, resp)
def test_echo(self):
T = [[
(self._make_payload('jsonrpc.echo', version=v), 'Hello Flask JSON-RPC'),
(self._make_payload('jsonrpc.echo', ['Flask'], version=v), 'Hello Flask'),
(self._make_payload('jsonrpc.echo', None, version=v), 'Hello Flask JSON-RPC'),
(self._make_payload('jsonrpc.echo', [], version=v), 'Hello Flask JSON-RPC'),
(self._make_payload('jsonrpc.echo', {}, version=v), 'Hello Flask JSON-RPC'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_my_str(self):
T = [[
(self._make_payload('jsonrpc.echoMyStr', ['Hello Flask JSON-RPC'], version=v), 'Hello Flask JSON-RPC'),
(self._make_payload('jsonrpc.echoMyStr', ['Hello Flask'], version=v), 'Hello Flask'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_auth(self):
T = [[
(self._make_payload('jsonrpc.echoAuth', ['username', 'password', 'Hello Flask JSON-RPC'], version=v), 'Hello Flask JSON-RPC'),
(self._make_payload('jsonrpc.echoAuth', ['username', 'password', 'Hello Flask'], version=v), 'Hello Flask'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_auth_checked(self):
T = [[
(self._make_payload('jsonrpc.echoAuthChecked', {'username': 'flask', 'password': 'jsonrpc', 'string': 'Hello Flask JSON-RPC'}, version=v), 'Hello Flask JSON-RPC'),
(self._make_payload('jsonrpc.echoAuthChecked', {'username': 'flask', 'password': 'jsonrpc', 'string': 'Hello Flask'}, version=v), 'Hello Flask'),
] for v in ['1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_notify(self):
T = [[
(self._make_payload('jsonrpc.notify', ['this is a string'], version=v, is_notify=True), ''),
] for v in ['2.0']]
[[self._assert_equals((self.app.post(self.service_url, data=req)).data, resp) for req, resp in t] for t in T]
def test_strangeEcho(self):
T = [
(self._make_payload('jsonrpc.strangeEcho', {u'1': u'this is a string', u'2': u'this is omg', u'wtf': u'pants', u'nowai': 'nopants'}, version='1.0'), ['1', '2', 'wtf', 'nowai', 'Default']),
(self._make_payload('jsonrpc.strangeEcho', {u'1': u'this is a string', u'2': u'this is omg', u'wtf': u'pants', u'nowai': 'nopants'}, version='1.1'), [u'this is a string', u'this is omg', u'pants', u'nopants', u'Default']),
(self._make_payload('jsonrpc.strangeEcho', {u'string': u'this is a string', u'omg': u'this is omg', u'wtf': u'pants', u'nowai': 'nopants'}, version='2.0'), [u'this is a string', u'this is omg', u'pants', u'nopants', u'Default']),
]
[self._assert_equals(self._call(req)['result'], resp) for req, resp in T]
def test_safeEcho(self):
T = [[
(self._make_payload('jsonrpc.safeEcho', [u'this is string'], version=v), 'this is string'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_strangeSafeEcho(self):
T = [
(self._make_payload('jsonrpc.strangeSafeEcho', {u'1': u'this is a string', u'2': u'this is omg', u'wtf': u'pants', u'nowai': 'nopants'}, version='1.0'), ['1', '2', 'wtf', 'nowai', 'Default']),
(self._make_payload('jsonrpc.strangeSafeEcho', {u'1': u'this is a string', u'2': u'this is omg', u'wtf': u'pants', u'nowai': 'nopants'}, version='1.1'), [u'this is a string', u'this is omg', u'pants', u'nopants', u'Default']),
(self._make_payload('jsonrpc.strangeSafeEcho', {u'string': u'this is a string', u'omg': u'this is omg', u'wtf': u'pants', u'nowai': 'nopants'}, version='2.0'), [u'this is a string', u'this is omg', u'pants', u'nopants', u'Default']),
]
[self._assert_equals(self._call(req)['result'], resp) for req, resp in T]
def test_protectedEcho(self):
T = [[
(self._make_payload('jsonrpc.checkedEcho', ['hai', 'hai'], version=v), 'haihai'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_protectedArgsEcho(self):
T = [[
(self._make_payload('jsonrpc.checkedArgsEcho', ['hai', 'hai'], version=v), 'haihai'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_protectedReturnEcho(self):
T = [[
(self._make_payload('jsonrpc.checkedReturnEcho', [], version=v), 'this is a string'),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_authCheckedEcho(self):
T = [[
(self._make_payload('jsonrpc.authCheckedEcho', [1.0, [1,2,3]], version=v), {'obj1': 1.0, 'arr1': [1,2,3]}),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
def test_checkedVarArgsEcho(self):
T = [[
(self._make_payload('jsonrpc.varArgs', ['hai', 'hai', ' -> \o/'], version=v), ['hai', 'hai', ' -> \o/']),
] for v in ['1.0', '1.1', '2.0']]
[[self._assert_equals(self._call(req)['result'], resp) for req, resp in t] for t in T]
class FlaskTestClient(object):
proc = None
def __enter__(self):
return self._run()
def __exit__(self, type, value, traceback):
self._kill()
def _run(self):
if FlaskTestClient.proc is None:
FlaskTestClient.proc = subprocess.Popen([sys.executable,
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'flask_jsonrpc_tests.py'), '--run'])
time.sleep(1)
return FlaskTestClient.proc
def _kill(self):
if not FlaskTestClient.proc is None \
and not FlaskTestClient.proc.poll() is None:
FlaskTestClient.proc.wait()
FlaskTestClient.proc.terminate()
time.sleep(1)
if not FlaskTestClient.proc.poll() is None:
FlaskTestClient.proc.kill()
def main():
parser = OptionParser(usage='usage: %prog [options]')
parser.add_option('-r', '--run',
action='store_true', dest='run', default=False,
help='Running Flask in subprocess')
(options, args) = parser.parse_args()
if options.run:
return app.run(host='0.0.0.0', debug=True)
with FlaskTestClient():
unittest.main()
if __name__ == '__main__':
main()