Browse Source

HUe-950 [desktop] Adding tests

Romain Rigaux 12 years ago
parent
commit
64c6736a4b

+ 2 - 0
desktop/core/src/desktop/management/commands/test.py

@@ -73,6 +73,8 @@ class Command(BaseCommand):
 
 
     # Patch South things in
     # Patch South things in
     south.management.commands.patch_for_test_db_setup()
     south.management.commands.patch_for_test_db_setup()
+    south_logger = logging.getLogger('south')
+    south_logger.setLevel(logging.INFO)
 
 
     if len(args) == 0:
     if len(args) == 0:
       print self.help
       print self.help

+ 30 - 5
desktop/core/src/desktop/models.py

@@ -66,7 +66,7 @@ class DocumentTagManager(models.Manager):
   def add_or_create_tag(self, owner, doc_id, tag_name, tag_id=None):
   def add_or_create_tag(self, owner, doc_id, tag_name, tag_id=None):
     try:
     try:
       tag = DocumentTag.objects.get(id=tag_id, owner=owner)
       tag = DocumentTag.objects.get(id=tag_id, owner=owner)
-      if tag.id == DocumentTag.get_trash_tag(owner):
+      if tag == DocumentTag.get_trash_tag(owner):
         raise Exception(_("Can't add trash tag. Please trash the document from instead."))
         raise Exception(_("Can't add trash tag. Please trash the document from instead."))
     except:
     except:
       tag = DocumentTag.objects.create(tag=tag_name, owner=owner)
       tag = DocumentTag.objects.create(tag=tag_name, owner=owner)
@@ -77,7 +77,7 @@ class DocumentTagManager(models.Manager):
 
 
   def remove_tag(self, tag_id, owner, doc_id):
   def remove_tag(self, tag_id, owner, doc_id):
     tag = DocumentTag.objects.get(id=tag_id, owner=owner)
     tag = DocumentTag.objects.get(id=tag_id, owner=owner)
-    if tag.id == DocumentTag.get_trash_tag(owner):
+    if tag == DocumentTag.get_trash_tag(owner):
       raise Exception(_("Can't remove trash tag. Please restore the document from the trash instead."))
       raise Exception(_("Can't remove trash tag. Please restore the document from the trash instead."))
 
 
     doc = Document.objects.get_doc(doc_id, owner=owner)
     doc = Document.objects.get_doc(doc_id, owner=owner)
@@ -85,6 +85,31 @@ class DocumentTagManager(models.Manager):
     if tag.tag != DocumentTag.TRASH and not tag.document_set.exists():
     if tag.tag != DocumentTag.TRASH and not tag.document_set.exists():
       tag.delete()
       tag.delete()
 
 
+  def delete_tag(self, tag_id, owner):
+    tag = DocumentTag.objects.get(id=tag_id, owner=owner)
+    if tag == DocumentTag.get_trash_tag(owner):
+      raise Exception(_("Can't remove trash tag. Please restore the document from the trash instead."))
+    
+    for doc in Document.objects.get_docs(owner):
+      doc.remove_tag(tag)
+    if tag.tag != DocumentTag.TRASH and not tag.document_set.exists():
+      tag.delete()
+
+  @classmethod
+  def update_tags(self, owner, doc_id, tag_ids):
+    doc = Document.objects.get_doc(doc_id, owner)
+    
+    for tag in doc.tags.all():
+      if tag.tag != DocumentTag.TRASH:
+        doc.remove_tag(tag)
+    
+    for tag_id in tag_ids:
+      tag = DocumentTag.objects.get(id=tag_id, owner=owner)
+      if tag.tag != DocumentTag.TRASH:
+        doc.add_tag(tag)
+    
+    return doc
+
 
 
 class DocumentTag(models.Model):
 class DocumentTag(models.Model):
   owner = models.ForeignKey(auth_models.User, db_index=True)
   owner = models.ForeignKey(auth_models.User, db_index=True)
@@ -184,7 +209,7 @@ class DocumentManager(models.Manager):
           if job.is_shared:
           if job.is_shared:
             DocumentPermission.share_to_default(doc)
             DocumentPermission.share_to_default(doc)
     except Exception, e:
     except Exception, e:
-      print e
+      LOG.warn(force_unicode(e))
 
 
     try:
     try:
       for job in SavedQuery.objects.all():
       for job in SavedQuery.objects.all():
@@ -195,7 +220,7 @@ class DocumentManager(models.Manager):
           if job.is_trashed:
           if job.is_trashed:
             doc.send_to_trash()
             doc.send_to_trash()
     except Exception, e:
     except Exception, e:
