From 3e1ce19665f8197caabd6b2a3b0f4ddfe9afa34c Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 1 Sep 2023 19:34:26 -0400 Subject: [PATCH] test: adds inheritance view testing for ssp-assemble Signed-off-by: Jennifer Power --- tests/test_utils.py | 14 +++++++++++ .../trestle/core/commands/author/ssp_test.py | 23 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 922335f9b..36e01b194 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -242,6 +242,20 @@ def replace_line_in_file_after_tag(file_path: pathlib.Path, tag: str, new_line: return False +def replace_in_file(file_path: pathlib.Path, search_text: str, replace_text: str) -> None: + """Replace all occurrences of search_text with replace_text in file_path.""" + if not file_path.exists(): + raise TrestleError(f'Test file {file_path} not found.') + + with open(file_path, 'r') as file: + file_content = file.read() + + updated_content = file_content.replace(search_text, replace_text) + + with open(file_path, 'w') as file: + file.write(updated_content) + + def substitute_text_in_file(file_path: pathlib.Path, tag: str, new_str: str) -> bool: """Substitute first match of string with new string in file.""" if not file_path.exists(): diff --git a/tests/trestle/core/commands/author/ssp_test.py b/tests/trestle/core/commands/author/ssp_test.py index ff71d6bcb..2dd68da19 100644 --- a/tests/trestle/core/commands/author/ssp_test.py +++ b/tests/trestle/core/commands/author/ssp_test.py @@ -548,8 +548,8 @@ def test_ssp_generate_resolved_catalog(tmp_trestle_dir: pathlib.Path) -> None: resolved_catalog.oscal_write(new_catalog_path) -def test_ssp_assemble_w_inhert(tmp_trestle_dir: pathlib.Path) -> None: - """Test ssp assemble from cli.""" +def test_ssp_assemble_with_inheritance(tmp_trestle_dir: pathlib.Path) -> None: + """Test ssp assemble from cli with inheritance view.""" gen_args, _ = setup_for_ssp(tmp_trestle_dir, prof_name, ssp_name, False, 'leveraged_ssp') args_compdefs = gen_args.compdefs @@ -557,6 +557,14 @@ def test_ssp_assemble_w_inhert(tmp_trestle_dir: pathlib.Path) -> None: ssp_gen = SSPGenerate() assert ssp_gen._run(gen_args) == 0 + this_system_dir = tmp_trestle_dir / ssp_name / const.INHERITANCE_VIEW_DIR / 'This System' + + expected_uuid = '11111111-0000-4000-9009-001001002001' + ac_21 = this_system_dir / 'ac-2.1' + test_provided = ac_21 / f'{expected_uuid}.md' + + test_utils.replace_in_file(test_provided, 'REPLACE_ME', 'comp_aa') + # now assemble the edited controls into json ssp ssp_assemble = SSPAssemble() args = argparse.Namespace( @@ -571,6 +579,17 @@ def test_ssp_assemble_w_inhert(tmp_trestle_dir: pathlib.Path) -> None: ) assert ssp_assemble._run(args) == 0 + ssp, _ = ModelUtils.load_model_for_class(tmp_trestle_dir, ssp_name, ossp.SystemSecurityPlan, FileContentType.JSON) + + imp_reqs = ssp.control_implementation.implemented_requirements + imp_req = next((i_req for i_req in imp_reqs if i_req.control_id == 'ac-2.1'), None) + inherited = imp_req.by_components[1].inherited[0] # type: ignore + assert inherited.description == ( + 'Consumer-appropriate description of what may be inherited.\n\n\ +In the context of the application component in satisfaction of AC-2.1.' + ) + assert inherited.provided_uuid == expected_uuid + def test_ssp_filter(tmp_trestle_dir: pathlib.Path) -> None: """Test the ssp filter."""