-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
0 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +0,0 @@ | ||
import json | ||
from contextlib import contextmanager | ||
|
||
from tests.factories import user_factory | ||
from redash.models import db | ||
from redash.utils import json_dumps | ||
from redash.wsgi import app | ||
|
||
app.config['TESTING'] = True | ||
|
||
|
||
def authenticate_request(c, user): | ||
with c.session_transaction() as sess: | ||
if user.id is None: | ||
db.session.flush() | ||
sess['user_id'] = user.id | ||
|
||
|
||
@contextmanager | ||
def authenticated_user(c, user=None): | ||
if not user: | ||
user = user_factory.create() | ||
db.session.commit() | ||
authenticate_request(c, user) | ||
|
||
yield user | ||
|
||
|
||
def json_request(method, path, data=None): | ||
if data: | ||
response = method(path, data=json_dumps(data)) | ||
else: | ||
response = method(path) | ||
|
||
if response.data: | ||
response.json = json.loads(response.data) | ||
else: | ||
response.json = None | ||
|
||
return response | ||
|
||
|
||
def make_request(method, path, user, data=None, is_json=True): | ||
with app.test_client() as c: | ||
if user: | ||
authenticate_request(c, user) | ||
|
||
method_fn = getattr(c, 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(path, org=None): | ||
if org: | ||
path = "/{}{}".format(org.slug, path) | ||
|
||
with app.test_client() as c: | ||
return c.get(path) | ||
|
||
|
||
def post_request(path, data=None, org=None): | ||
if org: | ||
path = "/{}{}".format(org.slug, path) | ||
|
||
with app.test_client() as c: | ||
return c.post(path, data=data) | ||