Example #1
0
def test_update_action(sample_nist_component_def):
    """Test update action."""
    element = Element(sample_nist_component_def)

    metadata = common.Metadata(
        **{
            'title': 'My simple catalog',
            'last-modified': datetime.now().astimezone(),
            'version': '0.0.0',
            'oscal-version': OSCAL_VERSION
        })

    sub_element_path = ElementPath('component-definition.metadata')
    prev_metadata = element.get_at(sub_element_path)

    uac = UpdateAction(metadata, element, sub_element_path)

    uac.execute()

    assert element.get_at(sub_element_path) is not prev_metadata
    assert element.get_at(sub_element_path) == metadata

    uac.rollback()

    assert element.get_at(sub_element_path) == prev_metadata
    assert element.get_at(sub_element_path) is not metadata
Example #2
0
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 = common.Metadata(
        **{
            'title': 'My simple catalog',
            'last-modified': datetime.now(tz=taz),
            'version': '0.0.0',
            'oscal-version': trestle.oscal.OSCAL_VERSION
        })
    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)
Example #3
0
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
Example #4
0
def test_copy_from() -> None:
    """Test copy from function."""
    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()))

    target_md = common.Metadata(
        **{
            'title': 'My simple target_title',
            'last-modified': datetime.now().astimezone(),
            'version': '99.0.0',
            'oscal-version': trestle.oscal.OSCAL_VERSION
        })
    catalog.metadata.copy_from(target_md)

    assert catalog.metadata.title == target_md.title
Example #5
0
def test_element_set_at(sample_nist_component_def: component.ComponentDefinition):
    """Test element get method."""
    element = Element(sample_nist_component_def)

    metadata = common.Metadata(
        **{
            'title': 'My simple catalog', 'last-modified': datetime.now(), 'version': '0.0.0', 'oscal-version': '1.0.0'
        }
    )

    parties: List[common.Party] = []
    parties.append(
        common.Party(**{
            'uuid': 'ff47836c-877c-4007-bbf3-c9d9bd805000', 'name': 'TEST1', 'type': 'organization'
        })
    )
    parties.append(
        common.Party(**{
            'uuid': 'ee88836c-877c-4007-bbf3-c9d9bd805000', 'name': 'TEST2', 'type': 'organization'
        })
    )

    assert element.set_at(ElementPath('component-definition.metadata'),
                          metadata).get_at(ElementPath('component-definition.metadata')) == metadata

    assert element.set_at(ElementPath('component-definition.metadata.parties'),
                          parties).get_at(ElementPath('component-definition.metadata.parties')) == parties

    assert element.set_at(ElementPath('component-definition.metadata.parties.*'),
                          parties).get_at(ElementPath('component-definition.metadata.parties')) == parties

    # unset
    assert element.set_at(ElementPath('component-definition.metadata.parties'),
                          None).get_at(ElementPath('component-definition.metadata.parties')) is None

    # string element path
    assert element.set_at('component-definition.metadata.parties',
                          parties).get_at(ElementPath('component-definition.metadata.parties')) == parties

    with pytest.raises(TrestleError):
        assert element.set_at(ElementPath('component-definition.metadata'),
                              parties).get_at(ElementPath('component-definition.metadata.parties')) == parties

    # wildcard requires it to be an OscalBaseModel or list
    with pytest.raises(TrestleError):
        assert element.set_at(ElementPath('component-definition.metadata.parties.*'), 'INVALID')

    # invalid attribute
    with pytest.raises(TrestleError):
        assert element.set_at(ElementPath('component-definition.metadata.groups.*'), parties)
Example #6
0
def test_copy_to() -> None:
    """Test the copy to functionality."""
    # Complex variable
    c_m = common.Metadata(
        **{
            'title': 'My simple catalog',
            'last-modified': datetime.now().astimezone(),
            'version': '0.0.0',
            'oscal-version': trestle.oscal.OSCAL_VERSION
        })

    target_metadata = c_m.copy_to(common.Metadata)
    assert (target_metadata.title == c_m.title)
    # Non matching object
    with pytest.raises(err.TrestleError):
        c_m.copy_to(component.DefinedComponent)

    # Testing of root fields. This is is subject to change.
    # component.Remarks (type str)
    # poam.RiskStatus (type str)
    # note the testing conduction
    remark = common.Remarks(__root__='hello')
    _ = remark.copy_to(common.RiskStatus)