Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BI-4885: add tests for RLS queries #153

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/dl_api_lib/dl_api_lib_tests/db/control_api/test_rls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import pytest

from dl_api_commons.base_models import RequestContextInfo
from dl_api_lib.dataset.view import DatasetView
from dl_api_lib.query.formalization.block_formalizer import BlockFormalizer
from dl_api_lib.query.formalization.legend_formalizer import ResultLegendFormalizer
from dl_api_lib.query.formalization.raw_specs import (
IdFieldRef,
RawQuerySpecUnion,
RawSelectFieldSpec,
)
from dl_api_lib_testing.rls import (
RLS_CONFIG_CASES,
config_to_comparable,
Expand Down Expand Up @@ -59,3 +68,34 @@ def test_create_rls_from_invalid_config(self, control_api, saved_dataset):
assert rls_resp.bi_status_code == "ERR.DS_API.RLS.PARSE"
assert rls_resp.json["message"] == "RLS: Parsing failed at line 2"
assert rls_resp.json["details"] == {"description": "Wrong format"}

def test_rls_filter_expr(self, control_api, saved_dataset, sync_us_manager):
config = load_rls_config("dl_api_lib_test_config")
field_a, field_b = saved_dataset.result_schema[0].id, saved_dataset.result_schema[1].id
saved_dataset.rls = {field_a: config, field_b: config}
control_api.save_dataset(saved_dataset, fail_ok=False)

ds = sync_us_manager.get_by_id(saved_dataset.id)
sync_us_manager.load_dependencies(ds)

rci = RequestContextInfo(user_id="user1")
raw_query_spec_union = RawQuerySpecUnion(
select_specs=[
RawSelectFieldSpec(ref=IdFieldRef(id=field_a)),
RawSelectFieldSpec(ref=IdFieldRef(id=field_b)),
],
)
legend = ResultLegendFormalizer(dataset=ds).make_legend(raw_query_spec_union=raw_query_spec_union)
block_legend = BlockFormalizer(dataset=ds).make_block_legend(
raw_query_spec_union=raw_query_spec_union, legend=legend
)
ds_view = DatasetView(
ds,
us_manager=sync_us_manager,
block_spec=block_legend.blocks[0],
rci=rci,
)

exec_info = ds_view.build_exec_info()
src_query = next(iter(exec_info.translated_multi_query.iter_queries()))
assert len(src_query.where) == 2 # field_a in ... and field_b in ...
20 changes: 13 additions & 7 deletions lib/dl_api_lib/dl_api_lib_tests/db/data_api/test_rls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import re

import pytest

from dl_api_client.dsmaker.shortcuts.result_data import get_data_rows
from dl_api_lib_testing.app import TestingSubjectResolver
from dl_api_lib_testing.rls import MAIN_TEST_CASE
from dl_api_lib_testing.rls import load_rls_config
from dl_api_lib_tests.db.base import DefaultApiTestBase


class TestRLS(DefaultApiTestBase):
@pytest.fixture(scope="function")
def dataset_with_rls(self, control_api, saved_dataset):
ds = saved_dataset
field_guid = ds.result_schema[0].id
ds.rls = {field_guid: MAIN_TEST_CASE["config"]}
field_guid = ds.result_schema[1].id
ds.rls = {field_guid: load_rls_config("dl_api_lib_test_config")}

control_api.save_dataset(ds, fail_ok=False)
resp = control_api.load_dataset(ds)
Expand All @@ -25,13 +28,16 @@ def get_subjects_by_names_mock(self, names):
rls_val_modifier = "\n'x': *\n" if modify_rls else ""
ds.rls = {key: val + rls_val_modifier for key, val in ds.rls.items() if val}
monkeypatch.setattr(TestingSubjectResolver, "get_subjects_by_names", get_subjects_by_names_mock)
return data_api.get_preview(dataset=ds, limit=13, fail_ok=True)
return data_api.get_preview(dataset=ds, fail_ok=True)

def test_preview_with_saved_rls(self, dataset_with_rls, data_api, monkeypatch):
resp = self._get_rls_preview_response(dataset_with_rls, data_api, monkeypatch, modify_rls=False)
resp_data = resp.json
assert resp.status_code == 200, resp_data
assert resp_data
assert resp.status_code == 200, resp.json

rls_data = [row[1] for row in get_data_rows(resp)]
assert rls_data
# ensure all values from the RLS config are presented and no other values are
assert set(rls_data) == set(re.findall("'(.+?)'", load_rls_config("dl_api_lib_test_config")))

def test_preview_with_updated_rls(self, dataset_with_rls, data_api, monkeypatch):
resp = self._get_rls_preview_response(dataset_with_rls, data_api, monkeypatch, modify_rls=True)
Expand Down
32 changes: 23 additions & 9 deletions lib/dl_api_lib_testing/dl_api_lib_testing/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,29 @@ def rqe_config_subprocess_cm(self) -> Generator[RQEConfig, None, None]:
@attr.s
class TestingSubjectResolver(BaseSubjectResolver):
def get_subjects_by_names(self, names: list[str]) -> list[RLSSubject]:
"""Mock resolver. Considers a user real if his name starts with 'user'"""
return [
RLSSubject(
subject_id="",
subject_type=RLSSubjectType.user if name.startswith("user") else RLSSubjectType.notfound,
subject_name=name if name.startswith("user") else RLS_FAILED_USER_NAME_PREFIX + name,
)
for name in names
]
"""
Mock resolver. Considers a user real if the name starts with a 'user' or
if it's equals to '_the_tests_asyncapp_user_name_'
"""
subjects = []
for name in names:
if name == "_the_tests_asyncapp_user_name_":
subjects.append(
RLSSubject(
subject_id="_the_tests_asyncapp_user_id_",
subject_type=RLSSubjectType.user,
subject_name=name,
)
)
else:
subjects.append(
RLSSubject(
subject_id="",
subject_type=RLSSubjectType.user if name.startswith("user") else RLSSubjectType.notfound,
subject_name=name if name.startswith("user") else RLS_FAILED_USER_NAME_PREFIX + name,
)
)
return subjects


@attr.s
Expand Down
1 change: 0 additions & 1 deletion lib/dl_api_lib_testing/dl_api_lib_testing/rls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def load_rls(name: str) -> list[RLSEntry]:
rls_entries_updated=load_rls("missing_login_updated.json"),
),
]
MAIN_TEST_CASE = RLS_CONFIG_CASES[0]


def config_to_comparable(conf: str):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'Naperville': _the_tests_asyncapp_user_name_
'Philadelphia': *
Loading