2018-01-20 14:18:13 +00:00
|
|
|
import os
|
2015-12-01 13:44:08 +00:00
|
|
|
import time
|
|
|
|
|
2018-10-05 10:48:01 +00:00
|
|
|
from six.moves import reload_module
|
2017-06-12 06:47:08 +00:00
|
|
|
|
2018-12-05 10:21:18 +00:00
|
|
|
from flask import request
|
|
|
|
from mock import patch
|
2017-06-12 06:47:08 +00:00
|
|
|
from redash import models, settings
|
2016-11-28 10:16:32 +00:00
|
|
|
from redash.authentication import (api_key_load_user_from_request,
|
2017-06-12 06:47:08 +00:00
|
|
|
get_login_url, hmac_load_user_from_request,
|
|
|
|
sign)
|
2016-11-28 10:16:32 +00:00
|
|
|
from redash.authentication.google_oauth import (create_and_login_user,
|
|
|
|
verify_profile)
|
2018-12-05 10:21:18 +00:00
|
|
|
from redash.utils import utcnow
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
from tests import BaseTestCase
|
2015-03-10 15:51:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestApiKeyAuthentication(BaseTestCase):
|
|
|
|
#
|
|
|
|
# This is a bad way to write these tests, but the way Flask works doesn't make it easy to write them properly...
|
|
|
|
#
|
|
|
|
def setUp(self):
|
|
|
|
super(TestApiKeyAuthentication, self).setUp()
|
2016-11-28 14:41:29 +00:00
|
|
|
self.api_key = '10'
|
2015-12-01 13:44:08 +00:00
|
|
|
self.query = self.factory.create_query(api_key=self.api_key)
|
2016-11-28 14:41:29 +00:00
|
|
|
models.db.session.flush()
|
2016-01-03 14:05:29 +00:00
|
|
|
self.query_url = '/{}/api/queries/{}'.format(self.factory.org.slug, self.query.id)
|
|
|
|
self.queries_url = '/{}/api/queries'.format(self.factory.org.slug)
|
2015-03-10 15:51:17 +00:00
|
|
|
|
|
|
|
def test_no_api_key(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.query_url)
|
|
|
|
self.assertIsNone(api_key_load_user_from_request(request))
|
2015-03-10 15:51:17 +00:00
|
|
|
|
|
|
|
def test_wrong_api_key(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.query_url, query_string={'api_key': 'whatever'})
|
|
|
|
self.assertIsNone(api_key_load_user_from_request(request))
|
2015-03-10 15:51:17 +00:00
|
|
|
|
|
|
|
def test_correct_api_key(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.query_url, query_string={'api_key': self.api_key})
|
|
|
|
self.assertIsNotNone(api_key_load_user_from_request(request))
|
2015-03-10 15:51:17 +00:00
|
|
|
|
|
|
|
def test_no_query_id(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.queries_url, query_string={'api_key': self.api_key})
|
|
|
|
self.assertIsNone(api_key_load_user_from_request(request))
|
2014-03-02 13:41:38 +00:00
|
|
|
|
2015-07-08 17:59:07 +00:00
|
|
|
def test_user_api_key(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
user = self.factory.create_user(api_key="user_key")
|
2016-11-28 14:41:29 +00:00
|
|
|
models.db.session.flush()
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.queries_url, query_string={'api_key': user.api_key})
|
|
|
|
self.assertEqual(user.id, api_key_load_user_from_request(request).id)
|
2015-07-08 17:59:07 +00:00
|
|
|
|
2018-12-05 10:21:18 +00:00
|
|
|
def test_disabled_user_api_key(self):
|
|
|
|
user = self.factory.create_user(api_key="user_key")
|
|
|
|
user.disable()
|
|
|
|
models.db.session.flush()
|
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.queries_url, query_string={'api_key': user.api_key})
|
|
|
|
self.assertEqual(None, api_key_load_user_from_request(request))
|
|
|
|
|
2015-10-11 08:54:21 +00:00
|
|
|
def test_api_key_header(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.query_url, headers={'Authorization': "Key {}".format(self.api_key)})
|
|
|
|
self.assertIsNotNone(api_key_load_user_from_request(request))
|
2015-10-11 08:54:21 +00:00
|
|
|
|
|
|
|
def test_api_key_header_with_wrong_key(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.query_url, headers={'Authorization': "Key oops"})
|
|
|
|
self.assertIsNone(api_key_load_user_from_request(request))
|
2015-10-11 08:54:21 +00:00
|
|
|
|
2016-01-03 14:05:29 +00:00
|
|
|
def test_api_key_for_wrong_org(self):
|
|
|
|
other_user = self.factory.create_admin(org=self.factory.create_org())
|
|
|
|
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.query_url, headers={'Authorization': "Key {}".format(other_user.api_key)})
|
|
|
|
self.assertEqual(404, rv.status_code)
|
2016-01-03 14:05:29 +00:00
|
|
|
|
2015-10-11 08:54:21 +00:00
|
|
|
|
2015-07-08 17:59:07 +00:00
|
|
|
class TestHMACAuthentication(BaseTestCase):
|
|
|
|
#
|
|
|
|
# This is a bad way to write these tests, but the way Flask works doesn't make it easy to write them properly...
|
|
|
|
#
|
|
|
|
def setUp(self):
|
|
|
|
super(TestHMACAuthentication, self).setUp()
|
2016-11-28 14:41:29 +00:00
|
|
|
self.api_key = '10'
|
2015-12-01 13:44:08 +00:00
|
|
|
self.query = self.factory.create_query(api_key=self.api_key)
|
2016-11-28 14:41:29 +00:00
|
|
|
models.db.session.flush()
|
2016-02-25 08:07:47 +00:00
|
|
|
self.path = '/{}/api/queries/{}'.format(self.query.org.slug, self.query.id)
|
2015-07-08 17:59:07 +00:00
|
|
|
self.expires = time.time() + 1800
|
|
|
|
|
|
|
|
def signature(self, expires):
|
|
|
|
return sign(self.query.api_key, self.path, expires)
|
|
|
|
|
|
|
|
def test_no_signature(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.path)
|
|
|
|
self.assertIsNone(hmac_load_user_from_request(request))
|
2015-07-08 17:59:07 +00:00
|
|
|
|
|
|
|
def test_wrong_signature(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.path, query_string={'signature': 'whatever', 'expires': self.expires})
|
|
|
|
self.assertIsNone(hmac_load_user_from_request(request))
|
2015-07-08 17:59:07 +00:00
|
|
|
|
|
|
|
def test_correct_signature(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(self.path, query_string={'signature': self.signature(self.expires), 'expires': self.expires})
|
|
|
|
self.assertIsNotNone(hmac_load_user_from_request(request))
|
2015-07-08 17:59:07 +00:00
|
|
|
|
|
|
|
def test_no_query_id(self):
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get('/{}/api/queries'.format(self.query.org.slug), query_string={'api_key': self.api_key})
|
|
|
|
self.assertIsNone(hmac_load_user_from_request(request))
|
2015-07-08 17:59:07 +00:00
|
|
|
|
|
|
|
def test_user_api_key(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
user = self.factory.create_user(api_key="user_key")
|
2015-07-08 17:59:07 +00:00
|
|
|
path = '/api/queries/'
|
2016-11-28 14:41:29 +00:00
|
|
|
models.db.session.flush()
|
2016-11-28 10:16:32 +00:00
|
|
|
|
|
|
|
signature = sign(user.api_key, path, self.expires)
|
2016-11-30 12:18:00 +00:00
|
|
|
with self.app.test_client() as c:
|
|
|
|
rv = c.get(path, query_string={'signature': signature, 'expires': self.expires, 'user_id': user.id})
|
|
|
|
self.assertEqual(user.id, hmac_load_user_from_request(request).id)
|
2014-03-02 13:41:38 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2019-06-12 08:45:28 +00:00
|
|
|
class TestSessionAuthentication(BaseTestCase):
|
|
|
|
def test_prefers_api_key_over_session_user_id(self):
|
|
|
|
user = self.factory.create_user()
|
|
|
|
query = self.factory.create_query(user=user)
|
|
|
|
|
|
|
|
other_org = self.factory.create_org()
|
|
|
|
other_user = self.factory.create_user(org=other_org)
|
|
|
|
models.db.session.flush()
|
|
|
|
|
|
|
|
rv = self.make_request('get', '/api/queries/{}?api_key={}'.format(query.id, query.api_key), user=other_user)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
|
|
|
|
2014-03-02 13:41:38 +00:00
|
|
|
class TestCreateAndLoginUser(BaseTestCase):
|
|
|
|
def test_logins_valid_user(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
user = self.factory.create_user(email=u'test@example.com')
|
2014-03-02 13:41:38 +00:00
|
|
|
|
2018-03-09 09:42:10 +00:00
|
|
|
with patch('redash.authentication.login_user') as login_user_mock:
|
2015-12-01 13:44:08 +00:00
|
|
|
create_and_login_user(self.factory.org, user.name, user.email)
|
2014-03-02 13:41:38 +00:00
|
|
|
login_user_mock.assert_called_once_with(user, remember=True)
|
|
|
|
|
|
|
|
def test_creates_vaild_new_user(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
email = u'test@example.com'
|
2014-09-21 07:11:03 +00:00
|
|
|
name = 'Test User'
|
2014-03-02 13:41:38 +00:00
|
|
|
|
2018-03-09 09:42:10 +00:00
|
|
|
with patch('redash.authentication.login_user') as login_user_mock:
|
2015-12-01 13:44:08 +00:00
|
|
|
create_and_login_user(self.factory.org, name, email)
|
2014-03-03 09:53:49 +00:00
|
|
|
|
|
|
|
self.assertTrue(login_user_mock.called)
|
2016-11-28 14:41:29 +00:00
|
|
|
user = models.User.query.filter(models.User.email == email).one()
|
|
|
|
self.assertEqual(user.email, email)
|
2016-03-14 09:19:23 +00:00
|
|
|
|
2016-12-06 12:07:42 +00:00
|
|
|
def test_updates_user_name(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
user = self.factory.create_user(email=u'test@example.com')
|
2016-12-06 12:07:42 +00:00
|
|
|
|
2018-03-09 09:42:10 +00:00
|
|
|
with patch('redash.authentication.login_user') as login_user_mock:
|
2016-12-06 12:07:42 +00:00
|
|
|
create_and_login_user(self.factory.org, "New Name", user.email)
|
|
|
|
login_user_mock.assert_called_once_with(user, remember=True)
|
|
|
|
|
2016-11-30 12:18:00 +00:00
|
|
|
|
2016-03-14 09:19:23 +00:00
|
|
|
class TestVerifyProfile(BaseTestCase):
|
|
|
|
def test_no_domain_allowed_for_org(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
profile = dict(email=u'arik@example.com')
|
2016-03-14 09:19:23 +00:00
|
|
|
self.assertFalse(verify_profile(self.factory.org, profile))
|
|
|
|
|
|
|
|
def test_domain_not_in_org_domains_list(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
profile = dict(email=u'arik@example.com')
|
2016-03-14 09:19:23 +00:00
|
|
|
self.factory.org.settings[models.Organization.SETTING_GOOGLE_APPS_DOMAINS] = ['example.org']
|
|
|
|
self.assertFalse(verify_profile(self.factory.org, profile))
|
|
|
|
|
|
|
|
def test_domain_in_org_domains_list(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
profile = dict(email=u'arik@example.com')
|
2016-03-14 09:19:23 +00:00
|
|
|
self.factory.org.settings[models.Organization.SETTING_GOOGLE_APPS_DOMAINS] = ['example.com']
|
|
|
|
self.assertTrue(verify_profile(self.factory.org, profile))
|
|
|
|
|
|
|
|
self.factory.org.settings[models.Organization.SETTING_GOOGLE_APPS_DOMAINS] = ['example.org', 'example.com']
|
|
|
|
self.assertTrue(verify_profile(self.factory.org, profile))
|
|
|
|
|
|
|
|
def test_org_in_public_mode_accepts_any_domain(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
profile = dict(email=u'arik@example.com')
|
2016-03-14 09:19:23 +00:00
|
|
|
self.factory.org.settings[models.Organization.SETTING_IS_PUBLIC] = True
|
|
|
|
self.factory.org.settings[models.Organization.SETTING_GOOGLE_APPS_DOMAINS] = []
|
|
|
|
self.assertTrue(verify_profile(self.factory.org, profile))
|
|
|
|
|
|
|
|
def test_user_not_in_domain_but_account_exists(self):
|
2018-08-05 13:14:36 +00:00
|
|
|
profile = dict(email=u'arik@example.com')
|
|
|
|
self.factory.create_user(email=u'arik@example.com')
|
2016-03-14 09:19:23 +00:00
|
|
|
self.factory.org.settings[models.Organization.SETTING_GOOGLE_APPS_DOMAINS] = ['example.org']
|
|
|
|
self.assertTrue(verify_profile(self.factory.org, profile))
|
2017-06-12 06:47:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestGetLoginUrl(BaseTestCase):
|
|
|
|
def test_when_multi_org_enabled_and_org_exists(self):
|
|
|
|
with self.app.test_request_context('/{}/'.format(self.factory.org.slug)):
|
|
|
|
self.assertEqual(get_login_url(next=None), '/{}/login'.format(self.factory.org.slug))
|
|
|
|
|
|
|
|
def test_when_multi_org_enabled_and_org_doesnt_exist(self):
|
|
|
|
with self.app.test_request_context('/{}_notexists/'.format(self.factory.org.slug)):
|
|
|
|
self.assertEqual(get_login_url(next=None), '/')
|
2018-01-20 14:18:13 +00:00
|
|
|
|
2018-10-17 18:55:58 +00:00
|
|
|
|
|
|
|
class TestRedirectToUrlAfterLoggingIn(BaseTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(TestRedirectToUrlAfterLoggingIn, self).setUp()
|
|
|
|
self.user = self.factory.user
|
|
|
|
self.password = 'test1234'
|
|
|
|
|
|
|
|
def test_no_next_param(self):
|
|
|
|
response = self.post_request('/login', data={'email': self.user.email, 'password': self.password}, org=self.factory.org)
|
|
|
|
self.assertEqual(response.location, 'http://localhost/{}/'.format(self.user.org.slug))
|
|
|
|
|
|
|
|
def test_simple_path_in_next_param(self):
|
|
|
|
response = self.post_request('/login?next=queries', data={'email': self.user.email, 'password': self.password}, org=self.factory.org)
|
|
|
|
self.assertEqual(response.location, 'http://localhost/queries')
|
|
|
|
|
|
|
|
def test_starts_scheme_url_in_next_param(self):
|
|
|
|
response = self.post_request('/login?next=https://redash.io', data={'email': self.user.email, 'password': self.password}, org=self.factory.org)
|
|
|
|
self.assertEqual(response.location, 'http://localhost/')
|
|
|
|
|
|
|
|
def test_without_scheme_url_in_next_param(self):
|
|
|
|
response = self.post_request('/login?next=//redash.io', data={'email': self.user.email, 'password': self.password}, org=self.factory.org)
|
|
|
|
self.assertEqual(response.location, 'http://localhost/')
|
|
|
|
|
|
|
|
def test_without_scheme_with_path_url_in_next_param(self):
|
|
|
|
response = self.post_request('/login?next=//localhost/queries', data={'email': self.user.email, 'password': self.password}, org=self.factory.org)
|
|
|
|
self.assertEqual(response.location, 'http://localhost/queries')
|
|
|
|
|
|
|
|
|
2018-01-20 14:18:13 +00:00
|
|
|
class TestRemoteUserAuth(BaseTestCase):
|
|
|
|
DEFAULT_SETTING_OVERRIDES = {
|
|
|
|
'REDASH_REMOTE_USER_LOGIN_ENABLED': 'true'
|
|
|
|
}
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Apply default setting overrides to every test
|
2018-02-02 14:50:43 +00:00
|
|
|
self.override_settings(None)
|
2018-01-20 14:18:13 +00:00
|
|
|
|
|
|
|
super(TestRemoteUserAuth, self).setUp()
|
|
|
|
|
2018-02-02 14:50:43 +00:00
|
|
|
def override_settings(self, overrides):
|
2018-01-20 14:18:13 +00:00
|
|
|
"""Override settings for testing purposes.
|
|
|
|
|
|
|
|
This helper method can be used to override specific environmental
|
|
|
|
variables to enable / disable Re:Dash features for the duration
|
|
|
|
of the test.
|
|
|
|
|
|
|
|
Note that these overrides only affect code that checks the value of
|
|
|
|
the setting at runtime. It doesn't affect code that only checks the
|
|
|
|
value during program initialization.
|
|
|
|
|
|
|
|
:param dict overrides: a dict of environmental variables to override
|
|
|
|
when the settings are reloaded
|
|
|
|
"""
|
|
|
|
variables = self.DEFAULT_SETTING_OVERRIDES.copy()
|
|
|
|
variables.update(overrides or {})
|
|
|
|
with patch.dict(os.environ, variables):
|
2018-10-05 10:48:01 +00:00
|
|
|
reload_module(settings)
|
2018-01-20 14:18:13 +00:00
|
|
|
|
|
|
|
# Queue a cleanup routine that reloads the settings without overrides
|
|
|
|
# once the test ends
|
2018-10-05 10:48:01 +00:00
|
|
|
self.addCleanup(lambda: reload_module(settings))
|
2018-01-20 14:18:13 +00:00
|
|
|
|
2018-02-02 14:50:43 +00:00
|
|
|
def assert_correct_user_attributes(self, user, email='test@example.com', name='test@example.com', groups=None, org=None):
|
2018-01-20 14:18:13 +00:00
|
|
|
"""Helper to assert that the user attributes are correct."""
|
|
|
|
groups = groups or []
|
|
|
|
if self.factory.org.default_group.id not in groups:
|
|
|
|
groups.append(self.factory.org.default_group.id)
|
|
|
|
|
|
|
|
self.assertIsNotNone(user)
|
|
|
|
self.assertEqual(user.email, email)
|
|
|
|
self.assertEqual(user.name, name)
|
|
|
|
self.assertEqual(user.org, org or self.factory.org)
|
|
|
|
self.assertItemsEqual(user.group_ids, groups)
|
|
|
|
|
2018-02-02 14:50:43 +00:00
|
|
|
def get_test_user(self, email='test@example.com', org=None):
|
2018-01-20 14:18:13 +00:00
|
|
|
"""Helper to fetch an user from the database."""
|
|
|
|
|
|
|
|
# Expire all cached objects to ensure these values are read directly
|
|
|
|
# from the database.
|
|
|
|
models.db.session.expire_all()
|
|
|
|
|
|
|
|
return models.User.get_by_email_and_org(email, org or self.factory.org)
|
|
|
|
|
|
|
|
def test_remote_login_disabled(self):
|
2018-02-02 14:50:43 +00:00
|
|
|
self.override_settings({
|
2018-01-20 14:18:13 +00:00
|
|
|
'REDASH_REMOTE_USER_LOGIN_ENABLED': 'false'
|
|
|
|
})
|
|
|
|
|
|
|
|
self.get_request('/remote_user/login', org=self.factory.org, headers={
|
|
|
|
'X-Forwarded-Remote-User': 'test@example.com'
|
|
|
|
})
|
|
|
|
|
|
|
|
with self.assertRaises(NoResultFound):
|
2018-02-02 14:50:43 +00:00
|
|
|
self.get_test_user()
|
2018-01-20 14:18:13 +00:00
|
|
|
|
|
|
|
def test_remote_login_default_header(self):
|
|
|
|
self.get_request('/remote_user/login', org=self.factory.org, headers={
|
|
|
|
'X-Forwarded-Remote-User': 'test@example.com'
|
|
|
|
})
|
|
|
|
|
2018-02-02 14:50:43 +00:00
|
|
|
self.assert_correct_user_attributes(self.get_test_user())
|
2018-01-20 14:18:13 +00:00
|
|
|
|
|
|
|
def test_remote_login_custom_header(self):
|
2018-02-02 14:50:43 +00:00
|
|
|
self.override_settings({
|
2018-01-20 14:18:13 +00:00
|
|
|
'REDASH_REMOTE_USER_HEADER': 'X-Custom-User'
|
|
|
|
})
|
|
|
|
|
|
|
|
self.get_request('/remote_user/login', org=self.factory.org, headers={
|
|
|
|
'X-Custom-User': 'test@example.com'
|
|
|
|
})
|
|
|
|
|
2018-02-02 14:50:43 +00:00
|
|
|
self.assert_correct_user_attributes(self.get_test_user())
|