| 1 |
import urllib.parse |
| 2 |
|
| 3 |
import pytest |
| 4 |
import responses |
| 5 |
from responses import matchers |
| 6 |
|
| 7 |
from censys.asm.risks import Risks |
| 8 |
|
| 9 |
from ..utils import CensysTestCase |
| 10 |
from .utils import V2_URL |
| 11 |
|
| 12 |
TEST_EVENT_JSON = { |
| 13 |
"delta": "string", |
| 14 |
"id": 0, |
| 15 |
"op": "string", |
| 16 |
"reason": "string", |
| 17 |
"src": "string", |
| 18 |
"srcID": "string", |
| 19 |
"ts": "2022-02-15T20:22:30.262Z", |
| 20 |
} |
| 21 |
TEST_RISK_EVENTS_JSON = { |
| 22 |
"total": 1, |
| 23 |
"next": "eyJhZnRlcklEIjo3NzQwLCJsaW1pdCI6MTAwfQ==", |
| 24 |
"events": [TEST_EVENT_JSON], |
| 25 |
"endOfEvents": True, |
| 26 |
} |
| 27 |
TEST_RISK_INSTANCE_JSON = { |
| 28 |
"id": 0, |
| 29 |
"events": [TEST_EVENT_JSON], |
| 30 |
"metadata": [{"src": "system"}], |
| 31 |
} |
| 32 |
TEST_RISK_INSTANCES_JSON = { |
| 33 |
"risks": [TEST_RISK_INSTANCE_JSON], |
| 34 |
} |
| 35 |
TEST_PATCH_RISK_INSTANCE_JSON = { |
| 36 |
"categoriesChanges": 0, |
| 37 |
"changes": 0, |
| 38 |
"metadataAdded": 0, |
| 39 |
"metadataRemoved": 0, |
| 40 |
"severityChanges": 0, |
| 41 |
"skips": 0, |
| 42 |
"userStatusChanges": 0, |
| 43 |
} |
| 44 |
TEST_RISK_TYPE = "service.authentication.http_weak_auth/after-slash" |
| 45 |
ESCAPED_TEST_RISK_TYPE = urllib.parse.quote(TEST_RISK_TYPE, safe="") |
| 46 |
TEST_RISK_TYPE_JSON = { |
| 47 |
"config": "string", |
| 48 |
"contextType": "string", |
| 49 |
"defaultCategories": [["string"]], |
| 50 |
"defaultSeverity": "low", |
| 51 |
"description": "string", |
| 52 |
"enabled": True, |
| 53 |
"events": [TEST_EVENT_JSON], |
| 54 |
"lastDisableReason": "string", |
| 55 |
"lastSeverityChangeReason": "string", |
| 56 |
"lastUpdatedAt": "2022-02-15T20:22:30.262Z", |
| 57 |
"name": "string", |
| 58 |
"recommendedSeverity": "low", |
| 59 |
"remediations": ["string"], |
| 60 |
"riskCount": 0, |
| 61 |
"subjectType": "string", |
| 62 |
"type": "string", |
| 63 |
} |
| 64 |
TEST_RISK_TYPES_JSON = { |
| 65 |
"type": [TEST_RISK_TYPE_JSON], |
| 66 |
} |
| 67 |
TEST_PATCH_RISK_TYPE_JSON = { |
| 68 |
"changes": 0, |
| 69 |
"configChanges": 0, |
| 70 |
"defaultCategoriesChanges": 0, |
| 71 |
"defaultSeverityChanges": 0, |
| 72 |
"enabledChanges": 0, |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
class RisksTests(CensysTestCase): |
| 77 |
api: Risks |
| 78 |
|
| 79 |
def setup_method(self): |
| 80 |
self.setUpApi(Risks(self.api_key)) |
| 81 |
|
| 82 |
@pytest.mark.parametrize( |
| 83 |
("kwargs", "params"), |
| 84 |
[ |
| 85 |
({}, ""), |
| 86 |
( |
| 87 |
{"start": "2023-12-14T03:27:00Z", "end": "2022-03-18T09:18:54Z"}, |
| 88 |
"?start=2023-12-14T03:27:00Z&end=2022-03-18T09:18:54Z", |
| 89 |
), |
| 90 |
({"after_id": 489, "limit": 100}, "?afterID=489&limit=100"), |
| 91 |
( |
| 92 |
{"cursor": "eyJhZnRlcklEIjo3NzQwLCJsaW1pdCI6MTAwfQ=="}, |
| 93 |
"?cursor=eyJhZnRlcklEIjo3NzQwLCJsaW1pdCI6MTAwfQ==", |
| 94 |
), |
| 95 |
], |
| 96 |
) |
| 97 |
def test_get_risk_events(self, kwargs, params): |
| 98 |
# Setup response |
| 99 |
self.responses.add( |
| 100 |
responses.GET, |
| 101 |
V2_URL + f"/risk-events{params}", |
| 102 |
status=200, |
| 103 |
json=TEST_RISK_EVENTS_JSON, |
| 104 |
) |
| 105 |
# Actual call |
| 106 |
res = self.api.get_risk_events(**kwargs) |
| 107 |
# Assertions |
| 108 |
assert res == TEST_RISK_EVENTS_JSON |
| 109 |
|
| 110 |
@pytest.mark.parametrize( |
| 111 |
("kwargs", "params"), |
| 112 |
[ |
| 113 |
({}, ""), |
| 114 |
({"include_events": True}, "?includeEvents=True"), |
| 115 |
({"include_events": False}, "?includeEvents=False"), |
| 116 |
], |
| 117 |
) |
| 118 |
def test_get_risk_instances(self, kwargs, params): |
| 119 |
# Setup response |
| 120 |
self.responses.add( |
| 121 |
responses.GET, |
| 122 |
V2_URL + f"/risk-instances{params}", |
| 123 |
status=200, |
| 124 |
json=TEST_RISK_INSTANCES_JSON, |
| 125 |
) |
| 126 |
# Actual call |
| 127 |
res = self.api.get_risk_instances(**kwargs) |
| 128 |
# Assertions |
| 129 |
assert res == TEST_RISK_INSTANCES_JSON |
| 130 |
|
| 131 |
def test_patch_risk_instances(self): |
| 132 |
# Mock |
| 133 |
mock_patch = self.mocker.patch.dict( |
| 134 |
{ |
| 135 |
"id": 0, |
| 136 |
"categories": ["test-patch"], |
| 137 |
} |
| 138 |
) |
| 139 |
self.responses.add( |
| 140 |
responses.PATCH, |
| 141 |
V2_URL + "/risk-instances", |
| 142 |
status=200, |
| 143 |
json=TEST_PATCH_RISK_INSTANCE_JSON, |
| 144 |
match=[matchers.json_params_matcher(mock_patch)], |
| 145 |
) |
| 146 |
# Actual call |
| 147 |
res = self.api.patch_risk_instances(mock_patch) |
| 148 |
# Assertions |
| 149 |
assert res == TEST_PATCH_RISK_INSTANCE_JSON |
| 150 |
|
| 151 |
def test_search_risk_instances(self): |
| 152 |
# Mock |
| 153 |
mock_search = self.mocker.patch.dict( |
| 154 |
{ |
| 155 |
"fields": ["id", "context", "type", "metadata", "events"], |
| 156 |
"limit": 1000, |
| 157 |
"page": 1, |
| 158 |
"query": { |
| 159 |
"field": "firstComputedAt", |
| 160 |
"operator": "=", |
| 161 |
"value": "2022-02-15T17:46:10.328Z", |
| 162 |
}, |
| 163 |
"sort": ["severity", {"context.type": "asc"}], |
| 164 |
} |
| 165 |
) |
| 166 |
self.responses.add( |
| 167 |
responses.POST, |
| 168 |
V2_URL + "/risk-instances/search", |
| 169 |
status=200, |
| 170 |
json=TEST_RISK_TYPE_JSON, |
| 171 |
match=[matchers.json_params_matcher(mock_search)], |
| 172 |
) |
| 173 |
# Actual call |
| 174 |
res = self.api.search_risk_instances(mock_search) |
| 175 |
# Assertions |
| 176 |
assert res == TEST_RISK_TYPE_JSON |
| 177 |
|
| 178 |
@pytest.mark.parametrize( |
| 179 |
("kwargs", "params"), |
| 180 |
[ |
| 181 |
({"risk_instance_id": 0}, "0"), |
| 182 |
({"risk_instance_id": 1, "include_events": True}, "1?includeEvents=True"), |
| 183 |
({"risk_instance_id": 2, "include_events": False}, "2?includeEvents=False"), |
| 184 |
], |
| 185 |
) |
| 186 |
def test_get_risk_instance(self, kwargs, params): |
| 187 |
# Setup response |
| 188 |
self.responses.add( |
| 189 |
responses.GET, |
| 190 |
V2_URL + f"/risk-instances/{params}", |
| 191 |
status=200, |
| 192 |
json=TEST_RISK_INSTANCE_JSON, |
| 193 |
) |
| 194 |
# Actual call |
| 195 |
res = self.api.get_risk_instance(**kwargs) |
| 196 |
# Assertions |
| 197 |
assert res == TEST_RISK_INSTANCE_JSON |
| 198 |
|
| 199 |
def test_patch_risk_instance(self): |
| 200 |
# Mock |
| 201 |
mock_risk_id = 0 |
| 202 |
mock_patch = self.mocker.patch.dict( |
| 203 |
{ |
| 204 |
"id": mock_risk_id, |
| 205 |
"categories": ["mock-patch"], |
| 206 |
} |
| 207 |
) |
| 208 |
self.responses.add( |
| 209 |
responses.PATCH, |
| 210 |
V2_URL + f"/risk-instances/{mock_risk_id}", |
| 211 |
status=200, |
| 212 |
json=TEST_PATCH_RISK_INSTANCE_JSON, |
| 213 |
match=[matchers.json_params_matcher(mock_patch)], |
| 214 |
) |
| 215 |
# Actual call |
| 216 |
res = self.api.patch_risk_instance(mock_risk_id, mock_patch) |
| 217 |
# Assertions |
| 218 |
assert res == TEST_PATCH_RISK_INSTANCE_JSON |
| 219 |
|
| 220 |
@pytest.mark.parametrize( |
| 221 |
("kwargs", "params"), |
| 222 |
[ |
| 223 |
({}, ""), |
| 224 |
({"include_events": True}, "?includeEvents=True"), |
| 225 |
({"include_events": False}, "?includeEvents=False"), |
| 226 |
({"sort": ["severity", "type:asc"]}, "?sort=severity&sort=type:asc"), |
| 227 |
({"page": 1, "limit": 10000}, "?page=1&limit=10000"), |
| 228 |
({"page": 10}, "?page=10"), |
| 229 |
], |
| 230 |
) |
| 231 |
def test_get_risk_types(self, kwargs, params): |
| 232 |
# Setup response |
| 233 |
self.responses.add( |
| 234 |
responses.GET, |
| 235 |
V2_URL + f"/risk-types{params}", |
| 236 |
status=200, |
| 237 |
json=TEST_RISK_TYPES_JSON, |
| 238 |
) |
| 239 |
# Actual call |
| 240 |
res = self.api.get_risk_types(**kwargs) |
| 241 |
# Assertions |
| 242 |
assert res == TEST_RISK_TYPES_JSON |
| 243 |
|
| 244 |
@pytest.mark.parametrize( |
| 245 |
("kwargs", "params"), |
| 246 |
[ |
| 247 |
({"risk_type": TEST_RISK_TYPE}, ESCAPED_TEST_RISK_TYPE), |
| 248 |
( |
| 249 |
{"risk_type": TEST_RISK_TYPE, "include_events": True}, |
| 250 |
ESCAPED_TEST_RISK_TYPE + "?includeEvents=True", |
| 251 |
), |
| 252 |
( |
| 253 |
{"risk_type": TEST_RISK_TYPE, "include_events": False}, |
| 254 |
ESCAPED_TEST_RISK_TYPE + "?includeEvents=False", |
| 255 |
), |
| 256 |
], |
| 257 |
) |
| 258 |
def test_get_risk_type(self, kwargs, params): |
| 259 |
# Setup respnonse |
| 260 |
self.responses.add( |
| 261 |
responses.GET, |
| 262 |
V2_URL + f"/risk-types/{params}", |
| 263 |
status=200, |
| 264 |
json=TEST_RISK_TYPE_JSON, |
| 265 |
) |
| 266 |
# Actual call |
| 267 |
res = self.api.get_risk_type(**kwargs) |
| 268 |
# Assertions |
| 269 |
assert res == TEST_RISK_TYPE_JSON |
| 270 |
|
| 271 |
def test_patch_risk_type(self): |
| 272 |
# Mock |
| 273 |
mock_patch = self.mocker.patch.dict( |
| 274 |
{ |
| 275 |
"type": TEST_RISK_TYPE, |
| 276 |
"categories": ["mock-patch"], |
| 277 |
} |
| 278 |
) |
| 279 |
self.responses.add( |
| 280 |
responses.PATCH, |
| 281 |
V2_URL + f"/risk-types/{ESCAPED_TEST_RISK_TYPE}", |
| 282 |
status=200, |
| 283 |
json=TEST_PATCH_RISK_TYPE_JSON, |
| 284 |
match=[matchers.json_params_matcher(mock_patch)], |
| 285 |
) |
| 286 |
# Actual call |
| 287 |
res = self.api.patch_risk_type(TEST_RISK_TYPE, mock_patch) |
| 288 |
# Assertions |
| 289 |
assert res == TEST_PATCH_RISK_TYPE_JSON |