Browse Source

[raz] Add tests for UrlConnection with RAZ client

Romain Rigaux 4 years ago
parent
commit
19c0f767b8

+ 16 - 20
desktop/libs/aws/src/aws/s3/s3connection.py

@@ -48,7 +48,7 @@ class UrlConnection():
     LOG.debug(response)
     LOG.debug(response.content)
 
-    rs = ResultSet([('Bucket', self.connection.bucket_class)])
+    rs = ResultSet([('Bucket', UrlBucket)])
     h = boto.handler.XmlHandler(rs, None)
     xml.sax.parseString(response.content, h)
     LOG.debug(rs)
@@ -169,7 +169,7 @@ class UrlBucket(Bucket):
     LOG.debug(response)
     LOG.debug(response.content)
 
-    rs = ResultSet([('Contents', Key), ('CommonPrefixes', Prefix)])  # Or BucketListResultSet?
+    rs = ResultSet([('Contents', UrlKey), ('CommonPrefixes', Prefix)])  # Or BucketListResultSet?
     h = boto.handler.XmlHandler(rs, self)
     xml.sax.parseString(response.content, h)
     LOG.debug(rs)
@@ -199,37 +199,33 @@ class BotoUrlConnection(UrlConnection):
     self.expiration = 3600
 
     self.connection.make_request = None  # We make sure we never call via regular boto connection directly
-    self.connection.set_bucket_class(UrlBucket)  # We use our bucket class to keep overriding any direct call to S3
+    self.connection.set_bucket_class(UrlBucket)  # Use our bucket class to keep overriding any direct call to S3 made from list buckets
 
 
   def get_all_buckets(self, headers=None):
+    LOG.debug('get_all_buckets: %s' % headers)
     kwargs = {'action': 'GET'}
-    LOG.debug('get_all_buckets: %s' % kwargs)
-    try:
-      tmp_url = self._generate_url(**kwargs)
-    except BotoClientError as e:
-      LOG.error(e)
-      return None
 
-    LOG.debug(tmp_url)
-    response = requests.get(tmp_url)
-    LOG.debug(response)
-    LOG.debug(response.content)
-
-    rs = ResultSet([('Bucket', self.connection.bucket_class)])
-    h = boto.handler.XmlHandler(rs, None)
-    xml.sax.parseString(response.content, h)
-    LOG.debug(rs)
+    signed_url = self._generate_url(**kwargs)
 
-    return rs
+    return self._get_all_buckets(signed_url)
 
 
   def get_bucket(self, bucket_name, validate=True, headers=None):
+    LOG.debug('get_bucket: %s' % bucket_name)
     kwargs = {'action': 'GET', 'bucket': bucket_name}
 
     signed_url = self._generate_url(**kwargs)
 
-    return self._get_all_buckets(signed_url)
+    response = requests.get(signed_url)
+
+    LOG.debug(response)
+    LOG.debug(response.content)
+
+    rs = self.connection.bucket_class(self.connection, bucket_name, key_class=UrlKey)  # Using content?
+    LOG.debug(rs)
+
+    return rs
 
 
   def _generate_url(self, action='GET', **kwargs):

+ 26 - 4
desktop/libs/aws/src/aws/s3/s3connection_test.py

@@ -20,14 +20,14 @@ import sys
 from nose.tools import assert_equal, assert_false, assert_true, assert_raises
 
 from aws.client import _make_client
-from aws.s3.s3connection import BotoUrlConnection, UrlBucket
+from aws.s3.s3connection import BotoUrlConnection, UrlBucket, RazUrlConnection
 from aws.s3.s3test_utils import S3TestBase
 
 
 if sys.version_info[0] > 2:
-  from unittest.mock import patch, Mock, MagicMock
+  from unittest.mock import patch, Mock
 else:
-  from mock import patch, Mock, MagicMock
+  from mock import patch, Mock
 
 
 class TestBotoUrlConnection():
@@ -47,12 +47,34 @@ class TestBotoUrlConnection():
         )
 
         connection = Mock()
-        connection.bucket_class = UrlBucket
         buckets = BotoUrlConnection(connection=connection).get_all_buckets()
 
         assert_equal('[<Bucket: demo-gethue>, <Bucket: gethue-test>]', str(buckets))
 
 
+class TestRazUrlConnection():
+
+  def test_get_buckets(self):
+    with patch('aws.s3.s3connection.RazUrlConnection._generate_url') as _generate_url:
+      with patch('aws.s3.s3connection.requests.get') as requests_get:
+
+        # TODO: update with potentially slightly different URL/headers
+        _generate_url.return_value = 'https://gethue-test.s3.amazonaws.com/?AWSAccessKeyId=AKIA23E77ZX2HVY76YGL' + \
+            '&Signature=3lhK%2BwtQ9Q2u5VDIqb4MEpoY3X4%3D&Expires=1617207304'
+
+        requests_get.return_value = Mock(
+          content=b'<?xml version="1.0" encoding="UTF-8"?>\n<ListAllMyBucketsResult '
+            b'xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>0429b0aed2900f450655928a09e06e7aaac9939bc9141fc5aeeccd8b93b9778f'
+            b'</ID><DisplayName>team</DisplayName></Owner><Buckets><Bucket><Name>demo-gethue</Name><CreationDate>2020-08-22T08:03:18.000Z'
+            b'</CreationDate></Bucket><Bucket><Name>gethue-test</Name><CreationDate>2021-03-31T14:47:14.000Z</CreationDate></Bucket>'
+            b'</Buckets></ListAllMyBucketsResult>'
+        )
+
+        buckets = RazUrlConnection().get_all_buckets()
+
+        assert_equal('[<Bucket: demo-gethue>, <Bucket: gethue-test>]', str(buckets))
+
+
 class TestBotoUrlConnectionIntegration(S3TestBase):
   #
   # To trigger: