Skip to content

Commit

Permalink
Fixed embedded queries (#6497)
Browse files Browse the repository at this point in the history
* refactored users models

* added tests
  • Loading branch information
Vladislav Denisov authored Oct 4, 2023
1 parent 09ec299 commit a19b17b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
20 changes: 13 additions & 7 deletions redash/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from functools import reduce
from operator import or_

from flask import current_app as app
from flask import request_started, url_for
from flask import current_app, request_started, url_for
from flask_login import AnonymousUserMixin, UserMixin, current_user
from passlib.apps import custom_app_context as pwd_context
from sqlalchemy.dialects import postgresql
Expand Down Expand Up @@ -129,7 +128,7 @@ def regenerate_api_key(self):
def to_dict(self, with_api_key=False):
profile_image_url = self.profile_image_url
if self.is_disabled:
assets = app.extensions["webpack"]["assets"] or {}
assets = current_app.extensions["webpack"]["assets"] or {}
path = "images/avatar.svg"
profile_image_url = url_for("static", filename=assets.get(path, path))

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

return d

def is_api_user(self):
@staticmethod
def is_api_user():
return False

@property
Expand Down Expand Up @@ -377,7 +377,8 @@ class AnonymousUser(AnonymousUserMixin, PermissionsCheckMixin):
def permissions(self):
return []

def is_api_user(self):
@staticmethod
def is_api_user():
return False


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

def is_api_user(self):
@staticmethod
def is_api_user():
return True

@property
Expand All @@ -410,5 +412,9 @@ def org_id(self):
def permissions(self):
return ["view_query"]

def has_access(self, obj, access_type):
@staticmethod
def has_access(obj, access_type):
return False

def get_actual_user(self):
return repr(self)
15 changes: 14 additions & 1 deletion tests/models/test_users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from redash import redis_connection
from redash.models import User, db
from redash.models import ApiUser, User, db
from redash.models.users import LAST_ACTIVE_KEY, sync_last_active_at
from redash.utils import dt_from_timestamp
from tests import BaseTestCase, authenticated_user
Expand Down Expand Up @@ -103,3 +103,16 @@ def test_sync(self):
user_reloaded = User.query.filter(User.id == user.id).first()
self.assertIn("active_at", user_reloaded.details)
self.assertEqual(user_reloaded.active_at, timestamp)


class TestUserGetActualUser(BaseTestCase):
def test_default_user(self):
user_email = "[email protected]"
user = self.factory.create_user(email=user_email)
self.assertEqual(user.get_actual_user(), user_email)

def test_api_user(self):
user_email = "[email protected]"
user = self.factory.create_user(email=user_email)
api_user = ApiUser(user.api_key, user.org, user.group_ids)
self.assertEqual(api_user.get_actual_user(), repr(api_user))

0 comments on commit a19b17b

Please sign in to comment.