From 0f120b47966286dff07f261aa0bb3ed1a8c40a68 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Mon, 18 Jun 2018 10:08:41 +0100
Subject: [PATCH] Update the tests

---
 opentech/apply/funds/models.py                 |  5 +++++
 opentech/apply/funds/tests/factories/models.py |  3 ++-
 opentech/apply/funds/tests/test_views.py       | 15 +++++++++++++--
 opentech/apply/funds/workflow.py               | 11 +++++++++--
 requirements.txt                               |  2 +-
 5 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index dda5c235b..993b4f8a6 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -529,12 +529,14 @@ class AddTransitions(models.base.ModelBase):
                     # Provide a neat name for graph viz display
                     transition_state.__name__ = slugify(action['display'])
 
+                    conditions = [attrs[condition] for condition in action.get('conditions', [])]
                     # Wrap with transition decorator
                     transition_func = transition(
                         attrs['status'],
                         source=phase,
                         target=transition_name,
                         permission=permission_func,
+                        conditions=conditions,
                     )(transition_state)
 
                     # Attach to new class
@@ -597,6 +599,9 @@ class ApplicationSubmission(WorkflowHelpers, BaseStreamForm, AbstractFormSubmiss
 
     objects = ApplicationSubmissionQueryset.as_manager()
 
+    def not_progressed(self):
+        return not self.next
+
     @transition(
         status, source='*',
         target=RETURN_VALUE(INITIAL_STATE, 'draft_proposal', 'invited_to_proposal'),
diff --git a/opentech/apply/funds/tests/factories/models.py b/opentech/apply/funds/tests/factories/models.py
index 378193aeb..abef318fc 100644
--- a/opentech/apply/funds/tests/factories/models.py
+++ b/opentech/apply/funds/tests/factories/models.py
@@ -191,8 +191,9 @@ class ApplicationSubmissionFactory(factory.DjangoModelFactory):
     form_data = factory.SubFactory(FormDataFactory, form_fields=factory.SelfAttribute('..form_fields'))
     page = factory.SubFactory(FundTypeFactory)
     workflow_name = factory.LazyAttribute(lambda o: list(FundType.WORKFLOW_CHOICES.keys())[o.workflow_stages - 1])
-    round = factory.SubFactory(RoundFactory, workflow_name=factory.SelfAttribute('..workflow_name'))
+    round = factory.SubFactory(RoundFactory, workflow_name=factory.SelfAttribute('..workflow_name'), lead=factory.SelfAttribute('..lead'))
     user = factory.SubFactory(UserFactory)
+    lead = factory.SubFactory(StaffFactory)
 
     @classmethod
     def _generate(cls, strat, params):
diff --git a/opentech/apply/funds/tests/test_views.py b/opentech/apply/funds/tests/test_views.py
index a78795c8c..26ff30950 100644
--- a/opentech/apply/funds/tests/test_views.py
+++ b/opentech/apply/funds/tests/test_views.py
@@ -22,6 +22,9 @@ class SubmissionTestCase(TestCase):
     def post_submission_page(self, submission, data, view_name='detail'):
         return self.client.post(self.submission_url(submission, view_name), data)
 
+    def refresh(self, instance):
+        return instance.__class__.objects.get(id=instance.id)
+
 
 class TestStaffSubmissionView(SubmissionTestCase):
     user_factory = StaffFactory
@@ -32,17 +35,25 @@ class TestStaffSubmissionView(SubmissionTestCase):
         self.assertContains(response, submission.title)
 
     def test_can_progress_stage(self):
-        submission = ApplicationSubmissionFactory(status='concept_review_discussion', workflow_stages=2)
+        submission = ApplicationSubmissionFactory(status='concept_review_discussion', workflow_stages=2, lead=self.user)
         response = self.post_submission_page(submission, {'form-submitted-progress_form': '', 'action': 'invited_to_proposal'})
 
         # Cant use refresh from DB with FSM
-        submission_origional = submission.__class__.objects.get(id=submission.id)
+        submission_origional = self.refresh(submission)
         submission_next = submission_origional.next
 
         self.assertRedirects(response, self.submission_url(submission_next))
         self.assertEqual(submission_origional.status, 'invited_to_proposal')
         self.assertEqual(submission_next.status, 'draft_proposal')
 
+    def test_cant_progress_stage_if_not_lead(self):
+        submission = ApplicationSubmissionFactory(status='concept_review_discussion', workflow_stages=2)
+        response = self.post_submission_page(submission, {'form-submitted-progress_form': '', 'action': 'invited_to_proposal'})
+        submission = self.refresh(submission)
+
+        self.assertEqual(submission.status, 'concept_review_discussion')
+        self.assertIsNone(submission.next)
+
 
 class TestApplicantSubmissionView(SubmissionTestCase):
     user_factory = UserFactory
diff --git a/opentech/apply/funds/workflow.py b/opentech/apply/funds/workflow.py
index 3194bb13f..a3ee7a001 100644
--- a/opentech/apply/funds/workflow.py
+++ b/opentech/apply/funds/workflow.py
@@ -60,6 +60,8 @@ class Phase:
                 transition['permissions'] = default_permissions
             else:
                 transition['method'] = action.get('method')
+                conditions = action.get('conditions', '')
+                transition['conditions'] = conditions.split(',') if conditions else []
                 transition['permissions'] = action.get('permissions', default_permissions)
             self.transitions[transition_target] = transition
 
@@ -212,7 +214,7 @@ DoubleStageDefinition = {
     },
     'concept_review_discussion': {
         'transitions': {
-            'invited_to_proposal': 'Invite to Proposal',
+            'invited_to_proposal': {'display': 'Invited to Proposal', 'permissions': {UserPermissions.ADMIN, UserPermissions.LEAD}},
             'concept_rejected': {'display': 'Reject', 'permissions': {UserPermissions.ADMIN, UserPermissions.LEAD}},
             'concept_review_more_info': 'Request More Information',
         },
@@ -233,7 +235,12 @@ DoubleStageDefinition = {
     'invited_to_proposal': {
         'display': 'Concept Accepted',
         'transitions': {
-            'draft_proposal': {'display': 'Progress', 'action': 'progress_application', 'permissions': {}},
+            'draft_proposal': {
+                'display': 'Progress',
+                'method': 'progress_application',
+                'permissions': {UserPermissions.ADMIN, UserPermissions.LEAD},
+                'conditions': 'not_progressed',
+            },
         },
         'stage': Concept,
         'permissions': Permission(),
diff --git a/requirements.txt b/requirements.txt
index 3d21931b7..ffdcd69b1 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -14,7 +14,7 @@ uwsgidecorators==1.1.0
 
 factory_boy==2.9.2
 # wagtail_factories - waiting on merge and release form master branch
-git+git://github.com/todd-dembrey/wagtail-factories.git#egg=wagtail_factories
+git+git://github.com/mvantellingen/wagtail-factories.git#egg=wagtail_factories
 
 flake8
 
-- 
GitLab