Example #1
0
def test_embedded_property():
    emb_prop = EmbeddedObjectProperty(type=EmailMIMEComponent)
    mime = EmailMIMEComponent(content_type="text/plain; charset=utf-8",
                              content_disposition="inline",
                              body="Cats are funny!")
    assert emb_prop.clean(mime)

    with pytest.raises(ValueError):
        emb_prop.clean("string")
Example #2
0
def test_embedded_property_custom():
    emb_prop = EmbeddedObjectProperty(type=stix2.v20.EmailMIMEComponent)
    mime = stix2.v20.EmailMIMEComponent(
        content_type="text/plain; charset=utf-8",
        content_disposition="inline",
        body="Cats are funny!",
        foo=123,
        allow_custom=True,
    )

    with pytest.raises(CustomContentError):
        emb_prop.clean(mime, False)

    result = emb_prop.clean(mime, True)
    assert result == (mime, True)
Example #3
0
def test_list_property_property_type_custom():
    class TestObj(stix2.base._STIXBase):
        _type = "test"
        _properties = {
            "foo": StringProperty(),
        }

    p = ListProperty(EmbeddedObjectProperty(type=TestObj))

    objs_custom = [
        TestObj(foo="abc", bar=123, allow_custom=True),
        TestObj(foo="xyz"),
    ]

    assert p.clean(objs_custom)

    dicts_custom = [
        {
            "foo": "abc",
            "bar": 123
        },
        {
            "foo": "xyz"
        },
    ]

    # no opportunity to set allow_custom=True when using dicts
    with pytest.raises(ExtraPropertiesError):
        p.clean(dicts_custom)
Example #4
0
def test_embedded_property_dict_custom():
    emb_prop = EmbeddedObjectProperty(type=stix2.v20.EmailMIMEComponent)
    mime = {
        "content_type": "text/plain; charset=utf-8",
        "content_disposition": "inline",
        "body": "Cats are funny!",
        "foo": 123,
    }

    with pytest.raises(ExtraPropertiesError):
        emb_prop.clean(mime, False)

    result = emb_prop.clean(mime, True)
    assert isinstance(result[0], stix2.v20.EmailMIMEComponent)
    assert result[0]["body"] == "Cats are funny!"
    assert result[1]
def test_list_property_property_type_custom():
    class TestObj(_STIXBase):
        _type = "test"
        _properties = {
            "foo": StringProperty(),
        }

    p = ListProperty(EmbeddedObjectProperty(type=TestObj))

    objs_custom = [
        TestObj(foo="abc", bar=123, allow_custom=True),
        TestObj(foo="xyz"),
    ]

    result = p.clean(objs_custom, True)
    assert result == (objs_custom, True)

    with pytest.raises(CustomContentError):
        p.clean(objs_custom, False)

    dicts_custom = [
        {
            "foo": "abc",
            "bar": 123
        },
        {
            "foo": "xyz"
        },
    ]

    result = p.clean(dicts_custom, True)
    assert result == (objs_custom, True)

    with pytest.raises(ExtraPropertiesError):
        p.clean(dicts_custom, False)
Example #6
0
def test_embedded_property_dict():
    emb_prop = EmbeddedObjectProperty(type=stix2.v20.EmailMIMEComponent)
    mime = {
        "content_type": "text/plain; charset=utf-8",
        "content_disposition": "inline",
        "body": "Cats are funny!",
    }

    result = emb_prop.clean(mime, False)
    assert isinstance(result[0], stix2.v20.EmailMIMEComponent)
    assert result[0]["body"] == "Cats are funny!"
    assert not result[1]

    result = emb_prop.clean(mime, True)
    assert isinstance(result[0], stix2.v20.EmailMIMEComponent)
    assert result[0]["body"] == "Cats are funny!"
    assert not result[1]
Example #7
0
 class SomeSCO(stix2.v21._Observable):
     _type = "some-sco"
     _properties = OrderedDict((
         ('type', TypeProperty(_type, spec_version='2.1')),
         ('id', IDProperty(_type, spec_version='2.1')),
         ('extensions', ExtensionsProperty(spec_version='2.1')),
         ('sub_obj', EmbeddedObjectProperty(type=SubObj)),
     ))
     _id_contributing_properties = ['sub_obj']
Example #8
0
def test_embedded_property():
    emb_prop = EmbeddedObjectProperty(type=stix2.v20.EmailMIMEComponent)
    mime = stix2.v20.EmailMIMEComponent(
        content_type="text/plain; charset=utf-8",
        content_disposition="inline",
        body="Cats are funny!",
    )
    result = emb_prop.clean(mime, False)
    assert result == (mime, False)

    result = emb_prop.clean(mime, True)
    assert result == (mime, False)

    with pytest.raises(ValueError):
        emb_prop.clean("string", False)