2015-03-30 08:08:57 +00:00
|
|
|
import os
|
2016-11-28 09:21:45 +00:00
|
|
|
import logging
|
|
|
|
import datetime
|
2016-11-28 10:16:32 +00:00
|
|
|
import json
|
2016-11-28 09:21:45 +00:00
|
|
|
from unittest import TestCase
|
2016-11-28 10:16:32 +00:00
|
|
|
from contextlib import contextmanager
|
2016-11-28 09:21:45 +00:00
|
|
|
|
2015-03-30 08:08:57 +00:00
|
|
|
os.environ['REDASH_REDIS_URL'] = "redis://localhost:6379/5"
|
2015-08-16 19:55:24 +00:00
|
|
|
# Use different url for Celery to avoid DB being cleaned up:
|
|
|
|
os.environ['REDASH_CELERY_BROKER'] = "redis://localhost:6379/6"
|
|
|
|
|
2015-12-26 21:19:37 +00:00
|
|
|
# Dummy values for oauth login
|
|
|
|
os.environ['REDASH_GOOGLE_CLIENT_ID'] = "dummy"
|
|
|
|
os.environ['REDASH_GOOGLE_CLIENT_SECRET'] = "dummy"
|
2016-01-03 14:05:29 +00:00
|
|
|
os.environ['REDASH_MULTI_ORG'] = "true"
|
2015-08-16 19:55:24 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
import redash.models
|
2016-11-28 09:21:45 +00:00
|
|
|
from redash import create_app
|
|
|
|
from redash import redis_connection
|
2016-11-28 10:16:32 +00:00
|
|
|
from redash.utils import json_dumps
|
|
|
|
from tests.factories import Factory, user_factory
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
logging.disable("INFO")
|
2015-12-27 07:43:36 +00:00
|
|
|
logging.getLogger("metrics").setLevel("ERROR")
|
2014-02-05 09:01:06 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:16:32 +00:00
|
|
|
def authenticate_request(c, user):
|
|
|
|
with c.session_transaction() as sess:
|
|
|
|
sess['user_id'] = user.id
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def authenticated_user(c, user=None):
|
|
|
|
if not user:
|
|
|
|
user = user_factory.create()
|
|
|
|
|
|
|
|
authenticate_request(c, user)
|
|
|
|
|
|
|
|
yield user
|
|
|
|
|
|
|
|
|
2014-02-05 09:01:06 +00:00
|
|
|
class BaseTestCase(TestCase):
|
|
|
|
def setUp(self):
|
2016-11-21 15:34:44 +00:00
|
|
|
self.app = create_app()
|
2016-11-28 10:16:32 +00:00
|
|
|
self.app.config['TESTING'] = True
|
|
|
|
self.client = self.app.test_client()
|
2016-11-21 15:34:44 +00:00
|
|
|
self.app_ctx = self.app.app_context()
|
|
|
|
self.app_ctx.push()
|
2016-11-28 10:16:32 +00:00
|
|
|
redash.models.db.session.remove()
|
2015-12-01 13:44:08 +00:00
|
|
|
redash.models.create_db(True, True)
|
|
|
|
self.factory = Factory()
|
2014-02-05 09:01:06 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
redash.models.create_db(False, True)
|
2016-11-21 15:34:44 +00:00
|
|
|
self.app_ctx.pop()
|
2015-03-30 08:05:20 +00:00
|
|
|
redis_connection.flushdb()
|
2015-03-22 12:42:06 +00:00
|
|
|
|
2016-01-03 14:05:29 +00:00
|
|
|
def make_request(self, method, path, org=None, user=None, data=None, is_json=True):
|
2015-12-01 13:44:08 +00:00
|
|
|
if user is None:
|
|
|
|
user = self.factory.user
|
2016-01-03 14:05:29 +00:00
|
|
|
|
|
|
|
if org is None:
|
|
|
|
org = self.factory.org
|
|
|
|
|
|
|
|
if org is not False:
|
|
|
|
path = "/{}{}".format(org.slug, path)
|
|
|
|
|
2016-11-28 10:16:32 +00:00
|
|
|
if user:
|
|
|
|
authenticate_request(self.client, user)
|
|
|
|
|
|
|
|
method_fn = getattr(self.client, method.lower())
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
if data and is_json:
|
|
|
|
data = json_dumps(data)
|
|
|
|
|
|
|
|
if is_json:
|
|
|
|
content_type = 'application/json'
|
|
|
|
else:
|
|
|
|
content_type = None
|
|
|
|
|
|
|
|
response = method_fn(path, data=data, headers=headers, content_type=content_type)
|
|
|
|
|
|
|
|
if response.data and is_json:
|
|
|
|
response.json = json.loads(response.data)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_request(self, path, org=None):
|
|
|
|
if org:
|
|
|
|
path = "/{}{}".format(org.slug, path)
|
|
|
|
|
|
|
|
return self.client.get(path)
|
|
|
|
|
|
|
|
def post_request(self, path, data=None, org=None):
|
|
|
|
if org:
|
|
|
|
path = "/{}{}".format(org.slug, path)
|
|
|
|
|
|
|
|
return self.client.post(path, data=data)
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2015-03-22 12:42:06 +00:00
|
|
|
def assertResponseEqual(self, expected, actual):
|
|
|
|
for k, v in expected.iteritems():
|
|
|
|
if isinstance(v, datetime.datetime) or isinstance(actual[k], datetime.datetime):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(v, list):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(v, dict):
|
|
|
|
self.assertResponseEqual(v, actual[k])
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.assertEqual(v, actual[k], "{} not equal (expected: {}, actual: {}).".format(k, v, actual[k]))
|