def test_update_action(sample_target):
    """Test update action."""
    element = Element(sample_target)

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

    sub_element_path = ElementPath('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
Beispiel #2
0
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_element_set_at(sample_target_def: target.TargetDefinition):
    """Test element get method."""
    element = Element(sample_target_def)

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

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

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

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

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

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

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

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

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

    # invalid attribute
    with pytest.raises(TrestleError):
        assert element.set_at(ElementPath('target-definition.metadata.groups.*'), parties)
Beispiel #4
0
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