From cae9fc46d70c3b1a42a2ff48a4602190512efa83 Mon Sep 17 00:00:00 2001 From: Nikolay Proskurin Date: Mon, 30 Oct 2023 15:07:53 +0100 Subject: [PATCH 1/3] BI-4885: move rls test data to dl_api_lib_testing, add base test case --- .../dl_api_lib_testing/test_data/__init__.py | 0 .../test_data/rls_configs/bad | 0 .../test_data/rls_configs/bad_login | 0 .../test_data/rls_configs/missing_login | 0 .../test_data/rls_configs/missing_login.json | 0 .../rls_configs/missing_login_to_compare | 0 .../rls_configs/missing_login_updated | 0 .../rls_configs/missing_login_updated.json | 0 .../test_data/rls_configs/simple | 0 .../test_data/rls_configs/simple.json | 0 .../test_data/rls_configs/simple_to_compare | 0 .../test_data/rls_configs/simple_updated | 0 .../test_data/rls_configs/simple_updated.json | 0 .../test_data/rls_configs/wildcards | 0 .../test_data/rls_configs/wildcards.json | 0 .../rls_configs/wildcards_to_compare | 0 .../test_data/rls_configs/iam_subjects.json | 38 ------------------- 17 files changed, 38 deletions(-) create mode 100644 lib/dl_api_lib_testing/dl_api_lib_testing/test_data/__init__.py rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/bad (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/bad_login (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/missing_login (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/missing_login.json (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/missing_login_to_compare (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/missing_login_updated (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/missing_login_updated.json (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/simple (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/simple.json (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/simple_to_compare (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/simple_updated (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/simple_updated.json (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/wildcards (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/wildcards.json (100%) rename lib/{dl_testing/dl_testing => dl_api_lib_testing/dl_api_lib_testing}/test_data/rls_configs/wildcards_to_compare (100%) delete mode 100644 lib/dl_testing/dl_testing/test_data/rls_configs/iam_subjects.json diff --git a/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/__init__.py b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/bad b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/bad similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/bad rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/bad diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/bad_login b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/bad_login similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/bad_login rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/bad_login diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/missing_login b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/missing_login rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/missing_login.json b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login.json similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/missing_login.json rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login.json diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/missing_login_to_compare b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login_to_compare similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/missing_login_to_compare rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login_to_compare diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/missing_login_updated b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login_updated similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/missing_login_updated rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login_updated diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/missing_login_updated.json b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login_updated.json similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/missing_login_updated.json rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/missing_login_updated.json diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/simple b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/simple rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/simple.json b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple.json similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/simple.json rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple.json diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/simple_to_compare b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple_to_compare similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/simple_to_compare rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple_to_compare diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/simple_updated b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple_updated similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/simple_updated rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple_updated diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/simple_updated.json b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple_updated.json similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/simple_updated.json rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/simple_updated.json diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/wildcards b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/wildcards similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/wildcards rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/wildcards diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/wildcards.json b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/wildcards.json similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/wildcards.json rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/wildcards.json diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/wildcards_to_compare b/lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/wildcards_to_compare similarity index 100% rename from lib/dl_testing/dl_testing/test_data/rls_configs/wildcards_to_compare rename to lib/dl_api_lib_testing/dl_api_lib_testing/test_data/rls_configs/wildcards_to_compare diff --git a/lib/dl_testing/dl_testing/test_data/rls_configs/iam_subjects.json b/lib/dl_testing/dl_testing/test_data/rls_configs/iam_subjects.json deleted file mode 100644 index 461e1378b..000000000 --- a/lib/dl_testing/dl_testing/test_data/rls_configs/iam_subjects.json +++ /dev/null @@ -1,38 +0,0 @@ -[ - { - "id": "1120000000000529", - "email": "user1@yandex.ru", - "title": "user1", - "subject_type": "USER_ACCOUNT" - }, - { - "id": "1120000000001062", - "email": "user4@yandex.ru", - "title": "user4", - "subject_type": "USER_ACCOUNT" - }, - { - "id": "1120000000000251", - "email": "user5@yandex.ru", - "title": "user5", - "subject_type": "USER_ACCOUNT" - }, - { - "id": "1120000000013893", - "email": "user6@yandex.ru", - "title": "user6", - "subject_type": "USER_ACCOUNT" - }, - { - "id": "1120000000019416", - "email": "user2@yandex.ru", - "title": "user2", - "subject_type": "USER_ACCOUNT" - }, - { - "id": "1120000000001001", - "email": "user3@yandex.ru", - "title": "user3", - "subject_type": "USER_ACCOUNT" - } -] From 1cd526eb5c5c233a8627cc84da38b2b39fa31219 Mon Sep 17 00:00:00 2001 From: Nikolay Proskurin Date: Mon, 30 Oct 2023 15:08:35 +0100 Subject: [PATCH 2/3] add --- .../dl_api_lib_testing/rls.py | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/dl_api_lib_testing/dl_api_lib_testing/rls.py diff --git a/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py b/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py new file mode 100644 index 000000000..ac46be5f9 --- /dev/null +++ b/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py @@ -0,0 +1,85 @@ +import json +import pkgutil + +import dl_api_lib_testing.test_data +from dl_api_lib.utils.rls import FieldRLSSerializer +from dl_constants.enums import RLSSubjectType +from dl_core.rls import ( + BaseSubjectResolver, + RLSEntry, + RLSPatternType, + RLSSubject, +) + + +def load_rls_config(name: str) -> str: + data = pkgutil.get_data(dl_api_lib_testing.test_data.__name__, "rls_configs/" + name) + return data.decode("utf-8") + + +def load_rls(name: str) -> list[RLSEntry]: + data = json.loads(load_rls_config(name)) + # TODO: use the actual deserialization for this? + return [ + RLSEntry( + field_guid="", # not used in this case + pattern_type=RLSPatternType[entry.get("pattern_type", "value")], + allowed_value=entry["allowed_value"], + subject=RLSSubject( + subject_type=RLSSubjectType[entry["subject"]["subject_type"]], + subject_id=entry["subject"]["subject_id"], + subject_name=entry["subject"]["subject_name"], + ), + ) + for entry in data + ] + + +RLS_CONFIG_CASES = [ + dict( + name="simple", + field_guid="c139ac51-a519-49a9-996a-49c7656fe56b", + config=load_rls_config("simple"), + config_to_compare=load_rls_config("simple_to_compare"), + rls_entries=load_rls("simple.json"), + config_updated=load_rls_config("simple_updated"), + rls_entries_updated=load_rls("simple_updated.json"), + ), + dict( + name="wildcards", + field_guid="c139ac51-a519-49a9-996a-49c7656fe56b", + config=load_rls_config("wildcards"), + config_to_compare=load_rls_config("wildcards_to_compare"), + rls_entries=load_rls("wildcards.json"), + ), + dict( + name="missing_login", + field_guid="c139ac51-a519-49a9-996a-49c7656fe56b", + config=load_rls_config("missing_login"), + config_to_compare=load_rls_config("missing_login_to_compare"), + rls_entries=load_rls("missing_login.json"), + config_updated=load_rls_config("missing_login_updated"), + rls_entries_updated=load_rls("missing_login_updated.json"), + ), +] +MAIN_TEST_CASE = RLS_CONFIG_CASES[0] + + +def check_text_config_to_rls_entries(case: dict, subject_resolver: BaseSubjectResolver) -> None: + field_guid, config, expected_rls_entries = case["field_guid"], case["config"], case["rls_entries"] + entries = FieldRLSSerializer.from_text_config(config, field_guid, subject_resolver=subject_resolver) + + assert len(entries) == len(expected_rls_entries) + + for exp_entry in expected_rls_entries: + matched_entries = [ + entry + for entry in entries + if entry.pattern_type == exp_entry.pattern_type + and entry.allowed_value == exp_entry.allowed_value + and entry.subject.subject_id == exp_entry.subject.subject_id + and entry.subject.subject_type.name == exp_entry.subject.subject_type.name + and entry.subject.subject_name == exp_entry.subject.subject_name + ] + assert matched_entries, ("Expected to find an entry", exp_entry) + assert len(matched_entries) < 2, ("Expected to find exactly one entry", exp_entry) From 511f154b40295d6dc723e17d9362801654e6fcd7 Mon Sep 17 00:00:00 2001 From: Nikolay Proskurin Date: Mon, 30 Oct 2023 15:38:18 +0100 Subject: [PATCH 3/3] cq --- lib/dl_api_lib_testing/dl_api_lib_testing/rls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py b/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py index ac46be5f9..15234d0de 100644 --- a/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py +++ b/lib/dl_api_lib_testing/dl_api_lib_testing/rls.py @@ -1,8 +1,8 @@ import json import pkgutil -import dl_api_lib_testing.test_data from dl_api_lib.utils.rls import FieldRLSSerializer +import dl_api_lib_testing.test_data from dl_constants.enums import RLSSubjectType from dl_core.rls import ( BaseSubjectResolver,