From 5fe13b250757491b6bd2d30b323218665ad65f18 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Mon, 30 Jul 2018 11:15:33 +0100
Subject: [PATCH] Make sure that recipients works for all adapters

---
 opentech/apply/activity/messaging.py              |  5 ++---
 opentech/apply/activity/tests/test_messaging.py   | 14 ++++++++++++++
 opentech/apply/determinations/forms.py            |  5 -----
 opentech/apply/determinations/tests/test_views.py |  2 +-
 opentech/apply/determinations/views.py            |  2 +-
 opentech/apply/funds/models.py                    |  1 -
 opentech/apply/users/tests/factories.py           |  8 ++++++++
 7 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/opentech/apply/activity/messaging.py b/opentech/apply/activity/messaging.py
index 2a80763a3..38115ea0b 100644
--- a/opentech/apply/activity/messaging.py
+++ b/opentech/apply/activity/messaging.py
@@ -61,10 +61,9 @@ class AdapterBase:
         if not message:
             return
 
-        recipients = self.recipients(message_type, **kwargs)
         kwargs.update(self.extra_kwargs(message_type, **kwargs))
 
-        for recipient in recipients:
+        for recipient in self.recipients(message_type, **kwargs):
             if settings.SEND_MESSAGES or self.always_send:
                 self.send_message(message, recipient=recipient, **kwargs)
 
@@ -142,7 +141,7 @@ class SlackAdapter(AdapterBase):
         link = link_to(submission, request)
         return {'link': link}
 
-    def recipients(self, message_type, message, **kwargs):
+    def recipients(self, message_type, submission, **kwargs):
         return [self.slack_id(submission.lead)]
 
     def notify_reviewers(self, submission, **kwargs):
diff --git a/opentech/apply/activity/tests/test_messaging.py b/opentech/apply/activity/tests/test_messaging.py
index 41ba22622..77c1452b4 100644
--- a/opentech/apply/activity/tests/test_messaging.py
+++ b/opentech/apply/activity/tests/test_messaging.py
@@ -203,6 +203,20 @@ class TestSlackAdapter(TestCase):
             }
         )
 
+    @responses.activate
+    def test_gets_lead_if_slack_set(self):
+        adapter = SlackAdapter()
+        submission = ApplicationSubmissionFactory()
+        recipients = adapter.recipients(MESSAGES.COMMENT, submission)
+        self.assertTrue(submission.lead.slack in recipients[0])
+
+    @responses.activate
+    def test_gets_black_if_slack_not_set(self):
+        adapter = SlackAdapter()
+        submission = ApplicationSubmissionFactory(lead__slack='')
+        recipients = adapter.recipients(MESSAGES.COMMENT, submission)
+        self.assertTrue(submission.lead.slack in recipients[0])
+
 
 @override_settings(SEND_MESSAGES=True)
 class TestEmailAdapter(TestCase):
diff --git a/opentech/apply/determinations/forms.py b/opentech/apply/determinations/forms.py
index b8d24a711..14a877393 100644
--- a/opentech/apply/determinations/forms.py
+++ b/opentech/apply/determinations/forms.py
@@ -1,13 +1,9 @@
 from django import forms
 from django.core.exceptions import NON_FIELD_ERRORS
 
-from opentech.apply.funds.workflow import DETERMINATION_OUTCOMES
 from .models import (
     Determination,
     DETERMINATION_CHOICES,
-    NEEDS_MORE_INFO,
-    REJECTED,
-    ACCEPTED,
     TRANSITION_DETERMINATION,
 )
 
@@ -101,7 +97,6 @@ class BaseDeterminationForm(forms.ModelForm):
 
         return available_choices
 
-
     @classmethod
     def get_detailed_response(cls, saved_data):
         data = {}
diff --git a/opentech/apply/determinations/tests/test_views.py b/opentech/apply/determinations/tests/test_views.py
index b0d064f8c..5f3f0c005 100644
--- a/opentech/apply/determinations/tests/test_views.py
+++ b/opentech/apply/determinations/tests/test_views.py
@@ -113,7 +113,7 @@ class DeterminationFormTestCase(BaseTestCase):
         submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user)
         determination = DeterminationFactory(submission=submission, author=self.user)
         determination_message = 'This is the message'
-        response = self.post_page(
+        self.post_page(
             submission,
             {'data': 'value', 'outcome': determination.outcome, 'message': determination_message},
             'form',
diff --git a/opentech/apply/determinations/views.py b/opentech/apply/determinations/views.py
index 7d15370c0..d39cfac32 100644
--- a/opentech/apply/determinations/views.py
+++ b/opentech/apply/determinations/views.py
@@ -1,5 +1,5 @@
 from django.contrib.auth.decorators import login_required
-from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
+from django.core.exceptions import PermissionDenied
 from django.http import HttpResponseRedirect
 from django.shortcuts import get_object_or_404
 from django.urls import reverse_lazy
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index b74f734fa..cb4024d94 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -12,7 +12,6 @@ from django.db.models import Q
 from django.db.models.expressions import RawSQL, OrderBy
 from django.dispatch import receiver
 from django.http import Http404
-from django.template.loader import render_to_string
 from django.urls import reverse
 from django.utils.text import mark_safe, slugify
 from django.utils.translation import ugettext_lazy as _
diff --git a/opentech/apply/users/tests/factories.py b/opentech/apply/users/tests/factories.py
index bbeead335..1ee80d85f 100644
--- a/opentech/apply/users/tests/factories.py
+++ b/opentech/apply/users/tests/factories.py
@@ -42,6 +42,14 @@ class AdminFactory(UserFactory):
 
 
 class StaffFactory(UserFactory):
+    class Meta:
+        exclude = ('slack_temp', )
+
+    # Required to generate the fake data add pass to LazyAttribute
+    slack_temp = factory.Faker('word')
+
+    slack = factory.LazyAttribute(lambda p: '@{}'.format(p.slack_temp))
+
     @factory.post_generation
     def groups(self, create, extracted, **kwargs):
         if create:
-- 
GitLab