Skip to content

Commit

Permalink
BI-4885: add more RLS tests (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCPN authored Nov 28, 2023
1 parent 0dbbd09 commit 998d2c1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/dl_api_lib/dl_api_lib_tests/db/control_api/test_rls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
from dl_api_lib_tests.db.base import DefaultApiTestBase


class TestDataset(DefaultApiTestBase):
class TestRLS(DefaultApiTestBase):
@staticmethod
def add_rls_to_dataset(control_api, dataset, rls_config):
field_guid = dataset.result_schema[0].id
dataset.rls = {field_guid: rls_config}
resp = control_api.save_dataset(dataset, fail_ok=True)
return field_guid, resp

def test_dataset_without_configured_rls(self, saved_dataset):
assert all(rls_value == "" for rls_value in saved_dataset.rls.values())

@pytest.mark.parametrize("case", RLS_CONFIG_CASES, ids=[c["name"] for c in RLS_CONFIG_CASES])
def test_create_and_update_rls(self, control_api, saved_dataset, case):
config = case["config"]
Expand Down
41 changes: 41 additions & 0 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
@@ -0,0 +1,41 @@
import pytest

from dl_api_lib_testing.app import TestingSubjectResolver
from dl_api_lib_testing.rls import MAIN_TEST_CASE
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"]}

control_api.save_dataset(ds, fail_ok=False)
resp = control_api.load_dataset(ds)
assert resp.status_code == 200, resp.json
return resp.dataset

@staticmethod
def _get_rls_preview_response(ds, data_api, monkeypatch, modify_rls):
def get_subjects_by_names_mock(self, names):
raise RuntimeError("Shouldn't be invoked in preview")

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)

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

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)
resp_data = resp.json
assert resp.status_code == 400, resp_data
assert resp.bi_status_code == "ERR.DS_API.RLS.PARSE"
assert resp_data["message"] == "For this feature to work, save dataset after editing the RLS config."
17 changes: 17 additions & 0 deletions lib/dl_api_lib/dl_api_lib_tests/unit/test_rls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from dl_api_lib.utils.rls import FieldRLSSerializer
from dl_api_lib_testing.rls import (
RLS_CONFIG_CASES,
config_to_comparable,
)


@pytest.mark.parametrize("case", RLS_CONFIG_CASES, ids=[c["name"] for c in RLS_CONFIG_CASES])
def test_rls_entries_to_text_config(case):
expected_config = case["config_to_compare"]
rls_entries = case["rls_entries"]

config = FieldRLSSerializer.to_text_config(rls_entries)

assert config_to_comparable(config) == config_to_comparable(expected_config)

0 comments on commit 998d2c1

Please sign in to comment.