Skip to content

Commit

Permalink
Use SimpleFlaskPlugin test extension in middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed Jun 14, 2016
1 parent b37298b commit 7995561
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions ckan/tests/config/test_middleware.py
Expand Up @@ -414,22 +414,37 @@ class TestFlaskUserIdentifiedInRequest(helpers.FunctionalTestBase):

'''Flask identifies user during each request.
API route has been migrated to Flask, so using as an example.
Flask route provided by test.helpers.SimpleFlaskPlugin.
'''

@classmethod
def setup_class(cls):
super(TestFlaskUserIdentifiedInRequest, cls).setup_class()
cls.app = cls._get_test_app()
cls.flask_app = helpers.find_flask_app(cls.app)

if not p.plugin_loaded('test_simple_flask_plugin'):
p.load('test_simple_flask_plugin')
plugin = p.get_plugin('test_simple_flask_plugin')
cls.flask_app.register_blueprint(plugin.get_blueprint(),
prioritise_rules=True)

@classmethod
def teardown_class(cls):
super(TestFlaskUserIdentifiedInRequest, cls).teardown_class()
p.unload('test_simple_flask_plugin')

def test_user_objects_in_g_normal_user(self):
'''
A normal logged in user request will have expected user objects added
to request.
'''
self.app = helpers._get_test_app()
flask_app = helpers.find_flask_app(self.app)
user = factories.User()
test_user_obj = model.User.by_name(user['name'])

with flask_app.app_context():
with self.flask_app.app_context():
self.app.get(
'/api/action/status_show',
'/simple_flask',
extra_environ={'REMOTE_USER': user['name'].encode('ascii')},)
eq_(flask.g.user, user['name'])
eq_(flask.g.userobj, test_user_obj)
Expand All @@ -440,12 +455,9 @@ def test_user_objects_in_g_anon_user(self):
'''
An anon user request will have expected user objects added to request.
'''
self.app = helpers._get_test_app()
flask_app = helpers.find_flask_app(self.app)

with flask_app.app_context():
with self.flask_app.app_context():
self.app.get(
'/api/action/status_show',
'/simple_flask',
extra_environ={'REMOTE_USER': ''},)
eq_(flask.g.user, '')
eq_(flask.g.userobj, None)
Expand All @@ -457,14 +469,12 @@ def test_user_objects_in_g_sysadmin(self):
A sysadmin user request will have expected user objects added to
request.
'''
self.app = helpers._get_test_app()
flask_app = helpers.find_flask_app(self.app)
user = factories.Sysadmin()
test_user_obj = model.User.by_name(user['name'])

with flask_app.app_context():
with self.flask_app.app_context():
self.app.get(
'/api/action/status_show',
'/simple_flask',
extra_environ={'REMOTE_USER': user['name'].encode('ascii')},)
eq_(flask.g.user, user['name'])
eq_(flask.g.userobj, test_user_obj)
Expand Down

0 comments on commit 7995561

Please sign in to comment.