diff --git a/lib/dl_api_lib/dl_api_lib_tests/db/control_api/test_rls.py b/lib/dl_api_lib/dl_api_lib_tests/db/control_api/test_rls.py index 078144e34..9be5cac80 100644 --- a/lib/dl_api_lib/dl_api_lib_tests/db/control_api/test_rls.py +++ b/lib/dl_api_lib/dl_api_lib_tests/db/control_api/test_rls.py @@ -8,7 +8,7 @@ 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 @@ -16,6 +16,9 @@ def add_rls_to_dataset(control_api, dataset, 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"] diff --git a/lib/dl_api_lib/dl_api_lib_tests/db/data_api/test_rls.py b/lib/dl_api_lib/dl_api_lib_tests/db/data_api/test_rls.py new file mode 100644 index 000000000..90e5fcf2e --- /dev/null +++ b/lib/dl_api_lib/dl_api_lib_tests/db/data_api/test_rls.py @@ -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." diff --git a/lib/dl_api_lib/dl_api_lib_tests/unit/test_rls.py b/lib/dl_api_lib/dl_api_lib_tests/unit/test_rls.py new file mode 100644 index 000000000..145003ce1 --- /dev/null +++ b/lib/dl_api_lib/dl_api_lib_tests/unit/test_rls.py @@ -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)