From 4dec187d80a59cbe0d1a8b78d58dab20131516f7 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 5 Oct 2019 15:20:43 +0100 Subject: [PATCH] #1834 - Check item size in DynamoDB --- moto/dynamodb2/responses.py | 4 ++++ tests/test_dynamodb2/test_dynamodb.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index d07beefd6..e7f5f2825 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -225,6 +225,10 @@ class DynamoHandler(BaseResponse): er = 'com.amazonaws.dynamodb.v20111205#ValidationException' return self.error(er, 'Return values set to invalid value') + if len(str(item).encode('utf-8')) > 405000: + er = 'com.amazonaws.dynamodb.v20111205#ValidationException' + return self.error(er, 'Item size has exceeded the maximum allowed size') + if has_empty_keys_or_values(item): return get_empty_str_error() diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index b0952f101..4fd0fb472 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -2278,6 +2278,27 @@ def test_index_with_unknown_attributes_should_fail(): ex.exception.response['Error']['Message'].should.contain(expected_exception) +# https://github.com/spulec/moto/issues/1874 +@mock_dynamodb2 +def test_item_size_is_under_400KB(): + dynamodb = boto3.client('dynamodb', region_name='us-east-1') + + dynamodb.create_table( + TableName='moto-test', + KeySchema=[{'AttributeName': 'id', 'KeyType': 'HASH'}], + AttributeDefinitions=[{'AttributeName': 'id', 'AttributeType': 'S'}], + ProvisionedThroughput={'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1} + ) + + with assert_raises(ClientError) as ex: + large_item = 'x' * 410 * 1000 + dynamodb.put_item( + TableName='moto-test', + Item={'id': {'S': 'foo'}, 'item': {'S': large_item}}) + ex.exception.response['Error']['Code'].should.equal('ValidationException') + ex.exception.response['Error']['Message'].should.equal('Item size has exceeded the maximum allowed size') + + def _create_user_table(): client = boto3.client('dynamodb', region_name='us-east-1') client.create_table(