2017-04-25 00:29:56 +02:00
|
|
|
import datetime as dt
|
|
|
|
|
2017-04-19 15:22:08 +02:00
|
|
|
import pytest
|
|
|
|
import pytz
|
2017-05-09 21:10:53 +02:00
|
|
|
|
2017-02-24 18:56:55 +01:00
|
|
|
import stix2
|
|
|
|
|
2017-04-19 15:22:08 +02:00
|
|
|
from .constants import IDENTITY_ID
|
|
|
|
|
2017-02-24 18:56:55 +01:00
|
|
|
EXPECTED = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"type": "identity",
|
2018-06-27 18:34:49 +02:00
|
|
|
"id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2017-08-15 19:41:51 +02:00
|
|
|
"created": "2015-12-21T19:59:11.000Z",
|
2017-06-23 00:47:35 +02:00
|
|
|
"modified": "2015-12-21T19:59:11.000Z",
|
2017-02-24 18:56:55 +01:00
|
|
|
"name": "John Smith",
|
2017-08-15 19:41:51 +02:00
|
|
|
"identity_class": "individual"
|
2017-02-24 18:56:55 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_identity_example():
|
2018-07-05 21:23:25 +02:00
|
|
|
identity = stix2.v20.Identity(
|
2019-01-23 03:25:09 +01:00
|
|
|
id=IDENTITY_ID,
|
2017-06-23 00:47:35 +02:00
|
|
|
created="2015-12-21T19:59:11.000Z",
|
|
|
|
modified="2015-12-21T19:59:11.000Z",
|
2017-02-24 18:56:55 +01:00
|
|
|
name="John Smith",
|
|
|
|
identity_class="individual",
|
|
|
|
)
|
|
|
|
|
2017-06-09 18:20:40 +02:00
|
|
|
assert str(identity) == EXPECTED
|
2017-02-24 18:56:55 +01:00
|
|
|
|
2017-04-19 15:22:08 +02:00
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED,
|
|
|
|
{
|
|
|
|
"created": "2015-12-21T19:59:11.000Z",
|
2019-01-23 03:25:09 +01:00
|
|
|
"id": IDENTITY_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
"identity_class": "individual",
|
|
|
|
"modified": "2015-12-21T19:59:11.000Z",
|
|
|
|
"name": "John Smith",
|
|
|
|
"type": "identity",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2017-04-19 15:22:08 +02:00
|
|
|
def test_parse_identity(data):
|
2018-07-05 21:23:25 +02:00
|
|
|
identity = stix2.parse(data, version="2.0")
|
2017-04-19 15:22:08 +02:00
|
|
|
|
|
|
|
assert identity.type == 'identity'
|
|
|
|
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"
|
|
|
|
|
2017-06-09 18:20:40 +02:00
|
|
|
|
2017-06-12 18:54:05 +02:00
|
|
|
def test_parse_no_type():
|
|
|
|
with pytest.raises(stix2.exceptions.ParseError):
|
2018-07-13 17:10:05 +02:00
|
|
|
stix2.parse(
|
|
|
|
"""
|
2017-06-12 18:54:05 +02:00
|
|
|
{
|
2018-06-27 18:34:49 +02:00
|
|
|
"id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2017-06-23 00:47:35 +02:00
|
|
|
"created": "2015-12-21T19:59:11.000Z",
|
|
|
|
"modified": "2015-12-21T19:59:11.000Z",
|
2017-06-12 18:54:05 +02:00
|
|
|
"name": "John Smith",
|
|
|
|
"identity_class": "individual"
|
2018-07-13 17:10:05 +02:00
|
|
|
}""", version="2.0",
|
|
|
|
)
|
2017-06-12 18:54:05 +02:00
|
|
|
|
2017-11-03 19:17:36 +01:00
|
|
|
|
|
|
|
def test_identity_with_custom():
|
2018-07-05 21:23:25 +02:00
|
|
|
identity = stix2.v20.Identity(
|
2017-11-03 19:17:36 +01:00
|
|
|
name="John Smith",
|
|
|
|
identity_class="individual",
|
2018-07-13 17:10:05 +02:00
|
|
|
custom_properties={'x_foo': 'bar'},
|
2017-11-03 19:17:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert identity.x_foo == "bar"
|
|
|
|
assert "x_foo" in identity.object_properties()
|
|
|
|
|
2017-02-24 18:56:55 +01:00
|
|
|
# TODO: Add other examples
|