From e517c8edca2e1ad5eb12577df23e8dc6c11bb86d Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 27 Jul 2018 11:55:56 +0100
Subject: [PATCH] Add tests for the slack configuration

---
 opentech/apply/activity/messaging.py          | 11 +++--
 .../apply/activity/tests/test_messaging.py    | 48 +++++++++++++++++++
 requirements.txt                              |  1 +
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/opentech/apply/activity/messaging.py b/opentech/apply/activity/messaging.py
index ecfcd71bb..62f769575 100644
--- a/opentech/apply/activity/messaging.py
+++ b/opentech/apply/activity/messaging.py
@@ -29,7 +29,12 @@ class AdapterBase:
     always_send = False
 
     def message(self, message_type, **kwargs):
-        message = self.messages[message_type]
+        try:
+            message = self.messages[message_type]
+        except KeyError:
+            # We don't know how to handle that message type
+            return
+
         try:
             # see if its a method on the adapter
             method = getattr(self, message)
@@ -127,14 +132,14 @@ class SlackAdapter(AdapterBase):
         return f'<{user.slack}>'
 
     def send_message(self, message, **kwargs):
-        if not self.destination and not self.target_room:
+        if not self.destination or not self.target_room:
             return
 
         data = {
             "room": self.target_room,
             "message": message,
         }
-        requests.post(self.destination, data=data)
+        requests.post(self.destination, json=data)
 
 
 class MessengerBackend:
diff --git a/opentech/apply/activity/tests/test_messaging.py b/opentech/apply/activity/tests/test_messaging.py
index adbd07a09..c0fa21b54 100644
--- a/opentech/apply/activity/tests/test_messaging.py
+++ b/opentech/apply/activity/tests/test_messaging.py
@@ -1,5 +1,8 @@
+import json
 from unittest.mock import Mock, patch
 
+import responses
+
 from django.test import TestCase, override_settings
 from django.contrib.messages import get_messages
 
@@ -13,6 +16,7 @@ from ..messaging import (
     ActivityAdapter,
     MessengerBackend,
     MESSAGES,
+    SlackAdapter,
 )
 
 
@@ -149,3 +153,47 @@ class TestActivityAdapter(TestCase):
         self.assertTrue('Removed' in message)
         self.assertTrue('1' in message)
         self.assertTrue('2' in message)
+
+
+class TestSlackAdapter(TestCase):
+    target_url = 'https://my-slack-backend.com/incoming/my-very-secret-key'
+    target_room = '<ROOM ID>'
+
+    @override_settings(
+        SLACK_DESTINATION_URL=target_url,
+        SLACK_DESTINATION_ROOM=None,
+    )
+    @responses.activate
+    def test_cant_send_with_no_room(self):
+        adapter = SlackAdapter()
+        adapter.send_message('my message')
+        self.assertEqual(len(responses.calls), 0)
+
+    @override_settings(
+        SLACK_DESTINATION_URL=None,
+        SLACK_DESTINATION_ROOM=target_room,
+    )
+    @responses.activate
+    def test_cant_send_with_no_url(self):
+        adapter = SlackAdapter()
+        adapter.send_message('my message')
+        self.assertEqual(len(responses.calls), 0)
+
+    @override_settings(
+        SLACK_DESTINATION_URL=target_url,
+        SLACK_DESTINATION_ROOM=target_room,
+    )
+    @responses.activate
+    def test_correct_payload(self):
+        responses.add(responses.POST, self.target_url, status=200)
+        adapter = SlackAdapter()
+        message = 'my message'
+        adapter.send_message(message)
+        self.assertEqual(len(responses.calls), 1)
+        self.assertDictEqual(
+            json.loads(responses.calls[0].request.body),
+            {
+                'room': self.target_room,
+                'message': message,
+            }
+        )
diff --git a/requirements.txt b/requirements.txt
index c70c8301f..beb571c64 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -16,6 +16,7 @@ django-hijack==2.1.9
 factory_boy==2.9.2
 # wagtail_factories - waiting on merge and release form master branch
 git+git://github.com/mvantellingen/wagtail-factories.git#egg=wagtail_factories
+responses == 0.9.0
 
 flake8
 
-- 
GitLab