Browse Source

Initial views unit tests

Harshg999 4 years ago
parent
commit
f81cf4f22d

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

@@ -71,12 +71,14 @@ def parse_events(event_message):
     say_hi_user(channel, user_id)
   
 def say_hi_user(channel, user_id):
-  # App greets when user says "hello hue"
+  """Bot sends Hi<username> message in a specific channel"""
   bot_message = f'Hi <@{user_id}> :wave:'
-  slack_client.api_call(api_method='chat.postMessage', json={'channel': channel, 'text': bot_message})
-  return HttpResponse(status=200)
+  response = slack_client.api_call(api_method='chat.postMessage', json={'channel': channel, 'text': bot_message})
+  if response["ok"]:
+    return HttpResponse(status=200)
 
 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:

+ 24 - 5
desktop/core/src/desktop/lib/botserver/views_tests.py

@@ -21,7 +21,7 @@ import unittest
 import sys
 
 from nose.tools import assert_equal, assert_true
-from django.test import TestCase
+from django.test import TestCase, Client
 from desktop.lib.botserver.views import *
 
 if sys.version_info[0] > 2:
@@ -33,8 +33,8 @@ LOG = logging.getLogger(__name__)
 
 class TestBotServer(unittest.TestCase):
   def test_get_bot_id(self):
-    with patch('desktop.lib.botserver.views.slack_client') as slack_client_mock:
-      slack_client_mock.api_call("users.list").return_value = {
+    with patch('desktop.lib.botserver.views.slack_client.api_call') as api_call:
+      api_call.return_value = {
         'members': [
           {
             'name': 'hue_bot',
@@ -43,8 +43,27 @@ class TestBotServer(unittest.TestCase):
           }
         ]
       }
-      bot_id = get_bot_id('hue_bot')
-      assert_equal(bot_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_equal(response.status_code, 200)
+