-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into publish-to-pypi
- Loading branch information
Showing
19 changed files
with
352 additions
and
52 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
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,5 +1,5 @@ | ||
""" | ||
? | ||
Exports ServiceConfig class | ||
""" | ||
|
||
from aikido_firewall.helpers.match_endpoint import match_endpoint | ||
|
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Exports extract_data_from_xml_body helper function""" | ||
|
||
import aikido_firewall.context as ctx | ||
|
||
|
||
def extract_data_from_xml_body(user_input, root_element): | ||
"""Extracts all attributes from the xml and adds them to context""" | ||
context = ctx.get_current_context() | ||
if not isinstance(context.body, str) or user_input != context.body: | ||
return | ||
|
||
extracted_xml_attrs = context.xml | ||
for el in root_element: | ||
print(extracted_xml_attrs) | ||
for k, v in el.items(): | ||
print("Key : %s, Value : %s", k, v) | ||
if not extracted_xml_attrs.get(k): | ||
extracted_xml_attrs[k] = set() | ||
extracted_xml_attrs[k].add(v) | ||
context.set_as_current_context() |
110 changes: 110 additions & 0 deletions
110
aikido_firewall/helpers/extract_data_from_xml_body_test.py
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import pytest | ||
from unittest.mock import MagicMock | ||
import aikido_firewall.context as ctx | ||
from .extract_data_from_xml_body import ( | ||
extract_data_from_xml_body, | ||
) # Replace 'your_module' with the actual module name | ||
|
||
|
||
@pytest.fixture | ||
def mock_context(): | ||
"""Fixture to mock the context.""" | ||
mock_ctx = MagicMock() | ||
mock_ctx.body = "valid_input" | ||
mock_ctx.xml = {} # Initialize with an empty dictionary | ||
ctx.get_current_context = MagicMock(return_value=mock_ctx) | ||
return mock_ctx | ||
|
||
|
||
def test_extract_data_from_xml_body_valid_input(mock_context): | ||
"""Test with valid user input and root_element.""" | ||
user_input = "valid_input" | ||
root_element = [ | ||
{"attr1": "value1", "attr2": "value2"}, | ||
{"attr1": "value3", "attr3": "value4"}, | ||
] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
assert mock_context.xml == { | ||
"attr1": {"value1", "value3"}, | ||
"attr2": {"value2"}, | ||
"attr3": {"value4"}, | ||
} | ||
|
||
|
||
def test_extract_data_from_xml_body_invalid_user_input(mock_context): | ||
"""Test with invalid user input.""" | ||
user_input = "invalid_input" | ||
root_element = [{"attr1": "value1"}] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
assert mock_context.xml == {} | ||
|
||
|
||
def test_extract_data_from_xml_body_empty_root_element(mock_context): | ||
"""Test with an empty root_element.""" | ||
user_input = "valid_input" | ||
root_element = [] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
assert mock_context.xml == {} | ||
|
||
|
||
def test_extract_data_from_xml_body_non_string_context_body(mock_context): | ||
"""Test with non-string context body.""" | ||
mock_context.body = 123 # Set body to a non-string value | ||
user_input = "valid_input" | ||
root_element = [{"attr1": "value1"}] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
assert mock_context.xml == {} | ||
|
||
|
||
def test_extract_data_from_xml_body_multiple_calls(mock_context): | ||
"""Test multiple calls with the same user input.""" | ||
user_input = "valid_input" | ||
root_element1 = [{"attr1": "value1"}] | ||
root_element2 = [{"attr1": "value2"}] | ||
|
||
extract_data_from_xml_body(user_input, root_element1) | ||
extract_data_from_xml_body(user_input, root_element2) | ||
|
||
assert mock_context.xml == {"attr1": {"value1", "value2"}} | ||
|
||
|
||
def test_extract_data_from_xml_body_duplicate_attributes(mock_context): | ||
"""Test with duplicate attributes in root_element.""" | ||
user_input = "valid_input" | ||
root_element = [ | ||
{"attr1": "value1"}, | ||
{"attr1": "value1"}, # Duplicate | ||
{"attr2": "value2"}, | ||
] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
assert mock_context.xml == {"attr1": {"value1"}, "attr2": {"value2"}} | ||
|
||
|
||
def test_extract_data_from_xml_body_no_attributes(mock_context): | ||
"""Test with elements that have no attributes.""" | ||
user_input = "valid_input" | ||
root_element = [{}] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
assert mock_context.xml == {} | ||
|
||
|
||
def test_extract_data_from_xml_body_context_set_as_current(mock_context): | ||
"""Test if context.set_as_current_context is called.""" | ||
user_input = "valid_input" | ||
root_element = [{"attr1": "value1"}] | ||
|
||
extract_data_from_xml_body(user_input, root_element) | ||
|
||
mock_context.set_as_current_context.assert_called_once() |
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
Oops, something went wrong.