|
|
@@ -18,7 +18,7 @@
|
|
|
|
|
|
import json
|
|
|
|
|
|
-from nose.tools import assert_equal, assert_true
|
|
|
+from nose.tools import assert_equal, assert_false, assert_true
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
from desktop.lib.django_test_util import make_logged_in_client
|
|
|
@@ -29,7 +29,6 @@ from beeswax.models import SavedQuery
|
|
|
from beeswax.design import hql_query
|
|
|
|
|
|
|
|
|
-
|
|
|
class TestDocument2(object):
|
|
|
|
|
|
def setUp(self):
|
|
|
@@ -37,12 +36,17 @@ class TestDocument2(object):
|
|
|
self.user = User.objects.get(username="doc2")
|
|
|
grant_access("doc2", "doc2", "beeswax")
|
|
|
|
|
|
- # Setup Home dir this way currently
|
|
|
+ # This creates the user directories for the new user
|
|
|
response = self.client.get('/desktop/api2/docs/')
|
|
|
data = json.loads(response.content)
|
|
|
-
|
|
|
assert_equal('/', data['document']['path'], data)
|
|
|
|
|
|
+ self.home_dir = Document2.objects.get_home_directory(user=self.user)
|
|
|
+
|
|
|
+
|
|
|
+ def test_trash_directory(self):
|
|
|
+ assert_true(Directory.objects.filter(owner=self.user, name=Document2.TRASH_DIR, type='directory').exists())
|
|
|
+
|
|
|
|
|
|
def test_document_create(self):
|
|
|
sql = 'SELECT * FROM sample_07'
|
|
|
@@ -80,8 +84,7 @@ class TestDocument2(object):
|
|
|
|
|
|
|
|
|
def test_directory_create(self):
|
|
|
- home_dir = Document2.objects.get_home_directory(self.user)
|
|
|
- response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(home_dir.uuid), 'name': json.dumps('test_mkdir')})
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(self.home_dir.uuid), 'name': json.dumps('test_mkdir')})
|
|
|
data = json.loads(response.content)
|
|
|
|
|
|
assert_equal(0, data['status'], data)
|
|
|
@@ -91,12 +94,11 @@ class TestDocument2(object):
|
|
|
|
|
|
|
|
|
def test_directory_move(self):
|
|
|
- home_dir = Document2.objects.get_home_directory(self.user)
|
|
|
- response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(home_dir.uuid), 'name': json.dumps('test_mv')})
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(self.home_dir.uuid), 'name': json.dumps('test_mv')})
|
|
|
data = json.loads(response.content)
|
|
|
assert_equal(0, data['status'], data)
|
|
|
|
|
|
- response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(home_dir.uuid), 'name': json.dumps('test_mv_dst')})
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(self.home_dir.uuid), 'name': json.dumps('test_mv_dst')})
|
|
|
data = json.loads(response.content)
|
|
|
assert_equal(0, data['status'], data)
|
|
|
|
|
|
@@ -110,16 +112,15 @@ class TestDocument2(object):
|
|
|
assert_equal(Directory.objects.get(name='test_mv', owner=self.user).path, '/test_mv_dst/test_mv')
|
|
|
|
|
|
|
|
|
- def test_directory_documents(self):
|
|
|
- home_dir = Directory.objects.get(owner=self.user, name='')
|
|
|
-
|
|
|
+ def test_directory_children(self):
|
|
|
+ # Creates 2 directories and 2 queries and saves to home directory
|
|
|
dir1 = Directory.objects.create(name='test_dir1', owner=self.user)
|
|
|
dir2 = Directory.objects.create(name='test_dir2', owner=self.user)
|
|
|
query1 = Document2.objects.create(name='query1.sql', type='query-hive', owner=self.user, data={})
|
|
|
query2 = Document2.objects.create(name='query2.sql', type='query-hive', owner=self.user, data={})
|
|
|
children = [dir1, dir2, query1, query2]
|
|
|
|
|
|
- home_dir.children.add(*children)
|
|
|
+ self.home_dir.children.add(*children)
|
|
|
|
|
|
# Test that all children directories and documents are returned
|
|
|
response = self.client.get('/desktop/api2/docs', {'path': '/'})
|
|
|
@@ -146,3 +147,85 @@ class TestDocument2(object):
|
|
|
data = json.loads(response.content)
|
|
|
assert_equal(5, data['count'])
|
|
|
assert_equal(2, len(data['children']))
|
|
|
+
|
|
|
+
|
|
|
+ def test_document_trash(self):
|
|
|
+ # Create document under home and directory under home with child document
|
|
|
+ dir = Directory.objects.create(name='test_dir', owner=self.user, parent_directory=self.home_dir)
|
|
|
+ nested_query = Document2.objects.create(name='query1.sql', type='query-hive', owner=self.user, data={}, parent_directory=dir)
|
|
|
+ query = Document2.objects.create(name='query2.sql', type='query-hive', owner=self.user, data={}, parent_directory=self.home_dir)
|
|
|
+
|
|
|
+ # Test that .Trash is currently empty
|
|
|
+ response = self.client.get('/desktop/api2/docs', {'path': '/.Trash'})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(0, data['count'])
|
|
|
+
|
|
|
+ # Delete document
|
|
|
+ response = self.client.post('/desktop/api2/doc/delete', {'uuid': json.dumps(query.uuid)})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(0, data['status'])
|
|
|
+
|
|
|
+ response = self.client.get('/desktop/api2/docs', {'path': '/.Trash'})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(1, data['count'])
|
|
|
+ assert_equal(data['children'][0]['uuid'], query.uuid)
|
|
|
+
|
|
|
+ # Delete directory
|
|
|
+ response = self.client.post('/desktop/api2/doc/delete', {'uuid': json.dumps(dir.uuid)})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(0, data['status'], data)
|
|
|
+
|
|
|
+ response = self.client.get('/desktop/api2/docs', {'path': '/.Trash'})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(2, data['count'])
|
|
|
+
|
|
|
+ # Verify that only doc in home is .Trash
|
|
|
+ response = self.client.get('/desktop/api2/docs', {'path': '/'})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_true('children' in data)
|
|
|
+ assert_equal(1, data['count'])
|
|
|
+ assert_equal(Document2.TRASH_DIR, data['children'][0]['name'])
|
|
|
+
|
|
|
+
|
|
|
+ def test_validations(self):
|
|
|
+ # Test invalid names
|
|
|
+ invalid_name = '/invalid'
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(self.home_dir.uuid), 'name': json.dumps(invalid_name)})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(-1, data['status'], data)
|
|
|
+ assert_true('invalid character' in data['message'])
|
|
|
+
|
|
|
+ # Test error on creating documents with same name and location
|
|
|
+ test_dir = Directory.objects.create(name='test_dir', owner=self.user, parent_directory=self.home_dir)
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(self.home_dir.uuid), 'name': json.dumps('test_dir')})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(-1, data['status'], data)
|
|
|
+ assert_true('/test_dir already exists' in data['message'])
|
|
|
+
|
|
|
+ # But can create same name in different location
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(test_dir.uuid), 'name': json.dumps('test_dir')})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(0, data['status'], data)
|
|
|
+
|
|
|
+ # Test that home and Trash directories cannot be recreated or modified
|
|
|
+ response = self.client.post('/desktop/api2/doc/mkdir', {'parent_uuid': json.dumps(test_dir.uuid), 'name': json.dumps(Document2.TRASH_DIR)})
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(-1, data['status'], data)
|
|
|
+ assert_equal('Cannot create or modify the home or .Trash directory.', data['message'])
|
|
|
+
|
|
|
+ response = self.client.post('/desktop/api2/doc/move', {
|
|
|
+ 'source_doc_uuid': json.dumps(self.home_dir.uuid),
|
|
|
+ 'destination_doc_uuid': json.dumps(test_dir.uuid)
|
|
|
+ })
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(-1, data['status'], data)
|
|
|
+ assert_equal('Cannot create or modify the home or .Trash directory.', data['message'])
|
|
|
+
|
|
|
+ trash_dir = Directory.objects.get(name=Document2.TRASH_DIR, owner=self.user)
|
|
|
+ response = self.client.post('/desktop/api2/doc/move', {
|
|
|
+ 'source_doc_uuid': json.dumps(trash_dir.uuid),
|
|
|
+ 'destination_doc_uuid': json.dumps(test_dir.uuid)
|
|
|
+ })
|
|
|
+ data = json.loads(response.content)
|
|
|
+ assert_equal(-1, data['status'], data)
|
|
|
+ assert_equal('Cannot create or modify the home or .Trash directory.', data['message'])
|