|
| 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 |
0 commit comments