Browse Source

HUE-9096 [doc] Adding test skeleton for Link permission

Romain 6 years ago
parent
commit
080814e00f
2 changed files with 85 additions and 14 deletions
  1. 72 11
      desktop/core/src/desktop/document2_tests.py
  2. 13 3
      desktop/core/src/desktop/models.py

+ 72 - 11
desktop/core/src/desktop/tests_doc2.py → desktop/core/src/desktop/document2_tests.py

@@ -585,8 +585,10 @@ class TestDocument2Permissions(object):
     data = json.loads(response.content)
     assert_equal(new_doc.uuid, data['document']['uuid'], data)
     assert_true('perms' in data['document'])
-    assert_equal({'read': {'users': [], 'groups': []}, 'write': {'users': [], 'groups': []}},
-                 data['document']['perms'])
+    assert_equal(
+        {'read': {'users': [], 'groups': []}, 'write': {'users': [], 'groups': []}},
+        data['document']['perms']
+    )
 
 
   def test_share_document_read_by_user(self):
@@ -918,10 +920,16 @@ class TestDocument2Permissions(object):
 
     response = self.client.get('/desktop/api2/doc/', {'uuid': doc1.uuid})
     data = json.loads(response.content)
-    assert_equal([{'id': self.default_group.id, 'name': self.default_group.name}],
-                 data['document']['perms']['read']['groups'], data)
-    assert_equal([{'id': self.user_not_me.id, 'username': self.user_not_me.username}],
-                 data['document']['perms']['write']['users'], data)
+    assert_equal(
+        [{'id': self.default_group.id, 'name': self.default_group.name}],
+        data['document']['perms']['read']['groups'],
+        data
+    )
+    assert_equal(
+        [{'id': self.user_not_me.id, 'username': self.user_not_me.username}],
+        data['document']['perms']['write']['users'],
+        data
+    )
 
 
   def test_search_documents(self):
@@ -956,13 +964,13 @@ class TestDocument2Permissions(object):
     doc_names = [doc['name'] for doc in data['documents']]
     assert_true('history.sql' in doc_names)
 
+
   def test_x_share_directory_y_add_file_x_share(self):
     # Test that when another User, Y, adds a doc to dir shared by User X, User X doesn't fail to share the dir next time:
     # /
     #   test_dir/
     #     query1.sql
 
-
     # Dir owned by self.user
     parent_dir = Directory.objects.create(name='test_dir', owner=self.user, parent_directory=self.home_dir)
     child_doc = Document2.objects.create(name='query1.sql', type='query-hive', owner=self.user, data={}, parent_directory=parent_dir)
@@ -984,8 +992,13 @@ class TestDocument2Permissions(object):
       })
     })
 
-    user_y_child_doc = Document2.objects.create(name='other_query1.sql', type='query-hive', owner=user_y, data={},
-                                          parent_directory=parent_dir)
+    user_y_child_doc = Document2.objects.create(
+        name='other_query1.sql',
+        type='query-hive',
+        owner=user_y,
+        data={},
+        parent_directory=parent_dir
+    )
 
     share_test_user = User.objects.create(username='share_test_user', password="share_test_user")
 
@@ -1013,8 +1026,13 @@ class TestDocument2Permissions(object):
 
 
   def test_unicode_name(self):
-    doc = Document2.objects.create(name='My Bundle a voté « non » à l’accord', type='oozie-workflow2', owner=self.user,
-                                   data={}, parent_directory=self.home_dir)
+    doc = Document2.objects.create(
+        name='My Bundle a voté « non » à l’accord',
+        type='oozie-workflow2',
+        owner=self.user,
+        data={},
+        parent_directory=self.home_dir
+    )
 
     # Verify that home directory contents return correctly
     response = self.client.get('/desktop/api2/doc/', {'uuid': self.home_dir.uuid})
