Skip to content

Commit

Permalink
Rename process_xml to extract_data_from_xml_body
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout Feys committed Aug 22, 2024
1 parent 6f3e02d commit 7337e5c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Exports process_xml helper function"""
"""Exports extract_data_from_xml_body helper function"""

import aikido_firewall.context as ctx


def process_xml(user_input, root_element):
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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from unittest.mock import MagicMock
import aikido_firewall.context as ctx
from .process_xml import (
process_xml,
from .extract_data_from_xml_body import (
extract_data_from_xml_body,
) # Replace 'your_module' with the actual module name


Expand All @@ -16,15 +16,15 @@ def mock_context():
return mock_ctx


def test_process_xml_valid_input(mock_context):
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"},
]

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

assert mock_context.xml == {
"attr1": {"value1", "value3"},
Expand All @@ -33,50 +33,50 @@ def test_process_xml_valid_input(mock_context):
}


def test_process_xml_invalid_user_input(mock_context):
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"}]

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

assert mock_context.xml == {}


def test_process_xml_empty_root_element(mock_context):
def test_extract_data_from_xml_body_empty_root_element(mock_context):
"""Test with an empty root_element."""
user_input = "valid_input"
root_element = []

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

assert mock_context.xml == {}


def test_process_xml_non_string_context_body(mock_context):
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"}]

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

assert mock_context.xml == {}


def test_process_xml_multiple_calls(mock_context):
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"}]

process_xml(user_input, root_element1)
process_xml(user_input, root_element2)
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_process_xml_duplicate_attributes(mock_context):
def test_extract_data_from_xml_body_duplicate_attributes(mock_context):
"""Test with duplicate attributes in root_element."""
user_input = "valid_input"
root_element = [
Expand All @@ -85,26 +85,26 @@ def test_process_xml_duplicate_attributes(mock_context):
{"attr2": "value2"},
]

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

assert mock_context.xml == {"attr1": {"value1"}, "attr2": {"value2"}}


def test_process_xml_no_attributes(mock_context):
def test_extract_data_from_xml_body_no_attributes(mock_context):
"""Test with elements that have no attributes."""
user_input = "valid_input"
root_element = [{}]

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

assert mock_context.xml == {}


def test_process_xml_context_set_as_current(mock_context):
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"}]

process_xml(user_input, root_element)
extract_data_from_xml_body(user_input, root_element)

mock_context.set_as_current_context.assert_called_once()
6 changes: 4 additions & 2 deletions aikido_firewall/sources/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import copy
import importhook
from aikido_firewall.helpers.logging import logger
from aikido_firewall.helpers.process_xml import process_xml
from aikido_firewall.helpers.extract_data_from_xml_body import (
extract_data_from_xml_body,
)


@importhook.on_import("xml.etree.ElementTree")
Expand Down Expand Up @@ -36,7 +38,7 @@ def feed(data):
# Fetch the data, this should just return an internal attribute and not close a stream
# Or something that is noticable by the end-user
parsed_xml = self.target.close()
process_xml(user_input=data, root_element=parsed_xml)
extract_data_from_xml_body(user_input=data, root_element=parsed_xml)

Check warning on line 41 in aikido_firewall/sources/xml.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sources/xml.py#L40-L41

Added lines #L40 - L41 were not covered by tests

return former_feed_result

Check warning on line 43 in aikido_firewall/sources/xml.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sources/xml.py#L43

Added line #L43 was not covered by tests

Expand Down

0 comments on commit 7337e5c

Please sign in to comment.