Merge "Return HTTP code 400 when creating an audit with wrong parameters" into stable/2025.1
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
fixes:
|
||||||
|
- |
|
||||||
|
`Bug #2110538 <https://bugs.launchpad.net/watcher/+bug/2110538>`_:
|
||||||
|
Corrected the HTTP error code returned when watcher users try to create
|
||||||
|
audits with invalid parameters. The API now correctly returns a 400 Bad
|
||||||
|
Request error.
|
||||||
@@ -33,6 +33,7 @@ import datetime
|
|||||||
from dateutil import tz
|
from dateutil import tz
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
import jsonschema
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
from oslo_utils import timeutils
|
from oslo_utils import timeutils
|
||||||
import pecan
|
import pecan
|
||||||
@@ -627,8 +628,12 @@ class AuditsController(rest.RestController):
|
|||||||
if schema:
|
if schema:
|
||||||
# validate input parameter with default value feedback
|
# validate input parameter with default value feedback
|
||||||
no_schema = False
|
no_schema = False
|
||||||
utils.StrictDefaultValidatingDraft4Validator(schema).validate(
|
try:
|
||||||
audit.parameters)
|
utils.StrictDefaultValidatingDraft4Validator(
|
||||||
|
schema).validate(audit.parameters)
|
||||||
|
except jsonschema.exceptions.ValidationError as e:
|
||||||
|
raise exception.Invalid(
|
||||||
|
_('Invalid parameters for strategy: %s') % e)
|
||||||
|
|
||||||
if no_schema and audit.parameters:
|
if no_schema and audit.parameters:
|
||||||
raise exception.Invalid(_('Specify parameters but no predefined '
|
raise exception.Invalid(_('Specify parameters but no predefined '
|
||||||
|
|||||||
@@ -843,16 +843,12 @@ class TestPost(api_base.FunctionalTest):
|
|||||||
del audit_dict[k]
|
del audit_dict[k]
|
||||||
|
|
||||||
response = self.post_json('/audits', audit_dict, expect_errors=True)
|
response = self.post_json('/audits', audit_dict, expect_errors=True)
|
||||||
# (amoralej) This should return HTTPStatus.BAD_REQUEST, however this
|
self.assertEqual(HTTPStatus.BAD_REQUEST, response.status_int)
|
||||||
# review is adding the test to show wrong code is returned. I will
|
|
||||||
# switch this to be HTTPStatus.BAD_REQUEST in the fixing review.
|
|
||||||
self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, response.status_int)
|
|
||||||
self.assertEqual("application/json", response.content_type)
|
self.assertEqual("application/json", response.content_type)
|
||||||
# (amoralej) uncomment with the fix
|
expected_error_msg = (
|
||||||
# expected_error_msg = (
|
"Invalid parameters for strategy: 'fake1' is a required property")
|
||||||
# "Invalid parameters for strategy: 'fake1' is a required property")
|
self.assertTrue(response.json['error_message'])
|
||||||
# self.assertTrue(response.json['error_message'])
|
self.assertIn(expected_error_msg, response.json['error_message'])
|
||||||
# self.assertIn(expected_error_msg, response.json['error_message'])
|
|
||||||
assert not mock_trigger_audit.called
|
assert not mock_trigger_audit.called
|
||||||
|
|
||||||
def prepare_audit_template_strategy_with_parameter(self):
|
def prepare_audit_template_strategy_with_parameter(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user