Explorar o código

[slack] Add Document2.DoesNotExist and update UT

Harshg999 %!s(int64=4) %!d(string=hai) anos
pai
achega
101f7ee607

+ 14 - 11
desktop/core/src/desktop/lib/botserver/views.py

@@ -95,17 +95,20 @@ def handle_on_link_shared(channel_id, message_ts, links):
     path = urlsplit(item['url'])[2]
     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
+    try:
+      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
+    except Document2.DoesNotExist:
+      raise PopupException(_("Document with {key}={value} does not exist").format(key='uuid' if id_type == 'uuid' else 'id', value=qid_or_uuid))
 
     payload = _make_unfurl_payload(item['url'], statement, dialect, created_by)
     response = slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload)

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

@@ -107,6 +107,17 @@ class TestBotServer(unittest.TestCase):
             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", "12.1", [{"url": "https://demo.gethue.com/hue/editor/?type=4"}])
-            assert_raises(PopupException, handle_on_link_shared, "channel", "12.1", [{"url": "http://demo.gethue.com/hue/gist?uuids/=xyz"}])
+            # Cannot unfurl link with invalid links
+            inv_qhistory_url = "https://demo.gethue.com/hue/editor/?type=4"
+            inv_gist_url = "http://demo.gethue.com/hue/gist?uuids/=xyz"
+            assert_raises(PopupException, handle_on_link_shared, "channel", "12.1", [{"url": inv_qhistory_url}])
+            assert_raises(PopupException, handle_on_link_shared, "channel", "12.1", [{"url": inv_gist_url}])
+
+            # Document does not exist
+            document2_objects_get.side_effect = PopupException('message')
+            _get_gist_document.side_effect = PopupException('message')
+            
+            qhistory_url = "https://demo.gethue.com/hue/editor?editor=109644"
+            gist_url = "https://demo.gethue.com/hue/gist?uuid=6d1c407b-d999-4dfd-ad23-d3a46c19a427"
+            assert_raises(PopupException, handle_on_link_shared, "channel", "12.1", [{"url": qhistory_url}])
+            assert_raises(PopupException, handle_on_link_shared, "channel", "12.1", [{"url": gist_url}])