cti-python-stix2/stix2/test/v21/test_opinion.py

95 lines
2.9 KiB
Python
Raw Normal View History

2017-10-23 14:06:29 +02:00
import datetime as dt
import re
import pytest
import pytz
import stix2
from .constants import OPINION_ID
2018-10-15 20:48:52 +02:00
EXPLANATION = (
'This doesn\'t seem like it is feasible. We\'ve seen how '
'PandaCat has attacked Spanish infrastructure over the '
'last 3 years, so this change in targeting seems too great'
' to be viable. The methods used are more commonly '
'associated with the FlameDragonCrew.'
)
2017-10-23 14:06:29 +02:00
EXPECTED_OPINION = """{
"type": "opinion",
"spec_version": "2.1",
2017-10-23 14:06:29 +02:00
"id": "opinion--b01efc25-77b4-4003-b18b-f6e24b5cd9f7",
"created": "2016-05-12T08:17:27.000Z",
"modified": "2016-05-12T08:17:27.000Z",
2018-10-15 20:48:52 +02:00
"explanation": "%s",
"opinion": "strongly-disagree",
2017-10-23 14:06:29 +02:00
"object_refs": [
"relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471"
]
2018-10-15 20:48:52 +02:00
}""" % EXPLANATION
2017-10-23 14:06:29 +02:00
EXPECTED_OPINION_REPR = "Opinion(" + " ".join((
"""
2017-10-23 14:06:29 +02:00
type='opinion',
spec_version='2.1',
2017-10-23 14:06:29 +02:00
id='opinion--b01efc25-77b4-4003-b18b-f6e24b5cd9f7',
created='2016-05-12T08:17:27.000Z',
modified='2016-05-12T08:17:27.000Z',
2018-10-15 20:48:52 +02:00
explanation="%s",
opinion='strongly-disagree',
object_refs=['relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471'],
revoked=False
""" % EXPLANATION
).split()) + ")"
2017-10-23 14:06:29 +02:00
def test_opinion_with_required_properties():
now = dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
opi = stix2.v21.Opinion(
2017-10-23 14:06:29 +02:00
type='opinion',
id=OPINION_ID,
created=now,
modified=now,
object_refs=['relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471'],
opinion='strongly-disagree',
2018-10-15 20:48:52 +02:00
explanation=EXPLANATION,
2017-10-23 14:06:29 +02:00
)
assert opi.serialize(pretty=True) == EXPECTED_OPINION
2017-10-23 14:06:29 +02:00
rep = re.sub(r"(\[|=| )u('|\"|\\\'|\\\")", r"\g<1>\g<2>", repr(opi))
assert rep == EXPECTED_OPINION_REPR
@pytest.mark.parametrize(
"data", [
EXPECTED_OPINION,
{
"type": "opinion",
"spec_version": "2.1",
2019-01-23 16:56:20 +01:00
"id": OPINION_ID,
"created": "2016-05-12T08:17:27.000Z",
"modified": "2016-05-12T08:17:27.000Z",
2018-10-15 20:48:52 +02:00
"explanation": EXPLANATION,
"object_refs": [
"relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471",
],
"opinion": "strongly-disagree",
},
],
)
2017-10-23 14:06:29 +02:00
def test_parse_opinion(data):
opinion = stix2.parse(data, version="2.1")
2017-10-23 14:06:29 +02:00
assert opinion.type == 'opinion'
assert opinion.spec_version == '2.1'
2017-10-23 14:06:29 +02:00
assert opinion.id == OPINION_ID
assert opinion.created == dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
assert opinion.modified == dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
assert opinion.opinion == 'strongly-disagree'
assert opinion.object_refs[0] == 'relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471'
2018-10-15 20:48:52 +02:00
assert opinion.explanation == EXPLANATION
2017-10-23 14:06:29 +02:00
rep = re.sub(r"(\[|=| )u('|\"|\\\'|\\\")", r"\g<1>\g<2>", repr(opinion))
assert rep == EXPECTED_OPINION_REPR