[slack] Update link_shared UT by mocking document
@@ -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,6 +73,8 @@ 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")
@@ -88,23 +90,16 @@ class TestBotServer(unittest.TestCase):
"statement_raw": "SELECT 5000",
}]
}
- doc = Document2.objects.create(id=123456, data=json.dumps(doc_data), owner=user)
- try:
+ document2_objects_get.return_value = Mock(data=json.dumps(doc_data), owner=user)
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)
# 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)}]
+ _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)
- finally:
- gist_doc.delete()
mock_unfurl_payload.assert_called_with(links[0]["url"], "SELECT 98765", "Mysql", "test")
assert_true(chat_unfurl.called)
