示例#1
0
 def test_put_stamp(self, conn_class, key_class):
     from pkg_resources import get_distribution
     from van.static.cdn import _PutS3
     target_url = 's3://mybucket/path/to/dir'
     putter = _PutS3(target_url, aws_access_key='key', aws_secret_key='secret')
     dist = get_distribution('pyramid')
     from tempfile import mkstemp
     f, tempfile = mkstemp()
     f = os.fdopen(f, 'w')
     f.write('Stomped')
     f.close()
     putter.put(_iter_to_dict([
         ('static', tempfile, 'pyramid', dist, 'stamp'),
         ]))
     stamp_dist = get_distribution('van.static')
     stamp_key  = key_class()()
     self.assertEqual(
             stamp_key.key,
             '/path/to/dir/van.static/%s/pyramid-%s-ON2GC5DJMM======.stamp' %
             (stamp_dist.version, dist.version))
     args, kw = stamp_key.set_contents_from_filename.call_args
     # the file uploaded was the stamp
     f = open(args[0], 'r')
     try:
         file_contents = f.read()
     finally:
         f.close()
     self.assertEqual(file_contents, 'Stomped')
     self.assertEqual(kw, dict(
             reduced_redundancy=True,
             policy='public-read'))
     putter.close()
     os.remove(tempfile)
示例#2
0
 def test_has_stamp_with_encodings(self, exists):
     from pkg_resources import get_distribution
     from van.static.cdn import _PutS3
     target_url = 's3://mybucket/path/to/dir'
     putter = _PutS3(target_url, aws_access_key='key', aws_secret_key='secret', encodings=['gzip', 'deflate'])
     dist = get_distribution('pyramid')
     stamp_dist = get_distribution('van.static')
     putter.has_stamp(dist, 'static')
     exists.assert_called_once_with(stamp_dist, 'pyramid-%s-deflate-gzip-ON2GC5DJMM======.stamp' % dist.version)
     putter.close()
示例#3
0
 def test_put(self, conn_class, key_class):
     conn = Mock()
     key = Mock()
     css_key = Mock()
     txt_key = Mock()
     rv = [css_key, txt_key]
     def se(bucket):
         return rv.pop(0)
     key.side_effect = se
     conn_class.return_value = conn
     key_class.return_value = key
     target_url = 's3://mybucket/path/to/dir'
     from pkg_resources import get_distribution
     dist = get_distribution('van.static')
     here = os.path.dirname(__file__)
     from van.static.cdn import _PutS3
     putter = _PutS3(target_url, aws_access_key='key', aws_secret_key='secret')
     putter.put(_iter_to_dict([
         ('tests/example', here + '/example', 'van.static', dist, 'dir'),
         ('tests/example/css', here + '/example/css', 'van.static', dist, 'dir'),
         ('tests/example/css/example.css', here + '/example/css/example.css', 'van.static', dist, 'file'),
         ('tests/example/example.txt', here + '/example/example.txt', 'van.static', dist, 'file'),
         ]))
     conn.assert_called_once_with('key', 'secret')
     conn().get_bucket.assert_called_once_with('mybucket', validate=False)
     bucket = conn().get_bucket()
     self.assertEqual(key.call_args_list, [
         ((bucket, ), ),
         ((bucket, ), ),
         ])
     self.assertEqual(
             css_key.key,
             '/path/to/dir/van.static/%s/tests/example/css/example.css' % dist.version)
     css_key.set_contents_from_filename.assert_called_once_with(
             here + '/example/css/example.css',
             reduced_redundancy=True,
             headers={'Cache-Control': 'max-age=32140800',
                 'Content-Type': 'text/css'},
             policy='public-read')
     self.assertEqual(
             txt_key.key,
             '/path/to/dir/van.static/%s/tests/example/example.txt' % dist.version)
     txt_key.set_contents_from_filename.assert_called_once_with(
             here + '/example/example.txt',
             reduced_redundancy=True,
             headers={'Cache-Control': 'max-age=32140800',
                 'Content-Type': 'text/plain'},
             policy='public-read')
     putter.close()
示例#4
0
 def test_exists(self, conn_class):
     conn = Mock()
     conn_class.return_value = conn
     target_url = 's3://mybucket/path/to/dir'
     from pkg_resources import get_distribution
     dist = get_distribution('van.static')
     here = os.path.dirname(__file__)
     from van.static.cdn import _PutS3
     putter = _PutS3(target_url, aws_access_key='key', aws_secret_key='secret')
     bucket = conn().get_bucket()
     bucket.get_key.return_value = None
     self.assertFalse(putter.exists(dist, 'whatever/wherever'))
     bucket.get_key.assert_called_once_with('/path/to/dir/%s/%s/whatever/wherever' % (dist.project_name, dist.version))
     bucket.get_key.return_value = 'not none'
     self.assertTrue(putter.exists(dist, 'whatever/wherever'))
     putter.close()
