Skip to content

Commit 33362fc

Browse files
committed
add: [galaxies] basic tests
1 parent d3dca84 commit 33362fc

File tree

2 files changed

+195
-1
lines changed

2 files changed

+195
-1
lines changed

api/app/tests/api/test_galaxies.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import pytest
2+
from app.auth import auth
3+
from app.models import galaxy as galaxies_models
4+
from app.tests.api_tester import ApiTester
5+
from fastapi import status
6+
from fastapi.testclient import TestClient
7+
8+
9+
class TestTaxonomiesResource(ApiTester):
10+
@pytest.mark.parametrize("scopes", [["galaxies:read"]])
11+
def test_get_galaxies(
12+
self,
13+
client: TestClient,
14+
threat_actor_galaxy: galaxies_models.Galaxy,
15+
threat_actor_galaxy_cluster_apt29: galaxies_models.GalaxyCluster,
16+
auth_token: auth.Token,
17+
):
18+
response = client.get(
19+
"/galaxies/", headers={"Authorization": "Bearer " + auth_token}
20+
)
21+
data = response.json()
22+
23+
assert response.status_code == status.HTTP_200_OK
24+
25+
assert len(data["items"]) == 1
26+
assert data["items"][0]["id"] == threat_actor_galaxy.id
27+
assert data["items"][0]["name"] == threat_actor_galaxy.name
28+
assert data["items"][0]["type"] == threat_actor_galaxy.type
29+
assert data["items"][0]["description"] == threat_actor_galaxy.description
30+
assert data["items"][0]["namespace"] == threat_actor_galaxy.namespace
31+
assert data["items"][0]["icon"] == threat_actor_galaxy.icon
32+
assert data["items"][0]["enabled"] == threat_actor_galaxy.enabled
33+
assert data["items"][0]["local_only"] == threat_actor_galaxy.local_only
34+
assert data["items"][0]["default"] == threat_actor_galaxy.default
35+
assert data["items"][0]["org_id"] == threat_actor_galaxy.org_id
36+
assert data["items"][0]["orgc_id"] == threat_actor_galaxy.orgc_id
37+
38+
# check clusters
39+
assert len(data["items"][0]["clusters"]) == 1
40+
assert data["items"][0]["clusters"][0]["galaxy_id"] == threat_actor_galaxy.id
41+
assert (
42+
data["items"][0]["clusters"][0]["id"]
43+
== threat_actor_galaxy_cluster_apt29.id
44+
)
45+
assert (
46+
data["items"][0]["clusters"][0]["value"]
47+
== threat_actor_galaxy_cluster_apt29.value
48+
)
49+
assert (
50+
data["items"][0]["clusters"][0]["value"]
51+
== threat_actor_galaxy_cluster_apt29.value
52+
)
53+
assert (
54+
data["items"][0]["clusters"][0]["tag_name"]
55+
== threat_actor_galaxy_cluster_apt29.tag_name
56+
)
57+
58+
@pytest.mark.parametrize("scopes", [["galaxies:read"]])
59+
def test_get_galaxy_by_id(
60+
self,
61+
client: TestClient,
62+
threat_actor_galaxy: galaxies_models.Galaxy,
63+
threat_actor_galaxy_cluster_apt29: galaxies_models.GalaxyCluster,
64+
auth_token: auth.Token,
65+
):
66+
response = client.get(
67+
f"/galaxies/{threat_actor_galaxy.id}",
68+
headers={"Authorization": "Bearer " + auth_token},
69+
)
70+
data = response.json()
71+
72+
assert response.status_code == status.HTTP_200_OK
73+
assert data["id"] == threat_actor_galaxy.id
74+
assert data["name"] == threat_actor_galaxy.name
75+
assert data["type"] == threat_actor_galaxy.type
76+
assert data["description"] == threat_actor_galaxy.description
77+
assert data["namespace"] == threat_actor_galaxy.namespace
78+
assert data["icon"] == threat_actor_galaxy.icon
79+
assert data["enabled"] == threat_actor_galaxy.enabled
80+
assert data["local_only"] == threat_actor_galaxy.local_only
81+
assert data["default"] == threat_actor_galaxy.default
82+
assert data["org_id"] == threat_actor_galaxy.org_id
83+
assert data["orgc_id"] == threat_actor_galaxy.orgc_id
84+
85+
# check clusters
86+
assert len(data["clusters"]) == 1
87+
assert data["clusters"][0]["galaxy_id"] == threat_actor_galaxy.id
88+
assert data["clusters"][0]["id"] == threat_actor_galaxy_cluster_apt29.id
89+
assert data["clusters"][0]["value"] == threat_actor_galaxy_cluster_apt29.value
90+
assert data["clusters"][0]["value"] == threat_actor_galaxy_cluster_apt29.value
91+
assert (
92+
data["clusters"][0]["tag_name"]
93+
== threat_actor_galaxy_cluster_apt29.tag_name
94+
)
95+
96+
@pytest.mark.parametrize("scopes", [["galaxies:update"]])
97+
def test_patch_taxonomy(
98+
self,
99+
client: TestClient,
100+
threat_actor_galaxy: galaxies_models.Galaxy,
101+
auth_token: auth.Token,
102+
):
103+
response = client.patch(
104+
f"/galaxies/{threat_actor_galaxy.id}",
105+
headers={"Authorization": "Bearer " + auth_token},
106+
json={
107+
"enabled": False,
108+
"local_only": True,
109+
"default": True,
110+
},
111+
)
112+
data = response.json()
113+
114+
assert response.status_code == status.HTTP_200_OK
115+
116+
assert data["id"] == threat_actor_galaxy.id
117+
assert data["name"] == threat_actor_galaxy.name
118+
assert data["enabled"] is False
119+
assert data["local_only"] is True
120+
assert data["default"] is True
121+
122+
@pytest.mark.parametrize("scopes", [["galaxies:update"]])
123+
def test_update_galaxies(
124+
self,
125+
client: TestClient,
126+
auth_token: auth.Token,
127+
):
128+
# TODO: implement test without importing all the misp-galaxies (takes too long)
129+
pass

