| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 # Copyright (c) 2006-2011 Mitch Garnaat http://garnaat.org/ | 2 # Copyright (c) 2006-2011 Mitch Garnaat http://garnaat.org/ |
| 3 # | 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: | 10 # lowing conditions: |
| 11 # | 11 # |
| 12 # The above copyright notice and this permission notice shall be included | 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. | 13 # in all copies or substantial portions of the Software. |
| 14 # | 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. | 21 # IN THE SOFTWARE. |
| 22 | 22 |
| 23 """ | 23 """ |
| 24 Some unit tests for the S3Connection | 24 Some unit tests for the S3Connection |
| 25 """ | 25 """ |
| 26 | 26 |
| 27 import unittest | 27 import unittest |
| 28 import time | 28 import time |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 except S3PermissionsError: | 189 except S3PermissionsError: |
| 190 pass | 190 pass |
| 191 # now try to create an RRS key | 191 # now try to create an RRS key |
| 192 k = bucket.new_key('reduced_redundancy') | 192 k = bucket.new_key('reduced_redundancy') |
| 193 k.set_contents_from_string('This key has reduced redundancy', | 193 k.set_contents_from_string('This key has reduced redundancy', |
| 194 reduced_redundancy=True) | 194 reduced_redundancy=True) |
| 195 | 195 |
| 196 # now try to inject a response header | 196 # now try to inject a response header |
| 197 data = k.get_contents_as_string(response_headers={'response-content-type
' : 'foo/bar'}) | 197 data = k.get_contents_as_string(response_headers={'response-content-type
' : 'foo/bar'}) |
| 198 assert k.content_type == 'foo/bar' | 198 assert k.content_type == 'foo/bar' |
| 199 | 199 |
| 200 # now delete all keys in bucket | 200 # now delete all keys in bucket |
| 201 for k in bucket: | 201 for k in bucket: |
| 202 if k.name == 'reduced_redundancy': | 202 if k.name == 'reduced_redundancy': |
| 203 assert k.storage_class == 'REDUCED_REDUNDANCY' | 203 assert k.storage_class == 'REDUCED_REDUNDANCY' |
| 204 bucket.delete_key(k) | 204 bucket.delete_key(k) |
| 205 # now delete bucket | 205 # now delete bucket |
| 206 time.sleep(5) | 206 time.sleep(5) |
| 207 c.delete_bucket(bucket) | 207 c.delete_bucket(bucket) |
| 208 print '--- tests completed ---' | 208 print '--- tests completed ---' |
| 209 | 209 |
| 210 def test_basic_anon(self): | 210 def test_basic_anon(self): |
| 211 auth_con = S3Connection() | 211 auth_con = S3Connection() |
| 212 # create a new, empty bucket | 212 # create a new, empty bucket |
| 213 bucket_name = 'test-%d' % int(time.time()) | 213 bucket_name = 'test-%d' % int(time.time()) |
| 214 auth_bucket = auth_con.create_bucket(bucket_name) | 214 auth_bucket = auth_con.create_bucket(bucket_name) |
| 215 | 215 |
| 216 # try read the bucket anonymously | 216 # try read the bucket anonymously |
| 217 anon_con = S3Connection(anon=True) | 217 anon_con = S3Connection(anon=True) |
| 218 anon_bucket = Bucket(anon_con, bucket_name) | 218 anon_bucket = Bucket(anon_con, bucket_name) |
| 219 try: | 219 try: |
| 220 iter(anon_bucket.list()).next() | 220 iter(anon_bucket.list()).next() |
| 221 self.fail("anon bucket list should fail") | 221 self.fail("anon bucket list should fail") |
| 222 except S3ResponseError: | 222 except S3ResponseError: |
| 223 pass | 223 pass |
| 224 | 224 |
| 225 # give bucket anon user access and anon read again | 225 # give bucket anon user access and anon read again |
| 226 auth_bucket.set_acl('public-read') | 226 auth_bucket.set_acl('public-read') |
| 227 time.sleep(5) |
| 227 try: | 228 try: |
| 228 iter(anon_bucket.list()).next() | 229 iter(anon_bucket.list()).next() |
| 229 self.fail("not expecting contents") | 230 self.fail("not expecting contents") |
| 230 except S3ResponseError, e: | 231 except S3ResponseError, e: |
| 231 self.fail("We should have public-read access, but received " | 232 self.fail("We should have public-read access, but received " |
| 232 "an error: %s" % e) | 233 "an error: %s" % e) |
| 233 except StopIteration: | 234 except StopIteration: |
| 234 pass | 235 pass |
| 235 | 236 |
| 236 # cleanup | 237 # cleanup |
| 237 auth_con.delete_bucket(auth_bucket) | 238 auth_con.delete_bucket(auth_bucket) |
| 238 | 239 |
| 239 def test_error_code_populated(self): | 240 def test_error_code_populated(self): |
| 240 c = S3Connection() | 241 c = S3Connection() |
| 241 try: | 242 try: |
| 242 c.create_bucket('bad$bucket$name') | 243 c.create_bucket('bad$bucket$name') |
| 243 except S3ResponseError, e: | 244 except S3ResponseError, e: |
| 244 self.assertEqual(e.error_code, 'InvalidBucketName') | 245 self.assertEqual(e.error_code, 'InvalidBucketName') |
| 245 else: | 246 else: |
| 246 self.fail("S3ResponseError not raised.") | 247 self.fail("S3ResponseError not raised.") |
| OLD | NEW |