-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1223 from SEKOIA-IO/fix/synchronize-asset-format
Fix/synchronize asset format
- Loading branch information
Showing
4 changed files
with
111 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import pytest | ||
import json | ||
from urllib.parse import urljoin | ||
from pydantic import BaseModel | ||
from typing import List, Dict, Any | ||
|
@@ -58,6 +59,100 @@ def arguments(self): | |
community_uuid="community-1234", | ||
) | ||
|
||
def test_no_asset_found_and_create(self, requests_mock, action_instance, arguments): | ||
""" | ||
Test the SynchronizeAssetsWithAD action for the scenario where multiple assets are found, | ||
and one of them is edited while merging the others. | ||
""" | ||
# Extract configuration from the mock module | ||
base_url = action_instance.module.configuration["base_url"] | ||
api_key = action_instance.module.configuration["api_key"] | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {api_key}", | ||
} | ||
|
||
# URLs | ||
assets_url = urljoin(base_url + "/", "v2/asset-management/assets") | ||
merge_url = urljoin(base_url + "/", "v2/asset-management/assets/merge") | ||
update_url = urljoin(base_url + "/", "v2/asset-management/assets/asset-uuid-1") | ||
create_url = urljoin(base_url + "/", "v2/asset-management/assets") | ||
|
||
# Helper functions to match specific GET requests | ||
def match_asset_name(request): | ||
search_query = request.qs.get("search", [None])[0] | ||
also_search = "also_search_in_detection_properties" in request.qs | ||
return search_query == "jdoe" and not also_search | ||
|
||
def match_detection_properties(request): | ||
return request.qs.get("also_search_in_detection_properties", [None])[0] == "true" | ||
|
||
# Mock GET request for asset name search (should return one asset) | ||
requests_mock.get( | ||
assets_url, | ||
additional_matcher=match_asset_name, | ||
json={ | ||
"total": 0, | ||
"items": [], | ||
}, | ||
status_code=200, | ||
) | ||
|
||
# Mock GET request for detection properties (should return two additional assets) | ||
requests_mock.get( | ||
assets_url, | ||
additional_matcher=match_detection_properties, | ||
json={ | ||
"total": 0, | ||
"items": [], | ||
}, | ||
status_code=200, | ||
) | ||
|
||
# Mock PUT request to update the destination asset | ||
requests_mock.post( | ||
create_url, | ||
json={"uuid": "asset_uuid_in_response"}, # Assume successful update with empty response | ||
status_code=200, | ||
) | ||
|
||
# Execute the action | ||
response = action_instance.run(arguments) | ||
|
||
# Assertions | ||
assert response["created_asset"] is True, "Asset should not be created since it exists." | ||
assert response["destination_asset"] == "asset_uuid_in_response", "Destination asset UUID mismatch." | ||
assert set(response["found_assets"]) == set(), "Found assets mismatch." | ||
|
||
# Verify that the correct number of requests were made | ||
# Expected Requests: | ||
# 1. GET asset name search | ||
# 2. 2 GET for all the detection properties search | ||
# 3. PUT update asset | ||
# 4. POST merge assets | ||
assert ( | ||
len(requests_mock.request_history) == 4 | ||
), f"Expected 4 HTTP requests, got {len(requests_mock.request_history)}." | ||
|
||
# Optionally, verify the payloads of PUT and POST requests | ||
# Verify PUT request payload | ||
post_requests = [req for req in requests_mock.request_history if req.method == "POST"] | ||
assert len(post_requests) == 1, "Expected one POST request." | ||
post_request = post_requests[0] | ||
expected_post_payload = { | ||
"name": "jdoe", | ||
"description": "", | ||
"type": "account", | ||
"category": "user", | ||
"reviewed": True, | ||
"source": "manual", | ||
"props": {"dept": "engineering"}, | ||
"atoms": {"email": ["[email protected]"], "department": ["engineering"]}, | ||
"community_uuid": "community-1234", | ||
} | ||
assert post_request.json() == json.dumps(expected_post_payload), "POST request payload mismatch." | ||
|
||
def test_on_asset_found_and_merge(self, requests_mock, action_instance, arguments): | ||
""" | ||
Test the SynchronizeAssetsWithAD action for the scenario where multiple assets are found, | ||
|
@@ -147,12 +242,9 @@ def match_detection_properties(request): | |
"reviewed": True, | ||
"source": "manual", | ||
"props": {"dept": "engineering"}, | ||
"atoms": [ | ||
{"email": ["[email protected]"]}, | ||
{"department": ["engineering"]}, | ||
], | ||
"atoms": {"email": ["[email protected]"], "department": ["engineering"]}, | ||
} | ||
assert put_request.json() == expected_put_payload, "PUT request payload mismatch." | ||
assert put_request.json() == json.dumps(expected_put_payload), "PUT request payload mismatch." | ||
|
||
def test_multiple_assets_found_and_merge(self, requests_mock, action_instance, arguments): | ||
""" | ||
|
@@ -257,12 +349,9 @@ def match_detection_properties(request): | |
"reviewed": True, | ||
"source": "manual", | ||
"props": {"dept": "engineering"}, | ||
"atoms": [ | ||
{"email": ["[email protected]"]}, | ||
{"department": ["engineering"]}, | ||
], | ||
"atoms": {"email": ["[email protected]"], "department": ["engineering"]}, | ||
} | ||
assert put_request.json() == expected_put_payload, "PUT request payload mismatch." | ||
assert put_request.json() == json.dumps(expected_put_payload), "PUT request payload mismatch." | ||
|
||
# Verify POST merge request payload | ||
post_merge_requests = [ | ||
|