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 = (
|
2018-07-13 17:10:05 +02:00
|
|
|
'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",
|
2018-06-14 02:09:07 +02:00
|
|
|
"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",
|
2019-07-16 22:10:25 +02:00
|
|
|
"opinion": "strongly-disagree",
|
2017-10-23 14:06:29 +02:00
|
|
|
"object_refs": [
|
|
|
|
"relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471"
|
2019-07-16 22:10:25 +02:00
|
|
|
]
|
2018-10-15 20:48:52 +02:00
|
|
|
}""" % EXPLANATION
|
2017-10-23 14:06:29 +02:00
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
EXPECTED_OPINION_REPR = "Opinion(" + " ".join((
|
|
|
|
"""
|
2017-10-23 14:06:29 +02:00
|
|
|
type='opinion',
|
2018-06-14 02:09:07 +02:00
|
|
|
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",
|
2019-07-16 22:10:25 +02:00
|
|
|
opinion='strongly-disagree',
|
2021-07-07 02:32:58 +02:00
|
|
|
object_refs=['relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471'],
|
|
|
|
revoked=False
|
2019-07-16 22:10:25 +02:00
|
|
|
""" % EXPLANATION
|
2018-07-13 17:10:05 +02:00
|
|
|
).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)
|
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-03-31 18:39:14 +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
|
|
|
|
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED_OPINION,
|
|
|
|
{
|
|
|
|
"type": "opinion",
|
|
|
|
"spec_version": "2.1",
|
2019-01-23 16:56:20 +01:00
|
|
|
"id": OPINION_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
"created": "2016-05-12T08:17:27.000Z",
|
|
|
|
"modified": "2016-05-12T08:17:27.000Z",
|
2018-10-15 20:48:52 +02:00
|
|
|
"explanation": EXPLANATION,
|
2018-07-13 17:10:05 +02:00
|
|
|
"object_refs": [
|
|
|
|
"relationship--16d2358f-3b0d-4c88-b047-0da2f7ed4471",
|
|
|
|
],
|
|
|
|
"opinion": "strongly-disagree",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2017-10-23 14:06:29 +02:00
|
|
|
def test_parse_opinion(data):
|
2018-07-03 15:40:51 +02:00
|
|
|
opinion = stix2.parse(data, version="2.1")
|
2017-10-23 14:06:29 +02:00
|
|
|
|
|
|
|
assert opinion.type == 'opinion'
|
2018-07-03 15:40:51 +02:00
|
|
|
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
|