From ba360daa0f7b386f88b8368ca1cb91cad111b90d Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Mon, 18 Feb 2013 17:31:15 -0500 Subject: [PATCH] add get_all_buckets --- moto/s3/models.py | 5 ++++- moto/s3/responses.py | 25 ++++++++++++++++++++++++- tests/test_s3/test_s3.py | 10 ++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/moto/s3/models.py b/moto/s3/models.py index 65b191620..45598a583 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -23,7 +23,7 @@ class FakeBucket(object): class S3Backend(BaseBackend): - base_url = "https://(.+).s3.amazonaws.com" + base_url = "https://(.*)s3.amazonaws.com" def __init__(self): self.buckets = {} @@ -33,6 +33,9 @@ class S3Backend(BaseBackend): self.buckets[bucket_name] = new_bucket return new_bucket + def get_all_buckets(self): + return self.buckets.values() + def get_bucket(self, bucket_name): return self.buckets.get(bucket_name) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index fff5beb7e..e877a4d97 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -4,9 +4,17 @@ from .models import s3_backend def bucket_response(uri, body, headers): hostname = uri.hostname - bucket_name = hostname.replace(".s3.amazonaws.com", "") method = uri.method + s3_base_url = "s3.amazonaws.com" + if hostname == s3_base_url: + # No bucket specified. Listing all buckets + all_buckets = s3_backend.get_all_buckets() + template = Template(S3_ALL_BUCKETS) + return template.render(buckets=all_buckets) + + bucket_name = hostname.replace(".s3.amazonaws.com", "") + if method == 'GET': bucket = s3_backend.get_bucket(bucket_name) if bucket: @@ -66,6 +74,21 @@ def key_response(uri_info, body, headers): import pdb;pdb.set_trace() +S3_ALL_BUCKETS = """ + + bcaf1ffd86f41161ca5fb16fd081034f + webfile + + + {% for bucket in buckets %} + + {{ bucket.name }} + 2006-02-03T16:45:09.000Z + + {% endfor %} + +""" + S3_BUCKET_GET_RESPONSE = """\ {{ bucket.name }}\ notes/\ diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 08439e6f9..49ffb38fa 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -53,3 +53,13 @@ def test_bucket_deletion(): conn.delete_bucket("foobar") conn.get_bucket.when.called_with("foobar").should.throw(S3ResponseError) + + +@mock_s3 +def test_get_all_buckets(): + conn = boto.connect_s3('the_key', 'the_secret') + bucket = conn.create_bucket("foobar") + bucket = conn.create_bucket("foobar2") + buckets = conn.get_all_buckets() + + buckets.should.have.length_of(2)