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: move rls test data to dl_api_lib_testing, add base test case #52

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
85 changes: 85 additions & 0 deletions lib/dl_api_lib_testing/dl_api_lib_testing/rls.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
38 changes: 0 additions & 38 deletions lib/dl_testing/dl_testing/test_data/rls_configs/iam_subjects.json

This file was deleted.

Loading