Explorar el Código

[slack] Remove get_bot_id() and its UT along with SkipTest

Harshg999 hace 4 años
padre
commit
b9b0632ef2

+ 3 - 18
desktop/core/src/desktop/lib/botserver/views.py

@@ -31,12 +31,10 @@ LOG = logging.getLogger(__name__)
 SLACK_VERIFICATION_TOKEN = conf.SLACK.SLACK_VERIFICATION_TOKEN.get()
 SLACK_BOT_USER_TOKEN = conf.SLACK.SLACK_BOT_USER_TOKEN.get()
 
-slack_client, appname = None, None
+slack_client = None
 if conf.SLACK.IS_ENABLED.get():
   from slack_sdk import WebClient
   slack_client = WebClient(token=SLACK_BOT_USER_TOKEN)
-  appname = "hue_bot"
-
 
 @login_notrequired
 @csrf_exempt
@@ -66,12 +64,8 @@ def parse_events(event_message):
   text = event_message.get('text')
   channel = event_message.get('channel')
 
-  BOT_ID = None
-  if appname is not None:
-    BOT_ID = get_bot_id(appname)
-
-  # ignore bot's own message
-  if BOT_ID == user_id:
+  # ignore bot's own message since that will cause an infinite loop of messages if we respond
+  if event_message.get('bot_id'):
     return HttpResponse(status=200)
   
   if slack_client is not None:
@@ -88,12 +82,3 @@ def say_hi_user(channel, user_id):
 
   bot_message = 'Hi <@{}> :wave:'.format(user_id)
   return slack_client.api_call(api_method='chat.postMessage', json={'channel': channel, 'text': bot_message})
-
-def get_bot_id(botusername):
-  """Takes in bot username, Returns the bot id"""
-  
-  response = slack_client.api_call('users.list')
-  users = response['members']
-  for user in users:
-    if botusername in user.get('name', '') and not user.get('deleted'):
-      return user.get('id')

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

@@ -21,6 +21,7 @@ import unittest
 import sys
 
 from nose.tools import assert_equal, assert_true
+from nose.plugins.skip import SkipTest
 from django.test import TestCase, Client
 from desktop.lib.botserver.views import *
 from desktop import conf
@@ -32,36 +33,17 @@ else:
 
 LOG = logging.getLogger(__name__)
 
-if conf.SLACK.IS_ENABLED.get():
-  class TestBotServer(unittest.TestCase):
-    def test_get_bot_id(self):
-      with patch('desktop.lib.botserver.views.slack_client.api_call') as api_call:
-        api_call.return_value = {
-          'members': [
-            {
-              'name': 'hue_bot',
-              'deleted': False,
-              'id': 'U01K99VEDR9'
-            }
-          ]
-        }
-        assert_equal(get_bot_id('hue_bot'), 'U01K99VEDR9')
+class TestBotServer(unittest.TestCase):
+  
+  @classmethod
+  def setUpClass(cls):
+    if not conf.SLACK.IS_ENABLED.get():
+      raise SkipTest
 
-        api_call.return_value = {
-          'members': [
-            {
-              'name': 'hue_bot',
-              'deleted': True,
-              'id': 'U01K99VEDR9'
-            }
-          ]
-        }
-        assert_equal(get_bot_id('hue_bot'), None)
-
-    def test_say_hi_user(self):
-      with patch('desktop.lib.botserver.views.slack_client.api_call') as api_call:
-        api_call.return_value = {
-          "ok": True
-        }
-        response = say_hi_user("channel", "user_id")
-        assert_true(response['ok'])
+  def test_say_hi_user(self):
+    with patch('desktop.lib.botserver.views.slack_client.api_call') as api_call:
+      api_call.return_value = {
+        "ok": True
+      }
+      response = say_hi_user("channel", "user_id")
+      assert_true(response['ok'])