Skip to content

Commit

Permalink
tests: simplify tests for ExportReader test data generation
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Sep 22, 2023
1 parent 27b28e6 commit d78567e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 101 deletions.
45 changes: 45 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,51 @@ def generate_test_by_comp() -> ssp.ByComponent:
return by_comp


def generate_test_inheritance_md(
provided_uuid: str, responsibility_uuid: str, leveraged_statement_names: List[str]
) -> str:
"""
Generate a inheritance statement with placeholders replaced by provided values.
Args:
provided_uuid (str): UUID for provided statement.
responsibility_uuid (str): UUID for responsibility statement.
leveraged_statement_names (list of str): Names for leveraged statements (as a list).
leveraged_ssp_href (str): Href for leveraged SSP.
Returns:
str: The template with placeholders replaced.
"""
# Convert the list of leveraged statement names into a YAML list
leveraged_statement_list = '\n'.join([f' - name: {name}' for name in leveraged_statement_names])

md_template = f"""---
x-trestle-statement:
# Add or modify leveraged SSP Statements here.
provided-uuid: {provided_uuid}
responsibility-uuid: {responsibility_uuid}
x-trestle-leveraging-comp:
# Leveraged statements can be optionally associated with components in this system.
# Associate leveraged statements to Components of this system here:
{leveraged_statement_list}
---
# Provided Statement Description
provided statement description
# Responsibility Statement Description
resp statement description
# Satisfied Statement Description
<!-- Use this section to explain how the inherited responsibility is being satisfied. -->
My Satisfied Description
"""
return md_template


class FileChecker:
"""Check for changes in files after test operations."""

Expand Down
141 changes: 40 additions & 101 deletions tests/trestle/core/crm/exports_reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,90 +29,21 @@
expected_appliance_uuid = '22222222-0000-4000-9001-000000000003'
expected_saas_uuid = '22222222-0000-4000-9001-000000000001'

inheritance_text = """---
x-trestle-statement:
# Add or modify leveraged SSP Statements here.
provided-uuid: 18ac4e2a-b5f2-46e4-94fa-cc84ab6fe114
responsibility-uuid: 4b34c68f-75fa-4b38-baf0-e50158c13ac2
x-trestle-leveraging-comp:
# Leveraged statements can be optionally associated with components in this system.
# Associate leveraged statements to Components of this system here:
- name: Access Control Appliance
- name: THIS SYSTEM (SaaS)
---
example_provided_uuid = '18ac4e2a-b5f2-46e4-94fa-cc84ab6fe114'
example_responsibility_uuid = '4b34c68f-75fa-4b38-baf0-e50158c13ac2'

# Provided Statement Description

provided statement description
# Responsibility Statement Description
resp statement description
# Satisfied Statement Description
<!-- Use this section to explain how the inherited responsibility is being satisfied. -->
My Satisfied Description
"""

inheritance_text_2 = """---
x-trestle-statement:
# Add or modify leveraged SSP Statements here.
provided-uuid: 18ac4e2a-b5f2-46e4-94fa-cc84ab6fe115
responsibility-uuid: 4b34c68f-75fa-4b38-baf0-e50158c13ac3
x-trestle-leveraging-comp:
# Leveraged statements can be optionally associated with components in this system.
# Associate leveraged statements to Components of this system here:
- name: Access Control Appliance
---
# Provided Statement Description
provided statement description
# Responsibility Statement Description
resp statement description
# Satisfied Statement Description
<!-- Use this section to explain how the inherited responsibility is being satisfied. -->
My Satisfied Description
"""

unmapped_inheritance = """---
x-trestle-statement:
# Add or modify leveraged SSP Statements here.
provided-uuid: 18ac4e2a-b5f2-46e4-94fa-cc84ab6fe115
responsibility-uuid: 4b34c68f-75fa-4b38-baf0-e50158c13ac3
x-trestle-leveraging-comp:
# Leveraged statements can be optionally associated with components in this system.
# Associate leveraged statements to Components of this system here:
- name: REPLACE_ME
---
# Provided Statement Description
provided statement description
# Responsibility Statement Description
resp statement description
# Satisfied Statement Description
<!-- Use this section to explain how the inherited responsibility is being satisfied. -->
"""


def test_read_exports_from_markdown(tmp_trestle_dir: pathlib.Path) -> None:
"""Test exports reader with inheritance view."""
ipath = tmp_trestle_dir.joinpath(leveraging_ssp, const.INHERITANCE_VIEW_DIR)

ac_appliance_dir = ipath.joinpath('Access Control Appliance')
def prep_inheritance_dir(ac_appliance_dir: pathlib.Path) -> None:
"""Prepare inheritance directory with basic information."""
ac_2 = ac_appliance_dir.joinpath('ac-2')
ac_2.mkdir(parents=True)

inheritance_text = test_utils.generate_test_inheritance_md(
provided_uuid=example_provided_uuid,
responsibility_uuid=example_responsibility_uuid,
leveraged_statement_names=['Access Control Appliance', 'THIS SYSTEM (SaaS)']
)

file = ac_2 / f'{expected_appliance_uuid}.md'
with open(file, 'w') as f:
f.write(inheritance_text)
Expand All @@ -125,6 +56,13 @@ def test_read_exports_from_markdown(tmp_trestle_dir: pathlib.Path) -> None:
with open(file, 'w') as f:
f.write(inheritance_text)


