5
5
from py42 .response import Py42Response
6
6
from Code42 import (
7
7
Code42Client ,
8
- Code42AlertNotFoundError ,
9
- Code42UserNotFoundError ,
10
8
Code42LegalHoldMatterNotFoundError ,
11
9
Code42InvalidLegalHoldMembershipError ,
12
10
build_query_payload ,
34
32
legal_hold_remove_user_command ,
35
33
download_file_command ,
36
34
fetch_incidents ,
35
+ Code42AlertNotFoundError ,
36
+ Code42UserNotFoundError ,
37
+ Code42OrgNotFoundError ,
38
+ Code42UnsupportedHashError ,
39
+ Code42MissingSearchArgumentsError ,
37
40
)
38
41
import time
39
42
@@ -1293,8 +1296,13 @@ def assert_detection_list_outputs_match_response_items(outputs_list, response_it
1293
1296
"""TESTS"""
1294
1297
1295
1298
1296
- def test_client_lazily_inits_sdk (mocker ):
1297
- mocker .patch ("py42.sdk.from_local_account" )
1299
+ def test_client_lazily_inits_sdk (mocker , code42_sdk_mock ):
1300
+ sdk_factory_mock = mocker .patch ("py42.sdk.from_local_account" )
1301
+ response_json_mock = """{"total": 1, "users": [{"username": "Test"}]}"""
1302
+ code42_sdk_mock .users .get_by_username .return_value = create_mock_code42_sdk_response (
1303
+ mocker , response_json_mock
1304
+ )
1305
+ sdk_factory_mock .return_value = code42_sdk_mock
1298
1306
1299
1307
# test that sdk does not init during ctor
1300
1308
client = Code42Client (sdk = None , base_url = MOCK_URL , auth = MOCK_AUTH , verify = False , proxy = False )
@@ -1305,23 +1313,28 @@ def test_client_lazily_inits_sdk(mocker):
1305
1313
assert client ._sdk is not None
1306
1314
1307
1315
1308
- def test_client_when_no_alert_found_raises_alert_not_found (code42_sdk_mock ):
1309
- code42_sdk_mock .alerts .get_details .return_value = (
1310
- json .loads ('{"type$": "ALERT_DETAILS_RESPONSE", "alerts": []}' )
1316
+ def test_client_when_no_alert_found_raises_alert_not_found (mocker , code42_sdk_mock ):
1317
+ response_json = """{"alerts": []}"""
1318
+ code42_sdk_mock .alerts .get_details .return_value = create_mock_code42_sdk_response (
1319
+ mocker , response_json
1311
1320
)
1312
1321
client = create_client (code42_sdk_mock )
1313
1322
with pytest .raises (Code42AlertNotFoundError ):
1314
1323
client .get_alert_details ("mock-id" )
1315
1324
1316
1325
1317
- def test_client_when_no_user_found_raises_user_not_found (code42_sdk_mock ):
1318
- code42_sdk_mock .users .get_by_username .return_value = json .loads ('{"totalCount":0, "users":[]}' )
1326
+ def test_client_when_no_user_found_raises_user_not_found (mocker , code42_sdk_mock ):
1327
+ response_json = """{"totalCount": 0, "users": []}"""
1328
+ code42_sdk_mock .users .get_by_username .return_value = create_mock_code42_sdk_response (
1329
+ mocker , response_json
1330
+ )
1319
1331
client = create_client (code42_sdk_mock )
1320
1332
with pytest .raises (Code42UserNotFoundError ):
1321
1333
client .
get_user (
"[email protected] " )
1322
1334
1323
1335
1324
1336
def test_client_add_to_matter_when_no_legal_hold_matter_found_raises_matter_not_found (code42_sdk_mock , mocker ):
1337
+
1325
1338
code42_sdk_mock .legalhold .get_all_matters .return_value = (
1326
1339
get_empty_legalhold_matters_response (mocker , MOCK_GET_ALL_MATTERS_RESPONSE )
1327
1340
)
@@ -1331,8 +1344,9 @@ def test_client_add_to_matter_when_no_legal_hold_matter_found_raises_matter_not_
1331
1344
client .add_user_to_legal_hold_matter ("TESTUSERNAME" , "TESTMATTERNAME" )
1332
1345
1333
1346
1334
- def test_client_add_to_matter_when_no_user_found_raises_user_not_found (code42_sdk_mock ):
1335
- code42_sdk_mock .users .get_by_username .return_value = json .loads ('{"totalCount":0, "users":[]}' )
1347
+ def test_client_add_to_matter_when_no_user_found_raises_user_not_found (mocker , code42_sdk_mock ):
1348
+ response_json = '{"totalCount":0, "users":[]}'
1349
+ code42_sdk_mock .users .get_by_username .return_value = create_mock_code42_sdk_response (mocker , response_json )
1336
1350
client = create_client (code42_sdk_mock )
1337
1351
with pytest .raises (Code42UserNotFoundError ):
1338
1352
client .add_user_to_legal_hold_matter ("TESTUSERNAME" , "TESTMATTERNAME" )
@@ -1348,8 +1362,9 @@ def test_client_remove_from_matter_when_no_legal_hold_matter_found_raises_except
1348
1362
client .remove_user_from_legal_hold_matter ("TESTUSERNAME" , "TESTMATTERNAME" )
1349
1363
1350
1364
1351
- def test_client_remove_from_matter_when_no_user_found_raises_user_not_found (code42_sdk_mock ):
1352
- code42_sdk_mock .users .get_by_username .return_value = json .loads ('{"totalCount":0, "users":[]}' )
1365
+ def test_client_remove_from_matter_when_no_user_found_raises_user_not_found (mocker , code42_sdk_mock ):
1366
+ response_json = '{"totalCount":0, "users":[]}'
1367
+ code42_sdk_mock .users .get_by_username .return_value = create_mock_code42_sdk_response (mocker , response_json )
1353
1368
client = create_client (code42_sdk_mock )
1354
1369
with pytest .raises (Code42UserNotFoundError ):
1355
1370
client .remove_user_from_legal_hold_matter ("TESTUSERNAME" , "TESTMATTERNAME" )
@@ -1725,6 +1740,23 @@ def test_user_create_command(code42_users_mock):
1725
1740
assert cmd_res .
outputs [
"Email" ]
== "[email protected] "
1726
1741
1727
1742
1743
+ def test_user_create_command_when_org_not_found_raises_org_not_found (mocker , code42_users_mock ):
1744
+ response_json = """{"total": 0, "orgs": []}"""
1745
+ code42_users_mock .orgs .get_all .return_value = create_mock_code42_sdk_response_generator (
1746
+ mocker , [response_json ]
1747
+ )
1748
+ client = create_client (code42_users_mock )
1749
+ with pytest .raises (Code42OrgNotFoundError ):
1750
+ user_create_command (
1751
+ client ,
1752
+ {
1753
+ "orgname" : _TEST_ORG_NAME ,
1754
+
1755
+
1756
+ }
1757
+ )
1758
+
1759
+
1728
1760
def test_user_block_command (code42_users_mock ):
1729
1761
client = create_client (code42_users_mock )
1730
1762
cmd_res = user_block_command (
client , {
"username" :
"[email protected] " })
@@ -1798,6 +1830,12 @@ def test_security_data_search_command(code42_file_events_mock):
1798
1830
assert output_item == mapped_event
1799
1831
1800
1832
1833
+ def test_securitydata_search_command_when_not_given_any_queryable_args_raises_error (code42_file_events_mock ):
1834
+ client = create_client (code42_file_events_mock )
1835
+ with pytest .raises (Code42MissingSearchArgumentsError ):
1836
+ securitydata_search_command (client , {})
1837
+
1838
+
1801
1839
def test_download_file_command_when_given_md5 (code42_sdk_mock , mocker ):
1802
1840
fr = mocker .patch ("Code42.fileResult" )
1803
1841
client = create_client (code42_sdk_mock )
@@ -1817,6 +1855,15 @@ def test_download_file_command_when_given_sha256(code42_sdk_mock, mocker):
1817
1855
assert fr .call_count == 1
1818
1856
1819
1857
1858
+ def test_download_file_when_given_other_hash_raises_unsupported_hash (code42_sdk_mock , mocker ):
1859
+ mocker .patch ("Code42.fileResult" )
1860
+ _hash = "41966f10cc59ab466444add08974fde4cd37f88d79321d42da8e4c79b51c214941966f10cc59ab466444add08974fde4cd37" \
1861
+ "f88d79321d42da8e4c79b51c2149"
1862
+ client = create_client (code42_sdk_mock )
1863
+ with pytest .raises (Code42UnsupportedHashError ):
1864
+ _ = download_file_command (client , {"hash" : _hash })
1865
+
1866
+
1820
1867
def test_fetch_when_no_significant_file_categories_ignores_filter (
1821
1868
code42_fetch_incidents_mock , mocker
1822
1869
):
@@ -1840,8 +1887,8 @@ def test_fetch_when_no_significant_file_categories_ignores_filter(
1840
1887
assert "IMAGE" not in actual_query
1841
1888
1842
1889
1843
- def test_fetch_incidents_handles_single_severity (code42_sdk_mock ):
1844
- client = create_client (code42_sdk_mock )
1890
+ def test_fetch_incidents_handles_single_severity (code42_fetch_incidents_mock ):
1891
+ client = create_client (code42_fetch_incidents_mock )
1845
1892
fetch_incidents (
1846
1893
client = client ,
1847
1894
last_run = {"last_fetch" : None },
@@ -1851,7 +1898,7 @@ def test_fetch_incidents_handles_single_severity(code42_sdk_mock):
1851
1898
include_files = True ,
1852
1899
integration_context = None ,
1853
1900
)
1854
- assert "HIGH" in str (code42_sdk_mock .alerts .search .call_args [0 ][0 ])
1901
+ assert "HIGH" in str (code42_fetch_incidents_mock .alerts .search .call_args [0 ][0 ])
1855
1902
1856
1903
1857
1904
def test_fetch_incidents_handles_multi_severity (code42_fetch_incidents_mock ):
0 commit comments