Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation to engine dictionary #915

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 25 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,31 @@ 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):
_ = RestlerSettings({})
candidate_values = primitives.CandidateValuesPool()
marina-p marked this conversation as resolved.
Show resolved Hide resolved

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
Loading