From a19b17b844063f215266286ea8bd185086e3e27a Mon Sep 17 00:00:00 2001 From: Vladislav Denisov Date: Wed, 4 Oct 2023 08:12:17 +0300 Subject: [PATCH] Fixed embedded queries (#6497) * refactored users models * added tests --- redash/models/users.py | 20 +++++++++++++------- tests/models/test_users.py | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/redash/models/users.py b/redash/models/users.py index 48e1bea3bc..bab0c8b1fc 100644 --- a/redash/models/users.py +++ b/redash/models/users.py @@ -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 @@ -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)) @@ -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 @@ -377,7 +377,8 @@ class AnonymousUser(AnonymousUserMixin, PermissionsCheckMixin): def permissions(self): return [] - def is_api_user(self): + @staticmethod + def is_api_user(): return False @@ -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 @@ -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) diff --git a/tests/models/test_users.py b/tests/models/test_users.py index 9ad46a03dd..d69ee00db1 100644 --- a/tests/models/test_users.py +++ b/tests/models/test_users.py @@ -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 @@ -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 = "test@example.com" + user = self.factory.create_user(email=user_email) + self.assertEqual(user.get_actual_user(), user_email) + + def test_api_user(self): + user_email = "test@example.com" + 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))