api/app/tests/api_tester.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def tlp_taxonomy(
399399
db.commit()
400400
db.refresh(tlp_taxonomy)
401401

402-
return tlp_taxonomy
402+
yield tlp_taxonomy
403403

404404
@pytest.fixture(scope="class")
405405
def tlp_white_predicate(
@@ -423,3 +423,68 @@ def tlp_white_predicate(
423423
db.refresh(tlp_white_predicate)
424424

425425
yield tlp_white_predicate
426+
427+
@pytest.fixture(scope="class")
428+
def threat_actor_galaxy(
429+
self,
430+
db: Session,
431+
organisation_1: organisation_models.Organisation,
432+
user_1: user_models.User,
433+
):
434+
threat_actor_galaxy = galaxy_models.Galaxy(
435+
name="Threat Actor",
436+
type="threat-actor",
437+
description="Threat actors are characteristics of malicious actors (or adversaries) representing a cyber attack threat including presumed intent and historically observed behaviour.",
438+
version=3,
439+
namespace="misp",
440+
icon="user-secret",
441+
enabled=True,
442+
local_only=False,
443+
default=False,
444+
org_id=organisation_1.id,
445+
orgc_id=organisation_1.id,
446+
created="2020-01-01 01:01:01",
447+
modified="2020-01-01 01:01:01",
448+
)
449+
450+
db.add(threat_actor_galaxy)
451+
db.commit()
452+
db.refresh(threat_actor_galaxy)
453+
454+
yield threat_actor_galaxy
455+
456+
@pytest.fixture(scope="class")
457+
def threat_actor_galaxy_cluster_apt29(
458+
self,
459+
db: Session,
460+
organisation_1: organisation_models.Organisation,
461+
user_1: user_models.User,
462+
threat_actor_galaxy: galaxy_models.Galaxy,
463+
):
464+
465+
threat_actor_galaxy_cluster_apt29 = galaxy_models.GalaxyCluster(
466+
collection_uuid="7cdff317-a673-4474-84ec-4f1754947823",
467+
type="threat-actor",
468+
value="APT29",
469+
tag_name='misp-galaxy:threat-actor="APT29"',
470+
description="APT29 description.",
471+
galaxy_id=threat_actor_galaxy.id,
472+
source="MISP Project",
473+
authors=[
474+
"Author 1",
475+
"Author 2",
476+
],
477+
version=1,
478+
uuid="b2056ff0-00b9-482e-b11c-c771daa5f28a",
479+
distribution=event_models.DistributionLevel.ALL_COMMUNITIES,
480+
sharing_group_id=None,
481+
org_id=organisation_1.id,
482+
orgc_id=organisation_1.id,
483+
published=True,
484+
)
485+
486+
db.add(threat_actor_galaxy_cluster_apt29)
487+
db.commit()
488+
db.refresh(threat_actor_galaxy_cluster_apt29)
489+
490+
yield threat_actor_galaxy_cluster_apt29

0 commit comments

Comments
 (0)