-      print e
+      LOG.warn(force_unicode(e))
 
 
     try:
     try:
       from pig.models import PigScript
       from pig.models import PigScript
@@ -206,7 +231,7 @@ class DocumentManager(models.Manager):
           tag, created = DocumentTag.objects.get_or_create(owner=job.owner, tag='default')
           tag, created = DocumentTag.objects.get_or_create(owner=job.owner, tag='default')
           doc.tags.add(tag)
           doc.tags.add(tag)
     except Exception, e:
     except Exception, e:
-      print e
+      LOG.warn(force_unicode(e))
 
 
 
 
 class Document(models.Model):
 class Document(models.Model):

+ 3 - 3
desktop/core/src/desktop/templates/home.mako

@@ -77,13 +77,13 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
               <a href="#" data-toggle="dropdown"><i class="icon-plus-sign"></i> ${_('New document')}</a>
               <a href="#" data-toggle="dropdown"><i class="icon-plus-sign"></i> ${_('New document')}</a>
               <ul class="dropdown-menu" role="menu">
               <ul class="dropdown-menu" role="menu">
                 % if 'beeswax' in apps:
                 % if 'beeswax' in apps:
-                <li><a href="#"><img src="${ apps['beeswax'].icon_path }"/> ${_('Hive Query')}</a></li>
+                <li><a href="${ url('beeswax:index') }"><img src="${ apps['beeswax'].icon_path }"/> ${_('Hive Query')}</a></li>
                 % endif
                 % endif
                 % if 'impala' in apps:
                 % if 'impala' in apps:
-                <li><a href="#"><img src="${ apps['impala'].icon_path }"/> ${_('Impala Query')}</a></li>
+                <li><a href="${ url('impala:index') }"><img src="${ apps['impala'].icon_path }"/> ${_('Impala Query')}</a></li>
                 % endif
                 % endif
                 % if 'pig' in apps:
                 % if 'pig' in apps:
-                <li><a href="#"><img src="${ apps['pig'].icon_path }"/> ${_('Pig Script')}</a></li>
+                <li><a href="${ url('beeswax:index') }"><img src="${ apps['pig'].icon_path }"/> ${_('Pig Script')}</a></li>
                 % endif
                 % endif
                 % if 'sqoop' in apps:
                 % if 'sqoop' in apps:
                 <li><a href="#"><img src="${ apps['sqoop'].icon_path }"/> ${_('Sqoop Transfer')}</a></li>
                 <li><a href="#"><img src="${ apps['sqoop'].icon_path }"/> ${_('Sqoop Transfer')}</a></li>

+ 65 - 0
desktop/core/src/desktop/tests.py

@@ -20,6 +20,7 @@ import desktop
 import desktop.urls
 import desktop.urls
 import desktop.conf
 import desktop.conf
 import logging
 import logging
+import json
 import os
 import os
 import time
 import time
 
 
@@ -29,6 +30,7 @@ import proxy.conf
 from nose.plugins.attrib import attr
 from nose.plugins.attrib import attr
 from nose.tools import assert_true, assert_equal, assert_not_equal, assert_raises
 from nose.tools import assert_true, assert_equal, assert_not_equal, assert_raises
 from django.conf.urls.defaults import patterns, url
 from django.conf.urls.defaults import patterns, url
+from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
 from django.http import HttpResponse
 from django.http import HttpResponse
 from django.db.models import query, CharField, SmallIntegerField
 from django.db.models import query, CharField, SmallIntegerField
@@ -485,3 +487,66 @@ def test_check_config_ajax():
   c = make_logged_in_client()
   c = make_logged_in_client()
   response = c.get(reverse(check_config))
   response = c.get(reverse(check_config))
   assert_true("misconfiguration" in response.content, response.content)
   assert_true("misconfiguration" in response.content, response.content)
