forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_opencti_stix2.py
106 lines (87 loc) · 3.72 KB
/
test_opencti_stix2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import datetime
import pytest
from pycti.utils.opencti_stix2 import OpenCTIStix2
@pytest.fixture
def opencti_stix2(api_client):
return OpenCTIStix2(api_client)
def test_unknown_type(opencti_stix2: OpenCTIStix2, caplog):
opencti_stix2.unknown_type({"type": "foo"})
for record in caplog.records:
assert record.levelname == "ERROR"
assert 'Unknown object type "foo", doing nothing...' in caplog.text
def test_convert_markdown(opencti_stix2: OpenCTIStix2):
result = opencti_stix2.convert_markdown(
" my <code> is very </special> </code> to me"
)
assert " my ` is very </special> ` to me" == result
def test_convert_markdown_typo(opencti_stix2: OpenCTIStix2):
result = opencti_stix2.convert_markdown(
" my <code is very </special> </code> to me"
)
assert " my <code is very </special> ` to me" == result
def test_format_date_with_tz(opencti_stix2: OpenCTIStix2):
# Test all 4 format_date cases with timestamp + timezone
my_datetime = datetime.datetime(
2021, 3, 5, 13, 31, 19, 42621, tzinfo=datetime.timezone.utc
)
my_datetime_str = my_datetime.isoformat(timespec="milliseconds").replace(
"+00:00", "Z"
)
assert my_datetime_str == opencti_stix2.format_date(my_datetime)
my_date = my_datetime.date()
my_date_str = "2021-03-05T00:00:00.000Z"
assert my_date_str == opencti_stix2.format_date(my_date)
assert my_datetime_str == opencti_stix2.format_date(my_datetime_str)
assert (
str(
datetime.datetime.now(tz=datetime.timezone.utc)
.isoformat(timespec="seconds")
.replace("+00:00", "")
)
in opencti_stix2.format_date()
)
with pytest.raises(ValueError):
opencti_stix2.format_date("No time")
# Test all 4 format_date cases with timestamp w/o timezone
my_datetime = datetime.datetime(2021, 3, 5, 13, 31, 19, 42621)
my_datetime_str = (
my_datetime.replace(tzinfo=datetime.timezone.utc)
.isoformat(timespec="milliseconds")
.replace("+00:00", "Z")
)
assert my_datetime_str == opencti_stix2.format_date(my_datetime)
my_date = my_datetime.date()
my_date_str = "2021-03-05T00:00:00.000Z"
assert my_date_str == opencti_stix2.format_date(my_date)
assert my_datetime_str == opencti_stix2.format_date(my_datetime_str)
assert (
str(
datetime.datetime.now(tz=datetime.timezone.utc)
.isoformat(timespec="seconds")
.replace("+00:00", "")
)
in opencti_stix2.format_date()
)
with pytest.raises(ValueError):
opencti_stix2.format_date("No time")
def test_filter_objects(opencti_stix2: OpenCTIStix2):
objects = [{"id": "123"}, {"id": "124"}, {"id": "125"}, {"id": "126"}]
result = opencti_stix2.filter_objects(["123", "124", "126"], objects)
assert len(result) == 1
assert "126" not in result
def test_pick_aliases(opencti_stix2: OpenCTIStix2) -> None:
stix_object = {}
assert opencti_stix2.pick_aliases(stix_object) is None
stix_object["aliases"] = "alias"
assert opencti_stix2.pick_aliases(stix_object) == "alias"
stix_object["x_amitt_aliases"] = "amitt_alias"
assert opencti_stix2.pick_aliases(stix_object) == "amitt_alias"
stix_object["x_mitre_aliases"] = "mitre_alias"
assert opencti_stix2.pick_aliases(stix_object) == "mitre_alias"
stix_object["x_opencti_aliases"] = "opencti_alias"
assert opencti_stix2.pick_aliases(stix_object) == "opencti_alias"
def test_import_bundle_from_file(opencti_stix2: OpenCTIStix2, caplog) -> None:
opencti_stix2.import_bundle_from_file("foo.txt")
for record in caplog.records:
assert record.levelname == "ERROR"
assert "The bundle file does not exists" in caplog.text