2018-07-03 13:00:18 +02:00
|
|
|
import datetime as dt
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
import stix2
|
|
|
|
|
|
|
|
from .constants import IDENTITY_ID
|
|
|
|
|
|
|
|
EXPECTED = """{
|
|
|
|
"type": "identity",
|
|
|
|
"spec_version": "2.1",
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2018-07-03 13:00:18 +02:00
|
|
|
"created": "2015-12-21T19:59:11.000Z",
|
|
|
|
"modified": "2015-12-21T19:59:11.000Z",
|
|
|
|
"name": "John Smith",
|
|
|
|
"identity_class": "individual"
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_identity_example():
|
2018-07-03 15:40:51 +02:00
|
|
|
identity = stix2.v21.Identity(
|
2018-07-11 15:43:37 +02:00
|
|
|
id="identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2018-07-03 13:00:18 +02:00
|
|
|
created="2015-12-21T19:59:11.000Z",
|
|
|
|
modified="2015-12-21T19:59:11.000Z",
|
|
|
|
name="John Smith",
|
|
|
|
identity_class="individual",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(identity) == EXPECTED
|
|
|
|
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED,
|
|
|
|
{
|
|
|
|
"created": "2015-12-21T19:59:11.000Z",
|
|
|
|
"id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
|
|
|
"identity_class": "individual",
|
|
|
|
"modified": "2015-12-21T19:59:11.000Z",
|
|
|
|
"name": "John Smith",
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"type": "identity",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
def test_parse_identity(data):
|
2018-07-03 15:40:51 +02:00
|
|
|
identity = stix2.parse(data, version="2.1")
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
assert identity.type == 'identity'
|
2018-07-03 15:40:51 +02:00
|
|
|
assert identity.spec_version == '2.1'
|
2018-07-03 13:00:18 +02:00
|
|
|
assert identity.id == IDENTITY_ID
|
|
|
|
assert identity.created == dt.datetime(2015, 12, 21, 19, 59, 11, tzinfo=pytz.utc)
|
|
|
|
assert identity.modified == dt.datetime(2015, 12, 21, 19, 59, 11, tzinfo=pytz.utc)
|
|
|
|
assert identity.name == "John Smith"
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_no_type():
|
|
|
|
with pytest.raises(stix2.exceptions.ParseError):
|
2018-07-13 17:10:05 +02:00
|
|
|
stix2.parse(
|
|
|
|
"""
|
2018-07-03 13:00:18 +02:00
|
|
|
{
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2018-07-03 13:00:18 +02:00
|
|
|
"created": "2015-12-21T19:59:11.000Z",
|
|
|
|
"modified": "2015-12-21T19:59:11.000Z",
|
|
|
|
"name": "John Smith",
|
|
|
|
"identity_class": "individual"
|
2018-07-13 17:10:05 +02:00
|
|
|
}""", version="2.1",
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_identity_with_custom():
|
2018-07-03 15:40:51 +02:00
|
|
|
identity = stix2.v21.Identity(
|
2018-07-03 13:00:18 +02:00
|
|
|
name="John Smith",
|
|
|
|
identity_class="individual",
|
2018-07-13 17:10:05 +02:00
|
|
|
custom_properties={'x_foo': 'bar'},
|
2018-07-03 13:00:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert identity.x_foo == "bar"
|
|
|
|
assert "x_foo" in identity.object_properties()
|
|
|
|
|
|
|
|
# TODO: Add other examples
|