Browse Source

[slack] Update link_shared UT by mocking document

Harshg999 4 years ago
parent
commit
623d751901
1 changed files with 31 additions and 36 deletions
  1. 31 36
      desktop/core/src/desktop/lib/botserver/views_tests.py

+ 31 - 36
desktop/core/src/desktop/lib/botserver/views_tests.py

@@ -32,9 +32,9 @@ from useradmin.models import User
 
 
 if sys.version_info[0] > 2:
-  from unittest.mock import patch
+  from unittest.mock import patch, Mock
 else:
-  from mock import patch
+  from mock import patch, Mock
 
 LOG = logging.getLogger(__name__)
 
@@ -73,41 +73,36 @@ class TestBotServer(unittest.TestCase):
   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:
+        with patch('desktop.lib.botserver.views.Document2.objects.get') as document2_objects_get:
+          with patch('desktop.lib.botserver.views._get_gist_document') as _get_gist_document:
         
-        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 = "123.1"
+            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 = "123.1"
 
-        # qhistory link
-        links = [{"url": "https://demo.gethue.com/hue/editor?editor=123456"}]
-        doc_data = {
-          "dialect": "mysql",
-          "snippets": [{
-            "database": "hue",
-            "statement_raw": "SELECT 5000",
-          }]
-        }
-        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")
-        assert_true(chat_unfurl.called)
+            # 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_get.return_value = Mock(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"}
-        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")
-        assert_true(chat_unfurl.called)
+            # gist link
+            doc_data = {"statement_raw": "SELECT 98765"}
+            _get_gist_document.return_value = Mock(data=json.dumps(doc_data), owner=user, extra='mysql')
+            links = [{"url": "http://demo.gethue.com/hue/gist?uuid=random"}]
+            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", "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"}])
+            # Cannot unfurl link
+            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"}])