From d78567e91322850be337eaf1c268c9a527cacbea Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 22 Sep 2023 08:53:41 -0400 Subject: [PATCH] tests: simplify tests for ExportReader test data generation Signed-off-by: Jennifer Power --- tests/test_utils.py | 45 ++++++ tests/trestle/core/crm/exports_reader_test.py | 141 +++++------------- 2 files changed, 85 insertions(+), 101 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 36e01b194..c044e8503 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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 + + +My Satisfied Description + """ + return md_template + + class FileChecker: """Check for changes in files after test operations.""" diff --git a/tests/trestle/core/crm/exports_reader_test.py b/tests/trestle/core/crm/exports_reader_test.py index b576b8862..e90b73897 100644 --- a/tests/trestle/core/crm/exports_reader_test.py +++ b/tests/trestle/core/crm/exports_reader_test.py @@ -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 - - -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 - - -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 - - -""" - - -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) @@ -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( @@ -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 @@ -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) @@ -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'] @@ -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) @@ -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