@@ -1029,6 +1047,49 @@ class TestDocument2Permissions(object):
     assert_equal('/My%20Bundle%20a%20vot%C3%A9%20%C2%AB%20non%20%C2%BB%20%C3%A0%20l%E2%80%99accord', path)
 
 
+  def test_link_permissions(self):
+    doc = Document2.objects.create(
+        name='test_link_permissions.sql',
+        type='query-hive',
+        owner=self.user,
+        data={},
+        parent_directory=self.home_dir
+    )
+
+    try:
+      assert_true(doc.can_read(self.user))
+      assert_true(doc.can_write(self.user))
+      assert_false(doc.can_read(self.user_not_me))
+      assert_false(doc.can_write(self.user_not_me))
+
+      doc.share(name=Document2Permission.LINK_READ_PERM, is_link_on=True)
+
+      assert_true(doc.can_read(self.user))
+      assert_true(doc.can_write(self.user))
+      assert_true(doc.can_read(self.user_not_me))
+      assert_false(doc.can_write(self.user_not_me))
+
+      assert_false(doc.get_permission('read').users.all())  # There is no doc listing via links, only direct access
+      assert_false(doc.get_permission('read').groups.all())
+
+      doc.share(name=Document2Permission.LINK_READ_PERM, is_link_on=False)
+
+      assert_true(doc.can_read(self.user))
+      assert_true(doc.can_write(self.user))
+      assert_false(doc.can_read(self.user_not_me))
+      assert_false(doc.can_write(self.user_not_me))
+
+      doc.share(name=Document2Permission.LINK_READ_PERM, is_link_on=True)
+      doc.share(name=Document2Permission.LINK_WRITE_PERM, is_link_on=True)
+
+      assert_true(doc.can_read(self.user))
+      assert_true(doc.can_write(self.user))
+      assert_true(doc.can_read(self.user_not_me))
+      assert_true(doc.can_write(self.user_not_me))
+    finally:
+      doc.delete()
+
+
 class TestDocument2ImportExport(object):
 
   def setUp(self):

+ 13 - 3
desktop/core/src/desktop/models.py

@@ -1356,8 +1356,14 @@ class Document2(models.Model):
       raise PopupException(_("Document does not exist or you don't have the permission to access it."))
 
   def get_permission(self, perm='read'):
+    if perm == 'read':
+      perms = Q(perms=Document2Permission.READ_PERM) | (Q(perms=Document2Permission.LINK_READ_PERM) & Q(is_link_on=True))
+    elif perm == 'write':
+      perms = Q(perms=Document2Permission.WRITE_PERM) | (Q(perms=Document2Permission.LINK_WRITE_PERM) & Q(is_link_on=True))
+    else:
+      perms = Q(perms=Document2Permission.WRITE_PERM)
     try:
-      return Document2Permission.objects.get(doc=self, perms=perm)
+      return Document2Permission.objects.get(Q(doc=self) & perms)
     except Document2Permission.DoesNotExist:
       return None
 
@@ -1544,6 +1550,8 @@ class Document2Permission(models.Model):
   READ_PERM = 'read'
   WRITE_PERM = 'write'
   COMMENT_PERM = 'comment'
+  LINK_READ_PERM = 'link_read'
+  LINK_WRITE_PERM = 'link_write'
 
   doc = models.ForeignKey(Document2)
 
@@ -1554,10 +1562,12 @@ class Document2Permission(models.Model):
     (READ_PERM, 'read'),
     (WRITE_PERM, 'write'),
     (COMMENT_PERM, 'comment'), # PLAYER PERM?
+    (LINK_READ_PERM, 'link read'),
+    (LINK_WRITE_PERM, 'link write'),
   ))
 
-  # link = models.CharField(default=uuid_default, max_length=255, unique=True) # Short link like dropbox
-  # embed
+  link = models.CharField(default='', max_length=255, unique=True) # uuid_default
+  link_on = models.BooleanField(default=False)
 
   class Meta(object):
     unique_together = ('doc', 'perms')