Преглед на файлове

HUE-5310 [search] Use Doc2 modal in search_controller

krish преди 9 години
родител
ревизия
5740b5d
променени са 2 файла, в които са добавени 194 реда и са изтрити 60 реда
  1. 21 9
      apps/search/src/search/search_controller.py
  2. 173 51
      apps/search/src/search/tests.py

+ 21 - 9
apps/search/src/search/search_controller.py

@@ -20,6 +20,7 @@ import logging
 
 from django.db.models import Q
 
+from desktop.conf import USE_NEW_EDITOR
 from desktop.models import Document2, Document, SAMPLE_USER_OWNERS
 from libsolr.api import SolrApi
 
@@ -38,21 +39,32 @@ class SearchController(object):
     self.user = user
 
   def get_search_collections(self):
-    return [d.content_object for d in Document.objects.get_docs(self.user, Document2, extra='search-dashboard').order_by('-id')]
+    if USE_NEW_EDITOR.get():
+      return Document2.objects.documents(user=self.user).search_documents(types=['search-dashboard'], order_by='-id')
+    else:
+      return [d.content_object for d in Document.objects.get_docs(self.user, Document2, extra='search-dashboard').order_by('-id')]
 
   def get_shared_search_collections(self):
     # Those are the ones appearing in the menu
-    docs = Document.objects.filter(Q(owner=self.user) | Q(owner__username__in=SAMPLE_USER_OWNERS), extra='search-dashboard')
-
-    return [d.content_object for d in docs.order_by('-id')]
+    if USE_NEW_EDITOR.get():
+      return Document2.objects.filter(Q(owner=self.user) | Q(owner__username__in=SAMPLE_USER_OWNERS), type='search-dashboard').order_by('-id')
+    else:
+      docs = Document.objects.filter(Q(owner=self.user) | Q(owner__username__in=SAMPLE_USER_OWNERS), extra='search-dashboard')
+      return [d.content_object for d in docs.order_by('-id')]
 
   def get_owner_search_collections(self):
-    if self.user.is_superuser:
-      docs = Document.objects.filter(extra='search-dashboard')
+    if USE_NEW_EDITOR.get():
+      if self.user.is_superuser:
+        docs = Document2.objects.filter(type='search-dashboard')
+      else:
+        docs = Document2.objects.filter(type='search-dashboard', owner=self.user)
+      return docs
     else:
-      docs = Document.objects.filter(extra='search-dashboard', owner=self.user)
-
-    return [d.content_object for d in docs.order_by('-id')]
+      if self.user.is_superuser:
+        docs = Document.objects.filter(extra='search-dashboard')
+      else:
+        docs = Document.objects.filter(extra='search-dashboard', owner=self.user)
+      return [d.content_object for d in docs.order_by('-id')]
 
   def get_icon(self, name):
     if name == 'Twitter':

+ 173 - 51
apps/search/src/search/tests.py

@@ -30,6 +30,7 @@ from desktop.models import Document2
 
 from search.api import _round_number_range
 from search.models import Collection2
+from search.search_controller import SearchController
 
 
 QUERY = {'qs': [{'q': ''}], 'fqs': [], 'start': 0}
@@ -97,6 +98,35 @@ class TestSearchBase(object):
 
     self.collection = Collection2(user=self.user, name='collection_1')
 
+    MockResource.set_solr_response("""{
+      "responseHeader": {
+        "status": 0,
+        "QTime": 0,
+        "params": {
+          "indent": "true",
+          "q": "*:*",
+          "_": "1442953203972",
+          "wt": "json"
+        }
+      },
+      "response": {
+        "numFound": 1,
+        "start": 0,
+        "docs": [
+          {
+            "id": "change.me",
+            "title": [
+              "val1",
+              "val2",
+              "[val3]",
+              "val4"
+            ],
+            "_version_": 1513046095083602000
+          }
+        ]
+      }
+      }""")
+
   def tearDown(self):
     # Remove monkey patching
     resource.Resource = self.prev_resource
@@ -113,57 +143,149 @@ class TestWithMockedSolr(TestSearchBase):
     assert_true('search' in response.content, response.content)
 
   def test_share_dashboard(self):
