From f32db6e64aaa2f72cc8f8095fdfd79c9800ebf12 Mon Sep 17 00:00:00 2001 From: Mariusz Strzelecki Date: Tue, 18 Jun 2019 12:36:32 +0000 Subject: [PATCH] Raising MalformedXML exception when using boto3 client and s3.delete_objects() --- moto/s3/responses.py | 2 ++ tests/test_s3/test_s3.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 40449dbf9..704ac398c 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -566,6 +566,8 @@ class ResponseObject(_TemplateEnvironmentMixin): keys = minidom.parseString(body).getElementsByTagName('Key') deleted_names = [] error_names = [] + if len(keys) == 0: + raise MalformedXML() for k in keys: key_name = k.firstChild.nodeValue diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 697c47865..f0997b8d6 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -648,6 +648,7 @@ def test_delete_keys_with_invalid(): Key(bucket=bucket, name='file3').set_contents_from_string('abc') Key(bucket=bucket, name='file4').set_contents_from_string('abc') + # non-existing key case result = bucket.delete_keys(['abc', 'file3']) result.deleted.should.have.length_of(1) @@ -656,6 +657,17 @@ def test_delete_keys_with_invalid(): keys.should.have.length_of(3) keys[0].name.should.equal('file1') + # empty keys and boto2 client + result = bucket.delete_keys([]) + + result.deleted.should.have.length_of(0) + result.errors.should.have.length_of(0) + + # empty keys and boto3 client + with assert_raises(ClientError) as err: + boto3.client('s3').delete_objects(Bucket='foobar', Delete={'Objects': []}) + assert err.exception.response["Error"]["Code"] == "MalformedXML" + @mock_s3_deprecated def test_bucket_name_with_dot():