getmoto / moto

A library that allows you to easily mock out tests based on AWS infrastructure.

Home Page:http://docs.getmoto.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing field validation on put_events

fernando-alves opened this issue · comments

When using event bridge client, it's possible to publish events with empty require fields like DetailType and Source. If those are empty, I would expect the response to fail with an InvalidArgument error, similar to the result in aws:

[cloudshell-user@xxxx ~]$ aws events put-events --entries '[ { "Source": "com.testing.moto", "DetailType": "", "Detail": "{}", "EventBusName": "xxxxxx" } ]'
{
    "FailedEntryCount": 1,
    "Entries": [
        {
            "ErrorCode": "InvalidArgument",
            "ErrorMessage": "Parameter DetailType is not valid. Reason: DetailType is a required argument."
        }
    ]
}

I believe the current implementation checks for the presence of the fields, but not their value:

moto/moto/events/models.py

Lines 1303 to 1324 in d767a27

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.",
}
)

How to reproduce the issue:

import boto3
from moto import mock_aws


@mock_aws
def test_put_events_with_empty_detail_type():
    client = boto3.client("events", region_name="us-east-1")
    event_bus = client.create_event_bus(Name="testing-bus")
    event_bus_arn = event_bus["EventBusArn"]

    expected_response = {
        "FailedEntryCount": 1,
        "Entries": [
            {
                "ErrorCode": "InvalidArgument",
                "ErrorMessage": "Parameter DetailType is not valid. Reason: DetailType is a required argument.",
            }
        ],
    }

    response = client.put_events(
        Entries=[
            {
                "Source": "com.testing.moto",
                "DetailType": "",
                "Detail": "{}",
                "EventBusName": event_bus_arn,
            },
        ]
    )

    assert expected_response == response

This test fails with:

AssertionError: assert {'Entries': [...ntryCount': 1} == {'Entries': [...Attempts': 0}}
E
E         Differing items:
E         {'Entries': [{'ErrorCode': 'InvalidArgument', 'ErrorMessage': 'Parameter DetailType is not valid. Reason: DetailType is a required argument.'}]} != {'Entries': [{'EventId': '7f0ca1c6-5567-4702-a035-4f9a4be9f2aa'}]}
E         {'FailedEntryCount': 1} != {'FailedEntryCount': 0}
E         Right contains 1 more item:
E         {'ResponseMetadata': {'HTTPHeaders': {'date': 'Wed, 27 Mar 2024 22:55:45 GMT',
E                                               'server': 'amazon.com',...
E
E         ...Full output truncated (5 lines hidden), use '-vv' to show

test_put_events.py:32: AssertionError

What you expected to happen

I would expect the operation to fail for that entry and ErrorCode and ErrorMessage fields to be populated accordingly.

What version of Moto you're using

moto==5.0.4
boto3==1.34.72
botocore==1.34.72

@bblommers I can take a look

@rafcio19 Happy to work on a PR if we can confirm/agree that the validation should also check for empty strings.

@fernando-alves amazing, please go ahead with the PR. It should mimic AWS functionality so if that means validating empty then let's validate empty strings.

np, done!