Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 28aa7b5

Browse files
richard-julienaHenryJard
authored andcommitted
[client] Improve testing (#659)
1 parent 8f9150f commit 28aa7b5

File tree

3 files changed

+51
-52
lines changed

3 files changed

+51
-52
lines changed

pycti/utils/opencti_stix2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,11 @@ def get_stix_helper(self):
896896
"vocabulary": self.opencti.vocabulary,
897897
}
898898

899+
def generate_standard_id_from_stix(self, data):
900+
stix_helpers = self.get_stix_helper()
901+
helper = stix_helpers.get(data["type"])
902+
return helper.generate_id_from_data(data)
903+
899904
# region import
900905
def import_object(
901906
self, stix_object: Dict, update: bool = False, types: List = None

tests/01-unit/stix/test_bundle_ids_rewrite.py

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,90 +17,84 @@ def load_test_file():
1717
return bundle_data
1818

1919

20-
def gen_id(data_type, data):
21-
helper = get_cti_helper()
22-
stix_helpers = helper.get_stix_helper()
23-
helper = stix_helpers.get(data_type)
24-
return helper.generate_id_from_data(data)
25-
26-
2720
# fmt: off
2821
def test_ids_generation():
22+
gen_id = get_cti_helper().generate_standard_id_from_stix
2923
# attack-pattern
30-
assert gen_id("attack-pattern", {"name": "attack"}) =='attack-pattern--25f21617-8de8-5d5e-8cd4-b7e88547ba76'
31-
assert gen_id("attack-pattern", {"name": "attack", "x_mitre_id": 'MITREID'}) == 'attack-pattern--b74cfee2-7b14-585e-862f-fea45e802da9'
32-
assert gen_id("attack-pattern", {"x_mitre_id": "MITREID"}) == 'attack-pattern--b74cfee2-7b14-585e-862f-fea45e802da9'
24+
assert gen_id({"type": "attack-pattern", "name": "attack"}) =='attack-pattern--25f21617-8de8-5d5e-8cd4-b7e88547ba76'
25+
assert gen_id({"type": "attack-pattern", "name": "attack", "x_mitre_id": 'MITREID'}) == 'attack-pattern--b74cfee2-7b14-585e-862f-fea45e802da9'
26+
assert gen_id({"type": "attack-pattern", "x_mitre_id": "MITREID"}) == 'attack-pattern--b74cfee2-7b14-585e-862f-fea45e802da9'
3327
# campaign
34-
assert gen_id("campaign", {"name": "attack"}) == 'campaign--25f21617-8de8-5d5e-8cd4-b7e88547ba76'
28+
assert gen_id({"type": "campaign", "name": "attack"}) == 'campaign--25f21617-8de8-5d5e-8cd4-b7e88547ba76'
3529
# note
36-
assert gen_id("note", {"content": "My note content!"}) == "note--2b4ab5af-2307-58e1-8862-a6a269aae798"
37-
assert gen_id("note", {"content": "My note content!", "created": "2022-11-25T19:00:05.000Z"}) == "note--10861e5c-049e-54f6-9736-81c106e39a0b"
30+
assert gen_id({"type": "note", "content": "My note content!"}) == "note--2b4ab5af-2307-58e1-8862-a6a269aae798"
31+
assert gen_id({"type": "note", "content": "My note content!", "created": "2022-11-25T19:00:05.000Z"}) == "note--10861e5c-049e-54f6-9736-81c106e39a0b"
3832
# observed-data
39-
assert gen_id("observed-data", {"object_refs": ["id"]}) == "observed-data--4765c523-81bc-54c8-b1af-ee81d961dad1"
33+
assert gen_id({"type": "observed-data", "object_refs": ["id"]}) == "observed-data--4765c523-81bc-54c8-b1af-ee81d961dad1"
4034
# opinion
41-
assert gen_id("opinion", {"opinion": "Good"}) == "opinion--0aef8829-207e-508b-b1f1-9da07f3379cb"
42-
assert gen_id("opinion", {"opinion": "Good", "created": "2022-11-25T19:00:05.000Z"}) == "opinion--941dbd61-c6b1-5290-b63f-19a38983d7f7"
35+
assert gen_id({"type": "opinion", "opinion": "Good"}) == "opinion--0aef8829-207e-508b-b1f1-9da07f3379cb"
36+
assert gen_id({"type": "opinion", "opinion": "Good", "created": "2022-11-25T19:00:05.000Z"}) == "opinion--941dbd61-c6b1-5290-b63f-19a38983d7f7"
4337
# report
44-
assert gen_id("report", {"name": "Report", "published": "2022-11-25T19:00:05.000Z"}) == "report--761c6602-975f-5e5e-b220-7a2d41f33ce4"
38+
assert gen_id({"type": "report", "name": "Report", "published": "2022-11-25T19:00:05.000Z"}) == "report--761c6602-975f-5e5e-b220-7a2d41f33ce4"
4539
# course-of-action
46-
assert gen_id("course-of-action", {"x_mitre_id": "MITREID"}) == "course-of-action--b74cfee2-7b14-585e-862f-fea45e802da9"
47-
assert gen_id("course-of-action", {"x_mitre_id": "MITREID", "name": "Name"}) == "course-of-action--b74cfee2-7b14-585e-862f-fea45e802da9"
48-
assert gen_id("course-of-action", {"name": "Name"}) == "course-of-action--e6e2ee8d-e54d-50cd-b77c-df8c8eea7726"
40+
assert gen_id({"type": "course-of-action", "x_mitre_id": "MITREID"}) == "course-of-action--b74cfee2-7b14-585e-862f-fea45e802da9"
41+
assert gen_id({"type": "course-of-action", "x_mitre_id": "MITREID", "name": "Name"}) == "course-of-action--b74cfee2-7b14-585e-862f-fea45e802da9"
42+
assert gen_id({"type": "course-of-action", "name": "Name"}) == "course-of-action--e6e2ee8d-e54d-50cd-b77c-df8c8eea7726"
4943
# identity
50-
assert gen_id("identity", {"name": "julien", "identity_class": "Individual"}) == "identity--d969b177-497f-598d-8428-b128c8f5f819"
51-
assert gen_id("identity", {"name": "julien", "identity_class": "Sector"}) == "identity--14ffa2a4-e16a-522a-937a-784c0ac1fab0"
52-
assert gen_id("identity", {"name": "julien", "identity_class": "System"}) == "identity--8af97482-121d-53f7-a533-9c48f06b5a38"
53-
assert gen_id("identity", {"name": "organization", "identity_class": "individual"}) == "identity--00f7eb8c-6af2-5ed5-9ede-ede4c623de3b"
44+
assert gen_id({"type": "identity", "name": "julien", "identity_class": "Individual"}) == "identity--d969b177-497f-598d-8428-b128c8f5f819"
45+
assert gen_id({"type": "identity", "name": "julien", "identity_class": "Sector"}) == "identity--14ffa2a4-e16a-522a-937a-784c0ac1fab0"
46+
assert gen_id({"type": "identity", "name": "julien", "identity_class": "System"}) == "identity--8af97482-121d-53f7-a533-9c48f06b5a38"
47+
assert gen_id({"type": "identity", "name": "organization", "identity_class": "individual"}) == "identity--00f7eb8c-6af2-5ed5-9ede-ede4c623de3b"
5448
# infrastructure
55-
assert gen_id("infrastructure", {"name": "infra"}) == "infrastructure--8a20116f-5a41-5508-ae4b-c293ac67c527"
49+
assert gen_id({"type": "infrastructure", "name": "infra"}) == "infrastructure--8a20116f-5a41-5508-ae4b-c293ac67c527"
5650
# intrusion-set
57-
assert gen_id("intrusion-set", {"name": "intrusion"}) == "intrusion-set--30757026-c4bd-574d-ae52-8d8503b4818e"
51+
assert gen_id({"type": "intrusion-set", "name": "intrusion"}) == "intrusion-set--30757026-c4bd-574d-ae52-8d8503b4818e"
5852
# location
59-
assert gen_id("location", {"name": "Lyon", "x_opencti_location_type": "City"}) == "location--da430873-42c8-57ca-b08b-a797558c6cbd"
60-
assert gen_id("location", {"latitude": 5.12, "name": "Position1", "x_opencti_location_type": "Position"}) == "location--56b3fc50-5091-5f2e-bd19-7b40ee3881e4"
61-
assert gen_id("location", {"longitude": 5.12, "name": 'Position2', "x_opencti_location_type": "Position"}) == "location--dd2cf94c-1d58-58a1-b21f-0ede4059aaf0"
62-
assert gen_id("location", {"latitude": 5.12, "longitude": 5.12, "x_opencti_location_type": "Position"}) == "location--57acef55-747a-55ef-9c49-06ca85f8d749"
63-
assert gen_id("location", {"name": 'Position3', "x_opencti_location_type": "Position"}) == "location--a4152781-8721-5d44-ae2d-e492665bc35b"
53+
assert gen_id({"type": "location", "name": "Lyon", "x_opencti_location_type": "City"}) == "location--da430873-42c8-57ca-b08b-a797558c6cbd"
54+
assert gen_id({"type": "location", "latitude": 5.12, "name": "Position1", "x_opencti_location_type": "Position"}) == "location--56b3fc50-5091-5f2e-bd19-7b40ee3881e4"
55+
assert gen_id({"type": "location", "longitude": 5.12, "name": 'Position2', "x_opencti_location_type": "Position"}) == "location--dd2cf94c-1d58-58a1-b21f-0ede4059aaf0"
56+
assert gen_id({"type": "location", "latitude": 5.12, "longitude": 5.12, "x_opencti_location_type": "Position"}) == "location--57acef55-747a-55ef-9c49-06ca85f8d749"
57+
assert gen_id({"type": "location", "name": 'Position3', "x_opencti_location_type": "Position"}) == "location--a4152781-8721-5d44-ae2d-e492665bc35b"
6458
# malware
65-
assert gen_id("malware", {"name": "malware"}) == "malware--92ddf766-b27c-5159-8f46-27002bba2f04"
59+
assert gen_id({"type": "malware", "name": "malware"}) == "malware--92ddf766-b27c-5159-8f46-27002bba2f04"
6660
# threat-actor-group
67-
assert gen_id("threat-actor", {"name": "CARD04"}) == "threat-actor--6d458783-df3b-5398-8e30-282655ad7b94"
68-
assert gen_id("threat-actor", {"name": "CARD04", "x_opencti_type": "Threat-Actor-Group"}) == "threat-actor--6d458783-df3b-5398-8e30-282655ad7b94"
61+
assert gen_id({"type": "threat-actor", "name": "CARD04"}) == "threat-actor--6d458783-df3b-5398-8e30-282655ad7b94"
62+
assert gen_id({"type": "threat-actor", "name": "CARD04", "x_opencti_type": "Threat-Actor-Group"}) == "threat-actor--6d458783-df3b-5398-8e30-282655ad7b94"
6963
# tool
70-
assert gen_id("tool", {"name": "my-tool"}) == "tool--41cd21d0-f50e-5e3d-83fc-447e0def97b7"
64+
assert gen_id({"type": "tool", "name": "my-tool"}) == "tool--41cd21d0-f50e-5e3d-83fc-447e0def97b7"
7165
# vulnerability
72-
assert gen_id("vulnerability", {"name": "vulnerability"}) == "vulnerability--2c690168-aec3-57f1-8295-adf53f4dc3da"
66+
assert gen_id({"type": "vulnerability", "name": "vulnerability"}) == "vulnerability--2c690168-aec3-57f1-8295-adf53f4dc3da"
7367
# incident
74-
assert gen_id("incident", {"name": "incident", "created": "2022-11-25T19:00:05.000Z"}) == "incident--0e117c15-0a94-5ad3-b090-0395613f5b29"
68+
assert gen_id({"type": "incident", "name": "incident", "created": "2022-11-25T19:00:05.000Z"}) == "incident--0e117c15-0a94-5ad3-b090-0395613f5b29"
7569
# case-incident
76-
assert gen_id("case-incident", {"name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "case-incident--4838a141-bd19-542c-85d9-cce0382645b5"
70+
assert gen_id({"type": "case-incident", "name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "case-incident--4838a141-bd19-542c-85d9-cce0382645b5"
7771
# case-rfi
78-
assert gen_id("case-rfi", {"name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "case-rfi--4838a141-bd19-542c-85d9-cce0382645b5"
72+
assert gen_id({"type": "case-rfi", "name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "case-rfi--4838a141-bd19-542c-85d9-cce0382645b5"
7973
# case-rft
80-
assert gen_id("case-rft", {"name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "case-rft--4838a141-bd19-542c-85d9-cce0382645b5"
74+
assert gen_id({"type": "case-rft", "name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "case-rft--4838a141-bd19-542c-85d9-cce0382645b5"
8175
# feedback, not supported yet
8276
# assert gen_id("case-feedback", {"name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "feedback--4838a141-bd19-542c-85d9-cce0382645b5"
8377
# channel
84-
assert gen_id("channel", {"name": "channel"}) == "channel--4936cdd5-6b6a-5c92-a756-cae1f09dcd80"
78+
assert gen_id({"type": "channel", "name": "channel"}) == "channel--4936cdd5-6b6a-5c92-a756-cae1f09dcd80"
8579
# data-component
86-
assert gen_id("data-component", {"name": "data-component"}) == "data-component--32fdc52a-b4c5-5268-af2f-cdf820271f0b"
80+
assert gen_id({"type": "data-component", "name": "data-component"}) == "data-component--32fdc52a-b4c5-5268-af2f-cdf820271f0b"
8781
# data-source
88-
assert gen_id("data-source", {"name": "data-source"}) == "data-source--f0925972-35e1-5172-9161-4d7180908339"
82+
assert gen_id({"type": "data-source", "name": "data-source"}) == "data-source--f0925972-35e1-5172-9161-4d7180908339"
8983
# grouping
90-
assert gen_id("grouping", {"name": "grouping", "context": "context"}) == "grouping--8462bd42-4cad-54ae-a261-efc1a762d83d"
84+
assert gen_id({"type": "grouping", "name": "grouping", "context": "context"}) == "grouping--8462bd42-4cad-54ae-a261-efc1a762d83d"
9185
# language
92-
assert gen_id("language", {"name": "fr"}) == "language--0ef28873-9d49-5cdb-a53a-eb7613391ee9"
86+
assert gen_id({"type": "language", "name": "fr"}) == "language--0ef28873-9d49-5cdb-a53a-eb7613391ee9"
9387
# malware-analysis
94-
assert gen_id("malware-analysis", {"product": "linux", "result_name": "result"}) == "malware-analysis--3d501241-a4a5-574d-a503-301a6426f8c1"
95-
assert gen_id("malware-analysis", {"product": "linux", "result_name": "result", "submitted": "2022-11-25T19:00:05.000Z"}) == "malware-analysis--d7ffe68a-0d5f-5fea-a375-3338ba4ea13c"
88+
assert gen_id({"type": "malware-analysis", "product": "linux", "result_name": "result"}) == "malware-analysis--3d501241-a4a5-574d-a503-301a6426f8c1"
89+
assert gen_id({"type": "malware-analysis", "product": "linux", "result_name": "result", "submitted": "2022-11-25T19:00:05.000Z"}) == "malware-analysis--d7ffe68a-0d5f-5fea-a375-3338ba4ea13c"
9690
# narrative
97-
assert gen_id("narrative", {"name": "narrative"}) == "narrative--804a7e40-d39c-59b6-9e3f-1ba1bc92b739"
91+
assert gen_id({"type": "narrative", "name": "narrative"}) == "narrative--804a7e40-d39c-59b6-9e3f-1ba1bc92b739"
9892
# task
99-
assert gen_id("task", {"name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "task--4838a141-bd19-542c-85d9-cce0382645b5"
93+
assert gen_id({"type": "task", "name": "case", "created": "2022-11-25T19:00:05.000Z"}) == "task--4838a141-bd19-542c-85d9-cce0382645b5"
10094
# Threat-actor-individual
101-
assert gen_id("threat-actor", {"name": "CARD04", "x_opencti_type": "Threat-Actor-Individual"}) == "threat-actor--af15b6ae-a3dd-54d3-8fa0-3adfe0391d01"
95+
assert gen_id({"type": "threat-actor", "name": "CARD04", "x_opencti_type": "Threat-Actor-Individual"}) == "threat-actor--af15b6ae-a3dd-54d3-8fa0-3adfe0391d01"
10296
# vocabulary
103-
assert gen_id("vocabulary", {"name": "facebook", "category": "account_type_ov"}) == "vocabulary--85ae7185-ff6f-509b-a011-3069921614aa"
97+
assert gen_id({"type": "vocabulary", "name": "facebook", "category": "account_type_ov"}) == "vocabulary--85ae7185-ff6f-509b-a011-3069921614aa"
10498
# fmt: on
10599

106100

tests/02-integration/utils/test_stix_crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_entity_create(entity_class, api_stix, opencti_splitter):
2424
bundles_sent = api_stix.import_bundle_from_json(split_bundle, False, None, None)
2525

2626
assert len(bundles_sent) == 1
27-
assert bundles_sent[0]["id"] == stix_object["id"]
27+
assert bundles_sent[0]["id"] == api_stix.generate_standard_id_from_stix(stix_object)
2828
assert bundles_sent[0]["type"] == stix_object["type"]
2929

3030
entity_class.base_class().delete(id=stix_object["id"])

0 commit comments

Comments
 (0)