From f32db6e64aaa2f72cc8f8095fdfd79c9800ebf12 Mon Sep 17 00:00:00 2001 From: Mariusz Strzelecki Date: Tue, 18 Jun 2019 12:36:32 +0000 Subject: [PATCH 1/2] 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(): From 51d96ae8f38b16d96f780b597a40d45a06881c7d Mon Sep 17 00:00:00 2001 From: Mariusz Strzelecki Date: Tue, 23 Jul 2019 20:53:45 +0200 Subject: [PATCH 2/2] Test split into boto and boto3 part --- tests/test_s3/test_s3.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index f0997b8d6..2786ff38a 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -639,7 +639,7 @@ def test_delete_keys(): @mock_s3_deprecated -def test_delete_keys_with_invalid(): +def test_delete_keys_invalid(): conn = boto.connect_s3('the_key', 'the_secret') bucket = conn.create_bucket('foobar') @@ -657,13 +657,14 @@ def test_delete_keys_with_invalid(): keys.should.have.length_of(3) keys[0].name.should.equal('file1') - # empty keys and boto2 client + # empty keys result = bucket.delete_keys([]) result.deleted.should.have.length_of(0) result.errors.should.have.length_of(0) - # empty keys and boto3 client +@mock_s3 +def test_boto3_delete_empty_keys_list(): with assert_raises(ClientError) as err: boto3.client('s3').delete_objects(Bucket='foobar', Delete={'Objects': []}) assert err.exception.response["Error"]["Code"] == "MalformedXML"