From e1fd2a4632f061c20c4634271b997a05c5a218da Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 15 Aug 2019 13:02:31 +0100
Subject: [PATCH] Bugfix/ensure projects disbaled works correctly (#1415)

* Make the Projects Enabled consistent between local and remote testing

* Test that if projects are disabled we can still determine applications
---
 .circleci/config.yml                              |  1 -
 .travis.yml                                       |  1 -
 opentech/apply/determinations/tests/test_views.py | 10 +++++++++-
 opentech/apply/determinations/views.py            | 15 ++++++++-------
 opentech/settings/test.py                         |  2 ++
 5 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/.circleci/config.yml b/.circleci/config.yml
index c7e509566..5d1ba2991 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -13,7 +13,6 @@ jobs:
           PGUSER: root
           DJANGO_SETTINGS_MODULE: opentech.settings.test
           SEND_MESSAGES: false
-          PROJECTS_ENABLED: true
 
       - image: circleci/postgres:10.5
         environment:
diff --git a/.travis.yml b/.travis.yml
index d2e18cf9f..3a9983e24 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -21,7 +21,6 @@ env:
   global:
     - DJANGO_SETTINGS_MODULE=opentech.settings.test
     - DATABASE_URL=postgres://postgres@localhost/test_db
-    - PROJECTS_ENABLED=true
 
 before_script:
   # Create a database
diff --git a/opentech/apply/determinations/tests/test_views.py b/opentech/apply/determinations/tests/test_views.py
index 9adff7a7f..070836f76 100644
--- a/opentech/apply/determinations/tests/test_views.py
+++ b/opentech/apply/determinations/tests/test_views.py
@@ -2,7 +2,7 @@ import urllib
 
 from django.contrib.messages.storage.fallback import FallbackStorage
 from django.contrib.sessions.middleware import SessionMiddleware
-from django.test import RequestFactory
+from django.test import override_settings, RequestFactory
 from django.urls import reverse_lazy
 
 from opentech.apply.activity.models import Activity
@@ -88,6 +88,7 @@ class DeterminationFormTestCase(BaseViewTestCase):
         response = self.get_page(submission, 'form')
         self.assertNotContains(response, 'Update ')
 
+    @override_settings(PROJECTS_ENABLED=False)
     def test_can_edit_draft_determination_if_not_lead(self):
         submission = ApplicationSubmissionFactory(status='in_discussion')
         determination = DeterminationFactory(submission=submission, author=self.user, accepted=True)
@@ -95,6 +96,13 @@ class DeterminationFormTestCase(BaseViewTestCase):
         self.assertContains(response, 'Approved')
         self.assertRedirects(response, self.absolute_url(submission.get_absolute_url()))
 
+    def test_can_edit_draft_determination_if_not_lead_with_projects(self):
+        submission = ApplicationSubmissionFactory(status='in_discussion')
+        determination = DeterminationFactory(submission=submission, author=self.user, accepted=True)
+        response = self.post_page(submission, {'data': 'value', 'outcome': determination.outcome}, 'form')
+        self.assertContains(response, 'Approved')
+        self.assertRedirects(response, self.absolute_url(submission.get_absolute_url()))
+
     def test_sends_message_if_requires_more_info(self):
         submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user)
         determination = DeterminationFactory(submission=submission, author=self.user)
diff --git a/opentech/apply/determinations/views.py b/opentech/apply/determinations/views.py
index bee9a188b..5cb56920c 100644
--- a/opentech/apply/determinations/views.py
+++ b/opentech/apply/determinations/views.py
@@ -267,13 +267,14 @@ class DeterminationCreateOrUpdateView(CreateOrUpdateView):
 
             if self.submission.accepted_for_funding:
                 project = Project.create_from_submission(self.submission)
-                messenger(
-                    MESSAGES.CREATED_PROJECT,
-                    request=self.request,
-                    user=self.request.user,
-                    source=project,
-                    related=project.submission,
-                )
+                if project:
+                    messenger(
+                        MESSAGES.CREATED_PROJECT,
+                        request=self.request,
+                        user=self.request.user,
+                        source=project,
+                        related=project.submission,
+                    )
 
         messenger(
             MESSAGES.DETERMINATION_OUTCOME,
diff --git a/opentech/settings/test.py b/opentech/settings/test.py
index 11a71f78e..3d6102ba8 100644
--- a/opentech/settings/test.py
+++ b/opentech/settings/test.py
@@ -8,6 +8,8 @@ from .base import *  # noqa
 
 SECRET_KEY = 'NOT A SECRET'
 
+PROJECTS_ENABLED = True
+
 # Need this to ensure white noise doesn't kill the speed of testing
 # http://whitenoise.evans.io/en/latest/django.html#whitenoise-makes-my-tests-run-slow
 WHITENOISE_AUTOREFRESH = True
-- 
GitLab