Add validation to put_events (#3579)

* Add validation to put_events

* Add extra test for put_events

* Fix Python 2.7 error
This commit is contained in:
Anton Grübel 2021-01-11 10:41:31 +01:00 committed by GitHub
commit 17d94f9e09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 264 additions and 29 deletions

18
moto/events/exceptions.py Normal file
View file

@ -0,0 +1,18 @@
from __future__ import unicode_literals
from moto.core.exceptions import JsonRESTError
class ResourceNotFoundException(JsonRESTError):
code = 400
def __init__(self, message):
super(ResourceNotFoundException, self).__init__(
"ResourceNotFoundException", message
)
class ValidationException(JsonRESTError):
code = 400
def __init__(self, message):
super(ValidationException, self).__init__("ValidationException", message)

View file

@ -5,6 +5,7 @@ from boto3 import Session
from moto.core.exceptions import JsonRESTError
from moto.core import ACCOUNT_ID, BaseBackend, CloudFormationModel
from moto.events.exceptions import ValidationException, ResourceNotFoundException
from moto.utilities.tagging_service import TaggingService
from uuid import uuid4
@ -359,10 +360,52 @@ class EventsBackend(BaseBackend):
if num_events < 1:
raise JsonRESTError("ValidationError", "Need at least 1 event")
elif num_events > 10:
raise JsonRESTError("ValidationError", "Can only submit 10 events at once")
# the exact error text is longer, the Value list consists of all the put events
raise ValidationException(
"1 validation error detected: "
"Value '[PutEventsRequestEntry]' at 'entries' failed to satisfy constraint: "
"Member must have length less than or equal to 10"
)
entries = []
for event in events:
if "Source" not in event:
entries.append(
{
"ErrorCode": "InvalidArgument",
"ErrorMessage": "Parameter Source is not valid. Reason: Source is a required argument.",
}
)
elif "DetailType" not in event:
entries.append(
{
"ErrorCode": "InvalidArgument",
"ErrorMessage": "Parameter DetailType is not valid. Reason: DetailType is a required argument.",
}
)
elif "Detail" not in event:
entries.append(
{
"ErrorCode": "InvalidArgument",
"ErrorMessage": "Parameter Detail is not valid. Reason: Detail is a required argument.",
}
)
else:
try:
json.loads(event["Detail"])
except ValueError: # json.JSONDecodeError exists since Python 3.5
entries.append(
{
"ErrorCode": "MalformedDetail",
"ErrorMessage": "Detail is malformed.",
}
)
continue
entries.append({"EventId": str(uuid4())})
# We dont really need to store the events yet
return [{"EventId": str(uuid4())} for _ in events]
return entries
def remove_targets(self, name, ids):
rule = self.rules.get(name)
@ -479,8 +522,8 @@ class EventsBackend(BaseBackend):
name = arn.split("/")[-1]
if name in self.rules:
return self.tagger.list_tags_for_resource(self.rules[name].arn)
raise JsonRESTError(
"ResourceNotFoundException", "An entity that you specified does not exist."
raise ResourceNotFoundException(
"Rule {0} does not exist on EventBus default.".format(name)
)
def tag_resource(self, arn, tags):
@ -488,8 +531,8 @@ class EventsBackend(BaseBackend):
if name in self.rules:
self.tagger.tag_resource(self.rules[name].arn, tags)
return {}
raise JsonRESTError(
"ResourceNotFoundException", "An entity that you specified does not exist."
raise ResourceNotFoundException(
"Rule {0} does not exist on EventBus default.".format(name)
)
def untag_resource(self, arn, tag_names):
@ -497,8 +540,8 @@ class EventsBackend(BaseBackend):
if name in self.rules:
self.tagger.untag_resource_using_names(self.rules[name].arn, tag_names)
return {}
raise JsonRESTError(
"ResourceNotFoundException", "An entity that you specified does not exist."
raise ResourceNotFoundException(
"Rule {0} does not exist on EventBus default.".format(name)
)