From 323f720cb5234da5d0620c6d790a4cb7baf778ee Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Fri, 7 Aug 2015 22:43:24 -0400 Subject: [PATCH] Fix S3 bucket policy delete. Closes #396. --- moto/s3/models.py | 4 ++++ moto/s3/responses.py | 29 ++++++++++++++++------------- tests/test_s3/test_s3.py | 5 +++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/moto/s3/models.py b/moto/s3/models.py index 4a4601d3c..5d0bfce2d 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -276,6 +276,10 @@ class S3Backend(BaseBackend): def set_bucket_policy(self, bucket_name, policy): self.get_bucket(bucket_name).policy = policy + def delete_bucket_policy(self, bucket_name, body): + bucket = self.get_bucket(bucket_name) + bucket.policy = None + def set_bucket_lifecycle(self, bucket_name, rules): bucket = self.get_bucket(bucket_name) bucket.set_lifecycle(rules) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 551e22534..68d1bb318 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -59,14 +59,22 @@ class ResponseObject(_TemplateEnvironmentMixin): # If no bucket specified, list all buckets return self.all_buckets() + if hasattr(request, 'body'): + # Boto + body = request.body + else: + # Flask server + body = request.data + body = body.decode('utf-8') + if method == 'HEAD': return self._bucket_response_head(bucket_name, headers) elif method == 'GET': return self._bucket_response_get(bucket_name, querystring, headers) elif method == 'PUT': - return self._bucket_response_put(request, region_name, bucket_name, querystring, headers) + return self._bucket_response_put(body, region_name, bucket_name, querystring, headers) elif method == 'DELETE': - return self._bucket_response_delete(bucket_name, querystring, headers) + return self._bucket_response_delete(body, bucket_name, querystring, headers) elif method == 'POST': return self._bucket_response_post(request, bucket_name, headers) else: @@ -149,15 +157,7 @@ class ResponseObject(_TemplateEnvironmentMixin): result_folders=result_folders ) - def _bucket_response_put(self, request, region_name, bucket_name, querystring, headers): - if hasattr(request, 'body'): - # Boto - body = request.body - else: - # Flask server - body = request.data - body = body.decode('utf-8') - + def _bucket_response_put(self, body, region_name, bucket_name, querystring, headers): if 'versioning' in querystring: ver = re.search('([A-Za-z]+)', body) if ver: @@ -188,8 +188,11 @@ class ResponseObject(_TemplateEnvironmentMixin): template = self.response_template(S3_BUCKET_CREATE_RESPONSE) return 200, headers, template.render(bucket=new_bucket) - def _bucket_response_delete(self, bucket_name, querystring, headers): - if 'lifecycle' in querystring: + def _bucket_response_delete(self, body, bucket_name, querystring, headers): + if 'policy' in querystring: + self.backend.delete_bucket_policy(bucket_name, body) + return 204, headers, "" + elif 'lifecycle' in querystring: bucket = self.backend.get_bucket(bucket_name) bucket.delete_lifecycle() return 204, headers, "" diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 2b9fb7099..75a1348a7 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -871,6 +871,11 @@ def test_policy(): bucket.get_policy().decode('utf-8').should.equal(policy) + bucket.delete_policy() + + with assert_raises(S3ResponseError) as err: + bucket.get_policy() + """ boto3