Browse Source

[slack] Delete doc in UT and fix pylint issues

Harshg999 4 years ago
parent
commit
76a8f08ab2

+ 1 - 1
desktop/core/src/desktop/lib/botserver/views.py

@@ -110,7 +110,7 @@ def handle_on_link_shared(channel_id, message_ts, links):
     payload = _make_unfurl_payload(item['url'], statement, dialect, created_by)
     payload = _make_unfurl_payload(item['url'], statement, dialect, created_by)
     response = slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload)
     response = slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload)
     if not response['ok']:
     if not response['ok']:
-        raise PopupException(_("Cannot unfurl link"), detail=response["error"])
+      raise PopupException(_("Cannot unfurl link"), detail=response["error"])
 
 
 def _make_unfurl_payload(url, statement, dialect, created_by):
 def _make_unfurl_payload(url, statement, dialect, created_by):
   payload = {
   payload = {

+ 16 - 10
desktop/core/src/desktop/lib/botserver/views_tests.py

@@ -73,11 +73,11 @@ class TestBotServer(unittest.TestCase):
   def test_handle_on_link_shared(self):
   def test_handle_on_link_shared(self):
     with patch('desktop.lib.botserver.views.slack_client.chat_unfurl') as chat_unfurl:
     with patch('desktop.lib.botserver.views.slack_client.chat_unfurl') as chat_unfurl:
       with patch('desktop.lib.botserver.views._make_unfurl_payload') as mock_unfurl_payload:
       with patch('desktop.lib.botserver.views._make_unfurl_payload') as mock_unfurl_payload:
-
+        
         client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
         client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
         user = User.objects.get(username="test")
         user = User.objects.get(username="test")
         channel_id = "channel_id"
         channel_id = "channel_id"
-        message_ts = "12345.123"
+        message_ts = "123.1"
 
 
         # qhistory link
         # qhistory link
         links = [{"url": "https://demo.gethue.com/hue/editor?editor=123456"}]
         links = [{"url": "https://demo.gethue.com/hue/editor?editor=123456"}]
@@ -88,20 +88,26 @@ class TestBotServer(unittest.TestCase):
             "statement_raw": "SELECT 5000",
             "statement_raw": "SELECT 5000",
           }]
           }]
         }
         }
-
-        Document2.objects.create(id=123456, data=json.dumps(doc_data), owner=user)
-        handle_on_link_shared(channel_id, message_ts, links)
+        doc = Document2.objects.create(id=123456, data=json.dumps(doc_data), owner=user)
+        try:
+          handle_on_link_shared(channel_id, message_ts, links)
+        finally:
+          doc.delete()
         mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 5000", "Mysql", "test")
         mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 5000", "Mysql", "test")
         assert_true(chat_unfurl.called)
         assert_true(chat_unfurl.called)
 
 
         # gist link
         # gist link
         doc_data = {"statement_raw": "SELECT 98765"}
         doc_data = {"statement_raw": "SELECT 98765"}
-        gist_doc = Document2.objects.create(id=101010, data=json.dumps(doc_data), owner=user, extra='mysql', type='gist')
-        links = [{"url": "http://demo.gethue.com/hue/gist?uuid="+str(gist_doc.uuid)}]
-        handle_on_link_shared(channel_id, message_ts, links)
+        links = []
+        try:
+          gist_doc = Document2.objects.create(id=101010, data=json.dumps(doc_data), owner=user, extra='mysql', type='gist')
+          links = [{"url": "http://demo.gethue.com/hue/gist?uuid="+str(gist_doc.uuid)}]
+          handle_on_link_shared(channel_id, message_ts, links)
+        finally:
+          gist_doc.delete()
         mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 98765", "Mysql", "test")
         mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 98765", "Mysql", "test")
         assert_true(chat_unfurl.called)
         assert_true(chat_unfurl.called)
 
 
         # Cannot unfurl link
         # Cannot unfurl link
-        assert_raises(PopupException, handle_on_link_shared, "channel_id", "12345.123", [{"url": "https://demo.gethue.com/hue/editor/?type=4"}])
-        assert_raises(PopupException, handle_on_link_shared, "channel_id", "12345.123", [{"url": "http://demo.gethue.com/hue/gist?uuids/=something"}])
+        assert_raises(PopupException, handle_on_link_shared, "channel_id", "123.1", [{"url": "https://demo.gethue.com/hue/editor/?type=4"}])
+        assert_raises(PopupException, handle_on_link_shared, "channel_id", "123.1", [{"url": "http://demo.gethue.com/hue/gist?uuids/=xyz"}])