+
+
+class TestDocModelTags():
+  def setUp(self):
+    self.client = make_logged_in_client(username="tag_user")
+    self.client_not_me = make_logged_in_client(username="not_tag_user")
+    
+    self.user = User.objects.get(username="tag_user")
+    self.user_not_me = User.objects.get(username="not_tag_user")
+
+    grant_access(self.user.username, self.user.username, "desktop")
+    grant_access(self.user_not_me.username, self.user_not_me.username, "desktop")        
+
+  def test_add_tag(self):
+    response = self.client.get("/desktop/add_tag")    
+    assert_equal(-1, json.loads(response.content).status)
+
+    response = self.client.post("/desktop/add_tag", {'name': 'my_tag'})    
+    assert_equal(0, json.loads(response.content).status)
+  
+  def test_remove_tags(self):
+    response = self.client.get("/desktop/remove_tags")    
+    assert_equal(-1, json.loads(response.content).status)
+
+    response = self.client.post("/desktop/remove_tags", {'data': json.dumps({'tag_ids': []})})    
+    assert_equal(0, json.loads(response.content).status)  
+  
+  def test_list_tags(self):
+    response = self.client.get("/desktop/list_tags")        
+    assert_equal({}, json.loads(response.content))    
+  
+  def test_list_docs(self):
+    response = self.client.get("/desktop/list_docs")        
+    assert_equal({}, json.loads(response.content))   
+  
+  def test_add_or_create_tag(self):
+    response = self.client.get("/desktop/add_or_create_tag$")        
+    assert_equal({}, json.loads(response.content))   
+
+  def test_update_tags(self):
+    response = self.client.get("/desktop/update_tags")        
+    assert_equal({}, json.loads(response.content))  
+
+
+
+class TestDocModelPermissions():
+  def setUp(self):
+    self.client = make_logged_in_client(username="perm_user")
+    self.client_not_me = make_logged_in_client(username="not_perm_user")
+    
+    self.user = User.objects.get(username="perm_user")
+    self.user_not_me = User.objects.get(username="not_perm_user")
+
+    grant_access(self.user.username, self.user.username, "desktop")
+    grant_access(self.user_not_me.username, self.user_not_me.username, "desktop")        
+
+  def test_add_or_update_permission(self):
+    response = self.client.get("/desktop/add_or_update_permission")    
+    assert_equal(-1, json.loads(response.content).status)
+
+  def test_remove_permission(self):
+    response = self.client.get("/desktop/remove_permission")    
+    assert_equal(-1, json.loads(response.content).status)

+ 4 - 3
desktop/core/src/desktop/urls.py

@@ -69,12 +69,13 @@ dynamic_patterns += patterns('desktop.views',
   (r'^log_frontend_event$', 'log_frontend_event'),
   (r'^log_frontend_event$', 'log_frontend_event'),
 
 
   # Doc model
   # Doc model
-  (r'^tag/add_tag', 'add_tag'),
-  (r'^tag/remove_tags', 'remove_tags'),
-  (r'^tag/list_tags', 'list_tags'),
+  (r'^tag/add_tag$', 'add_tag'),
+  (r'^tag/remove_tags$', 'remove_tags'),
+  (r'^tag/list_tags$', 'list_tags'),
   (r'^doc/list_docs$', 'list_docs'),
   (r'^doc/list_docs$', 'list_docs'),
   (r'^doc/add_or_create_tag$', 'add_or_create_tag'),
   (r'^doc/add_or_create_tag$', 'add_or_create_tag'),
   (r'^doc/update_tags$', 'update_tags'),
   (r'^doc/update_tags$', 'update_tags'),
+
   (r'^doc/add_or_update_permission$', 'add_or_update_permission'),
   (r'^doc/add_or_update_permission$', 'add_or_update_permission'),
   (r'^doc/remove_permission$', 'remove_permission'),
   (r'^doc/remove_permission$', 'remove_permission'),
 
 

+ 3 - 6
desktop/core/src/desktop/views.py

@@ -144,9 +144,8 @@ def update_tags(request):
   if request.method == 'POST':
   if request.method == 'POST':
     request_json = json.loads(request.POST['data'])
     request_json = json.loads(request.POST['data'])
     try:
     try:
-      print request_json
-      #doc = DocumentTag.objects.update_tags(request.user, request_json['doc_id'], request_json['tag_ids'])
-      #response['doc'] = massage_doc_for_json(doc)
+      doc = DocumentTag.objects.update_tags(request.user, request_json['doc_id'], request_json['tag_ids'])
+      response['doc'] = massage_doc_for_json(doc)
     except Exception, e:
     except Exception, e:
       response['message'] = force_unicode(e)
       response['message'] = force_unicode(e)
   else:
   else:
@@ -160,11 +159,9 @@ def remove_tags(request):
 
 
   if request.method == 'POST':
   if request.method == 'POST':
     request_json = json.loads(request.POST['data'])
     request_json = json.loads(request.POST['data'])
-    print request_json
     try:
     try:
       for tag_id in request_json['tag_ids']:
       for tag_id in request_json['tag_ids']:
-        print tag_id
-        #DocumentTag.delete_tag(tag_id, request.user)
+        DocumentTag.objects.delete_tag(tag_id, request.user)
       response['message'] = _('Tag removed !')
       response['message'] = _('Tag removed !')
     except Exception, e:
     except Exception, e:
       response['message'] = force_unicode(e)
       response['message'] = force_unicode(e)