Run black on moto & test directories.

This commit is contained in:
Asher Foa 2019-10-31 08:44:26 -07:00
commit 96e5b1993d
507 changed files with 52541 additions and 47814 deletions

View file

@ -4,16 +4,16 @@ import sure # noqa
from flask.testing import FlaskClient
import moto.server as server
'''
"""
Test the different server responses
'''
"""
class AuthenticatedClient(FlaskClient):
def open(self, *args, **kwargs):
kwargs['headers'] = kwargs.get('headers', {})
kwargs['headers']['Authorization'] = "Any authorization header"
kwargs['content_length'] = 0 # Fixes content-length complaints.
kwargs["headers"] = kwargs.get("headers", {})
kwargs["headers"]["Authorization"] = "Any authorization header"
kwargs["content_length"] = 0 # Fixes content-length complaints.
return super(AuthenticatedClient, self).open(*args, **kwargs)
@ -26,42 +26,41 @@ def authenticated_client():
def test_s3_server_get():
test_client = authenticated_client()
res = test_client.get('/')
res = test_client.get("/")
res.data.should.contain(b'ListAllMyBucketsResult')
res.data.should.contain(b"ListAllMyBucketsResult")
def test_s3_server_bucket_create():
test_client = authenticated_client()
res = test_client.put('/foobar', 'http://localhost:5000')
res = test_client.put("/foobar", "http://localhost:5000")
res.status_code.should.equal(200)
res = test_client.get('/')
res.data.should.contain(b'<Name>foobar</Name>')
res = test_client.get("/")
res.data.should.contain(b"<Name>foobar</Name>")
res = test_client.get('/foobar', 'http://localhost:5000')
res = test_client.get("/foobar", "http://localhost:5000")
res.status_code.should.equal(200)
res.data.should.contain(b"ListBucketResult")
res = test_client.put('/foobar2/', 'http://localhost:5000')
res = test_client.put("/foobar2/", "http://localhost:5000")
res.status_code.should.equal(200)
res = test_client.get('/')
res.data.should.contain(b'<Name>foobar2</Name>')
res = test_client.get("/")
res.data.should.contain(b"<Name>foobar2</Name>")
res = test_client.get('/foobar2/', 'http://localhost:5000')
res = test_client.get("/foobar2/", "http://localhost:5000")
res.status_code.should.equal(200)
res.data.should.contain(b"ListBucketResult")
res = test_client.get('/missing-bucket', 'http://localhost:5000')
res = test_client.get("/missing-bucket", "http://localhost:5000")
res.status_code.should.equal(404)
res = test_client.put(
'/foobar/bar', 'http://localhost:5000', data='test value')
res = test_client.put("/foobar/bar", "http://localhost:5000", data="test value")
res.status_code.should.equal(200)
res = test_client.get('/foobar/bar', 'http://localhost:5000')
res = test_client.get("/foobar/bar", "http://localhost:5000")
res.status_code.should.equal(200)
res.data.should.equal(b"test value")
@ -69,15 +68,16 @@ def test_s3_server_bucket_create():
def test_s3_server_post_to_bucket():
test_client = authenticated_client()
res = test_client.put('/foobar2', 'http://localhost:5000/')
res = test_client.put("/foobar2", "http://localhost:5000/")
res.status_code.should.equal(200)
test_client.post('/foobar2', "https://localhost:5000/", data={
'key': 'the-key',
'file': 'nothing'
})
test_client.post(
"/foobar2",
"https://localhost:5000/",
data={"key": "the-key", "file": "nothing"},
)
res = test_client.get('/foobar2/the-key', 'http://localhost:5000/')
res = test_client.get("/foobar2/the-key", "http://localhost:5000/")
res.status_code.should.equal(200)
res.data.should.equal(b"nothing")
@ -85,15 +85,14 @@ def test_s3_server_post_to_bucket():
def test_s3_server_put_ipv6():
test_client = authenticated_client()
res = test_client.put('/foobar2', 'http://[::]:5000/')
res = test_client.put("/foobar2", "http://[::]:5000/")
res.status_code.should.equal(200)
test_client.post('/foobar2', "https://[::]:5000/", data={
'key': 'the-key',
'file': 'nothing'
})
test_client.post(
"/foobar2", "https://[::]:5000/", data={"key": "the-key", "file": "nothing"}
)
res = test_client.get('/foobar2/the-key', 'http://[::]:5000/')
res = test_client.get("/foobar2/the-key", "http://[::]:5000/")
res.status_code.should.equal(200)
res.data.should.equal(b"nothing")
@ -101,14 +100,15 @@ def test_s3_server_put_ipv6():
def test_s3_server_put_ipv4():
test_client = authenticated_client()
res = test_client.put('/foobar2', 'http://127.0.0.1:5000/')
res = test_client.put("/foobar2", "http://127.0.0.1:5000/")
res.status_code.should.equal(200)
test_client.post('/foobar2', "https://127.0.0.1:5000/", data={
'key': 'the-key',
'file': 'nothing'
})
test_client.post(
"/foobar2",
"https://127.0.0.1:5000/",
data={"key": "the-key", "file": "nothing"},
)
res = test_client.get('/foobar2/the-key', 'http://127.0.0.1:5000/')
res = test_client.get("/foobar2/the-key", "http://127.0.0.1:5000/")
res.status_code.should.equal(200)
res.data.should.equal(b"nothing")

View file

@ -20,14 +20,13 @@ def create_connection(key=None, secret=None):
class MyModel(object):
def __init__(self, name, value):
self.name = name
self.value = value
def save(self):
conn = create_connection('the_key', 'the_secret')
bucket = conn.get_bucket('mybucket')
conn = create_connection("the_key", "the_secret")
bucket = conn.get_bucket("mybucket")
k = Key(bucket)
k.key = self.name
k.set_contents_from_string(self.value)
@ -36,96 +35,95 @@ class MyModel(object):
@mock_s3_deprecated
def test_my_model_save():
# Create Bucket so that test can run
conn = create_connection('the_key', 'the_secret')
conn.create_bucket('mybucket')
conn = create_connection("the_key", "the_secret")
conn.create_bucket("mybucket")
####################################
model_instance = MyModel('steve', 'is awesome')
model_instance = MyModel("steve", "is awesome")
model_instance.save()
conn.get_bucket('mybucket').get_key(
'steve').get_contents_as_string().should.equal(b'is awesome')
conn.get_bucket("mybucket").get_key("steve").get_contents_as_string().should.equal(
b"is awesome"
)
@mock_s3_deprecated
def test_missing_key():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
bucket.get_key("the-key").should.equal(None)
@mock_s3_deprecated
def test_missing_key_urllib2():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
conn.create_bucket("foobar")
urlopen.when.called_with(
"http://s3.amazonaws.com/foobar/the-key").should.throw(HTTPError)
urlopen.when.called_with("http://s3.amazonaws.com/foobar/the-key").should.throw(
HTTPError
)
@mock_s3_deprecated
def test_empty_key():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
key = Key(bucket)
key.key = "the-key"
key.set_contents_from_string("")
bucket.get_key("the-key").get_contents_as_string().should.equal(b'')
bucket.get_key("the-key").get_contents_as_string().should.equal(b"")
@mock_s3_deprecated
def test_empty_key_set_on_existing_key():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("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(b'foobar')
bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar")
key.set_contents_from_string("")
bucket.get_key("the-key").get_contents_as_string().should.equal(b'')
bucket.get_key("the-key").get_contents_as_string().should.equal(b"")
@mock_s3_deprecated
def test_large_key_save():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("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(b'foobar' * 100000)
bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar" * 100000)
@mock_s3_deprecated
def test_copy_key():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
key = Key(bucket)
key.key = "the-key"
key.set_contents_from_string("some value")
bucket.copy_key('new-key', 'foobar', 'the-key')
bucket.copy_key("new-key", "foobar", "the-key")
bucket.get_key(
"the-key").get_contents_as_string().should.equal(b"some value")
bucket.get_key(
"new-key").get_contents_as_string().should.equal(b"some value")
bucket.get_key("the-key").get_contents_as_string().should.equal(b"some value")
bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value")
@mock_s3_deprecated
def test_set_metadata():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
key = Key(bucket)
key.key = 'the-key'
key.set_metadata('md', 'Metadatastring')
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')
bucket.get_key("the-key").get_metadata("md").should.equal("Metadatastring")
@freeze_time("2012-01-01 12:00:00")
@ -139,28 +137,28 @@ def test_last_modified():
key.set_contents_from_string("some value")
rs = bucket.get_all_keys()
rs[0].last_modified.should.equal('2012-01-01T12:00:00.000Z')
rs[0].last_modified.should.equal("2012-01-01T12:00:00.000Z")
bucket.get_key(
"the-key").last_modified.should.equal('Sun, 01 Jan 2012 12:00:00 GMT')
bucket.get_key("the-key").last_modified.should.equal(
"Sun, 01 Jan 2012 12:00:00 GMT"
)
@mock_s3_deprecated
def test_missing_bucket():
conn = create_connection('the_key', 'the_secret')
conn.get_bucket.when.called_with('mybucket').should.throw(S3ResponseError)
conn = create_connection("the_key", "the_secret")
conn.get_bucket.when.called_with("mybucket").should.throw(S3ResponseError)
@mock_s3_deprecated
def test_bucket_with_dash():
conn = create_connection('the_key', 'the_secret')
conn.get_bucket.when.called_with(
'mybucket-test').should.throw(S3ResponseError)
conn = create_connection("the_key", "the_secret")
conn.get_bucket.when.called_with("mybucket-test").should.throw(S3ResponseError)
@mock_s3_deprecated
def test_bucket_deletion():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
key = Key(bucket)
@ -182,7 +180,7 @@ def test_bucket_deletion():
@mock_s3_deprecated
def test_get_all_buckets():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
conn.create_bucket("foobar")
conn.create_bucket("foobar2")
buckets = conn.get_all_buckets()
@ -193,50 +191,48 @@ def test_get_all_buckets():
@mock_s3
@mock_s3_deprecated
def test_post_to_bucket():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
requests.post("https://s3.amazonaws.com/foobar", {
'key': 'the-key',
'file': 'nothing'
})
requests.post(
"https://s3.amazonaws.com/foobar", {"key": "the-key", "file": "nothing"}
)
bucket.get_key('the-key').get_contents_as_string().should.equal(b'nothing')
bucket.get_key("the-key").get_contents_as_string().should.equal(b"nothing")
@mock_s3
@mock_s3_deprecated
def test_post_with_metadata_to_bucket():
conn = create_connection('the_key', 'the_secret')
conn = create_connection("the_key", "the_secret")
bucket = conn.create_bucket("foobar")
requests.post("https://s3.amazonaws.com/foobar", {
'key': 'the-key',
'file': 'nothing',
'x-amz-meta-test': 'metadata'
})
requests.post(
"https://s3.amazonaws.com/foobar",
{"key": "the-key", "file": "nothing", "x-amz-meta-test": "metadata"},
)
bucket.get_key('the-key').get_metadata('test').should.equal('metadata')
bucket.get_key("the-key").get_metadata("test").should.equal("metadata")
@mock_s3_deprecated
def test_bucket_name_with_dot():
conn = create_connection()
bucket = conn.create_bucket('firstname.lastname')
bucket = conn.create_bucket("firstname.lastname")
k = Key(bucket, 'somekey')
k.set_contents_from_string('somedata')
k = Key(bucket, "somekey")
k.set_contents_from_string("somedata")
@mock_s3_deprecated
def test_key_with_special_characters():
conn = create_connection()
bucket = conn.create_bucket('test_bucket_name')
bucket = conn.create_bucket("test_bucket_name")
key = Key(bucket, 'test_list_keys_2/*x+?^@~!y')
key.set_contents_from_string('value1')
key = Key(bucket, "test_list_keys_2/*x+?^@~!y")
key.set_contents_from_string("value1")
key_list = bucket.list('test_list_keys_2/', '/')
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")
@ -244,78 +240,83 @@ def test_key_with_special_characters():
@mock_s3_deprecated
def test_bucket_key_listing_order():
conn = create_connection()
bucket = conn.create_bucket('test_bucket')
prefix = 'toplevel/'
bucket = conn.create_bucket("test_bucket")
prefix = "toplevel/"
def store(name):
k = Key(bucket, prefix + name)
k.set_contents_from_string('somedata')
k.set_contents_from_string("somedata")
names = ['x/key', 'y.key1', 'y.key2', 'y.key3', 'x/y/key', 'x/y/z/key']
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'
])
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 = '/'
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/'
])
keys.should.equal(
["toplevel/y.key1", "toplevel/y.key2", "toplevel/y.key3", "toplevel/x/"]
)
# Test delimiter with no prefix
delimiter = '/'
delimiter = "/"
keys = [x.name for x in bucket.list(prefix=None, delimiter=delimiter)]
keys.should.equal(['toplevel/'])
keys.should.equal(["toplevel/"])
delimiter = None
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
keys.should.equal(
['toplevel/x/key', 'toplevel/x/y/key', 'toplevel/x/y/z/key'])
keys = [x.name for x in bucket.list(prefix + "x", delimiter)]
keys.should.equal(["toplevel/x/key", "toplevel/x/y/key", "toplevel/x/y/z/key"])
delimiter = '/'
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
keys.should.equal(['toplevel/x/'])
delimiter = "/"
keys = [x.name for x in bucket.list(prefix + "x", delimiter)]
keys.should.equal(["toplevel/x/"])
@mock_s3_deprecated
def test_delete_keys():
conn = create_connection()
bucket = conn.create_bucket('foobar')
bucket = conn.create_bucket("foobar")
Key(bucket=bucket, name='file1').set_contents_from_string('abc')
Key(bucket=bucket, name='file2').set_contents_from_string('abc')
Key(bucket=bucket, name='file3').set_contents_from_string('abc')
Key(bucket=bucket, name='file4').set_contents_from_string('abc')
Key(bucket=bucket, name="file1").set_contents_from_string("abc")
Key(bucket=bucket, name="file2").set_contents_from_string("abc")
Key(bucket=bucket, name="file3").set_contents_from_string("abc")
Key(bucket=bucket, name="file4").set_contents_from_string("abc")
result = bucket.delete_keys(['file2', 'file3'])
result = bucket.delete_keys(["file2", "file3"])
result.deleted.should.have.length_of(2)
result.errors.should.have.length_of(0)
keys = bucket.get_all_keys()
keys.should.have.length_of(2)
keys[0].name.should.equal('file1')
keys[0].name.should.equal("file1")
@mock_s3_deprecated
def test_delete_keys_with_invalid():
conn = create_connection()
bucket = conn.create_bucket('foobar')
bucket = conn.create_bucket("foobar")
Key(bucket=bucket, name='file1').set_contents_from_string('abc')
Key(bucket=bucket, name='file2').set_contents_from_string('abc')
Key(bucket=bucket, name='file3').set_contents_from_string('abc')
Key(bucket=bucket, name='file4').set_contents_from_string('abc')
Key(bucket=bucket, name="file1").set_contents_from_string("abc")
Key(bucket=bucket, name="file2").set_contents_from_string("abc")
Key(bucket=bucket, name="file3").set_contents_from_string("abc")
Key(bucket=bucket, name="file4").set_contents_from_string("abc")
result = bucket.delete_keys(['abc', 'file3'])
result = bucket.delete_keys(["abc", "file3"])
result.deleted.should.have.length_of(1)
result.errors.should.have.length_of(1)
keys = bucket.get_all_keys()
keys.should.have.length_of(3)
keys[0].name.should.equal('file1')
keys[0].name.should.equal("file1")

View file

@ -14,12 +14,12 @@ def test_bucketpath_combo_serial():
@mock_s3_deprecated
def make_bucket_path():
conn = create_connection()
conn.create_bucket('mybucketpath')
conn.create_bucket("mybucketpath")
@mock_s3_deprecated
def make_bucket():
conn = boto.connect_s3('the_key', 'the_secret')
conn.create_bucket('mybucket')
conn = boto.connect_s3("the_key", "the_secret")
conn.create_bucket("mybucket")
make_bucket()
make_bucket_path()

View file

@ -4,13 +4,14 @@ from moto.s3bucket_path.utils import bucket_name_from_url
def test_base_url():
expect(bucket_name_from_url('https://s3.amazonaws.com/')).should.equal(None)
expect(bucket_name_from_url("https://s3.amazonaws.com/")).should.equal(None)
def test_localhost_bucket():
expect(bucket_name_from_url('https://localhost:5000/wfoobar/abc')
).should.equal("wfoobar")
expect(bucket_name_from_url("https://localhost:5000/wfoobar/abc")).should.equal(
"wfoobar"
)
def test_localhost_without_bucket():
expect(bucket_name_from_url('https://www.localhost:5000')).should.equal(None)
expect(bucket_name_from_url("https://www.localhost:5000")).should.equal(None)