Browse Source

[slack] Aggregate link_unfurl logic and its UTs

Harshg999 4 years ago
parent
commit
41da052aff

+ 29 - 80
desktop/core/src/desktop/lib/botserver/views.py

@@ -78,7 +78,7 @@ def parse_events(event):
 
 
 def handle_on_message(channel_id, bot_id, text, user_id):
-  # ignore bot's own message since that will cause an infinite loop of messages if we respond.
+  # Ignore bot's own message since that will cause an infinite loop of messages if we respond.
   if bot_id:
     return HttpResponse(status=200)
   
@@ -93,84 +93,26 @@ def handle_on_message(channel_id, bot_id, text, user_id):
 def handle_on_link_shared(channel_id, message_ts, links):
   for item in links:
     path = urlsplit(item['url'])[2]
-    queryid_or_uuid = urlsplit(item['url'])[3]  # if /hue/editor/ then query_id else if /hue/gist then uuid
-
-    if path == '/hue/editor':
-      query_id = queryid_or_uuid.split('=')[1]
-      doc2 = Document2.objects.get(id=query_id)
-      doc2_data = json.loads(doc2.data)
-
-      statement = doc2_data['snippets'][0]['statement_raw']
-      dialect = doc2_data['dialect'].capitalize()
-      database = doc2_data['snippets'][0]['database'].capitalize()
-      
-      payload = make_query_history_payload(item['url'], statement, dialect, database)
-      response = slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload)
-      if response['ok']:
-        raise PopupException(_("Cannot unfurl query history link"), detail=response["error"])
-
-    if path == '/hue/gist' and ENABLE_GIST_PREVIEW.get():
-      gist_uuid = queryid_or_uuid.split('=')[1]
-      gist_doc = _get_gist_document(uuid=gist_uuid)
-      gist_doc_data = json.loads(gist_doc.data)
-
-      statement = gist_doc_data['statement_raw']
-      created_by = gist_doc.owner.get_full_name() or gist_doc.owner.username
-      dialect = gist_doc.extra.capitalize()
-      
-      payload = make_gist_payload(item['url'], statement, dialect, created_by)
-      response = slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload)
-      if not response['ok']:
-        raise PopupException(_("Cannot unfurl gist link"), detail=response["error"])
-
-def say_hi_user(channel_id, user_id):
-  """
-  Sends Hi<user_id> message in a specific channel.
-
-  """
-  bot_message = 'Hi <@{}> :wave:'.format(user_id)
-  return slack_client.api_call(api_method='chat.postMessage', json={'channel': channel_id, 'text': bot_message})
-
-
-def make_gist_payload(url, statement, dialect, created_by):
-  gist_payload = {
-    url: {
-      "color": "#025BA6",
-      "blocks": [
-        {
-          "type": "section",
-          "text": {
-            "type": "mrkdwn",
-            "text": "\n*<{}|Hue - SQL Gist>*".format(url)
-          }
-        },
-        {
-          "type": "section",
-          "text": {
-            "type": "mrkdwn",
-            "text": statement if len(statement) < 150 else (statement[:150] + '...')
-          }
-        },
-        {
-          "type": "section",
-          "fields": [
-            {
-              "type": "mrkdwn",
-              "text": "*Dialect:*\n{}".format(dialect)
-            },
-            {
-              "type": "mrkdwn",
-              "text": "*Created By:*\n{}".format(created_by)
-            }
-          ]
-        }
-      ]
-    }
-  }
-  return gist_payload
-
-
-def make_query_history_payload(url, statement, dialect, database):
+    id_type, qid_or_uuid = urlsplit(item['url'])[3].split('=')
+
+    if path == '/hue/editor' and id_type == 'editor':
+      doc = Document2.objects.get(id=qid_or_uuid)
+    elif path == '/hue/gist' and id_type == 'uuid' and ENABLE_GIST_PREVIEW.get():
+      doc = _get_gist_document(uuid=qid_or_uuid)
+    else:
+      raise PopupException(_("Cannot unfurl link"))
+
+    doc_data = json.loads(doc.data)
+    statement = doc_data['snippets'][0]['statement_raw'] if id_type == 'editor' else doc_data['statement_raw']
+    dialect = doc_data['dialect'].capitalize() if id_type == 'editor' else doc.extra.capitalize()
+    created_by = doc.owner.get_full_name() or doc.owner.username
+
+    payload = _make_unfurl_payload(item['url'], statement, dialect, created_by)
+    response = slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload)
+    if not response['ok']:
+        raise PopupException(_("Cannot unfurl link"), detail=response["error"])
+
+def _make_unfurl_payload(url, statement, dialect, created_by):
   payload = {
     url: {
       "color": "#025BA6",
@@ -198,7 +140,7 @@ def make_query_history_payload(url, statement, dialect, database):
             },
             {
               "type": "mrkdwn",
-              "text": "*Database:*\n{}".format(database)
+              "text": "*Created By:*\n{}".format(created_by)
             }
           ]
         }
@@ -207,3 +149,10 @@ def make_query_history_payload(url, statement, dialect, database):
   }
   return payload
 
+def say_hi_user(channel_id, user_id):
+  """
+  Sends Hi<user_id> message in a specific channel.
+
+  """
+  bot_message = 'Hi <@{}> :wave:'.format(user_id)
+  return slack_client.api_call(api_method='chat.postMessage', json={'channel': channel_id, 'text': bot_message})

+ 44 - 3
desktop/core/src/desktop/lib/botserver/views_tests.py

@@ -20,11 +20,16 @@ import logging
 import unittest
 import sys
 
-from nose.tools import assert_equal, assert_true, assert_false
+from nose.tools import assert_equal, assert_true, assert_false, assert_raises
 from nose.plugins.skip import SkipTest
-from django.test import TestCase, Client
+from django.test import TestCase
+
 from desktop.lib.botserver.views import *
 from desktop import conf
+from desktop.models import Document2, _get_gist_document
+from desktop.lib.django_test_util import make_logged_in_client
+from useradmin.models import User
+
 
 if sys.version_info[0] > 2:
   from unittest.mock import patch
@@ -63,4 +68,40 @@ class TestBotServer(unittest.TestCase):
       assert_false(say_hi_user.called)
 
       handle_on_message("channel", None, "hello hue test", "user_id")
-      assert_true(say_hi_user.called)
+      assert_true(say_hi_user.called)
+
+  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._make_unfurl_payload') as mock_unfurl_payload:
+
+        client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
+        user = User.objects.get(username="test")
+        channel_id = "channel_id"
+        message_ts = "12345.123"
+
+        # qhistory link
+        links = [{"url": "https://demo.gethue.com/hue/editor?editor=123456"}]
+        doc_data = {
+          "dialect": "mysql",
+          "snippets": [{
+            "database": "hue",
+            "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)
+        mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 5000", "Mysql", "test")
+        assert_true(chat_unfurl.called)
+
+        # gist link
+        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)
+        mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 98765", "Mysql", "test")
+        assert_true(chat_unfurl.called)
+
+        # 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"}])