示例#5
0
 def test_put_stamp_with_encoding(self, conn_class, key_class):
     from pkg_resources import get_distribution
     from van.static.cdn import _PutS3
     target_url = 's3://mybucket/path/to/dir'
     putter = _PutS3(target_url, aws_access_key='key', aws_secret_key='secret', encodings=['gzip'])
     dist = get_distribution('pyramid')
     from tempfile import mkstemp
     f, tempfile = mkstemp()
     f = os.fdopen(f, 'w')
     f.write('Stomped')
     f.close()
     putter.put(_iter_to_dict([
         ('static', tempfile, 'pyramid', dist, 'stamp'),
         ]))
     stamp_dist = get_distribution('van.static')
     stamp_key  = key_class()()
     self.assertEqual(
             stamp_key.key,
             '/path/to/dir/van.static/%s/pyramid-%s-gzip-ON2GC5DJMM======.stamp' %
             (stamp_dist.version, dist.version))
     os.remove(tempfile)
示例#6
0
 def test_put_encodings(self, conn_class, key_class):
     keys = []
     def record_keys(bucket):
         mock = Mock()
         keys.append(mock)
         return mock
     key_class().side_effect = record_keys
     target_url = 's3://mybucket/path/to/dir'
     from pkg_resources import get_distribution
     dist = get_distribution('van.static')
     here = os.path.dirname(__file__)
     from van.static.cdn import _PutS3
     putter = _PutS3(target_url, aws_access_key='key', aws_secret_key='secret', encodings=['gzip'])
     putter.put(_iter_to_dict([
         ('tests/example/css/example.css', here + '/example/css/example.css', 'van.static', dist, 'file'),
         ('tests/example/images/example.jpg', here + '/example/images/example.jpg', 'van.static', dist, 'file'),
         ]))
     css_key, css_gz_key, jpg_key, jpg_gz_key = keys
     self.assertEqual(
             css_key.key,
             '/path/to/dir/van.static/%s/tests/example/css/example.css' % dist.version)
     css_key.set_contents_from_filename.assert_called_once_with(
             here + '/example/css/example.css',
             reduced_redundancy=True,
             headers={'Cache-Control': 'max-age=32140800',
                 'Content-Type': 'text/css'},
             policy='public-read')
     self.assertEqual(
             css_gz_key.key,
             '/path/to/dir/van.static/%s/gzip/tests/example/css/example.css' % dist.version)
     args, kw = css_gz_key.set_contents_from_filename.call_args
     # the file uploaded was a gzipped version of the CSS
     self.assertNotEqual(here + '/example/css/example.css', args[0])
     import gzip
     f = open(args[0], 'rb')
     try:
         file_contents = f.read()
         f.seek(0)
         gz_f = gzip.GzipFile('', 'r', fileobj=f)
         decoded_css = gz_f.read()
     finally:
         f.close()
         gz_f.close()
     self.assertTrue(file_contents.startswith(b('\x1f\x8b'))) # gzip magic number
     self.assertEqual(decoded_css.decode('ascii'), '.example {\n\twidth: 80px\n}\n')
     self.assertEqual(kw, dict(
             reduced_redundancy=True,
             headers={'Cache-Control': 'max-age=32140800',
                 'Content-Type': 'text/css',
                 'Content-Encoding': 'gzip'},
             policy='public-read'))
     self.assertEqual(
             jpg_key.key,
             '/path/to/dir/van.static/%s/tests/example/images/example.jpg' % dist.version)
     jpg_key.set_contents_from_filename.assert_called_once_with(
             here + '/example/images/example.jpg',
             reduced_redundancy=True,
             headers={'Cache-Control': 'max-age=32140800',
                 'Content-Type': 'image/jpeg'},
             policy='public-read')
     # the jpeg was re-uploaded to the gzip prefixed url but NOT compressed
     self.assertEqual(
             jpg_gz_key.key,
             '/path/to/dir/van.static/%s/gzip/tests/example/images/example.jpg' % dist.version)
     jpg_gz_key.set_contents_from_filename.assert_called_once_with(
             here + '/example/images/example.jpg',
             reduced_redundancy=True,
             headers={'Cache-Control': 'max-age=32140800',
                 'Content-Type': 'image/jpeg'},
             policy='public-read')
     putter.close()