def test_read_exports_from_markdown(tmp_trestle_dir: pathlib.Path) -> None:
"""Test exports reader with inheritance view."""
inheritance_path = tmp_trestle_dir.joinpath(leveraged_ssp, const.INHERITANCE_VIEW_DIR)
ac_appliance_dir = inheritance_path.joinpath('Access Control Appliance')
prep_inheritance_dir(ac_appliance_dir)

test_utils.load_from_json(tmp_trestle_dir, 'leveraging_ssp', leveraging_ssp, ossp.SystemSecurityPlan)

orig_ssp, _ = ModelUtils.load_model_for_class(
Expand All @@ -133,7 +71,7 @@ def test_read_exports_from_markdown(tmp_trestle_dir: pathlib.Path) -> None:
ossp.SystemSecurityPlan,
FileContentType.JSON)

reader = exportreader.ExportReader(ipath, orig_ssp) # type: ignore
reader = exportreader.ExportReader(inheritance_path, orig_ssp) # type: ignore
ssp = reader.read_exports_from_markdown()

implemented_requirements = ssp.control_implementation.implemented_requirements
Expand Down Expand Up @@ -161,22 +99,22 @@ def test_read_exports_from_markdown(tmp_trestle_dir: pathlib.Path) -> None:

def test_read_inheritance_markdown_dir(tmp_trestle_dir: pathlib.Path) -> None:
"""Test reading inheritance view directory."""
ipath = tmp_trestle_dir.joinpath(leveraging_ssp, const.INHERITANCE_VIEW_DIR)
ac_appliance_dir = ipath.joinpath('Access Control Appliance')
inheritance_path = tmp_trestle_dir.joinpath(leveraged_ssp, const.INHERITANCE_VIEW_DIR)
ac_appliance_dir = inheritance_path.joinpath('Access Control Appliance')
prep_inheritance_dir(ac_appliance_dir)

ac_2 = ac_appliance_dir.joinpath('ac-2')
ac_2.mkdir(parents=True)

file = ac_2 / f'{expected_appliance_uuid}.md'
with open(file, 'w') as f:
f.write(inheritance_text)
unmapped_text = test_utils.generate_test_inheritance_md(
provided_uuid=example_provided_uuid,
responsibility_uuid=example_responsibility_uuid,
leveraged_statement_names=[const.REPLACE_ME]
)

ac_21 = ac_appliance_dir.joinpath('ac-2.1')
ac_21.mkdir(parents=True)
# Ensure this file does not get added to the dictionary
file = ac_21 / f'{expected_appliance_uuid}.md'
with open(file, 'w') as f:
f.write(unmapped_inheritance)
f.write(unmapped_text)

test_utils.load_from_json(tmp_trestle_dir, 'leveraging_ssp', leveraging_ssp, ossp.SystemSecurityPlan)

Expand All @@ -186,10 +124,10 @@ def test_read_inheritance_markdown_dir(tmp_trestle_dir: pathlib.Path) -> None:
ossp.SystemSecurityPlan,
FileContentType.JSON)

reader = exportreader.ExportReader(ipath, orig_ssp) # type: ignore
reader = exportreader.ExportReader(inheritance_path, orig_ssp) # type: ignore
markdown_dict: exportreader.InheritanceViewDict = reader._read_inheritance_markdown_directory()

assert len(markdown_dict) == 1
assert len(markdown_dict) == 2
assert 'ac-2' in markdown_dict
assert len(markdown_dict['ac-2']) == 2
assert expected_appliance_uuid in markdown_dict['ac-2']
Expand All @@ -203,17 +141,18 @@ def test_read_inheritance_markdown_dir(tmp_trestle_dir: pathlib.Path) -> None:

def test_read_inheritance_markdown_dir_with_multiple_leveraged_components(tmp_trestle_dir: pathlib.Path) -> None:
"""Test reading inheritance view directory with components that span multiple leveraged components."""
ipath = tmp_trestle_dir.joinpath(leveraging_ssp, const.INHERITANCE_VIEW_DIR)
inheritance_path = tmp_trestle_dir.joinpath(leveraged_ssp, const.INHERITANCE_VIEW_DIR)

ac_appliance_dir = ipath.joinpath('Access Control Appliance')
ac_2 = ac_appliance_dir.joinpath('ac-2')
ac_2.mkdir(parents=True)
ac_appliance_dir = inheritance_path.joinpath('Access Control Appliance')
prep_inheritance_dir(ac_appliance_dir)

file = ac_2 / f'{expected_appliance_uuid}.md'
with open(file, 'w') as f:
f.write(inheritance_text)
inheritance_text_2 = test_utils.generate_test_inheritance_md(
provided_uuid=example_provided_uuid,
responsibility_uuid=example_responsibility_uuid,
leveraged_statement_names=['Access Control Appliance']
)

this_system_dir = ipath.joinpath('This System')
this_system_dir = inheritance_path.joinpath('This System')
ac_2 = this_system_dir.joinpath('ac-2')
ac_2.mkdir(parents=True)

Expand All @@ -229,10 +168,10 @@ def test_read_inheritance_markdown_dir_with_multiple_leveraged_components(tmp_tr
ossp.SystemSecurityPlan,
FileContentType.JSON)

reader = exportreader.ExportReader(ipath, orig_ssp) # type: ignore
reader = exportreader.ExportReader(inheritance_path, orig_ssp) # type: ignore
markdown_dict: exportreader.InheritanceViewDict = reader._read_inheritance_markdown_directory()

assert len(markdown_dict) == 1
assert len(markdown_dict) == 2
assert 'ac-2' in markdown_dict
assert len(markdown_dict['ac-2']) == 2

Expand Down

0 comments on commit d78567e

Please sign in to comment.