Skip to content

Commit

Permalink
Add validation to engine dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
bregwin committed Nov 13, 2024
1 parent 90c39b8 commit dc85c39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions restler/engine/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ def set_candidate_values(self, custom_values, per_endpoint_custom_mutations=None
@rtype : None
"""

def validate_string_values(dictionary):
""" Validates that all values in the provided dictionary are strings. """
for key, values in dictionary.items():
if not all(isinstance(value, str) for value in values):
raise ValueError(f"All values for '{key}' must be strings, but found: {values}")

validate_string_values(custom_values)

# Set default primitives
self.candidate_values = self._set_custom_values(self.candidate_values, custom_values)
if not self._dates_added:
Expand Down
24 changes: 24 additions & 0 deletions restler/unit_tests/test_restler_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,30 @@ def test_checkers_in_settings(self):
self.assertEqual(None, settings.get_checker_arg('leakage_rule_mode', 'mode'))
self.assertEqual(None, settings.get_checker_arg('resource_hierarchy_rule_mode', 'mode'))

def test_validate_string_values_in_candidate_values(self):
candidate_values = primitives.CandidateValuesPool()

invalid_dict = {
"restler_fuzzable_int": [
1 # Invalid as it's not a string
]
}

with self.assertRaises(ValueError):
candidate_values.set_candidate_values(invalid_dict)

valid_dict = {
"restler_fuzzable_int": [
"1" # Valid as it's a string
]
}

# No error should since input is valid
try:
candidate_values.set_candidate_values(valid_dict)
except ValueError as e:
self.fail(f"ValueError raised unexpectedly: {e}")

def test_invalid_entries(self):
user_args = {
'max_sequence_length': '10'
Expand Down

0 comments on commit dc85c39

Please sign in to comment.