dpgaspar commented on a change in pull request #17836:
URL: https://github.com/apache/superset/pull/17836#discussion_r789794205



##########
File path: tests/integration_tests/security/row_level_security_tests.py
##########
@@ -0,0 +1,304 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# isort:skip_file
+import re
+from typing import Any, Dict, List, Optional
+from unittest import mock
+
+import pytest
+from flask import g
+
+from superset import db, security_manager
+from superset.connectors.sqla.models import RowLevelSecurityFilter, SqlaTable
+from superset.security.guest_token import (
+    GuestTokenRlsRule,
+    GuestTokenResourceType,
+    GuestUser,
+)
+from ..base_tests import SupersetTestCase
+from tests.integration_tests.fixtures.birth_names_dashboard import (
+    load_birth_names_dashboard_with_slices,
+    load_birth_names_data,
+)
+from tests.integration_tests.fixtures.energy_dashboard import (
+    load_energy_table_with_slice,
+    load_energy_table_data,
+)
+from tests.integration_tests.fixtures.unicode_dashboard import (
+    load_unicode_dashboard_with_slice,
+    load_unicode_data,
+)
+
+
+query_obj: Dict[str, Any] = dict(
+    groupby=[],
+    metrics=None,
+    filter=[],
+    is_timeseries=False,
+    columns=["value"],
+    granularity=None,
+    from_dttm=None,
+    to_dttm=None,
+    extras={},
+)
+NAME_AB_ROLE = "NameAB"
+NAME_Q_ROLE = "NameQ"
+NAMES_A_REGEX = re.compile(r"name like 'A%'")
+NAMES_B_REGEX = re.compile(r"name like 'B%'")
+NAMES_Q_REGEX = re.compile(r"name like 'Q%'")
+BASE_FILTER_REGEX = re.compile(r"gender = 'boy'")
+
+
+class TestRowLevelSecurity(SupersetTestCase):
+    """
+    Testing Row Level Security
+    """
+
+    def setUp(self):
+        session = db.session
+
+        # Create roles
+        security_manager.add_role(NAME_AB_ROLE)

Review comment:
       nit: `self.role_ab = security_manager.add_role(NAME_AB_ROLE)` would 
avoid doing a find_role next

##########
File path: tests/integration_tests/security/row_level_security_tests.py
##########
@@ -0,0 +1,304 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# isort:skip_file
+import re
+from typing import Any, Dict, List, Optional
+from unittest import mock
+
+import pytest
+from flask import g
+
+from superset import db, security_manager
+from superset.connectors.sqla.models import RowLevelSecurityFilter, SqlaTable
+from superset.security.guest_token import (
+    GuestTokenRlsRule,
+    GuestTokenResourceType,
+    GuestUser,
+)
+from ..base_tests import SupersetTestCase
+from tests.integration_tests.fixtures.birth_names_dashboard import (
+    load_birth_names_dashboard_with_slices,
+    load_birth_names_data,
+)
+from tests.integration_tests.fixtures.energy_dashboard import (
+    load_energy_table_with_slice,
+    load_energy_table_data,
+)
+from tests.integration_tests.fixtures.unicode_dashboard import (
+    load_unicode_dashboard_with_slice,
+    load_unicode_data,
+)
+
+
+query_obj: Dict[str, Any] = dict(
+    groupby=[],
+    metrics=None,
+    filter=[],
+    is_timeseries=False,
+    columns=["value"],
+    granularity=None,
+    from_dttm=None,
+    to_dttm=None,
+    extras={},
+)
+NAME_AB_ROLE = "NameAB"
+NAME_Q_ROLE = "NameQ"
+NAMES_A_REGEX = re.compile(r"name like 'A%'")
+NAMES_B_REGEX = re.compile(r"name like 'B%'")
+NAMES_Q_REGEX = re.compile(r"name like 'Q%'")
+BASE_FILTER_REGEX = re.compile(r"gender = 'boy'")
+
+
+class TestRowLevelSecurity(SupersetTestCase):
+    """
+    Testing Row Level Security
+    """
+
+    def setUp(self):
+        session = db.session
+
+        # Create roles
+        security_manager.add_role(NAME_AB_ROLE)
+        security_manager.add_role(NAME_Q_ROLE)
+        gamma_user = security_manager.find_user(username="gamma")
+        gamma_user.roles.append(security_manager.find_role(NAME_AB_ROLE))
+        gamma_user.roles.append(security_manager.find_role(NAME_Q_ROLE))
+        self.create_user_with_roles("NoRlsRoleUser", ["Gamma"])
+        session.commit()
+
+        # Create regular RowLevelSecurityFilter (energy_usage, unicode_test)
+        self.rls_entry1 = RowLevelSecurityFilter()
+        self.rls_entry1.tables.extend(
+            session.query(SqlaTable)
+            .filter(SqlaTable.table_name.in_(["energy_usage", "unicode_test"]))
+            .all()
+        )
+        self.rls_entry1.filter_type = "Regular"
+        self.rls_entry1.clause = "value > {{ cache_key_wrapper(1) }}"
+        self.rls_entry1.group_key = None
+        self.rls_entry1.roles.append(security_manager.find_role("Gamma"))
+        self.rls_entry1.roles.append(security_manager.find_role("Alpha"))
+        db.session.add(self.rls_entry1)
+
+        # Create regular RowLevelSecurityFilter (birth_names name starts with 
A or B)
+        self.rls_entry2 = RowLevelSecurityFilter()
+        self.rls_entry2.tables.extend(
+            session.query(SqlaTable)
+            .filter(SqlaTable.table_name.in_(["birth_names"]))
+            .all()
+        )
+        self.rls_entry2.filter_type = "Regular"
+        self.rls_entry2.clause = "name like 'A%' or name like 'B%'"
+        self.rls_entry2.group_key = "name"
+        self.rls_entry2.roles.append(security_manager.find_role("NameAB"))
+        db.session.add(self.rls_entry2)
+
+        # Create Regular RowLevelSecurityFilter (birth_names name starts with 
Q)
+        self.rls_entry3 = RowLevelSecurityFilter()
+        self.rls_entry3.tables.extend(
+            session.query(SqlaTable)
+            .filter(SqlaTable.table_name.in_(["birth_names"]))
+            .all()
+        )
+        self.rls_entry3.filter_type = "Regular"
+        self.rls_entry3.clause = "name like 'Q%'"
+        self.rls_entry3.group_key = "name"
+        self.rls_entry3.roles.append(security_manager.find_role("NameQ"))
+        db.session.add(self.rls_entry3)
+
+        # Create Base RowLevelSecurityFilter (birth_names boys)
+        self.rls_entry4 = RowLevelSecurityFilter()
+        self.rls_entry4.tables.extend(
+            session.query(SqlaTable)
+            .filter(SqlaTable.table_name.in_(["birth_names"]))
+            .all()
+        )
+        self.rls_entry4.filter_type = "Base"
+        self.rls_entry4.clause = "gender = 'boy'"
+        self.rls_entry4.group_key = "gender"
+        self.rls_entry4.roles.append(security_manager.find_role("Admin"))
+        db.session.add(self.rls_entry4)
+
+        db.session.commit()
+
+    def tearDown(self):
+        session = db.session

Review comment:
       nit: consider making this a fixture also, the `setUp` and `tearDown`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to