-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_middleware.py
454 lines (308 loc) · 13.8 KB
/
test_middleware.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
# encoding: utf-8
import mock
import wsgiref
import nose
from nose.tools import assert_equals, assert_not_equals, eq_
from routes import url_for
from flask import Blueprint
import ckan.plugins as p
import ckan.tests.helpers as helpers
from ckan.config.middleware import AskAppDispatcherMiddleware, CKANFlask
from ckan.controllers.partyline import PartylineController
class TestPylonsResponseCleanupMiddleware(helpers.FunctionalTestBase):
@classmethod
def _apply_config_changes(cls, config):
config['ckan.use_pylons_response_cleanup_middleware'] = True
def test_homepage_with_middleware_activated(self):
'''Test the home page renders with the middleware activated
We are just testing the home page renders without any troubles and that
the middleware has not done anything strange to the response string'''
app = self._get_test_app()
response = app.get(url=url_for(controller='home', action='index'))
assert_equals(200, response.status_int)
# make sure we haven't overwritten the response too early.
assert_not_equals(
'response cleared by pylons response cleanup middleware',
response.body
)
class TestAppDispatcherPlain(object):
'''
These tests need the test app to be created at specific times to not affect
the mocks, so they don't extend FunctionalTestBase
'''
def test_invitations_are_sent(self):
with mock.patch.object(AskAppDispatcherMiddleware,
'send_invitations') as \
mock_send_invitations:
# This will create the whole WSGI stack
helpers._get_test_app()
assert mock_send_invitations.called
eq_(len(mock_send_invitations.call_args[0]), 1)
eq_(sorted(mock_send_invitations.call_args[0][0].keys()),
['flask_app', 'pylons_app'])
def test_flask_can_handle_request_is_called_with_environ(self):
with mock.patch.object(CKANFlask, 'can_handle_request') as \
mock_can_handle_request:
# We need set this otherwise the mock object is returned
mock_can_handle_request.return_value = (False, 'flask_app')
app = helpers._get_test_app()
# We want our CKAN app, not the WebTest one
ckan_app = app.app
environ = {
'PATH_INFO': '/',
}
wsgiref.util.setup_testing_defaults(environ)
start_response = mock.MagicMock()
ckan_app(environ, start_response)
assert mock_can_handle_request.called_with(environ)
def test_pylons_can_handle_request_is_called_with_environ(self):
with mock.patch.object(PartylineController, 'can_handle_request') as \
mock_can_handle_request:
# We need set this otherwise the mock object is returned
mock_can_handle_request.return_value = (True, 'pylons_app', 'core')
app = helpers._get_test_app()
# We want our CKAN app, not the WebTest one
ckan_app = app.app
environ = {
'PATH_INFO': '/',
}
wsgiref.util.setup_testing_defaults(environ)
start_response = mock.MagicMock()
ckan_app(environ, start_response)
assert mock_can_handle_request.called_with(environ)
class TestAppDispatcher(helpers.FunctionalTestBase):
def test_ask_around_is_called(self):
app = self._get_test_app()
with mock.patch.object(AskAppDispatcherMiddleware, 'ask_around') as \
mock_ask_around:
app.get('/')
assert mock_ask_around.called
def test_ask_around_is_called_with_args(self):
app = self._get_test_app()
ckan_app = app.app
environ = {}
start_response = mock.MagicMock()
wsgiref.util.setup_testing_defaults(environ)
with mock.patch.object(AskAppDispatcherMiddleware, 'ask_around') as \
mock_ask_around:
ckan_app(environ, start_response)
assert mock_ask_around.called
mock_ask_around.assert_called_with('can_handle_request', environ)
def test_ask_around_flask_core_route_get(self):
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/hello',
'REQUEST_METHOD': 'GET',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
# Even though this route is defined in Flask, there is catch all route
# in Pylons for all requests to point arbitrary urls to templates with
# the same name, so we get two positive answers
eq_(len(answers), 2)
eq_([a[0] for a in answers], [True, True])
eq_(sorted([a[1] for a in answers]), ['flask_app', 'pylons_app'])
# TODO: check origin (core/extension) when that is in place
def test_ask_around_flask_core_route_post(self):
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/hello',
'REQUEST_METHOD': 'POST',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
# Even though this route is defined in Flask, there is catch all route
# in Pylons for all requests to point arbitrary urls to templates with
# the same name, so we get two positive answers
eq_(len(answers), 2)
eq_([a[0] for a in answers], [True, True])
eq_(sorted([a[1] for a in answers]), ['flask_app', 'pylons_app'])
# TODO: check origin (core/extension) when that is in place
def test_ask_around_pylons_core_route_get(self):
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/dataset',
'REQUEST_METHOD': 'GET',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
eq_(len(answers), 1)
eq_(answers[0][0], True)
eq_(answers[0][1], 'pylons_app')
eq_(answers[0][2], 'core')
def test_ask_around_pylons_core_route_post(self):
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/dataset/new',
'REQUEST_METHOD': 'POST',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
eq_(len(answers), 1)
eq_(answers[0][0], True)
eq_(answers[0][1], 'pylons_app')
eq_(answers[0][2], 'core')
def test_ask_around_pylons_extension_route_get_before_map(self):
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/from_pylons_extension_before_map',
'REQUEST_METHOD': 'GET',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
eq_(len(answers), 1)
eq_(answers[0][0], True)
eq_(answers[0][1], 'pylons_app')
eq_(answers[0][2], 'extension')
p.unload('test_routing_plugin')
def test_ask_around_pylons_extension_route_post(self):
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/from_pylons_extension_before_map_post_only',
'REQUEST_METHOD': 'POST',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
eq_(len(answers), 1)
eq_(answers[0][0], True)
eq_(answers[0][1], 'pylons_app')
eq_(answers[0][2], 'extension')
p.unload('test_routing_plugin')
def test_ask_around_pylons_extension_route_post_using_get(self):
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/from_pylons_extension_before_map_post_only',
'REQUEST_METHOD': 'GET',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
# We are going to get an answer from Pylons, but just because it will
# match the catch-all template route, hence the `core` origin.
eq_(len(answers), 1)
eq_(answers[0][0], True)
eq_(answers[0][1], 'pylons_app')
eq_(answers[0][2], 'core')
p.unload('test_routing_plugin')
def test_ask_around_pylons_extension_route_get_after_map(self):
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
app = self._get_test_app()
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/from_pylons_extension_after_map',
'REQUEST_METHOD': 'GET',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
eq_(len(answers), 1)
eq_(answers[0][0], True)
eq_(answers[0][1], 'pylons_app')
eq_(answers[0][2], 'extension')
p.unload('test_routing_plugin')
def test_ask_around_flask_extension_and_pylons_extension_route(self):
app = self._get_test_app()
flask_app = helpers.find_flask_app(app)
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
plugin = p.get_plugin('test_routing_plugin')
flask_app.register_blueprint(plugin.get_blueprint(),
prioritise_rules=True)
# We want our CKAN app, not the WebTest one
app = app.app
environ = {
'PATH_INFO': '/pylons_and_flask',
'REQUEST_METHOD': 'GET',
}
wsgiref.util.setup_testing_defaults(environ)
answers = app.ask_around('can_handle_request', environ)
answers = sorted(answers, key=lambda a: a[1])
eq_(len(answers), 2)
eq_([a[0] for a in answers], [True, True])
eq_([a[1] for a in answers], ['flask_app', 'pylons_app'])
# TODO: we still can't distinguish between Flask core and extension
# eq_(answers[0][2], 'extension')
eq_(answers[1][2], 'extension')
p.unload('test_routing_plugin')
def test_flask_core_route_is_served_by_flask(self):
app = self._get_test_app()
res = app.get('/hello')
eq_(res.environ['ckan.app'], 'flask_app')
# TODO: test flask extension route
def test_pylons_core_route_is_served_by_pylons(self):
app = self._get_test_app()
res = app.get('/dataset')
eq_(res.environ['ckan.app'], 'pylons_app')
def test_pylons_extension_route_is_served_by_pylons(self):
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
app = self._get_test_app()
res = app.get('/from_pylons_extension_before_map')
eq_(res.environ['ckan.app'], 'pylons_app')
eq_(res.body, 'Hello World, this is served from a Pylons extension')
p.unload('test_routing_plugin')
def test_flask_core_and_pylons_extension_route_is_served_by_pylons(self):
if not p.plugin_loaded('test_routing_plugin'):
p.load('test_routing_plugin')
app = self._get_test_app()
res = app.get('/pylons_and_flask')
eq_(res.environ['ckan.app'], 'pylons_app')
eq_(res.body, 'Hello World, this is served from a Pylons extension')
p.unload('test_routing_plugin')
def test_flask_core_and_pylons_core_route_is_served_by_flask(self):
'''
This should never happen in core, but just in case
'''
app = self._get_test_app()
res = app.get('/api/action/status_show')
eq_(res.environ['ckan.app'], 'flask_app')
class MockRoutingPlugin(p.SingletonPlugin):
p.implements(p.IRoutes)
p.implements(p.IBlueprint)
controller = 'ckan.tests.config.test_middleware:MockPylonsController'
def before_map(self, _map):
_map.connect('/from_pylons_extension_before_map',
controller=self.controller, action='view')
_map.connect('/from_pylons_extension_before_map_post_only',
controller=self.controller, action='view',
conditions={'method': 'POST'})
# This one conflicts with a core Flask route
_map.connect('/pylons_and_flask',
controller=self.controller, action='view')
return _map
def after_map(self, _map):
_map.connect('/from_pylons_extension_after_map',
controller=self.controller, action='view')
return _map
def get_blueprint(self):
# Create Blueprint for plugin
blueprint = Blueprint(self.name, self.__module__)
# Add plugin url rule to Blueprint object
blueprint.add_url_rule('/pylons_and_flask', 'flask_plugin_view',
flask_plugin_view)
return blueprint
def flask_plugin_view(self):
return 'Hello World, this is served from a Flask extension'
class MockPylonsController(p.toolkit.BaseController):
def view(self):
return 'Hello World, this is served from a Pylons extension'