Skip to content

Commit a19b17b

Browse files
author
Vladislav Denisov
authored
Fixed embedded queries (#6497)
* refactored users models * added tests
1 parent 09ec299 commit a19b17b

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

redash/models/users.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from functools import reduce
66
from operator import or_
77

8-
from flask import current_app as app
9-
from flask import request_started, url_for
8+
from flask import current_app, request_started, url_for
109
from flask_login import AnonymousUserMixin, UserMixin, current_user
1110
from passlib.apps import custom_app_context as pwd_context
1211
from sqlalchemy.dialects import postgresql
@@ -129,7 +128,7 @@ def regenerate_api_key(self):
129128
def to_dict(self, with_api_key=False):
130129
profile_image_url = self.profile_image_url
131130
if self.is_disabled:
132-
assets = app.extensions["webpack"]["assets"] or {}
131+
assets = current_app.extensions["webpack"]["assets"] or {}
133132
path = "images/avatar.svg"
134133
profile_image_url = url_for("static", filename=assets.get(path, path))
135134

@@ -158,7 +157,8 @@ def to_dict(self, with_api_key=False):
158157

159158
return d
160159

161-
def is_api_user(self):
160+
@staticmethod
161+
def is_api_user():
162162
return False
163163

164164
@property
@@ -377,7 +377,8 @@ class AnonymousUser(AnonymousUserMixin, PermissionsCheckMixin):
377377
def permissions(self):
378378
return []
379379

380-
def is_api_user(self):
380+
@staticmethod
381+
def is_api_user():
381382
return False
382383

383384

@@ -397,7 +398,8 @@ def __init__(self, api_key, org, groups, name=None):
397398
def __repr__(self):
398399
return "<{}>".format(self.name)
399400

400-
def is_api_user(self):
401+
@staticmethod
402+
def is_api_user():
401403
return True
402404

403405
@property
@@ -410,5 +412,9 @@ def org_id(self):
410412
def permissions(self):
411413
return ["view_query"]
412414

413-
def has_access(self, obj, access_type):
415+
@staticmethod
416+
def has_access(obj, access_type):
414417
return False
418+
419+
def get_actual_user(self):
420+
return repr(self)

tests/models/test_users.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from redash import redis_connection
2-
from redash.models import User, db
2+
from redash.models import ApiUser, User, db
33
from redash.models.users import LAST_ACTIVE_KEY, sync_last_active_at
44
from redash.utils import dt_from_timestamp
55
from tests import BaseTestCase, authenticated_user
@@ -103,3 +103,16 @@ def test_sync(self):
103103
user_reloaded = User.query.filter(User.id == user.id).first()
104104
self.assertIn("active_at", user_reloaded.details)
105105
self.assertEqual(user_reloaded.active_at, timestamp)
106+
107+
108+
class TestUserGetActualUser(BaseTestCase):
109+
def test_default_user(self):
110+
user_email = "[email protected]"
111+
user = self.factory.create_user(email=user_email)
112+
self.assertEqual(user.get_actual_user(), user_email)
113+
114+
def test_api_user(self):
115+
user_email = "[email protected]"
116+
user = self.factory.create_user(email=user_email)
117+
api_user = ApiUser(user.api_key, user.org, user.group_ids)
118+
self.assertEqual(api_user.get_actual_user(), repr(api_user))

0 commit comments

Comments
 (0)