-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtests.py
661 lines (580 loc) · 20.3 KB
/
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
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
import collections
import json
import logging
import typing
from fixture import (BadRequest, CorsVerbService, MusicService,
NullDisallowedMethodService,
SatisfiedParametersService,
StatisticsService,
Unknown, UnsatisfiedParametersService)
from nirum.deserialize import deserialize_meta
from pytest import fixture, mark, raises
from six.moves import urllib
from werkzeug.test import Client
from werkzeug.wrappers import Response
from nirum_wsgi import (AnnotationError, LegacyWsgiApp, MethodArgumentError,
UriTemplateMatchResult, UriTemplateMatcher, WsgiApp,
import_string)
LEGACY = hasattr(MusicService, '__nirum_schema_version__')
class MusicServiceImpl(MusicService):
music_map = {
u'damien rice': [u'9 crimes', u'Elephant'],
u'ed sheeran': [u'Thinking out loud', u'Photograph'],
u'damien': [u'rice'],
}
def get_music_by_artist_name(self, artist_name):
if artist_name == 'error':
raise Unknown()
elif artist_name not in self.music_map:
raise BadRequest()
return self.music_map.get(artist_name)
def incorrect_return(self):
return 1
def get_artist_by_music(self, music):
for k, v in self.music_map.items():
if music in v:
return k
return u'none'
def raise_application_error_request(self):
raise ValueError('hello world')
class CorsVerbServiceImpl(CorsVerbService):
def get_foo(self, foo):
return True
def update_foo(self, foo):
return True
def delete_bar(self, bar):
return True
class StatisticsServiceImpl(StatisticsService):
def purchase_count(self, from_, to):
return list(range((to - from_).days))
def purchase_interval(self, from_, to, interval):
return list(range(int(interval)))
def daily_purchase(self, exclude):
if exclude is None:
return [1]
elif exclude:
return [1, 2]
else:
return [1, 2, 3]
class NullDisallowedMethodServiceImpl(NullDisallowedMethodService):
def __init__(self, value):
self.value = value
def null_disallowed_method(self):
return self.value
@fixture
def fx_music_wsgi():
return WsgiApp(MusicServiceImpl())
@fixture
def fx_test_client(fx_music_wsgi):
return Client(fx_music_wsgi, Response)
def assert_response(response, status_code, expect_json):
assert response.status_code == status_code, response.get_data(as_text=True)
actual_response_json = json.loads(
response.get_data(as_text=True)
)
assert actual_response_json == expect_json
def test_wsgi_app_error(caplog, fx_test_client):
# method not allowed
assert_response(
fx_test_client.get('/?method=get_music_by_artist_name'), 405,
{
'_type': 'error',
'_tag': 'method_not_allowed',
'message': 'The requested URL / was not allowed HTTP method GET.'
}
)
# method missing
assert_response(
fx_test_client.post('/'),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': u'`method` is missing.',
}
)
# invalid procedure name
assert_response(
fx_test_client.post('/?method=foo'),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': 'No service method `foo` found.'
}
)
# invalid json
assert_response(
fx_test_client.post(
'/?method=get_music_by_artist_name', data="!",
content_type='application/json'
),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': "Invalid JSON payload: '!'."
}
)
# incorrect return
caplog.handler.records = [] # Clear log records
response = fx_test_client.post('/?method=incorrect_return')
assert caplog.record_tuples and caplog.record_tuples[-1] == (
typing._type_repr(MusicServiceImpl) + '.incorrect_return',
logging.ERROR,
'''1 is an invalid return value for the return type of {0}.\
incorrect_return() method.'''.format(
typing._type_repr(MusicServiceImpl)
),
)
assert_response(
response,
500,
{
'_type': 'error',
'_tag': 'internal_server_error',
'message': '''The server-side implementation of the \
incorrect-return() method has tried to return a value of an invalid type. \
It is an internal server error and should be fixed by server-side.''',
}
)
def test_procedure_bad_request(fx_test_client):
assert_response(
fx_test_client.post('/?method=get_music_by_artist_name'),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': 'There are invalid arguments.',
'errors': [
{'path': '.artist_name', 'message': 'Expected to exist.'},
],
}
)
payload = {
'artist_name': 1
}
assert_response(
fx_test_client.post(
'/?method=get_music_by_artist_name',
data=json.dumps(payload),
content_type='application/json'
),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': 'There are invalid arguments.',
'errors': [
{
'path': '.artist_name',
'message': (
'Expected {0}, but int was given.'.format(
type(u'').__name__
)
if LEGACY
else 'Expected a string.'
)
},
],
}
)
@mark.parametrize(
'payload, expected_json',
[
({'artist_name': u'damien rice'}, [u'9 crimes', u'Elephant']),
(
{'artist_name': u'ed sheeran'},
[u'Thinking out loud', u'Photograph']
),
]
)
def test_wsgi_app_method(fx_test_client, payload, expected_json):
response = fx_test_client.post(
'/?method=get_music_by_artist_name',
data=json.dumps(payload),
content_type='application/json'
)
data = json.loads(response.get_data(as_text=True))
assert data == expected_json
def test_wsgi_app_http_error(fx_test_client):
response = fx_test_client.post('/foobar') # 404
assert response.status_code == 400
response_json = json.loads(response.get_data(as_text=True))
assert response_json == {
'_type': 'error',
'_tag': u'bad_request',
'message': u'`method` is missing.',
}
def test_wsgi_app_with_behind_name(fx_test_client):
payload = {'norae': u'9 crimes'}
assert_response(
fx_test_client.post(
'/?method=get_artist_by_music',
data=json.dumps(payload),
content_type='application/json'
),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': 'No service method `get_artist_by_music` found.'
}
)
assert_response(
fx_test_client.post(
'/?method=find_artist',
data=json.dumps({'music': '9 crimes'}),
content_type='application/json'
),
400,
{
'_type': 'error',
'_tag': 'bad_request',
'message': 'There are invalid arguments.',
'errors': [
{'path': '.norae', 'message': 'Expected to exist.'},
],
}
)
assert_response(
fx_test_client.post(
'/?method=find_artist',
data=json.dumps(payload),
content_type='application/json'
),
200,
u'damien rice'
)
@mark.parametrize('arity', [0, 1, 2, 4])
def test_wsgi_app_make_response_arity_check(arity):
class ExtendedWsgiApp(LegacyWsgiApp if LEGACY else WsgiApp):
def make_response(self, status_code, headers, content):
return (status_code, headers, content, None)[:arity]
wsgi_app = ExtendedWsgiApp(MusicServiceImpl())
client = Client(wsgi_app, Response)
with raises(TypeError) as e:
client.post('/?method=get_music_by_artist_name',
data=json.dumps({'artist_name': u'damien rice'}))
assert str(e.value).startswith('make_response() must return a triple of '
'(status_code, headers, content), not ')
@mark.parametrize('uri_template, pattern, variables, valid, invalid', [
(
u'/foo/{id}/bar.txt',
r'\/foo\/(?P<id>.+?)\/bar\.txt$',
{'id'},
['/foo/xyz/bar.txt', '/foo/123/bar.txt'],
['/bar/xyz/bar.txt', '/foo/bar.txt'],
),
(
u'/foo/{id}',
r'\/foo\/(?P<id>.+?)$',
{'id'},
['/foo/xyz'],
['/bar/xyz/bar.txt'],
),
(
u'/foo/{foo-id}',
r'\/foo\/(?P<foo_id>.+?)$',
{'foo_id'},
['/foo/xyz'],
['/bar/xyz/bar.txt'],
),
(
u'/foo/{id}/bar/{id2}',
r'\/foo\/(?P<id>.+?)\/bar\/(?P<id2>.+?)$',
{'id', 'id2'},
['/foo/xyz/bar/123', '/foo/123/bar/abc'],
['/bar/xyz/bar.txt', '/bar/bar.txt'],
),
(
u'/foo/bar',
r'\/foo\/bar$',
set(),
['/foo/bar'],
['/lorem/ipsum', '/prefix/foo/bar', '/foo/bar/postfix'],
),
])
def test_uri_template_matcher(uri_template, pattern, variables, valid,
invalid):
matcher = UriTemplateMatcher(uri_template)
assert matcher.names == variables
assert matcher.path_pattern.pattern == pattern
for v in valid:
assert matcher.path_pattern.match(v), v
assert matcher.match_path(v), v
for v in invalid:
assert not matcher.path_pattern.match(v), v
assert not matcher.match_path(v), v
@mark.parametrize('uri_template, variables, valid, invalid', [
(
u'/foo/?from={from}&to={to}',
{'from', 'to'},
['/foo/?from=1&to=2', '/foo/?to=2&from=1'],
['/foo/?from=1', '/foo/?to=2'],
),
(
u'/foo/?start-from={start-from}&end-to={end-to}',
{'start_from', 'end_to'},
['/foo?start-from=1&end-to=2', '/foo?end-to=2&start-from=1'],
['/foo?start-from=1', '/foo?end-to=2'],
),
])
def test_uri_template_matcher_querystring(
uri_template, variables, valid, invalid
):
matcher = UriTemplateMatcher(uri_template)
assert matcher.names == variables
for v in valid:
assert matcher.match_querystring(v), v
for v in invalid:
assert not matcher.match_querystring(v), v
def test_uri_template_matcher_duplicate_variable_error():
with raises(AnnotationError):
UriTemplateMatcher(u'/foo/{var}/bar/{var}')
@mark.parametrize('lval, rval, expected', [
(None, [('n1', 'v1'), ('n2', 'v2')], [('n1', 'v1'), ('n2', 'v2')]),
([('n1', 'v1'), ('n2', 'v2')], None, [('n1', 'v1'), ('n2', 'v2')]),
([('n1', 'v1'), ('n2', 'v2')], [('n1', 'v3'), ('n2', 'v4')],
[('n1', 'v1'), ('n2', 'v2'), ('n1', 'v3'), ('n2', 'v4')]),
(None, None, None),
])
def test_uri_template_match_result_update(lval, rval, expected):
lval_result = UriTemplateMatchResult(lval)
rval_result = UriTemplateMatchResult(rval)
lval_result.update(rval_result)
if expected is None:
assert lval is None
else:
assert list(lval_result.result) == expected
def test_import_string():
assert import_string('collections:OrderedDict') == collections.OrderedDict
assert (import_string('collections:OrderedDict({"a": 1})') ==
collections.OrderedDict({"a": 1}))
with raises(ValueError):
# malformed
import_string('world')
with raises(NameError):
# coudn't import
import_string('os:world')
with raises(ImportError):
# coudn't import
import_string('os.hello:world')
def test_unsatisfied_uri_template_parameters():
s = UnsatisfiedParametersService()
with raises(AnnotationError) as e:
WsgiApp(s)
assert str(e.value) == (
'"/foo/{bar}/" does not fully satisfy all parameters of foo_bar_baz() '
'method; unsatisfied parameters are: baz, foo'
)
# As parameter names overlapped to Python keywords append an underscore
# to their names, it should deal with the case as well.
s = SatisfiedParametersService()
WsgiApp(s) # Should not raise AnnotationError
def test_http_resource_route(fx_test_client):
assert_response(
fx_test_client.get('/artists/damien/'),
200,
[u'rice'],
)
def split(header, lower=False):
vs = [h.strip() for h in header.split(',')]
if lower:
vs = [v.lower() for v in vs]
return frozenset(vs)
def test_cors():
app = WsgiApp(
MusicServiceImpl(),
allowed_origins=frozenset(['example.com'])
)
client = Client(app, Response)
resp = client.options('/?method=get_music_by_artist_name', headers={
'Origin': 'https://example.com',
'Access-Control-Request-Method': 'POST',
})
assert resp.status_code == 200
assert resp.headers['Access-Control-Allow-Origin'] == 'https://example.com'
assert split(resp.headers['Access-Control-Allow-Methods']) == {
'POST', 'OPTIONS',
}
assert 'origin' in split(resp.headers['Vary'], lower=True)
resp2 = client.post(
'/?method=get_music_by_artist_name',
headers={
'Origin': 'https://example.com',
'Access-Control-Request-Method': 'POST',
'Content-Type': 'application/json',
},
data=json.dumps({'artist_name': 'damien'})
)
assert resp2.status_code == 200, resp2.get_data(as_text=True)
assert resp2.headers['Access-Control-Allow-Origin'] == \
'https://example.com'
assert {'POST', 'OPTIONS'} == split(
resp2.headers['Access-Control-Allow-Methods']
)
assert 'origin' in split(resp2.headers['Vary'], lower=True)
resp3 = client.options('/?method=get_music_by_artist_name', headers={
'Origin': 'https://disallowed.com',
'Access-Control-Request-Method': 'POST',
})
assert resp3.status_code == 200
allow_origin = resp3.headers.get('Access-Control-Allow-Origin', '')
assert 'disallowed.com' not in allow_origin
@mark.parametrize('origin, disallowed_origin_host', [
(u'https://example.com', u'disallowed.com'),
(u'https://foobar.prefix.example.com', u'foobar.nonprefix.example.com'),
(u'https://foobar.prefix.example.com', u'prefix.example.com'),
(u'https://foobar.prefix.example.com', u'foobarprefix.example.com'),
(u'https://infix.foobar.example.com', u'disallowed.foobar.example.com'),
])
@mark.parametrize(
'url, allow_methods, request_method',
[
(u'/foo/abc/', {u'GET', u'PUT', u'OPTIONS'}, u'GET'),
(u'/foo/abc/', {u'GET', u'PUT', u'OPTIONS'}, u'PUT'),
(u'/bar/abc/', {u'DELETE', u'OPTIONS'}, u'DELETE'),
],
)
def test_cors_http_resouce(origin, disallowed_origin_host,
url, allow_methods, request_method):
app = WsgiApp(
CorsVerbServiceImpl(),
allowed_origins=frozenset([
'example.com',
'*.prefix.example.com',
'infix.*.example.com',
])
)
assert app.allows_origin(origin)
assert not app.allows_origin(u'http://' + disallowed_origin_host)
assert not app.allows_origin(u'https://' + disallowed_origin_host)
client = Client(app, Response)
resp = client.options(url, headers={
'Origin': origin,
'Access-Control-Request-Method': request_method,
})
assert resp.status_code == 200
assert resp.headers['Access-Control-Allow-Origin'] == origin
assert split(resp.headers['Access-Control-Allow-Methods']) == allow_methods
assert u'origin' in split(resp.headers['Vary'], lower=True)
resp2 = getattr(client, request_method.lower())(
url,
headers={
'Origin': origin,
'Access-Control-Request-Method': request_method,
'Content-Type': u'application/json',
},
)
assert resp2.status_code == 200, resp2.get_data(as_text=True)
assert resp2.headers['Access-Control-Allow-Origin'] == origin
assert allow_methods == split(
resp2.headers['Access-Control-Allow-Methods']
)
assert 'origin' in split(resp2.headers['Vary'], lower=True)
resp3 = client.options(url, headers={
'Origin': u'https://' + disallowed_origin_host,
'Access-Control-Request-Method': request_method,
})
assert resp3.status_code == 200
allow_origin = resp3.headers.get('Access-Control-Allow-Origin', u'')
assert disallowed_origin_host not in allow_origin
@mark.parametrize('qs, expected', [
([('from', '2017-01-01'), ('to', '2017-01-30')], list(range(29))),
([('to', '2017-01-30'), ('from', '2017-01-01')], list(range(29))),
# Ignore unused argument.
(
[('to', '2017-01-30'), ('from', '2017-01-01'), ('x', 1)],
list(range(29))
),
# Match with `purchase-interval`
(
[('to', '2017-01-30'), ('from', '2017-01-01'), ('interval', 10)],
list(range(10))
),
# Match with `purchase-interval` ignore unused argument.
(
[
('to', '2017-01-30'),
('from', '2017-01-01'),
('interval', 10), ('x', 1)
],
list(range(10))
),
])
def test_resolve_querystring(qs, expected):
app = WsgiApp(
StatisticsServiceImpl(),
allowed_origins=frozenset(['example.com'])
)
client = Client(app, Response)
url = '/statistics/purchases/?' + urllib.parse.urlencode(qs)
response = client.get(url)
assert response.status_code == 200, response.get_data(as_text=True)
return_result = deserialize_meta(
typing.Sequence[int], json.loads(response.get_data(as_text=True))
)
assert return_result == expected
@mark.parametrize('payload, expected', [
({'exclude': False}, [1, 2, 3]),
({'exclude': True}, [1, 2]),
({'exclude': None}, [1]),
({}, [1]),
])
def test_omit_optional_parameter(payload, expected):
app = WsgiApp(StatisticsServiceImpl())
client = Client(app, Response)
response = client.post(
'/?method=daily_purchase',
data=json.dumps(payload),
content_type='application/json'
)
assert response.status_code == 200, response.get_data(as_text=True)
actual = json.loads(response.get_data(as_text=True))
assert actual == expected
def test_readable_error_when_null_returned_from_null_disallowed_method(caplog):
"""Even if the method implementation returns None (FYI Python functions
return None when it lacks return statement so that service methods are
prone to return None by mistake) the error message should be readable
and helpful for debugging.
"""
expected_message = '''The return type of null-disallowed-method() method \
is not optional (i.e., no trailing question mark), but its server-side \
implementation has tried to return nothing (i.e., null, nil, None). \
It is an internal server error and should be fixed by server-side.'''
app = WsgiApp(NullDisallowedMethodServiceImpl(None))
client = Client(app, Response)
caplog.handler.records = [] # Clear log records
response = client.post(
'/?method=null_disallowed_method',
data=json.dumps({}),
content_type='application/json'
)
assert caplog.record_tuples and caplog.record_tuples[-1] == (
'{0}.null_disallowed_method'.format(
typing._type_repr(NullDisallowedMethodServiceImpl)
),
logging.ERROR,
'''None is an invalid return value for the return type of {0}.\
null_disallowed_method() method.'''.format(
typing._type_repr(NullDisallowedMethodServiceImpl)
),
)
assert response.status_code == 500, response.get_data(as_text=True)
actual = json.loads(response.get_data(as_text=True))
assert actual == {
'_type': 'error',
'_tag': 'internal_server_error',
'message': expected_message,
}
def test_method_argument_error():
e = MethodArgumentError()
assert not e.errors
assert str(e) == ''
e.on_error('.foo', 'Message A.')
assert e.errors == {('.foo', 'Message A.')}
assert str(e) == '.foo: Message A.'
e.on_error('.bar', 'Message B.')
assert e.errors == {('.foo', 'Message A.'), ('.bar', 'Message B.')}
assert str(e) == '.foo: Message A.\n.bar: Message B.'