From 774a764b698fbf50dffd58f6518a344fef73af76 Mon Sep 17 00:00:00 2001 From: usmangani1 Date: Tue, 12 May 2020 19:29:07 +0530 Subject: [PATCH] Fix s3 Added Error handling in case of invalid uploadID (#2979) * Added Error handling in case of invalid uploadID * Linting * added assertions * Linting Co-authored-by: usmankb Co-authored-by: Bert Blommers --- moto/s3/exceptions.py | 9 +++++++++ moto/s3/models.py | 4 ++++ tests/test_s3/test_s3.py | 13 +++++++++++++ 3 files changed, 26 insertions(+) diff --git a/moto/s3/exceptions.py b/moto/s3/exceptions.py index c38a4f467..3ed385f1c 100644 --- a/moto/s3/exceptions.py +++ b/moto/s3/exceptions.py @@ -377,3 +377,12 @@ class NoSystemTags(S3ClientError): super(NoSystemTags, self).__init__( "InvalidTag", "System tags cannot be added/updated by requester" ) + + +class NoSuchUpload(S3ClientError): + code = 404 + + def __init__(self): + super(NoSuchUpload, self).__init__( + "NoSuchUpload", "The specified multipart upload does not exist." + ) diff --git a/moto/s3/models.py b/moto/s3/models.py index 866c5d007..3020fd45e 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -40,6 +40,7 @@ from .exceptions import ( NoSuchPublicAccessBlockConfiguration, InvalidPublicAccessBlockConfiguration, WrongPublicAccessBlockAccountIdError, + NoSuchUpload, ) from .utils import clean_key_name, _VersionedKeyStore @@ -1478,6 +1479,9 @@ class S3Backend(BaseBackend): def cancel_multipart(self, bucket_name, multipart_id): bucket = self.get_bucket(bucket_name) + multipart_data = bucket.multiparts.get(multipart_id, None) + if not multipart_data: + raise NoSuchUpload() del bucket.multiparts[multipart_id] def list_multipart(self, bucket_name, multipart_id): diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index f60e0293e..bcb9da87f 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -2149,6 +2149,19 @@ def test_boto3_copy_object_with_versioning(): data.should.equal(b"test2") +@mock_s3 +def test_s3_abort_multipart_data_with_invalid_upload_and_key(): + client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + + client.create_bucket(Bucket="blah") + + with assert_raises(Exception) as err: + client.abort_multipart_upload( + Bucket="blah", Key="foobar", UploadId="dummy_upload_id" + ) + err.exception.response["Error"]["Code"].should.equal("NoSuchUpload") + + @mock_s3 def test_boto3_copy_object_from_unversioned_to_versioned_bucket(): client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)