def test_recalculate_start_date_on_delete(self, idxs, expected_date): """Test recalculate start_date on delete expected={1}.""" start_date_1 = datetime.date(2017, 8, 10) start_date_2 = datetime.date(2017, 8, 11) with freezegun.freeze_time("2017-08-10"): with glob_factories.single_commit(): workflow = factories.WorkflowFactory( title="This is a test WF", unit=all_models.Workflow.WEEK_UNIT, repeat_every=1) tasks = ( factories.TaskGroupTaskFactory( task_group=factories.TaskGroupFactory( workflow=workflow), start_date=start_date_1, end_date=start_date_1 + datetime.timedelta(1), ), factories.TaskGroupTaskFactory( task_group=factories.TaskGroupFactory( workflow=workflow), start_date=start_date_2, end_date=start_date_2 + datetime.timedelta(1), ), ) wf_id = workflow.id task_ids = [t.id for t in tasks] self.generator.activate_workflow(workflow) workflow = all_models.Workflow.query.get(wf_id) self.assertEqual(datetime.date(2017, 8, 17), workflow.next_cycle_start_date) for idx in idxs: task = all_models.TaskGroupTask.query.get(task_ids[idx]) self.api.delete(task) workflow = all_models.Workflow.query.get(wf_id) self.assertEqual(expected_date, workflow.next_cycle_start_date)
def test_update_verification_false_flag_positive(self, import_value, expected_value): """Test update of verification flag before activation when is_verification_needed is FALSE """ slug = 'SomeCode' with freezegun.freeze_time("2017-08-10"): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( slug=slug, is_verification_needed=False) wf_factories.TaskGroupTaskFactory( task_group=wf_factories.TaskGroupFactory( workflow=workflow, context=factories.ContextFactory()), start_date=date(2017, 8, 3), end_date=date(2017, 8, 7)) wf_id = workflow.id resp = self.import_data( collections.OrderedDict([ ("object_type", "Workflow"), ("code", slug), ("title", "SomeTitle"), ("Need Verification", import_value), ("force real-time email updates", "no"), ("Admin", "*****@*****.**"), ])) self.assertEqual(1, resp[0]['updated']) workflow = Workflow.query.filter(Workflow.id == wf_id).first() self.assertEqual(workflow.is_verification_needed, expected_value)
def test_recurring_wf_start_end_cycle_dates(self, setup_date, freeze_date, repeat_every, unit, exp_start_date, exp_end_date): """Test case for correct cycle start and end dates""" with freeze_time(freeze_date): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( repeat_every=repeat_every, unit=unit) group = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=group, start_date=setup_date, end_date=setup_date + dtm.timedelta(days=4)) wf_factories.TaskGroupTaskFactory( task_group=group, start_date=setup_date - dtm.timedelta(days=1), end_date=setup_date + dtm.timedelta(days=3)) self.generator.activate_workflow(workflow) active_wf = db.session.query(Workflow).filter( Workflow.status == 'Active').one() # freeze_date is chosen so that we expect 3 cycles to be generated: self.assertEqual(len(active_wf.cycles), 3) cycle = active_wf.cycles[0] self.assertEqual(cycle.start_date, exp_start_date) self.assertEqual(cycle.end_date, exp_end_date)
def setup_workflow_scope(self, user_id, acr): """Set up base set of objects for permission tests of Workflow models. Args: acr: Instance of ACR that should be assigned for tested user. parent: Model name in scope of which objects should be set up. """ # pylint: disable=attribute-defined-outside-init with factories.single_commit(): self.workflow = wf_factories.WorkflowFactory() self.workflow_id = self.workflow.id self.assign_person(self.workflow, acr, user_id) self.task_group = wf_factories.TaskGroupFactory( workflow=self.workflow) self.task_group_id = self.task_group.id self.task = wf_factories.TaskGroupTaskFactory( task_group=self.task_group) self.task_id = self.task.id self.assign_person(self.task, acr, user_id) self.generate_cycle(self.workflow_id) cycle_task = all_models.CycleTaskGroupObjectTask.query.first() self.assign_person(cycle_task, acr, user_id)
def test_verification_flag_negative(self, flag): """Test immutable verification flag on active workflows.""" with freezegun.freeze_time("2017-08-10"): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( unit=all_models.Workflow.WEEK_UNIT, is_verification_needed=flag, repeat_every=1) wf_factories.TaskGroupTaskFactory( task_group=wf_factories.TaskGroupFactory( context=factories.ContextFactory(), workflow=workflow), # Two cycles should be created start_date=datetime.date(2017, 8, 3), end_date=datetime.date(2017, 8, 7)) workflow_id = workflow.id self.assertEqual(workflow.status, all_models.Workflow.DRAFT) self.generator.activate_workflow(workflow) workflow = all_models.Workflow.query.get(workflow_id) self.assertEqual(workflow.status, all_models.Workflow.ACTIVE) resp = self.api.put(workflow, {"is_verification_needed": not flag}) self.assert400(resp) workflow = all_models.Workflow.query.get(workflow_id) self.assertEqual(workflow.is_verification_needed, flag) # End all current cycles for cycle in workflow.cycles: self.generator.modify_object(cycle, {'is_current': False}) workflow = all_models.Workflow.query.filter( all_models.Workflow.id == workflow_id).first() self.assertEqual(workflow.status, all_models.Workflow.INACTIVE) resp = self.api.put(workflow, {"is_verification_needed": not flag}) self.assert400(resp) workflow = all_models.Workflow.query.get(workflow_id) self.assertEqual(workflow.is_verification_needed, flag)
def test_task_group_import_objects(self, model_name, is_mapped): """"Test import TaskGroup with mapping to object: {0}""" wf_slug = "WORKFLOW-1" tg_slug = "TASKGROUP-1" mapped_slug = "MAPPEDOBJECT-1" with factories.single_commit(): factories.get_model_factory(model_name)(slug=mapped_slug) workflow = wf_factories.WorkflowFactory(slug=wf_slug) wf_factories.TaskGroupFactory(slug=tg_slug, workflow=workflow) tg_data = collections.OrderedDict([ ("object_type", all_models.TaskGroup.__name__), ("code", tg_slug), ("workflow", wf_slug), ("objects", "{}: {}".format(model_name, mapped_slug)) ]) result = self.import_data(tg_data) task_group = all_models.TaskGroup.query.one() if is_mapped: self.assertEqual(len(task_group.task_group_objects), 1) self.assertEqual(task_group.task_group_objects[0].object.slug, mapped_slug) self.assertEqual(len(result[0]['row_warnings']), 0) else: self.assertEqual(len(task_group.task_group_objects), 0) self.assertEqual(len(result[0]['row_warnings']), 1) self.assertEqual( result[0]['row_warnings'][0], errors.INVALID_TASKGROUP_MAPPING_WARNING.format( line=3, object_class=model_name ) )
def test_cycle_task_model(self, model_name): """Test Relationship POST for CycleTask<->{0}. Args: model_name: Non-Relatable model with which we are trying to create Relationship. """ with factories.single_commit(): workflow = wf_factories.WorkflowFactory() cycle = wf_factories.CycleFactory(workflow=workflow) cycle_task_group = wf_factories.CycleTaskGroupFactory(cycle=cycle) task_group = wf_factories.TaskGroupFactory(workflow=workflow) task_group_task = wf_factories.TaskGroupTaskFactory( task_group=task_group) wf_factories.CycleTaskGroupObjectTaskFactory( cycle=cycle, cycle_task_group=cycle_task_group, task_group_task=task_group_task) cycle_task = all_models.CycleTaskGroupObjectTask.query.one() instance = getattr(all_models, model_name).query.one() data_ct_src = relationship_api.get_relationship_post_dict( cycle_task, instance) data_ct_dst = relationship_api.get_relationship_post_dict( instance, cycle_task) response = self.api_helper.post(all_models.Relationship, data_ct_src) self.assertEqual(response.status_code, 201) response = self.api_helper.post(all_models.Relationship, data_ct_dst) self.assertEqual(response.status_code, 201)
def test_control_with_tg_update(self): """Test updating of Control mapped to TaskGroup.""" with factories.single_commit(): control = factories.ControlFactory() task_group = wf_factories.TaskGroupFactory() wf_factories.TaskGroupObjectFactory(task_group=task_group, object=control) response = self.api.put(control, { "title": "new title", "task_group_objects": [], "task_groups": [], }) self.assert200(response) control = all_models.Control.query.get(control.id) self.assertEqual(control.title, "new title") tg_ids = [id_[0] for id_ in db.session.query(all_models.TaskGroup.id)] self.assertEqual(len(tg_ids), 1) self.assertEqual([tg.id for tg in control.task_groups], tg_ids) tgo_ids = [ id_[0] for id_ in db.session.query(all_models.TaskGroupObject.id) ] self.assertEqual(len(tgo_ids), 1) self.assertEqual([tgo.id for tgo in control.task_group_objects], tgo_ids)
def generate_tasks_for_cycle(self, group_count, task_count): """generate number of task groups and task for current task group""" role_names = ("Task Assignees", "Task Secondary Assignees") results = {} with single_commit(): workflow = factories.WorkflowFactory() cycle = factories.CycleFactory(workflow=workflow) task_group = factories.TaskGroupFactory(workflow=workflow) for idx in range(group_count): person = PersonFactory(name="user for group {}".format(idx)) cycle_task_group = factories.CycleTaskGroupFactory( cycle=cycle, contact=person) for _ in range(task_count): task_group_task = factories.TaskGroupTaskFactory( task_group=task_group) for r_name in role_names: ggrc_factories.AccessControlListFactory( object=task_group_task, person=person, ac_role_id=self.get_role_id_for_obj( task_group_task, r_name)) task = factories.CycleTaskFactory( cycle=cycle, cycle_task_group=cycle_task_group, task_group_task=task_group_task) for r_name in role_names: ggrc_factories.AccessControlListFactory( object=task, person=person, ac_role_id=self.get_role_id_for_obj(task, r_name)) results[task.id] = cycle_task_group.slug return results
def test_control_with_tg_update(self): """Test updating of Control mapped to TaskGroup.""" with factories.single_commit(): control = factories.ControlFactory() task_group = wf_factories.TaskGroupFactory() factories.RelationshipFactory( source=task_group, destination=control ) response = self.api.put(control, { "title": "new title", "task_groups": [], }) self.assert200(response) control = all_models.Control.query.get(control.id) self.assertEqual(control.title, "new title") tg_ids = [id_[0] for id_ in db.session.query(all_models.TaskGroup.id)] self.assertEqual(len(tg_ids), 1) self.assertEqual([tg.source_id for tg in control.related_sources], tg_ids) tg_mapped_obj_ids = [ id_[0] for id_ in db.session.query( all_models.Relationship.destination_id ).filter( all_models.Relationship.source_type == 'TaskGroup', all_models.Relationship.source_id.in_(tg_ids), ) ] self.assertEqual(len(tg_mapped_obj_ids), 1)
def test_assignee_not_updated(self, assignee, expected_warnings): """Tests cycle assignee wasn't updated with wrong user""" with factories.single_commit(): workflow = wf_factories.WorkflowFactory() group = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory( task_group=group, start_date=datetime.date(2017, 2, 28), end_date=datetime.date(2017, 2, 28) + datetime.timedelta(days=4)) _, cycle = self.generator.generate_cycle(workflow) data = OrderedDict([ ("object_type", "Cycle"), ("code", cycle.slug), ("title", cycle.title), ("Assignee", assignee), ]) response = self.import_data(data) expected_messages = { "Cycle": { "row_warnings": set(expected_warnings), }, } self._check_csv_response(response, expected_messages) cycle = all_models.Cycle.query.one() self.assertFalse(cycle.contact)
def test_delete_ctg_wf_admin(self, admin_role): """Check deletion cycle task group by workflow member""" admin = self.create_user_with_role(admin_role) exp_count = 0 with factories.single_commit(): workflow = wf_factories.WorkflowFactory() workflow.add_person_with_role_name(admin, 'Admin') task_group = wf_factories.TaskGroupFactory( title='TestTG', context=factories.ContextFactory(), workflow=workflow ) wf_factories.TaskGroupTaskFactory( title='TestTGT', task_group=task_group, start_date=datetime.date(2017, 8, 3), end_date=datetime.date(2017, 8, 7) ) cycle = wf_factories.CycleFactory(workflow=workflow) cycle_task_group = wf_factories.CycleTaskGroupFactory( cycle=cycle, title='Title1' ) ctg = all_models.CycleTaskGroup.query.get(cycle_task_group.id) self.generator.activate_workflow(workflow) self.api.set_user(admin) response = self.api.delete(ctg) self.assert200(response) count = len(all_models.CycleTaskGroup.query.all()) self.assertEquals(count, exp_count)
def test_create_ctg_wf_member(self, member_role, response_code, exp_count): """Check creation cycle task group by workflow member""" member = self.create_user_with_role(member_role) with factories.single_commit(): workflow = wf_factories.WorkflowFactory() workflow.add_person_with_role_name(member, 'Workflow Member') task_group = wf_factories.TaskGroupFactory( title='TestTG', context=factories.ContextFactory(), workflow=workflow) wf_factories.TaskGroupTaskFactory( title='TestTGT', task_group=task_group, start_date=datetime.date(2017, 8, 3), end_date=datetime.date(2017, 8, 7)) cycle = wf_factories.CycleFactory(workflow=workflow) self.generator.activate_workflow(workflow) self.api.set_user(member) response = self.api.post( all_models.CycleTaskGroup, { "cycle_task_group": { "cycle": { "id": cycle.id, }, "contact": { "id": member.id, }, "title": 'title1', }, }) self.assertEquals(response.status_code, response_code) count = len(all_models.CycleTaskGroup.query.all()) self.assertEquals(count, exp_count)
def test_filter_by_object_approval(self, filter_flag): """Test filter CTGOT by object_approval flag if value is {0}.""" with factories.single_commit(): ctgts = {} for flag in [True, False]: workflow = wf_factories.WorkflowFactory(object_approval=flag) task_group = wf_factories.TaskGroupFactory(workflow=workflow) tgt = wf_factories.TaskGroupTaskFactory(task_group=task_group) cycle = wf_factories.CycleFactory(workflow=workflow) ctgts[flag] = wf_factories.CycleTaskGroupObjectTaskFactory( cycle=cycle, task_group_task=tgt ) filter_params = "ids={}&object_approval={}".format( ",".join([str(c.id) for c in ctgts.values()]), "true" if filter_flag else "false" ) response = self.api_helper.get_query(all_models.CycleTaskGroupObjectTask, filter_params) self.assert200(response) colections = response.json["cycle_task_group_object_tasks_collection"] items = colections["cycle_task_group_object_tasks"] self.assertEqual(1, len(items)) self.assertEqual(ctgts[filter_flag].id, items[0]["id"])
def test_recurring_without_tgts_skip(self, has_tg): """Test that Active Workflow without TGTs is skipped on cron job""" with freeze_time(dtm.date(2017, 9, 25)): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( repeat_every=1, unit=Workflow.MONTH_UNIT) workflow_id = workflow.id group = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory( task_group=group, start_date=dtm.date(2017, 9, 26), end_date=dtm.date(2017, 9, 26) + dtm.timedelta(days=4)) self.generator.activate_workflow(workflow) active_wf = db.session.query(Workflow).filter( Workflow.id == workflow_id).one() self.assertEqual(active_wf.next_cycle_start_date, dtm.date(2017, 9, 26)) self.assertEqual(active_wf.recurrences, True) self.assertEqual(len(active_wf.cycles), 0) TaskGroupTask.query.delete() if not has_tg: TaskGroup.query.delete() db.session.commit() with freeze_time(dtm.date(2017, 10, 25)): start_recurring_cycles() active_wf = db.session.query(Workflow).filter( Workflow.id == workflow_id).one() self.assertEqual(active_wf.next_cycle_start_date, dtm.date(2017, 9, 26)) self.assertEqual(active_wf.recurrences, True) self.assertEqual(len(active_wf.cycles), 0)
def _create_propagation_acl_test_data(self): # noqa pylint: disable=invalid-name """Create objects for Workflow ACL propagation test.""" with freezegun.freeze_time("2017-08-9"): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( title='wf1', unit=all_models.Workflow.WEEK_UNIT, is_verification_needed=True, repeat_every=1) wf_factories.TaskGroupTaskFactory( title='tgt1', task_group=wf_factories.TaskGroupFactory( title='tg1', context=factories.ContextFactory(), workflow=workflow), # One cycle should be created start_date=datetime.date(2017, 8, 3), end_date=datetime.date(2017, 8, 7)) self.generator.activate_workflow(workflow) workflow = all_models.Workflow.query.one() acl_map = { self.people_ids[0]: WF_ROLES['Admin'], self.people_ids[1]: WF_ROLES['Workflow Member'], self.people_ids[2]: WF_ROLES['Workflow Member'], } put_params = { 'access_control_list': acl_helper.get_acl_list(acl_map) } response = self.api.put(workflow, put_params) self.assert200(response)
def generate_tasks_for_cycle(group_count, task_count): """generate number of task groups and task for current task group""" role_names = ("Task Assignees", "Task Secondary Assignees") results = {} with ggrc_factories.single_commit(): workflow = factories.WorkflowFactory() cycle = factories.CycleFactory(workflow=workflow) task_group = factories.TaskGroupFactory(workflow=workflow) for idx in range(group_count): person = ggrc_factories.PersonFactory( name="user for group {}".format(idx)) cycle_task_group = factories.CycleTaskGroupFactory( cycle=cycle, contact=person) for _ in range(task_count): task_group_task = factories.TaskGroupTaskFactory( task_group=task_group) for r_name in role_names: ggrc_factories.AccessControlPersonFactory( ac_list=task_group_task.acr_name_acl_map[r_name], person=person, ) task = factories.CycleTaskGroupObjectTaskFactory( cycle=cycle, cycle_task_group=cycle_task_group, task_group_task=task_group_task) for r_name in role_names: ggrc_factories.AccessControlPersonFactory( person=person, ac_list=task.acr_name_acl_map[r_name], ) results[task.id] = cycle_task_group.slug return results
def test_autogen_verification_flag(self, flag): """Check is_verification_needed flag for activate WF action.""" with factories.single_commit(): workflow = wf_factories.WorkflowFactory( is_verification_needed=flag) group = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=group) data = [{ "cycle": { "autogenerate": True, "isOverdue": False, "title": factories.random_str(prefix='cycle - '), "workflow": { "id": workflow.id, "type": "Workflow", }, "context": { "id": workflow.context_id, "type": "Context", }, } }] resp = self.api.send_request(self.api.client.post, api_link="/api/cycles", data=data) cycle_id = resp.json[0][1]["cycle"]["id"] self.assertEqual( flag, all_models.Cycle.query.get(cycle_id).is_verification_needed)
def setUp(self): """Set up.""" super(TestWfNotifsGenerator, self).setUp() self.api = Api() self.wf_generator = WorkflowsGenerator() self.object_generator = ObjectGenerator() Notification.query.delete() with freeze_time("2015-05-01 14:29:00"): wf_slug = "wf1" with factories.single_commit(): wf = wf_factories.WorkflowFactory(slug=wf_slug, is_verification_needed=True) task_group = wf_factories.TaskGroupFactory(workflow=wf) wf_factories.TaskGroupTaskFactory(task_group=task_group, title='task1', start_date=datetime.now(), end_date=datetime.now() + timedelta(7)) data = workflow_api.get_cycle_post_dict(wf) self.api.post(all_models.Cycle, data) wf = all_models.Workflow.query.filter_by(slug=wf_slug).one() self.cycle = all_models.Cycle.query.filter_by( workflow_id=wf.id).one() self.ctask = all_models.CycleTaskGroupObjectTask.query.filter_by( cycle_id=self.cycle.id).first()
def test_recurring_daily_workflow_dates(self, expected_date, expected_num, setup_date, freeze_date, repeat_every): """Test for correct weekdays for daily based workflows. When calculating the dates for daily workflows - only week working days are taken into account. So neither start date nor end date can fall on a weekend. But can fall on holiday. Params: expected - last generated cycle start date """ with freeze_time(freeze_date): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( repeat_every=repeat_every, unit=Workflow.DAY_UNIT) group = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory( task_group=group, start_date=setup_date, end_date=setup_date + dtm.timedelta(days=repeat_every)) self.generator.activate_workflow(workflow) active_wf = db.session.query(Workflow).filter( Workflow.status == 'Active').one() self.assertEqual(len(active_wf.cycles), expected_num) last_cycle_task = active_wf.cycles[-1].cycle_task_group_object_tasks[0] self.assertEqual(expected_date, last_cycle_task.start_date)
def test_update_verification_flag_positive(self, import_value, expected_value): workflow_test_data = { 'WORKFLOW_VERIF': True, 'WORKFLOW_NO_VERIF': False } with freezegun.freeze_time("2017-08-10"): for slug, db_value in workflow_test_data.iteritems(): with factories.single_commit(): workflow = wf_factories.WorkflowFactory( slug=slug, is_verification_needed=db_value) wf_factories.TaskGroupTaskFactory( task_group=wf_factories.TaskGroupFactory( workflow=workflow, context=factories.ContextFactory()), start_date=date(2017, 8, 3), end_date=date(2017, 8, 7)) person = factories.PersonFactory( email="{}@email.py".format(slug)) wf_id = workflow.id self.assertEqual(workflow.status, workflow.DRAFT) resp = self.import_data( collections.OrderedDict([ ("object_type", "Workflow"), ("code", slug), ("title", "SomeTitle"), ("Need Verification", import_value), ("force real-time email updates", "no"), ("Admin", person.email), ])) self.assertEqual(1, resp[0]['updated']) workflow = Workflow.query.filter(Workflow.id == wf_id).first() self.assertEqual(workflow.is_verification_needed, expected_value)
def _generate_workflows(approved, obj_num): """Generate workflows with appropriate data for tests""" generator = wf_generator.WorkflowsGenerator() with factories.single_commit(): workflows = [ wf_factories.WorkflowFactory(object_approval=approved[i]) for i in range(obj_num) ] taskgroups = [ wf_factories.TaskGroupFactory(workflow=workflows[i]) for i in range(obj_num) ] # flake8: noqa pylint: disable=unused-variable tasks = [ wf_factories.TaskGroupTaskFactory(task_group=taskgroups[i]) for i in range(obj_num) ] cycles = [ generator.generate_cycle(workflows[i])[1].id for i in range(obj_num) ] for workflow in workflows: generator.activate_workflow(workflow) return cycles
def setUp(self): super(TestCTGOT, self).setUp() self.api = api_helper.Api() with factories.single_commit(): assignee_1 = factories.PersonFactory(email=self.TASK_ASSIGNEE_1) assignee_2 = factories.PersonFactory(email=self.TASK_ASSIGNEE_2) workflow_owner = factories.PersonFactory(email=self.WORKFLOW_OWNER) nobody = factories.PersonFactory(email=self.NOBODY) workflow_owner_role = (all_models.Role.query .filter_by(name="WorkflowOwner").one()) reader_role = all_models.Role.query.filter_by(name="Reader").one() for person in [assignee_1, assignee_2, workflow_owner, nobody]: bp_factories.UserRoleFactory(person=person, role=reader_role, context=None) workflow = wf_factories.WorkflowFactory() taskgroup = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=taskgroup, contact=assignee_1) wf_factories.TaskGroupTaskFactory(task_group=taskgroup, contact=assignee_2) bp_factories.UserRoleFactory(person=workflow_owner, role=workflow_owner_role, context=workflow.context) generator = wf_generator.WorkflowsGenerator() self.cycle_id = generator.generate_cycle(workflow)[1].id generator.activate_workflow(workflow)
def _create_cycle_structure(self): """Create cycle structure. It will create workflow, cycle, group and 3 tasks in that group. Retruns tuple: workflow, cycle, group and list of tasks. """ wf_slug = "WF-SLUG-{}".format( factories.random_str(length=6, chars=string.ascii_letters)) with factories.single_commit(): workflow = wf_factories.WorkflowFactory(slug=wf_slug) task_group = wf_factories.TaskGroupFactory(workflow=workflow) for ind in xrange(3): wf_factories.TaskGroupTaskFactory( task_group=task_group, title='task{}'.format(ind), start_date=datetime.datetime.now(), end_date=datetime.datetime.now() + datetime.timedelta(7)) data = workflow_api.get_cycle_post_dict(workflow) self.api.post(all_models.Cycle, data) workflow = all_models.Workflow.query.filter_by(slug=wf_slug).one() cycle = all_models.Cycle.query.filter_by(workflow_id=workflow.id).one() group = all_models.CycleTaskGroup.query.filter_by( cycle_id=cycle.id).one() tasks = all_models.CycleTaskGroupObjectTask.query.filter_by( cycle_id=cycle.id).all() return workflow, cycle, group, tasks
def test_ending_archived_workflow_cycles(self): # noqa pylint: disable=invalid-name """Archived workflow should be INACTIVE if current cycles are ended.""" with freezegun.freeze_time("2017-08-10"): with glob_factories.single_commit(): workflow = factories.WorkflowFactory( title="This is a test WF", unit=all_models.Workflow.WEEK_UNIT, repeat_every=1) factories.TaskGroupTaskFactory( task_group=factories.TaskGroupFactory( workflow=workflow, context=glob_factories.ContextFactory(), ), # Two cycles should be created start_date=datetime.date(2017, 8, 3), end_date=datetime.date(2017, 8, 7), ) wf_id = workflow.id self.generator.activate_workflow(workflow) workflow = all_models.Workflow.query.get(wf_id) self.assertEqual(all_models.Workflow.ACTIVE, workflow.status) self.assertIs(workflow.recurrences, True) self.assertEqual(2, len(workflow.cycles)) # Archive workflow self.generator.modify_workflow(workflow, {'recurrences': False}) workflow = all_models.Workflow.query.get(wf_id) self.assertIs(workflow.recurrences, False) self.assertEqual(all_models.Workflow.ACTIVE, workflow.status) # End all current cycles for cycle in workflow.cycles: self.generator.modify_object(cycle, {'is_current': False}) # Archived workflow should be inactive workflow = all_models.Workflow.query.get(wf_id) self.assertEqual(all_models.Workflow.INACTIVE, workflow.status)
def test_archive_workflow(self, tgt_start_date, tgt_end_date, wf_status): """Test archive workflow with status={2}""" with freezegun.freeze_time("2017-08-10"): with glob_factories.single_commit(): workflow = factories.WorkflowFactory( title="This is a test WF", unit=all_models.Workflow.WEEK_UNIT, repeat_every=1) factories.TaskGroupTaskFactory( task_group=factories.TaskGroupFactory( workflow=workflow, context=glob_factories.ContextFactory(), ), start_date=tgt_start_date, end_date=tgt_end_date, ) wf_id = workflow.id self.generator.activate_workflow(workflow) workflow = all_models.Workflow.query.get(wf_id) self.assertEqual(all_models.Workflow.ACTIVE, workflow.status) self.assertIs(workflow.recurrences, True) # Archive workflow self.generator.modify_workflow(workflow, {'recurrences': False}) workflow = all_models.Workflow.query.get(wf_id) self.assertIs(workflow.recurrences, False) self.assertEqual(wf_status, workflow.status)
def test_recalculate_start_date_on_create(self, new_start_date, expected_date): """Test recalculate start_date on create update={} expected={}.""" with freezegun.freeze_time("2017-08-10"): with glob_factories.single_commit(): workflow = factories.WorkflowFactory( title="This is a test WF", unit=all_models.Workflow.WEEK_UNIT, repeat_every=1) task = factories.TaskGroupTaskFactory( task_group=factories.TaskGroupFactory( workflow=workflow, context=glob_factories.ContextFactory(), ), start_date=datetime.date(2017, 8, 10), end_date=datetime.date(2017, 8, 11), ) wf_id = workflow.id task_id = task.id self.generator.activate_workflow(workflow) workflow = all_models.Workflow.query.get(wf_id) task = all_models.TaskGroupTask.query.get(task_id) self.assertEqual(datetime.date(2017, 8, 17), workflow.next_cycle_start_date) self.generator.generate_task_group_task( task.task_group, { 'start_date': new_start_date, 'end_date': new_start_date + datetime.timedelta(1), }) workflow = all_models.Workflow.query.get(wf_id) self.assertEqual(expected_date, workflow.next_cycle_start_date)
def setUp(self): super(TestTaskGroupImport, self).setUp() with factories.single_commit(): workflow = wf_factories.WorkflowFactory(slug=self.WF_SLUG) task_group = wf_factories.TaskGroupFactory(slug=self.TG_SLUG, workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=task_group) factories.PersonFactory(email="*****@*****.**")
def test_workflow_task_group_mapping(self): # pylint: disable=invalid-name """Test workflow and task group mappings.""" with freeze_time("2017-03-07"): workflow = wf_factories.WorkflowFactory() workflow_slug = workflow.slug task_group1 = wf_factories.TaskGroupFactory(workflow=workflow) task_group1_slug = task_group1.slug task_group2 = wf_factories.TaskGroupFactory(workflow=workflow) task_group2_slug = task_group2.slug data = [ { "object_name": "Workflow", "filters": { "expression": { "op": { "name": "relevant" }, "object_name": "TaskGroup", "slugs": [task_group1_slug], }, }, "fields": "all", }, { "object_name": "TaskGroup", "filters": { "expression": { "op": { "name": "relevant" }, "object_name": "__previous__", "ids": [0], }, }, "fields": "all", }, ] response = self.export_csv(data) self.assert200(response) response_data = response.data self.assertEqual(3, response_data.count(workflow_slug)) self.assertIn(task_group1_slug, response_data) self.assertIn(task_group2_slug, response_data)
def test_tgt_has_view_dates(self): """Test get view only fields for TGT.""" workflow = wf_factories.WorkflowFactory() task_group = wf_factories.TaskGroupFactory(workflow=workflow) tgt = wf_factories.TaskGroupTaskFactory(task_group=task_group) resp = self.api.get(tgt, tgt.id) self.assertIn("task_group_task", resp.json) self.assertIn("view_start_date", resp.json["task_group_task"]) self.assertIn("view_end_date", resp.json["task_group_task"])