def test_managed_oscal(tmp_trestle_dir: pathlib.Path) -> None: """Test creating Managed OSCAL object.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') assert managed.model_dir == tmp_trestle_dir / 'catalogs' / 'imported'
def test_managed_split(tmp_trestle_dir: pathlib.Path) -> None: """Test model split.""" # generate catalog data and import filepath = test_utils.JSON_TEST_DATA_PATH / test_utils.SIMPLIFIED_NIST_CATALOG_NAME catalog_data = cat.Catalog.oscal_read(filepath) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') # store current working directory cwd = pathlib.Path.cwd() # test splitting success = managed.split(pathlib.Path('catalog.json'), ['catalog.metadata']) assert success assert pathlib.Path(tmp_trestle_dir / 'catalogs' / 'imported' / 'catalog' / 'metadata.json').exists() # test cwd is restored after splitting assert pathlib.Path.cwd() == cwd success = managed.split(pathlib.Path('catalog/metadata.json'), ['metadata.props']) assert success assert pathlib.Path(tmp_trestle_dir / 'catalogs' / 'imported' / 'catalog' / 'metadata' / 'props.json').exists()
def setup_for_multi_profile(trestle_root: pathlib.Path, big_profile: bool, import_nist_cat: bool) -> None: """Initiallize trestle directory with catalogs and profiles.""" repo = Repository(trestle_root) main_profile_name = 'main_profile' if big_profile: prof_path = JSON_TEST_DATA_PATH / SIMPLIFIED_NIST_PROFILE_NAME else: prof_path = JSON_TEST_DATA_PATH / 'simple_test_profile.json' repo.load_and_import_model(prof_path, main_profile_name) for letter in 'abcdefg': prof_name = f'test_profile_{letter}' prof_path = JSON_TEST_DATA_PATH / f'{prof_name}.json' repo.load_and_import_model(prof_path, prof_name) complex_cat = generate_complex_catalog() repo.import_model(complex_cat, 'complex_cat') cat_name = 'nist_cat' cat_path = JSON_TEST_DATA_PATH / SIMPLIFIED_NIST_CATALOG_NAME if import_nist_cat: repo.load_and_import_model(cat_path, cat_name) new_href = f'trestle://catalogs/{cat_name}/catalog.json' else: new_href = str(cat_path.resolve()) assert HrefCmd.change_import_href(trestle_root, main_profile_name, new_href, 0) == 0
def test_managed_read(tmp_trestle_dir: pathlib.Path) -> None: """Test model read.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') model = managed.read() assert model.uuid == catalog_data.uuid
def test_get(tmp_trestle_dir: pathlib.Path) -> None: """Test get model.""" # create a model catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) repo.import_model(catalog_data, 'imported') managed_oscal = repo.get_model(cat.Catalog, 'imported') assert managed_oscal._model_name == 'imported'
def test_managed_validate(tmp_trestle_dir: pathlib.Path) -> None: """Test model validate.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') success = managed.validate() assert success
def test_import_invalid_top_model(tmp_trestle_dir: pathlib.Path) -> None: """Invalid top model.""" # try to import Metadata metadata = generators.generate_sample_model(oscal.common.Metadata) repo = Repository(tmp_trestle_dir) with pytest.raises(TrestleError, match='not a top level model'): repo.import_model(metadata, 'imported')
def test_validate(tmp_trestle_dir: pathlib.Path) -> None: """Test validate model.""" # create a model catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) repo.import_model(catalog_data, 'imported') success = repo.validate_model(cat.Catalog, 'imported') assert success
def test_assemble(tmp_trestle_dir: pathlib.Path) -> None: """Test assemble model.""" # create a model catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) repo.import_model(catalog_data, 'imported') success = repo.assemble_model(cat.Catalog, 'imported') assert success dist_model_path = pathlib.Path(tmp_trestle_dir, 'dist', 'catalogs', 'imported.json') assert dist_model_path.exists()
def test_managed_file_not_exist(tmp_trestle_dir: pathlib.Path) -> None: """Test model file does not exist while creating a Managed OSCAL object.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') # delete file managed.filepath.unlink() with pytest.raises(TrestleError, match=r'Model file .* does not exist'): ManagedOSCAL(tmp_trestle_dir, cat.Catalog, 'imported')
def test_import(tmp_trestle_dir: pathlib.Path) -> None: """Test import.""" # Generate sample catalog model catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed_oscal = repo.import_model(catalog_data, 'imported') assert managed_oscal._root_dir == tmp_trestle_dir assert managed_oscal._model_name == 'imported' assert managed_oscal._model_type == catalog_data.__class__ assert managed_oscal.filepath.exists()
def test_import_model_exists(tmp_trestle_dir: pathlib.Path) -> None: """Model already exists.""" # Generate sample catalog model catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed_oscal = repo.import_model(catalog_data, 'imported') assert managed_oscal.filepath.exists() with pytest.raises(TrestleError, match=r'OSCAL file .* exists'): repo.import_model(catalog_data, 'imported')
def test_managed_write(tmp_trestle_dir: pathlib.Path) -> None: """Test model write.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') # generate another catalog data for writing catalog_data = generators.generate_sample_model(cat.Catalog) success = managed.write(catalog_data) assert success
def test_managed_write_invalid_top_model( tmp_trestle_dir: pathlib.Path) -> None: """Invalid top level model while writing.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') # generate another catalog data for writing catalog_data = generators.generate_sample_model(oscal.catalog.Group) with pytest.raises(TrestleError, match='not a top level model'): managed.write(catalog_data)
def test_managed_split_multi(tmp_trestle_dir: pathlib.Path) -> None: """Test model split multiple elements.""" # generate catalog data and import catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) managed = repo.import_model(catalog_data, 'imported') # store current working directory cwd = pathlib.Path.cwd() # test splitting success = managed.split(pathlib.Path('catalog.json'), ['catalog.metadata', 'catalog.groups.*']) assert success # test cwd is restored after splitting assert pathlib.Path.cwd() == cwd
def test_delete(tmp_trestle_dir: pathlib.Path) -> None: """Test delete model.""" # create a model catalog_data = generators.generate_sample_model(cat.Catalog) repo = Repository(tmp_trestle_dir) repo.import_model(catalog_data, 'imported') # created model is 'dist' folder also repo.assemble_model(cat.Catalog, 'imported') success = repo.delete_model(cat.Catalog, 'imported') assert success
def test_import_validation_fail(tmp_trestle_dir: pathlib.Path) -> None: """Validation failed.""" # catalog data dup_cat = { 'uuid': '525f94af-8007-4376-8069-aa40179e0f6e', 'metadata': { 'title': 'Generic catalog created by trestle.', 'last-modified': '2020-12-11T02:04:51.053+00:00', 'version': '0.0.0', 'oscal-version': oscal.OSCAL_VERSION }, 'back-matter': { 'resources': [{ 'uuid': 'b1101385-9e36-44a3-ba03-98b6ebe0a367' }, { 'uuid': 'b1101385-9e36-44a3-ba03-98b6ebe0a367' }] } } catalog_data = parser.parse_dict(dup_cat, 'trestle.oscal.catalog.Catalog') repo = Repository(tmp_trestle_dir) with pytest.raises(TrestleError, match=r'Validation .* did not pass'): repo.import_model(catalog_data, 'imported')
def test_ok_when_props_added(tmp_trestle_dir: pathlib.Path) -> None: """Test when by_id is not given and position is set to after or before it defaults to after.""" cat_path = test_utils.JSON_TEST_DATA_PATH / test_utils.SIMPLIFIED_NIST_CATALOG_NAME repo = Repository(tmp_trestle_dir) repo.load_and_import_model(cat_path, 'nist_cat') prof_path = test_utils.JSON_TEST_DATA_PATH / 'profile_with_alter_props.json' repo.load_and_import_model(prof_path, 'profile_with_alter_props') catalog = ProfileResolver.get_resolved_profile_catalog(tmp_trestle_dir, prof_path) assert catalog
def test_profile_missing_position(tmp_trestle_dir: pathlib.Path) -> None: """Test when alter adds parts is missing position it defaults to after.""" cat_path = test_utils.JSON_TEST_DATA_PATH / test_utils.SIMPLIFIED_NIST_CATALOG_NAME repo = Repository(tmp_trestle_dir) repo.load_and_import_model(cat_path, 'nist_cat') prof_path = test_utils.JSON_TEST_DATA_PATH / 'profile_missing_position.json' repo.load_and_import_model(prof_path, 'profile_missing_position') catalog = ProfileResolver.get_resolved_profile_catalog(tmp_trestle_dir, prof_path) assert catalog
def test_ok_when_reference_id_is_not_given_after_or_before(tmp_trestle_dir: pathlib.Path) -> None: """Test when by_id is not given and position is set to after or before it fails.""" cat_path = test_utils.JSON_TEST_DATA_PATH / test_utils.SIMPLIFIED_NIST_CATALOG_NAME repo = Repository(tmp_trestle_dir) repo.load_and_import_model(cat_path, 'nist_cat') prof_path = test_utils.JSON_TEST_DATA_PATH / 'profile_with_incorrect_alter.json' repo.load_and_import_model(prof_path, 'incorrect_profile') # this originally failed but now it is OK based on OSCAL saying to default to starting or ending if no by_id catalog = ProfileResolver.get_resolved_profile_catalog(tmp_trestle_dir, prof_path) assert catalog
def test_list(tmp_trestle_dir: pathlib.Path) -> None: """Test list models.""" # 1. Empty list repo = Repository(tmp_trestle_dir) model_list = repo.list_models(cat.Catalog) assert len(model_list) == 0 # model exists catalog_data = generators.generate_sample_model(cat.Catalog) repo.import_model(catalog_data, 'imported') model_list = repo.list_models(cat.Catalog) assert len(model_list) == 1 assert 'imported' in model_list
def test_all_positions_for_alter_can_be_resolved(tmp_trestle_dir: pathlib.Path) -> None: """Test that all alter adds positions can be resolved.""" cat_path = test_utils.JSON_TEST_DATA_PATH / test_utils.SIMPLIFIED_NIST_CATALOG_NAME repo = Repository(tmp_trestle_dir) repo.load_and_import_model(cat_path, 'nist_cat') prof_d_path = test_utils.JSON_TEST_DATA_PATH / 'test_profile_d.json' repo.load_and_import_model(prof_d_path, 'test_profile_d') cat = ProfileResolver.get_resolved_profile_catalog(tmp_trestle_dir, prof_d_path) interface = CatalogInterface(cat) control_a1 = interface.get_control('ac-1') control_a2 = interface.get_control('ac-2') assert control_a1.parts[0].id == 'ac-1_first_lev1' assert control_a1.parts[1].parts[3].id == 'ac-1_last_lev2' assert control_a1.parts[2].id == 'ac-1_after1_ac-1_smt_lev1' assert control_a1.parts[3].id == 'ac-1_after2_ac-1_smt_lev1' assert control_a1.parts[1].parts[0].parts[1].id == 'ac-1_smt_before1_a.2_lev3' assert control_a1.parts[1].parts[0].parts[2].id == 'ac-1_smt_before2_a.2_lev3' assert control_a1.parts[1].parts[0].parts[3].parts[0].id == 'ac-1_smt_inside1_at_the_end_a.2_lev4' assert control_a1.parts[1].parts[0].parts[3].parts[1].id == 'ac-1_smt_inside2_at_the_end_a.2_lev4' assert control_a2.parts[0].id == 'ac-2_implgdn_lev1'
def test_assemble_model_not_exists(tmp_trestle_dir: pathlib.Path) -> None: """Assemble model does not exists.""" repo = Repository(tmp_trestle_dir) with pytest.raises(TrestleError): repo.assemble_model(cat.Catalog, 'anything')
def test_validate_model_not_exists(tmp_trestle_dir: pathlib.Path) -> None: """Assemble model does not exists.""" repo = Repository(tmp_trestle_dir) success = repo.validate_model(cat.Catalog, 'anything') assert not success
def test_delete_model_not_exists(tmp_trestle_dir: pathlib.Path) -> None: """Delete model does not exists.""" repo = Repository(tmp_trestle_dir) with pytest.raises(TrestleError, match='does not exist'): repo.delete_model(cat.Catalog, 'anything')
def test_repo(tmp_trestle_dir: pathlib.Path) -> None: """Test creating Repository object.""" repo = Repository(tmp_trestle_dir) assert repo._root_dir == tmp_trestle_dir
def test_validate_invalid_top_model(tmp_trestle_dir: pathlib.Path) -> None: """Invalid top model.""" repo = Repository(tmp_trestle_dir) with pytest.raises(TrestleError, match='not a top level model'): repo.validate_model(oscal.common.Metadata, 'anything')
def test_repo_invalid_root(tmp_path: pathlib.Path) -> None: """Invalid trestle_root directory while creating Repository object.""" with pytest.raises(TrestleError, match='not a valid Trestle root'): Repository(tmp_path)