Bläddra i källkod

[slack] update UTs with config flag

Harshg999 4 år sedan
förälder
incheckning
f9365e0a69

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

@@ -74,27 +74,26 @@ def parse_events(event_message):
   if BOT_ID == user_id:
     return HttpResponse(status=200)
   
-  if 'hello hue' in text.lower():
-    response = say_hi_user(channel, user_id)
-    if response['ok']:
-      return HttpResponse(status=200)
-    else:
-      raise PopupException(response["error"])
+  if slack_client is not None:
+    if 'hello hue' in text.lower():
+      response = say_hi_user(channel, user_id)
+      if response['ok']:
+        return HttpResponse(status=200)
+      else:
+        raise PopupException(response["error"])
 
   
 def say_hi_user(channel, user_id):
   """Bot sends Hi<username> message in a specific channel"""
 
   bot_message = 'Hi <@{}> :wave:'.format(user_id)
-  if slack_client is not None:
-    return slack_client.api_call(api_method='chat.postMessage', json={'channel': channel, 'text': bot_message})
+  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"""
   
-  if slack_client is not None:
-    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')
+  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')

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

@@ -23,6 +23,7 @@ import sys
 from nose.tools import assert_equal, assert_true
 from django.test import TestCase, Client
 from desktop.lib.botserver.views import *
+from desktop import conf
 
 if sys.version_info[0] > 2:
   from unittest.mock import patch
@@ -31,44 +32,36 @@ else:
 
 LOG = logging.getLogger(__name__)
 
-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')
-
-      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'])
-      
-
-  
-  
-
-
-
+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')
 
+        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'])