-      doc = Document2.objects.create(name='test_dashboard', type='search-dashboard', owner=self.user,
-                                     data=self.collection.data, parent_directory=self.home_dir)
-
-      # owner can view document
-      response = self.c.get('/desktop/api2/doc/', {'uuid': doc.uuid})
-      data = json.loads(response.content)
-      assert_equal(doc.uuid, data['document']['uuid'], data)
-
-      # other user cannot view document
-      response = self.client_not_me.get('/desktop/api2/doc/', {'uuid': doc.uuid})
-      data = json.loads(response.content)
-      assert_equal(-1, data['status'])
-
-      # Share read perm by users
-      response = self.c.post("/desktop/api2/doc/share", {
-          'uuid': json.dumps(doc.uuid),
-          'data': json.dumps({
-              'read': {
-                  'user_ids': [
-                      self.user.id,
-                      self.user_not_me.id
-                  ],
-                  'group_ids': [],
-              },
-              'write': {
-                  'user_ids': [],
-                  'group_ids': [],
-              }
-          })
-      })
-
-      assert_equal(0, json.loads(response.content)['status'], response.content)
-      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))
-
-      # other user can view document
-      response = self.client_not_me.get('/desktop/api2/doc/', {'uuid': doc.uuid})
-      data = json.loads(response.content)
-      assert_equal(doc.uuid, data['document']['uuid'], data)
-
-      # other user can open dashboard
-      response = self.c.post(reverse('search:search'), {
-          'collection': json.dumps(self._get_collection_param(self.collection)),
-          'query': json.dumps(QUERY)
-      })
-
-      data = json.loads(response.content)
-      assert_true('response' in data, data)
-      assert_true('docs' in data['response'], data)
+    doc = Document2.objects.create(name='test_dashboard', type='search-dashboard', owner=self.user,
+                                   data=self.collection.data, parent_directory=self.home_dir)
+
+    # owner can view document
+    response = self.c.get('/desktop/api2/doc/', {'uuid': doc.uuid})
+    data = json.loads(response.content)
+    assert_equal(doc.uuid, data['document']['uuid'], data)
+
+    # other user cannot view document
+    response = self.client_not_me.get('/desktop/api2/doc/', {'uuid': doc.uuid})
+    data = json.loads(response.content)
+    assert_equal(-1, data['status'])
+
+    # There are no collections with user_not_me
+    search_controller = SearchController(self.user_not_me)
+    hue_collections = search_controller.get_search_collections()
+    assert_true(len(hue_collections) == 0)
+
+    # Share read perm by users
+    response = self.c.post("/desktop/api2/doc/share", {
+        'uuid': json.dumps(doc.uuid),
+        'data': json.dumps({
+            'read': {
+                'user_ids': [
+                    self.user.id,
+                    self.user_not_me.id
+                ],
+                'group_ids': [],
+            },
+            'write': {
+                'user_ids': [],
+                'group_ids': [],
+            }
+        })
+    })
+    assert_equal(0, json.loads(response.content)['status'], response.content)
+    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))
+
+    # other user can view document
+    response = self.client_not_me.get('/desktop/api2/doc/', {'uuid': doc.uuid})
+    data = json.loads(response.content)
+    assert_equal(doc.uuid, data['document']['uuid'], data)
+
+    # other user can open dashboard
+    response = self.c.post(reverse('search:search'), {
+        'collection': json.dumps(self._get_collection_param(self.collection)),
+        'query': json.dumps(QUERY)
+    })
+
+    data = json.loads(response.content)
+    assert_true('response' in data, data)
+    assert_true('docs' in data['response'], data)
+
+    # For self.user_not_me
+    search_controller = SearchController(self.user_not_me)
+    hue_collections = search_controller.get_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard')
+
+    hue_collections = search_controller.get_owner_search_collections()
+    assert_equal(len(hue_collections), 0)
+
+    hue_collections = search_controller.get_shared_search_collections()
+    assert_equal(len(hue_collections), 0)
+
+    # For self.user
+    search_controller = SearchController(self.user)
+    hue_collections = search_controller.get_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard')
+
+    hue_collections = search_controller.get_owner_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard')
+
+    hue_collections = search_controller.get_shared_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard')
+
+    user_not_me_home_dir = Document2.objects.get_home_directory(user=self.user_not_me)
+    doc1 = Document2.objects.create(name='test_dashboard1', type='search-dashboard', owner=self.user_not_me,
+                                   data=self.collection.data, parent_directory=user_not_me_home_dir)
+    # self.user_not_me can view document
+    response = self.client_not_me.get('/desktop/api2/doc/', {'uuid': doc1.uuid})
+    data = json.loads(response.content)
+    assert_equal(doc1.uuid, data['document']['uuid'], data)
+
+    # self.user cannot view document
+    response = self.c.get('/desktop/api2/doc/', {'uuid': doc1.uuid})
+    data = json.loads(response.content)
+    assert_equal(-1, data['status'])
+
+    # Share read perm by users
+    response = self.client_not_me.post("/desktop/api2/doc/share", {
+        'uuid': json.dumps(doc1.uuid),
+        'data': json.dumps({
+            'read': {
+                'user_ids': [
+                    self.user.id,
+                ],
+                'group_ids': [],
+            },
+            'write': {
+                'user_ids': [],
+                'group_ids': [],
+            }
+        })
+    })
+    assert_equal(0, json.loads(response.content)['status'], response.content)
+    assert_true(doc1.can_read(self.user))
+    assert_false(doc1.can_write(self.user))
+    assert_true(doc1.can_read(self.user_not_me))
+    assert_true(doc1.can_write(self.user_not_me))
+
+    # For self.user_not_me
+    search_controller = SearchController(self.user_not_me)
+    hue_collections = search_controller.get_search_collections()
+    assert_equal(len(hue_collections), 2)
+
+    hue_collections = search_controller.get_owner_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard1')
+
+    hue_collections = search_controller.get_shared_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard1')
+
+    # For self.user
+    search_controller = SearchController(self.user)
+    hue_collections = search_controller.get_search_collections()
+    assert_equal(len(hue_collections), 2)
+
+    hue_collections = search_controller.get_owner_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard')
+
+    hue_collections = search_controller.get_shared_search_collections()
+    assert_equal(len(hue_collections), 1)
+    assert_equal(hue_collections[0].name, 'test_dashboard')
+
 
   def test_update_document(self):
     # Regular user