فهرست منبع

HUE-5325 [fb] Add set of test about S3 permissions

Note: we currently return a PopupException which has a 500 code and not 40
Note: we currently return a PopupException which has a 500 code and not 4033
Romain Rigaux 9 سال پیش
والد
کامیت
cd9906e592

+ 47 - 1
apps/filebrowser/src/filebrowser/views_test.py

@@ -24,12 +24,14 @@ import tempfile
 import urlparse
 from avro import schema, datafile, io
 
+from aws.s3.s3fs import S3FileSystemException
+from aws.s3.s3test_utils import get_test_bucket
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.utils.encoding import smart_str
 from nose.plugins.attrib import attr
 from nose.plugins.skip import SkipTest
-from nose.tools import assert_true, assert_false, assert_equal, assert_not_equal
+from nose.tools import assert_true, assert_false, assert_equal, assert_not_equal, assert_raises
 
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.test_utils import grant_access, add_to_group
@@ -42,8 +44,10 @@ from conf import MAX_SNAPPY_DECOMPRESSION_SIZE
 from lib.rwx import expand_mode
 from views import snappy_installed
 
+
 LOG = logging.getLogger(__name__)
 
+
 def cleanup_tree(cluster, path):
   try:
     cluster.fs.rmtree(path)
@@ -1136,3 +1140,45 @@ def test_location_to_url():
   assert_equal(prefix + '/var/lib/hadoop-hdfs', location_to_url('hdfs://localhost:8020/var/lib/hadoop-hdfs'))
   assert_equal(prefix + '/', location_to_url('hdfs://localhost:8020'))
   assert_equal(prefix + 's3a%3A//bucket/key', location_to_url('s3a://bucket/key'))
+
+
+class TestS3AccessPermissions(object):
+
+  def setUp(self):
+    self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
+
+    self.user = User.objects.get(username="test")
+
+  def test_no_default_permissions(self):
+    response = self.client.get('/filebrowser/view=S3A://')
+    assert_equal(500, response.status_code)
+
+    response = self.client.get('/filebrowser/view=S3A://bucket')
+    assert_equal(500, response.status_code)
+
+    response = self.client.get('/filebrowser/view=S3A://bucket/hue')
+    assert_equal(500, response.status_code)
+
+    response = self.client.post('/filebrowser/rmtree', dict(path=['S3A://bucket/hue']))
+    assert_equal(500, response.status_code)
+
+    # 500 for real currently
+    assert_raises(IOError, self.client.get, '/filebrowser/edit=S3A://bucket/hue')
+
+    # 500 for real currently
+    with tempfile.NamedTemporaryFile() as local_file:
+      DEST_DIR = 'S3A://bucket/hue'
+      LOCAL_FILE = local_file.name
+      assert_raises(S3FileSystemException, self.client.post, '/filebrowser/upload/file?dest=%s' % DEST_DIR, dict(dest=DEST_DIR, hdfs_file=file(LOCAL_FILE)))
+
+  def test_has_default_permissions(self):
+    if not get_test_bucket():
+      raise SkipTest
+
+    add_permission(self.user.username, 'has_s3', permname='s3_access', appname='filebrowser')
+
+    try:
+      response = self.client.get('/filebrowser/view=S3A://')
+      assert_equal(200, response.status_code)
+    finally:
+      remove_from_group(self.user.username, 'has_s3')

+ 2 - 2
desktop/core/src/desktop/lib/fs/proxyfs.py

@@ -38,7 +38,8 @@ class ProxyFS(object):
     self._fs_set = set(self._fs_dict.values())
     self._default_scheme = default_scheme
     self._default_fs = self._fs_dict[self._default_scheme]
-    self.user = default_user
+    if default_user is not None:
+      self.setuser(default_user)
 
   def __getattr__(self, item):
     if hasattr(self, "_default_fs"):
@@ -91,7 +92,6 @@ class ProxyFS(object):
   def setuser(self, user):
     """Set a new user. Return the past current user."""
     curr = self.user
-    self.user = user
     for fs in self._fs_set:
       fs.setuser(user)
     return curr

+ 1 - 1
desktop/core/src/desktop/lib/fs/proxyfs_test.py

@@ -154,7 +154,7 @@ class TestFsPermissions(object):
       f('hdfs://path')
       f('/tmp')
     finally:
-      remove_from_group('test', 'has_s3')
+      remove_from_group(user.username, 'has_s3')
 
 
   def test_fs_permissions_admin_user(self):

+ 1 - 1
desktop/core/src/desktop/tests.py

@@ -1077,7 +1077,7 @@ def test_session_secure_cookie():
       finally:
         for reset in resets:
           reset()
- 
+
       resets = [
         desktop.conf.SSL_CERTIFICATE.set_for_testing(present=None),
         desktop.conf.SSL_PRIVATE_KEY.set_for_testing(present=None),

+ 1 - 1
desktop/libs/aws/src/aws/__init__.py

@@ -46,6 +46,6 @@ def _make_client(identifier):
   return Client.from_config(client_conf)
 
 
-def get_s3fs(user, identifier='default'):
+def get_s3fs(identifier='default'):
   connection = get_client(identifier).get_s3_connection()
   return S3FileSystem(connection)

+ 1 - 1
desktop/libs/aws/src/aws/s3/upload.py

@@ -117,7 +117,7 @@ class S3FileUploadHandler(FileUploadHandler):
     try:
       fs = request.fs
     except AttributeError:
-      fs = get_s3fs(request.user)
+      fs = get_s3fs()
 
     if not fs:
       raise S3FileUploadError(_("No S3 filesystem found."))