Merge branch 'master' of github.com:spulec/moto
Conflicts: moto/s3/models.py moto/s3/responses.py
This commit is contained in:
commit
f25caa872d
113 changed files with 4360 additions and 1446 deletions
|
|
@ -4,9 +4,10 @@ from io import BytesIO
|
|||
import boto
|
||||
from boto.exception import S3ResponseError
|
||||
from boto.s3.key import Key
|
||||
from freezegun import freeze_time
|
||||
import requests
|
||||
|
||||
import sure # flake8: noqa
|
||||
import sure # noqa
|
||||
|
||||
from moto import mock_s3
|
||||
|
||||
|
|
@ -83,6 +84,31 @@ def test_empty_key():
|
|||
bucket.get_key("the-key").get_contents_as_string().should.equal('')
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_empty_key_set_on_existing_key():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
bucket = conn.create_bucket("foobar")
|
||||
key = Key(bucket)
|
||||
key.key = "the-key"
|
||||
key.set_contents_from_string("foobar")
|
||||
|
||||
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar')
|
||||
|
||||
key.set_contents_from_string("")
|
||||
bucket.get_key("the-key").get_contents_as_string().should.equal('')
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_large_key_save():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
bucket = conn.create_bucket("foobar")
|
||||
key = Key(bucket)
|
||||
key.key = "the-key"
|
||||
key.set_contents_from_string("foobar" * 100000)
|
||||
|
||||
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar' * 100000)
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_copy_key():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
|
|
@ -98,39 +124,31 @@ def test_copy_key():
|
|||
|
||||
|
||||
@mock_s3
|
||||
def test_get_all_keys():
|
||||
def test_set_metadata():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
bucket = conn.create_bucket("foobar")
|
||||
key = Key(bucket)
|
||||
key.key = 'the-key'
|
||||
key.set_metadata('md', 'Metadatastring')
|
||||
key.set_contents_from_string("Testval")
|
||||
|
||||
bucket.get_key('the-key').get_metadata('md').should.equal('Metadatastring')
|
||||
|
||||
|
||||
@freeze_time("2012-01-01 12:00:00")
|
||||
@mock_s3
|
||||
def test_last_modified():
|
||||
# See https://github.com/boto/boto/issues/466
|
||||
conn = boto.connect_s3()
|
||||
bucket = conn.create_bucket("foobar")
|
||||
key = Key(bucket)
|
||||
key.key = "the-key"
|
||||
key.set_contents_from_string("some value")
|
||||
|
||||
key2 = Key(bucket)
|
||||
key2.key = "folder/some-stuff"
|
||||
key2.set_contents_from_string("some value")
|
||||
rs = bucket.get_all_keys()
|
||||
rs[0].last_modified.should.equal('2012-01-01T12:00:00Z')
|
||||
|
||||
key3 = Key(bucket)
|
||||
key3.key = "folder/more-folder/foobar"
|
||||
key3.set_contents_from_string("some value")
|
||||
|
||||
key4 = Key(bucket)
|
||||
key4.key = "a-key"
|
||||
key4.set_contents_from_string("some value")
|
||||
|
||||
keys = bucket.get_all_keys()
|
||||
keys.should.have.length_of(3)
|
||||
|
||||
keys[0].name.should.equal("a-key")
|
||||
keys[1].name.should.equal("the-key")
|
||||
|
||||
# Prefix
|
||||
keys[2].name.should.equal("folder")
|
||||
|
||||
keys = bucket.get_all_keys(prefix="folder/")
|
||||
keys.should.have.length_of(2)
|
||||
|
||||
keys[0].name.should.equal("folder/some-stuff")
|
||||
keys[1].name.should.equal("folder/more-folder")
|
||||
bucket.get_key("the-key").last_modified.should.equal('Sun, 01 Jan 2012 12:00:00 GMT')
|
||||
|
||||
|
||||
@mock_s3
|
||||
|
|
@ -177,11 +195,102 @@ def test_get_all_buckets():
|
|||
buckets.should.have.length_of(2)
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_post_to_bucket():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
bucket = conn.create_bucket("foobar")
|
||||
|
||||
requests.post("https://foobar.s3.amazonaws.com/", {
|
||||
'key': 'the-key',
|
||||
'file': 'nothing'
|
||||
})
|
||||
|
||||
bucket.get_key('the-key').get_contents_as_string().should.equal('nothing')
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_post_with_metadata_to_bucket():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
bucket = conn.create_bucket("foobar")
|
||||
|
||||
requests.post("https://foobar.s3.amazonaws.com/", {
|
||||
'key': 'the-key',
|
||||
'file': 'nothing',
|
||||
'x-amz-meta-test': 'metadata'
|
||||
})
|
||||
|
||||
bucket.get_key('the-key').get_metadata('test').should.equal('metadata')
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_bucket_method_not_implemented():
|
||||
requests.post.when.called_with("https://foobar.s3.amazonaws.com/").should.throw(NotImplementedError)
|
||||
requests.patch.when.called_with("https://foobar.s3.amazonaws.com/").should.throw(NotImplementedError)
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_key_method_not_implemented():
|
||||
requests.post.when.called_with("https://foobar.s3.amazonaws.com/foo").should.throw(NotImplementedError)
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_bucket_name_with_dot():
|
||||
conn = boto.connect_s3()
|
||||
bucket = conn.create_bucket('firstname.lastname')
|
||||
|
||||
k = Key(bucket, 'somekey')
|
||||
k.set_contents_from_string('somedata')
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_key_with_special_characters():
|
||||
conn = boto.connect_s3()
|
||||
bucket = conn.create_bucket('test_bucket_name')
|
||||
|
||||
key = Key(bucket, 'test_list_keys_2/x?y')
|
||||
key.set_contents_from_string('value1')
|
||||
|
||||
key_list = bucket.list('test_list_keys_2/', '/')
|
||||
keys = [x for x in key_list]
|
||||
keys[0].name.should.equal("test_list_keys_2/x?y")
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_bucket_key_listing_order():
|
||||
conn = boto.connect_s3()
|
||||
bucket = conn.create_bucket('test_bucket')
|
||||
prefix = 'toplevel/'
|
||||
|
||||
def store(name):
|
||||
k = Key(bucket, prefix + name)
|
||||
k.set_contents_from_string('somedata')
|
||||
|
||||
names = ['x/key', 'y.key1', 'y.key2', 'y.key3', 'x/y/key', 'x/y/z/key']
|
||||
|
||||
for name in names:
|
||||
store(name)
|
||||
|
||||
delimiter = None
|
||||
keys = [x.name for x in bucket.list(prefix, delimiter)]
|
||||
keys.should.equal([
|
||||
'toplevel/x/key', 'toplevel/x/y/key', 'toplevel/x/y/z/key',
|
||||
'toplevel/y.key1', 'toplevel/y.key2', 'toplevel/y.key3'
|
||||
])
|
||||
|
||||
delimiter = '/'
|
||||
keys = [x.name for x in bucket.list(prefix, delimiter)]
|
||||
keys.should.equal([
|
||||
'toplevel/y.key1', 'toplevel/y.key2', 'toplevel/y.key3', 'toplevel/x/'
|
||||
])
|
||||
|
||||
# Test delimiter with no prefix
|
||||
delimiter = '/'
|
||||
keys = [x.name for x in bucket.list(prefix=None, delimiter=delimiter)]
|
||||
keys.should.equal(['toplevel'])
|
||||
|
||||
delimiter = None
|
||||
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
|
||||
keys.should.equal([u'toplevel/x/key', u'toplevel/x/y/key', u'toplevel/x/y/z/key'])
|
||||
|
||||
delimiter = '/'
|
||||
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
|
||||
keys.should.equal([u'toplevel/x/'])
|
||||
|
|
|
|||
14
tests/test_s3/test_s3_utils.py
Normal file
14
tests/test_s3/test_s3_utils.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from sure import expect
|
||||
from moto.s3.utils import bucket_name_from_url
|
||||
|
||||
|
||||
def test_base_url():
|
||||
expect(bucket_name_from_url('https://s3.amazonaws.com/')).should.equal(None)
|
||||
|
||||
|
||||
def test_localhost_bucket():
|
||||
expect(bucket_name_from_url('https://wfoobar.localhost:5000/abc')).should.equal("wfoobar")
|
||||
|
||||
|
||||
def test_localhost_without_bucket():
|
||||
expect(bucket_name_from_url('https://www.localhost:5000/def')).should.equal(None)
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import sure # flake8: noqa
|
||||
import sure # noqa
|
||||
|
||||
import moto.server as server
|
||||
|
||||
|
|
@ -33,3 +33,18 @@ def test_s3_server_bucket_create():
|
|||
res = test_client.get('/bar', 'http://foobar.localhost:5000/')
|
||||
res.status_code.should.equal(200)
|
||||
res.data.should.equal("test value")
|
||||
|
||||
|
||||
def test_s3_server_post_to_bucket():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.put('/', 'http://foobar.localhost:5000/')
|
||||
res.status_code.should.equal(200)
|
||||
|
||||
test_client.post('/', "https://foobar.localhost:5000/", data={
|
||||
'key': 'the-key',
|
||||
'file': 'nothing'
|
||||
})
|
||||
|
||||
res = test_client.get('/the-key', 'http://foobar.localhost:5000/')
|
||||
res.status_code.should.equal(200)
|
||||
res.data.should.equal("nothing")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue