class TestCycleStartFailed(TestCase):
    """ This class contains simple one time workflow tests that are not
  in the gsheet test grid
  """
    def setUp(self):
        TestCase.setUp(self)
        self.api = Api()
        self.wf_generator = WorkflowsGenerator()
        self.object_generator = ObjectGenerator()
        Notification.query.delete()

        self.random_objects = self.object_generator.generate_random_objects(2)
        _, self.user = self.object_generator.generate_person(
            user_role="Administrator")
        self.create_test_cases()

        def init_decorator(init):
            def new_init(self, *args, **kwargs):
                init(self, *args, **kwargs)
                if hasattr(self, "created_at"):
                    self.created_at = datetime.now()

            return new_init

        Notification.__init__ = init_decorator(Notification.__init__)

    @patch("ggrc.notifications.common.send_email")
    def test_start_failed(self, mock_mail):

        wf_owner = "*****@*****.**"

        with freeze_time("2015-02-01 13:39:20"):
            _, wf = self.wf_generator.generate_workflow(self.quarterly_wf)
            response, wf = self.wf_generator.activate_workflow(wf)
            print wf.next_cycle_start_date

            self.assert200(response)

        with freeze_time("2015-01-01 13:39:20"):
            _, notif_data = common.get_daily_notifications()
            self.assertNotIn(wf_owner, notif_data)

        with freeze_time("2015-01-29 13:39:20"):
            _, notif_data = common.get_daily_notifications()
            self.assertIn(wf_owner, notif_data)
            self.assertIn("cycle_starts_in", notif_data[wf_owner])

        with freeze_time("2015-03-05 13:39:20"):
            _, notif_data = common.get_daily_notifications()
            self.assertIn(wf_owner, notif_data)
            self.assertNotIn("cycle_started", notif_data[wf_owner])
            self.assertIn(wf_owner, notif_data)
            self.assertIn("cycle_start_failed", notif_data[wf_owner])

            common.send_daily_digest_notifications()

            _, notif_data = common.get_daily_notifications()
            self.assertNotIn(wf_owner, notif_data)

    # TODO: investigate why next_cycle_start date remains the same after
    # start_recurring_cycles

    # @patch("ggrc.notifications.common.send_email")
    # def test_start_failed_send_notifications(self, mock_mail):

    #   wf_owner = "*****@*****.**"

    #   with freeze_time("2015-02-01 13:39:20"):
    #     _, wf = self.wf_generator.generate_workflow(self.quarterly_wf)
    #     response, wf = self.wf_generator.activate_workflow(wf)
    #     print wf.next_cycle_start_date

    #     self.assert200(response)

    #   with freeze_time("2015-01-01 13:39:20"):
    #     _, notif_data = common.get_daily_notifications()
    #     self.assertNotIn(wf_owner, notif_data)

    #   with freeze_time("2015-01-29 13:39:20"):
    #     _, notif_data = common.get_daily_notifications()
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertIn("cycle_starts_in", notif_data[wf_owner])

    #   with freeze_time("2015-02-05 13:39:20"):
    #     _, notif_data = common.get_daily_notifications()
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertNotIn("cycle_started", notif_data[wf_owner])
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertIn("cycle_start_failed", notif_data[wf_owner])

    #     start_recurring_cycles()
    #     _, notif_data = common.get_daily_notifications()
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertIn("cycle_started", notif_data[wf_owner])
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertNotIn("cycle_start_failed", notif_data[wf_owner])

    #     common.send_daily_digest_notifications()

    #     _, notif_data = common.get_daily_notifications()
    #     self.assertNotIn(wf_owner, notif_data)

    # @patch("ggrc.notifications.common.send_email")
    # def test_start_failed_send_notifications_monthly(self, mock_mail):

    #   wf_owner = "*****@*****.**"

    #   with freeze_time("2015-05-12 13:39:20"):
    #     _, wf = self.wf_generator.generate_workflow(self.monthly)
    #     response, wf = self.wf_generator.activate_workflow(wf)

    #   with freeze_time("2015-05-14 13:39:20"):
    #     _, wf = self.wf_generator.generate_workflow(self.monthly)
    #     response, wf = self.wf_generator.activate_workflow(wf)

    #     self.assert200(response)

    #     _, notif_data = common.get_daily_notifications()
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertNotIn("cycle_started", notif_data[wf_owner])
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertIn("cycle_start_failed", notif_data[wf_owner])

    #     start_recurring_cycles()
    #     _, notif_data = common.get_daily_notifications()
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertIn("cycle_started", notif_data[wf_owner])
    #     self.assertIn(wf_owner, notif_data)
    #     self.assertNotIn("cycle_start_failed", notif_data[wf_owner])

    #     common.send_daily_digest_notifications()

    #     _, notif_data = common.get_daily_notifications()
    #     self.assertNotIn(wf_owner, notif_data)

    def create_test_cases(self):
        def person_dict(person_id):
            return {
                "href": "/api/people/%d" % person_id,
                "id": person_id,
                "type": "Person"
            }

        self.quarterly_wf = {
            "title":
            "quarterly wf forced notifications",
            "notify_on_change":
            True,
            "description":
            "",
            "owners": [person_dict(self.user.id)],
            "frequency":
            "quarterly",
            "task_groups": [
                {
                    "title":
                    "tg_1",
                    "contact":
                    person_dict(self.user.id),
                    "task_group_tasks": [
                        {
                            "contact": person_dict(self.user.id),
                            "description": self.wf_generator.random_str(100),
                            "relative_start_day": 5,
                            "relative_start_month": 2,
                            "relative_end_day": 25,
                            "relative_end_month": 2,
                        },
                    ],
                },
            ]
        }
        self.monthly = {
            "title":
            "monthly",
            "notify_on_change":
            True,
            "description":
            "",
            "owners": [person_dict(self.user.id)],
            "frequency":
            "monthly",
            "task_groups": [
                {
                    "title":
                    "tg_1",
                    "contact":
                    person_dict(self.user.id),
                    "task_group_tasks": [
                        {
                            "contact": person_dict(self.user.id),
                            "description": self.wf_generator.random_str(100),
                            "relative_start_day": 14,
                            "relative_end_day": 25,
                        },
                    ],
                },
            ]
        }
Esempio n. 2
0
class TestRecurringCycleNotifications(TestCase):
    def setUp(self):
        TestCase.setUp(self)
        self.api = Api()
        self.generator = WorkflowsGenerator()
        self.object_generator = ObjectGenerator()

        _, self.assignee = self.object_generator.generate_person(
            user_role="Administrator")

        self.create_test_cases()

    def tearDown(self):
        pass

    def test_cycle_starts_in_less_than_X_days(self):

        with freeze_time("2015-02-01"):
            _, wf = self.generator.generate_workflow(self.quarterly_wf_1)
            response, wf = self.generator.activate_workflow(wf)

            self.assert200(response)

            assignee = Person.query.get(self.assignee.id)

        with freeze_time("2015-01-01"):
            _, notif_data = common.get_daily_notifications()
            self.assertNotIn(assignee.email, notif_data)

        with freeze_time("2015-01-29"):
            _, notif_data = common.get_daily_notifications()
            self.assertIn(assignee.email, notif_data)

        with freeze_time("2015-02-01"):
            _, notif_data = common.get_daily_notifications()
            self.assertIn(assignee.email, notif_data)

    # TODO: this should mock google email api.
    @patch("ggrc.notifications.common.send_email")
    def test_marking_sent_notifications(self, mail_mock):
        mail_mock.return_value = True

        with freeze_time("2015-02-01"):
            _, wf = self.generator.generate_workflow(self.quarterly_wf_1)
            response, wf = self.generator.activate_workflow(wf)

            self.assert200(response)

            assignee = Person.query.get(self.assignee.id)

        with freeze_time("2015-01-01"):
            _, notif_data = common.get_daily_notifications()
            self.assertNotIn(assignee.email, notif_data)

        with freeze_time("2015-01-29"):
            common.send_daily_digest_notifications()
            _, notif_data = common.get_daily_notifications()
            self.assertNotIn(assignee.email, notif_data)

        with freeze_time("2015-02-01"):
            _, notif_data = common.get_daily_notifications()
            self.assertNotIn(assignee.email, notif_data)

    def create_test_cases(self):
        def person_dict(person_id):
            return {
                "href": "/api/people/%d" % person_id,
                "id": person_id,
                "type": "Person"
            }

        self.quarterly_wf_1 = {
            "title":
            "quarterly wf 1",
            "description":
            "",
            "owners": [person_dict(self.assignee.id)],
            "frequency":
            "quarterly",
            "notify_on_change":
            True,
            "task_groups": [
                {
                    "title":
                    "tg_1",
                    "contact":
                    person_dict(self.assignee.id),
                    "task_group_tasks": [
                        {
                            "contact": person_dict(self.assignee.id),
                            "description": self.generator.random_str(100),
                            "relative_start_day": 5,
                            "relative_start_month": 2,
                            "relative_end_day": 25,
                            "relative_end_month": 2,
                        },
                    ],
                },
            ]
        }

        self.all_workflows = [
            self.quarterly_wf_1,
        ]
Esempio n. 3
0
class TestBasicWorkflowActions(TestCase):
    def setUp(self):
        TestCase.setUp(self)
        self.api = Api()
        self.generator = WorkflowsGenerator()
        self.object_generator = ObjectGenerator()

        self.random_objects = self.object_generator.generate_random_objects()
        self.create_test_cases()

    def tearDown(self):
        pass

    def test_create_workflows(self):
        _, wflow = self.generator.generate_workflow(self.one_time_workflow_1)
        self.assertIsInstance(wflow, Workflow)

        task_groups = db.session.query(TaskGroup)\
            .filter(TaskGroup.workflow_id == wflow.id).all()

        self.assertEqual(len(task_groups),
                         len(self.one_time_workflow_1["task_groups"]))

    def test_workflows(self):
        for workflow in self.all_workflows:
            _, wflow = self.generator.generate_workflow(workflow)
            self.assertIsInstance(wflow, Workflow)

            task_groups = db.session.query(TaskGroup)\
                .filter(TaskGroup.workflow_id == wflow.id).all()

            self.assertEqual(len(task_groups), len(workflow["task_groups"]))

    def test_activate_wf(self):
        for workflow in self.all_workflows:
            _, wflow = self.generator.generate_workflow(workflow)
            response, wflow = self.generator.activate_workflow(wflow)

            self.assert200(response)

    def test_one_time_workflow_edits(self):
        _, wflow = self.generator.generate_workflow(self.one_time_workflow_1)

        wf_dict = {"title": "modified one time wf"}
        self.generator.modify_workflow(wflow, data=wf_dict)

        modified_wf = db.session.query(Workflow).filter(
            Workflow.id == wflow.id).one()
        self.assertEqual(wf_dict["title"], modified_wf.title)

    def test_one_time_wf_activate(self):
        _, wflow = self.generator.generate_workflow(self.one_time_workflow_1)
        self.generator.generate_cycle(wflow)
        self.generator.activate_workflow(wflow)

        tasks = [
            len(tg.get("task_group_tasks", []))
            for tg in self.one_time_workflow_1["task_groups"]
        ]

        cycle_tasks = db.session.query(CycleTaskGroupObjectTask).join(
            Cycle).join(Workflow).filter(Workflow.id == wflow.id).all()
        active_wf = db.session.query(Workflow).filter(
            Workflow.id == wflow.id).one()

        self.assertEqual(sum(tasks), len(cycle_tasks))
        self.assertEqual(active_wf.status, "Active")

    def test_one_time_wf_state_transition_dates(self):
        _, wflow = self.generator.generate_workflow(self.one_time_workflow_1)
        self.generator.generate_cycle(wflow)
        self.generator.activate_workflow(wflow)

        cycle_tasks = db.session.query(CycleTaskGroupObjectTask).join(
            Cycle).join(Workflow).filter(Workflow.id == wflow.id).all()
        with freeze_time("2015-6-9 13:00:00"):
            today = datetime.now()
            transitions = [
                ("InProgress", None, None),
                ("Finished", today, None),
                ("Declined", None, None),
                ("Finished", today, None),
                ("Verified", today, today),
                ("Finished", today, None),
            ]
            # Iterate over possible transitions and check if dates got set correctly
            for (status, expected_finished, expected_verified) in transitions:
                cycle_task = cycle_tasks[0]
                _, task = self.generator.modify_object(cycle_task,
                                                       {"status": status})
                self.assertEqual(task.finished_date, expected_finished)
                self.assertEqual(task.verified_date, expected_verified)

    def test_delete_calls(self):
        _, workflow = self.generator.generate_workflow()
        self.generator.generate_task_group(workflow)
        _, task_group = self.generator.generate_task_group(workflow)
        task_groups = db.session.query(TaskGroup).filter(
            TaskGroup.workflow_id == workflow.id).all()
        self.assertEqual(len(task_groups), 2)

        response = self.generator.api.delete(task_group)
        self.assert200(response)

        task_groups = db.session.query(TaskGroup).filter(
            TaskGroup.workflow_id == workflow.id).all()
        self.assertEqual(len(task_groups), 1)

    def create_test_cases(self):

        self.quarterly_wf_1 = {
            "title":
            "quarterly wf 1",
            "description":
            "",
            "frequency":
            "quarterly",
            "task_groups": [
                {
                    "title":
                    "tg_1",
                    "task_group_tasks": [
                        {
                            "description": self.generator.random_str(100),
                            "relative_start_day": 5,
                            "relative_start_month": 1,
                            "relative_end_day": 25,
                            "relative_end_month": 2,
                        },
                        {
                            "description": self.generator.random_str(100),
                            "relative_start_day": 15,
                            "relative_start_month": 2,
                            "relative_end_day": 28,
                            "relative_end_month": 2,
                        },
                        {
                            "description": self.generator.random_str(100),
                            "relative_start_day": 1,
                            "relative_start_month": 1,
                            "relative_end_day": 1,
                            "relative_end_month": 1,
                        },
                    ],
                },
            ]
        }

        self.weekly_wf_1 = {
            "title":
            "weekly thingy",
            "description":
            "start this many a time",
            "frequency":
            "weekly",
            "task_groups": [
                {
                    "title":
                    "tg_2",
                    "task_group_tasks": [
                        {
                            "description": self.generator.random_str(100),
                            "relative_end_day": 1,
                            "relative_end_month": None,
                            "relative_start_day": 5,
                            "relative_start_month": None,
                        },
                        {
                            "title": "monday task",
                            "relative_end_day": 1,
                            "relative_end_month": None,
                            "relative_start_day": 1,
                            "relative_start_month": None,
                        },
                        {
                            "title": "weekend task",
                            "relative_end_day": 4,
                            "relative_end_month": None,
                            "relative_start_day": 1,
                            "relative_start_month": None,
                        },
                    ],
                    "task_group_objects":
                    self.random_objects
                },
            ]
        }

        self.one_time_workflow_1 = {
            "title":
            "one time wf test",
            "description":
            "some test workflow",
            "task_groups": [{
                "title": "tg_1",
                "task_group_tasks": [{}, {}, {}]
            }, {
                "title":
                "tg_2",
                "task_group_tasks": [{
                    "description":
                    self.generator.random_str(100)
                }, {}],
                "task_group_objects":
                self.random_objects[:2]
            }, {
                "title":
                "tg_3",
                "task_group_tasks": [{
                    "title":
                    "simple task 1",
                    "description":
                    self.generator.random_str(100)
                }, {
                    "title":
                    self.generator.random_str(),
                    "description":
                    self.generator.random_str(100)
                }, {
                    "title":
                    self.generator.random_str(),
                    "description":
                    self.generator.random_str(100)
                }],
                "task_group_objects":
                self.random_objects
            }]
        }
        self.one_time_workflow_2 = {
            "title":
            "test_wf_title",
            "description":
            "some test workflow",
            "task_groups": [{
                "title": "tg_1",
                "task_group_tasks": [{}, {}, {}]
            }, {
                "title":
                "tg_2",
                "task_group_tasks": [{
                    "description":
                    self.generator.random_str(100)
                }, {}],
                "task_group_objects":
                self.random_objects[:2]
            }, {
                "title":
                "tg_3",
                "task_group_tasks": [{
                    "title":
                    "simple task 1",
                    "description":
                    self.generator.random_str(100)
                }, {
                    "title":
                    self.generator.random_str(),
                    "description":
                    self.generator.random_str(100)
                }, {
                    "title":
                    self.generator.random_str(),
                    "description":
                    self.generator.random_str(100)
                }],
                "task_group_objects": []
            }]
        }

        self.monthly_workflow_1 = {
            "title":
            "monthly test wf",
            "description":
            "start this many a time",
            "frequency":
            "monthly",
            "task_groups": [
                {
                    "title":
                    "tg_2",
                    "task_group_tasks": [{
                        "description":
                        self.generator.random_str(100),
                        "relative_end_day":
                        1,
                        "relative_end_month":
                        None,
                        "relative_start_day":
                        5,
                        "relative_start_month":
                        None,
                    }, {
                        "title": "monday task",
                        "relative_end_day": 1,
                        "relative_end_month": None,
                        "relative_start_day": 1,
                        "relative_start_month": None,
                    }, {
                        "title": "weekend task",
                        "relative_end_day": 4,
                        "relative_end_month": None,
                        "relative_start_day": 1,
                        "relative_start_month": None,
                    }],
                    "task_group_objects":
                    self.random_objects
                },
            ]
        }
        self.all_workflows = [
            self.one_time_workflow_1,
            self.one_time_workflow_2,
            self.weekly_wf_1,
            self.monthly_workflow_1,
            self.quarterly_wf_1,
        ]
Esempio n. 4
0
class TestEnableAndDisableNotifications(TestCase):

  """ This class contains simple one time workflow tests that are not
  in the gsheet test grid
  """

  def setUp(self):
    TestCase.setUp(self)
    self.api = Api()
    self.wf_generator = WorkflowsGenerator()
    self.object_generator = ObjectGenerator()
    models.Notification.query.delete()

    self.random_objects = self.object_generator.generate_random_objects(2)
    _, self.user = self.object_generator.generate_person(
        user_role="Administrator")
    self.create_test_cases()

    def init_decorator(init):
      def new_init(self, *args, **kwargs):
        init(self, *args, **kwargs)
        if hasattr(self, "created_at"):
          self.created_at = datetime.now()
      return new_init

    models.Notification.__init__ = init_decorator(models.Notification.__init__)

  @patch("ggrc.notifications.common.send_email")
  def test_default_notifications_settings(self, mock_mail):

    with freeze_time("2015-02-01 13:39:20"):
      _, wf = self.wf_generator.generate_workflow(self.quarterly_wf)
      response, wf = self.wf_generator.activate_workflow(wf)

      self.assert200(response)

      user = models.Person.query.get(self.user.id)

    with freeze_time("2015-01-01 13:39:20"):
      _, notif_data = common.get_daily_notifications()
      self.assertNotIn(user.email, notif_data)

    with freeze_time("2015-01-29 13:39:20"):
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)

  @patch("ggrc.notifications.common.send_email")
  def test_disabled_notifications(self, mock_mail):

    with freeze_time("2015-02-01 13:39:20"):
      _, wf = self.wf_generator.generate_workflow(self.quarterly_wf)
      response, wf = self.wf_generator.activate_workflow(wf)

      self.assert200(response)

      self.object_generator.generate_notification_setting(
          self.user.id, "Email_Digest", False)

      user = models.Person.query.get(self.user.id)

    with freeze_time("2015-01-01 13:39:20"):
      _, notif_data = common.get_daily_notifications()
      self.assertNotIn(user.email, notif_data)

    with freeze_time("2015-01-29 13:39:20"):
      _, notif_data = common.get_daily_notifications()
      self.assertNotIn(user.email, notif_data)

  @patch("ggrc.notifications.common.send_email")
  def test_enabled_notifications(self, mock_mail):

    with freeze_time("2015-02-01 13:39:20"):
      _, wf = self.wf_generator.generate_workflow(self.quarterly_wf)
      response, wf = self.wf_generator.activate_workflow(wf)
      self.assert200(response)

    with freeze_time("2015-01-29 13:39:20"):
      user = models.Person.query.get(self.user.id)
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)

      self.object_generator.generate_notification_setting(
          self.user.id, "Email_Digest", True)

      user = models.Person.query.get(self.user.id)
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)

  @patch("ggrc.notifications.common.send_email")
  def test_forced_notifications(self, mock_mail):

    with freeze_time("2015-02-01 13:39:20"):
      _, wf = self.wf_generator.generate_workflow(self.quarterly_wf_forced)
      response, wf = self.wf_generator.activate_workflow(wf)

      self.assert200(response)

      user = models.Person.query.get(self.user.id)

    with freeze_time("2015-01-29 13:39:20"):
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)

      self.object_generator.generate_notification_setting(
          self.user.id, "Email_Digest", True)

      user = models.Person.query.get(self.user.id)
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)

  @patch("ggrc.notifications.common.send_email")
  def test_force_one_wf_notifications(self, mock_mail):

    with freeze_time("2015-02-01 13:39:20"):
      _, wf_forced = self.wf_generator.generate_workflow(
          self.quarterly_wf_forced)
      response, wf_forced = self.wf_generator.activate_workflow(wf_forced)
      _, wf = self.wf_generator.generate_workflow(self.quarterly_wf)
      response, wf = self.wf_generator.activate_workflow(wf)

      self.assert200(response)

      user = models.Person.query.get(self.user.id)

    with freeze_time("2015-01-29 13:39:20"):
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)
      self.assertIn("cycle_starts_in", notif_data[user.email])
      self.assertIn(wf_forced.id, notif_data[user.email]["cycle_starts_in"])
      self.assertIn(wf.id, notif_data[user.email]["cycle_starts_in"])

      self.object_generator.generate_notification_setting(
          self.user.id, "Email_Digest", False)

      user = models.Person.query.get(self.user.id)
      _, notif_data = common.get_daily_notifications()
      self.assertIn(user.email, notif_data)
      self.assertIn("cycle_starts_in", notif_data[user.email])
      self.assertIn(wf_forced.id, notif_data[user.email]["cycle_starts_in"])
      self.assertNotIn(wf.id, notif_data[user.email]["cycle_starts_in"])

  def create_test_cases(self):
    def person_dict(person_id):
      return {
          "href": "/api/people/%d" % person_id,
          "id": person_id,
          "type": "Person"
      }

    self.quarterly_wf_forced = {
        "title": "quarterly wf forced notifications",
        "notify_on_change": True,
        "description": "",
        "owners": [person_dict(self.user.id)],
        "frequency": "quarterly",
        "task_groups": [{
            "title": "tg_1",
            "contact": person_dict(self.user.id),
            "task_group_tasks": [{
                "contact": person_dict(self.user.id),
                "description": self.wf_generator.random_str(100),
                "relative_start_day": 5,
                "relative_start_month": 2,
                "relative_end_day": 25,
                "relative_end_month": 2,
            },
            ],
        },
        ]
    }

    self.quarterly_wf = {
        "title": "quarterly wf 1",
        "description": "",
        "owners": [person_dict(self.user.id)],
        "frequency": "quarterly",
        "task_groups": [{
            "title": "tg_1",
            "contact": person_dict(self.user.id),
            "task_group_tasks": [{
                "contact": person_dict(self.user.id),
                "description": self.wf_generator.random_str(100),
                "relative_start_day": 5,
                "relative_start_month": 2,
                "relative_end_day": 25,
                "relative_end_month": 2,
            },
            ],
        },
        ]
    }