def test_wrap_for_output(): """Test that an output object is wrapped and contains the appropriate content.""" m = catalog.Metadata( **{ 'title': 'my cool catalog', 'last-modified': datetime.now(), 'version': '0.0.1', 'oscal-version': '1.0.0' }) c = catalog.Catalog(metadata=m, uuid=str(uuid4())) wrapped = parser.wrap_for_output(c) assert (wrapped.catalog.metadata.title == c.metadata.title) # Test with target definition t_m = target.Metadata( **{ 'title': 'my cool target definition', 'last-modified': datetime.now(), 'version': '0.0.1', 'oscal-version': '1.0.0' }) t = target.TargetDefinition(metadata=t_m) wrapped = parser.wrap_for_output(t) assert (wrapped.target_definition.metadata.title == t.metadata.title)
def test_broken_tz() -> None: """Deliberately break tz to trigger exception.""" class BrokenTimezone(tzinfo): # TODO: Type annotations here. """Broken TZ class which returns null offset.""" def fromutc(self, dt): return dt def utcoffset(self, dt): return None def dst(self, dt): return dt def tzname(self, dt): return 'Broken' def _isdst(self, dt): return True taz = BrokenTimezone() m = oscatalog.Metadata( **{ 'title': 'My simple catalog', 'last-modified': datetime.now(tz=taz), 'version': '0.0.0', 'oscal-version': '1.0.0-Milestone3' }) catalog = oscatalog.Catalog(metadata=m, uuid=str(uuid4())) with pytest.raises(Exception): jsoned_catalog = catalog.json(exclude_none=True, by_alias=True, indent=2) type(jsoned_catalog)
def simple_catalog_with_tz() -> oscatalog.Catalog: """Return a skeleton catalog with datetime.now().""" m = oscatalog.Metadata( **{ 'title': 'My simple catalog', 'last-modified': datetime.now().astimezone(), 'version': '0.0.0', 'oscal-version': '1.0.0-Milestone3' }) catalog = oscatalog.Catalog(metadata=m, uuid=str(uuid4())) return catalog
def simple_catalog_with_tz() -> oscatalog.Catalog: """Return a skeleton catalog with datetime.now().""" m = common.Metadata( **{ 'title': 'My simple catalog', 'last-modified': datetime.now().astimezone(), 'version': '0.0.0', 'oscal-version': trestle.oscal.OSCAL_VERSION }) catalog = oscatalog.Catalog(metadata=m, uuid=str(uuid4())) return catalog
def test_generate_sample_model() -> None: """Test utils method generate_sample_model.""" # Create the expected catalog first expected_ctlg_dict = { 'uuid': 'ea784488-49a1-4ee5-9830-38058c7c10a4', 'metadata': { 'title': 'REPLACE_ME', 'last-modified': '2020-10-21T06:52:10.387+00:00', 'version': 'REPLACE_ME', 'oscal-version': oscal.OSCAL_VERSION } } expected_ctlg = catalog.Catalog(**expected_ctlg_dict) actual_ctlg = gens.generate_sample_model(catalog.Catalog) # Check if uuid is valid, then change to uuid of expected catalog, as newly generated # uuids will always be different assert is_valid_uuid(actual_ctlg.uuid) actual_ctlg.uuid = expected_ctlg.uuid # Check if last-modified datetime is of type datetime, and then equate in actual and expected assert type(actual_ctlg.metadata) is common.Metadata actual_ctlg.metadata.last_modified = expected_ctlg.metadata.last_modified # Check that expected generated catalog is now same a actual catalog assert expected_ctlg == actual_ctlg # Test list type models expected_role = common.Role(**{'id': 'REPLACE_ME', 'title': 'REPLACE_ME'}) list_role = gens.generate_sample_model(List[common.Role]) assert type(list_role) is list actual_role = list_role[0] assert expected_role == actual_role # Test dict type models if False: party_uuid = common.PartyUuid(__root__=const.SAMPLE_UUID_STR) expected_rp = {'role_id': 'REPLACE_ME', 'party-uuids': [party_uuid]} expected_rp = common.ResponsibleParty(**expected_rp) expected_rp_dict = {'REPLACE_ME': expected_rp} actual_rp_dict = gens.generate_sample_model( Dict[str, common.ResponsibleParty]) assert type(actual_rp_dict) is dict assert expected_rp_dict == actual_rp_dict
def test_get_sample_model(): """Test utils method get_sample_model.""" # Create the expected catalog first expected_ctlg_dict = { 'uuid': 'ea784488-49a1-4ee5-9830-38058c7c10a4', 'metadata': { 'title': 'REPLACE_ME', 'last-modified': '2020-10-21T06:52:10.387+00:00', 'version': 'REPLACE_ME', 'oscal-version': 'REPLACE_ME' } } expected_ctlg = catalog.Catalog(**expected_ctlg_dict) actual_ctlg = mutils.get_sample_model(catalog.Catalog) # Check if uuid is valid, then change to uuid of expected catalog, as newly generated # uuids will always be different assert is_valid_uuid(actual_ctlg.uuid) actual_ctlg.uuid = expected_ctlg.uuid # Check if last-modified datetime is of type datetime, and then equate in actual and expected assert type(actual_ctlg.metadata.last_modified) is catalog.LastModified actual_ctlg.metadata.last_modified = expected_ctlg.metadata.last_modified # Check that expected generated catalog is now same a actual catalog assert expected_ctlg == actual_ctlg # Test list type models expected_role = catalog.Role(**{'id': 'REPLACE_ME', 'title': 'REPLACE_ME'}) list_role = mutils.get_sample_model(typing.List[catalog.Role]) assert type(list_role) is list actual_role = list_role[0] assert expected_role == actual_role # Test dict type models expected_rp = {'party-uuids': ['00000000-0000-4000-8000-000000000000']} expected_rp = catalog.ResponsibleParty(**expected_rp) expected_rp_dict = {'REPLACE_ME': expected_rp} actual_rp_dict = mutils.get_sample_model( typing.Dict[str, catalog.ResponsibleParty]) assert type(actual_rp_dict) is dict assert expected_rp_dict == actual_rp_dict
def test_copy_from() -> None: """Test copy from function.""" m = oscatalog.Metadata( **{ 'title': 'My simple catalog', 'last-modified': datetime.now().astimezone(), 'version': '0.0.0', 'oscal-version': '1.0.0-Milestone3' }) catalog = oscatalog.Catalog(metadata=m, uuid=str(uuid4())) target_md = ostarget.Metadata( **{ 'title': 'My simple target_title', 'last-modified': datetime.now().astimezone(), 'version': '99.0.0', 'oscal-version': '1.0.0-Milestone3' }) catalog.metadata.copy_from(target_md) assert catalog.metadata.title == target_md.title