def test_shouldReturnNoApprovalWhenUserIsUnAuthorized(self): unauthorized_user = UserObjectFactory() state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") authorized_permission = PermissionObjectFactory() transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0, permissions=[authorized_permission]) BasicTestModelObjectFactory(workflow=workflow) available_approvals = BasicTestModel.river.my_field.get_available_approvals( as_user=unauthorized_user) assert_that(available_approvals, has_length(0))
def test__shouldReturnApprovalsOnTimeWhenTooManyWorkflowObject(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0, permissions=[authorized_permission]) self.objects = BasicTestModelObjectFactory.create_batch( 250, workflow=workflow) before = datetime.now() BasicTestModel.river.my_field.get_on_approval_objects( as_user=authorized_user) after = datetime.now() assert_that(after - before, less_than(timedelta(milliseconds=200))) print("Time taken %s" % str(after - before))
def test_shouldTransitToTheGivenNextStateWhenThereAreMultipleNextStates(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory(user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") state3 = StateObjectFactory(label="state3") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state3, priority=0, permissions=[authorized_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that(workflow_object.model.my_field, equal_to(state1)) workflow_object.model.river.my_field.approve(as_user=authorized_user, next_state=state3) assert_that(workflow_object.model.my_field, equal_to(state3))
def test_shouldInvokeTheRegisteredViaClassApiCallBackWhenATransitionHappens( self): self.test_args = None self.test_kwargs = None def test_callback(*args, **kwargs): self.test_args = args self.test_kwargs = kwargs authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") content_type = ContentType.objects.get_for_model(BasicTestModel) workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission]) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=1, permissions=[authorized_permission]) workflow_object = BasicTestModelObjectFactory() BasicTestModel.river.my_field.hook_post_transition(test_callback) assert_that(self.test_args, none()) assert_that(workflow_object.model.my_field, equal_to(state1)) workflow_object.model.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.model.my_field, equal_to(state1)) assert_that(self.test_args, none()) workflow_object.model.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.model.my_field, equal_to(state2)) assert_that(self.test_args, equal_to((workflow_object.model, "my_field"))) last_approval = TransitionApproval.objects.get( object_id=workflow_object.model.pk, source_state=state1, destination_state=state2, priority=1) assert_that( self.test_kwargs, has_entry(equal_to("transition_approval"), equal_to(last_approval)))
def test_shouldDictatePassingNextStateWhenThereAreMultiple(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory(user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") state3 = StateObjectFactory(label="state3") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state3, priority=0, permissions=[authorized_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that( calling(workflow_object.model.river.my_field.approve).with_args(as_user=authorized_user), raises(RiverException, "State must be given when there are multiple states for destination") )
def test_shouldNotReturnOtherObjectsApprovalsForTheAuthorizedUser(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory(user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) workflow_object1 = BasicTestModelObjectFactory() workflow_object2 = BasicTestModelObjectFactory() available_approvals = workflow_object1.model.river.my_field.get_available_approvals(as_user=authorized_user) assert_that(available_approvals, has_length(1)) assert_that(list(available_approvals), has_item( has_property("workflow_object", workflow_object1.model) )) assert_that(list(available_approvals), has_item( is_not(has_property("workflow_object", workflow_object2.model)) ))
def test_shouldNotTransitToNextStateWhenThereAreMultipleApprovalsToBeApproved(self): manager_permission = PermissionObjectFactory() team_leader_permission = PermissionObjectFactory() team_leader = UserObjectFactory(user_permissions=[team_leader_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=1, permissions=[manager_permission] ) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[team_leader_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that(workflow_object.model.my_field, equal_to(state1)) workflow_object.model.river.my_field.approve(team_leader) assert_that(workflow_object.model.my_field, equal_to(state1))
def test_shouldNotLetUserWhosePriorityComesLaterApproveProceed(self): manager_permission = PermissionObjectFactory() team_leader_permission = PermissionObjectFactory() manager = UserObjectFactory(user_permissions=[manager_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=1, permissions=[manager_permission] ) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[team_leader_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that( calling(workflow_object.model.river.my_field.approve).with_args(as_user=manager), raises(RiverException, "There is no available approval for the user") )
def test_shouldNotAllowWorkflowToBeDeletedWhenThereIsATransitionApproval( self): content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta, priority=0) BasicTestModelObjectFactory(workflow=workflow) TransitionApproval.objects.filter(workflow=workflow).update( status=APPROVED) approvals = TransitionApproval.objects.filter(workflow=workflow) assert_that(approvals, has_length(1)) assert_that( calling(workflow.delete), raises( ProtectedError, "Cannot delete some instances of model 'Workflow' because they are referenced through a protected foreign key" ))
def test_shouldInjectTheField(self): # pylint: disable=no-self-use assert_that(BasicTestModel, has_property('river', is_(instance_of(RiverObject)))) assert_that(BasicTestModel.river, has_property('my_field', is_(instance_of(ClassWorkflowObject)))) content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = StateObjectFactory.create(label="state1") state2 = StateObjectFactory.create(label="state2") workflow = WorkflowFactory(content_type=content_type, field_name="my_field", initial_state=state1) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0 ) test_model = BasicTestModel.objects.create() assert_that(test_model, has_property('river', is_(instance_of(RiverObject)))) assert_that(test_model.river, has_property('my_field', is_(instance_of(InstanceWorkflowObject)))) assert_that(BasicTestModel.river.my_field, has_property('initial_state', is_(instance_of(State)))) assert_that(BasicTestModel.river.my_field, has_property('final_states', is_(instance_of(QuerySet)))) assert_that(test_model.river.my_field, has_property('approve', has_property("__call__"))) assert_that(test_model.river.my_field, has_property('on_initial_state', is_(instance_of(bool)))) assert_that(test_model.river.my_field, has_property('on_final_state', is_(instance_of(bool))))
def test_shouldReturnAnApprovalWhenUserIsAuthorizedWithAPermission(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory(user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0, permissions=[authorized_permission] ) workflow_object = BasicTestModelObjectFactory() available_approvals = BasicTestModel.river.my_field.get_available_approvals(as_user=authorized_user) assert_that(available_approvals, has_length(1)) assert_that(list(available_approvals), has_item( all_of( has_property("workflow_object", workflow_object.model), has_property("workflow", workflow), has_property("transition", transition_meta.transitions.first()) ) ))
def test_injections(self): self.assertTrue(hasattr(TestModel, 'river')) self.assertIsInstance(TestModel.river, RiverObject) self.assertTrue(hasattr(TestModel.river, "my_field")) self.assertIsInstance(TestModel.river.my_field, ClassWorkflowObject) content_type = ContentType.objects.get_for_model(TestModel) state1 = StateObjectFactory.create(label="state1") state2 = StateObjectFactory.create(label="state2") TransitionApprovalMetaFactory.create( content_type=content_type, field_name="my_field", source_state=state1, destination_state=state2, priority=0 ) test_model = TestModel.objects.create() self.assertTrue(hasattr(test_model, "river")) self.assertIsInstance(test_model.river, RiverObject) self.assertTrue(hasattr(test_model.river, "my_field")) self.assertIsInstance(test_model.river.my_field, InstanceWorkflowObject) self.assertTrue(hasattr(test_model.river.my_field, "approve")) self.assertTrue(callable(test_model.river.my_field.approve)) self.assertTrue(test_model.river.my_field.on_initial_state) self.assertFalse(test_model.river.my_field.on_final_state) self.assertEqual(state1, TestModel.river.my_field.initial_state) self.assertEqual(1, TestModel.river.my_field.final_states.count()) self.assertEqual(state2, TestModel.river.my_field.final_states[0])
def test_shouldReturnAnApprovalWhenUserIsAuthorizedAsTransactioner(self): authorized_user = UserObjectFactory() state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state2, priority=0) workflow_object = BasicTestModelObjectFactory() TransitionApproval.objects.filter( workflow_object=workflow_object.model).update( transactioner=authorized_user) available_approvals = BasicTestModel.river.my_field.get_available_approvals( as_user=authorized_user) assert_that(available_approvals, has_length(1)) assert_that( list(available_approvals), has_item( all_of(has_property("workflow_object", workflow_object.model), has_property("workflow", workflow), has_property("destination_state", state2))))
def test__shouldMigrateTransitionApprovalStatusToStringInDB(self): out = StringIO() sys.stout = out state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory( initial_state=state1, content_type=ContentType.objects.get_for_model(BasicTestModel), field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta, priority=0) workflow_object = BasicTestModelObjectFactory() with connection.cursor() as cur: result = cur.execute( "select status from river_transitionapproval where object_id=%s;" % workflow_object.model.pk).fetchall() assert_that(result[0][0], equal_to("pending")) call_command('migrate', 'river', '0004', stdout=out) with connection.cursor() as cur: schema = cur.execute( "PRAGMA table_info('river_transitionapproval');").fetchall() status_col_type = list( filter(lambda column: column[1] == 'status', schema))[0][2] assert_that(status_col_type, equal_to("integer")) result = cur.execute( "select status from river_transitionapproval where object_id=%s;" % workflow_object.model.pk).fetchall() assert_that(result[0][0], equal_to(0)) call_command('migrate', 'river', '0005', stdout=out) with connection.cursor() as cur: schema = cur.execute( "PRAGMA table_info('river_transitionapproval');").fetchall() status_col_type = list( filter(lambda column: column[1] == 'status', schema))[0][2] assert_that(status_col_type, equal_to("varchar(100)")) result = cur.execute( "select status from river_transitionapproval where object_id=%s;" % workflow_object.model.pk).fetchall() assert_that(result[0][0], equal_to("pending"))
def test_shouldInvokeTheRegisteredViaInstanceApiCallBackWhenFlowIsCompleteForTheObject( self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") state3 = StateObjectFactory(label="state3") content_type = ContentType.objects.get_for_model(BasicTestModel) workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission]) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state2, destination_state=state3, priority=0, permissions=[authorized_permission]) workflow_object = BasicTestModelObjectFactory() self.test_args = None self.test_kwargs = None def test_callback(*args, **kwargs): self.test_args = args self.test_kwargs = kwargs workflow_object.model.river.my_field.hook_post_complete(test_callback) assert_that(self.test_args, none()) assert_that(workflow_object.model.my_field, equal_to(state1)) workflow_object.model.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.model.my_field, equal_to(state2)) assert_that(self.test_args, none()) workflow_object.model.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.model.my_field, equal_to(state3)) assert_that(self.test_args, equal_to((workflow_object.model, "my_field")))
def authorization_rule_with_permission(context, source_state_label, destination_state_label, permission_name, priority): from django.contrib.auth.models import Permission from river.models.factories import TransitionApprovalMetaFactory permission = Permission.objects.get(name=permission_name) transition_identifier = source_state_label + destination_state_label transition_meta = getattr(context, "transitions", {})[transition_identifier] TransitionApprovalMetaFactory.create( workflow=transition_meta.workflow, transition_meta=transition_meta, priority=priority, permissions=[permission] )
def test_shouldAssesFinalStateProperlyWhenThereAreMultiple(self): state1 = StateObjectFactory(label="state1") state21 = StateObjectFactory(label="state21") state22 = StateObjectFactory(label="state22") state31 = StateObjectFactory(label="state31") state32 = StateObjectFactory(label="state32") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state21, priority=0) TransitionApprovalMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state22, priority=0) TransitionApprovalMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state31, priority=0) TransitionApprovalMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state32, priority=0) assert_that(BasicTestModel.river.my_field.final_states, has_length(4)) assert_that(list(BasicTestModel.river.my_field.final_states), has_items(state21, state22, state31, state32))
def test_shouldAssesInitialStateProperly(self): state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state2, priority=0) assert_that(BasicTestModel.river.my_field.initial_state, equal_to(state1))
def test__shouldMigrationForIterationMustFinishInShortAmountOfTimeWithTooManyObject(self): out = StringIO() sys.stout = out state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") state3 = StateObjectFactory(label="state3") state4 = StateObjectFactory(label="state4") workflow = WorkflowFactory(initial_state=state1, content_type=ContentType.objects.get_for_model(BasicTestModel), field_name="my_field") transition_meta_1 = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state1, ) transition_meta_2 = TransitionMetaFactory.create( workflow=workflow, source_state=state2, destination_state=state3, ) transition_meta_3 = TransitionMetaFactory.create( workflow=workflow, source_state=state2, destination_state=state4, ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta_1, priority=0 ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta_2, priority=0 ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta_2, priority=1 ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta_3, priority=0 ) BasicTestModelObjectFactory.create_batch(250) call_command('migrate', 'river', '0006', stdout=out) before = datetime.now() call_command('migrate', 'river', '0007', stdout=out) after = datetime.now() assert_that(after - before, less_than(timedelta(minutes=5)))
def test_shouldNotDeleteApprovedTransitionWhenDeleted(self): content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) meta1 = TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0) BasicTestModelObjectFactory(workflow=workflow) TransitionApproval.objects.filter(workflow=workflow).update( status=APPROVED) approvals = TransitionApproval.objects.filter(workflow=workflow) assert_that(approvals, has_length(1)) assert_that(approvals, has_item(has_property("meta", meta1))) meta1.delete() approvals = TransitionApproval.objects.filter(workflow=workflow) assert_that(approvals, has_length(1)) assert_that(approvals, has_item(has_property("meta", none())))
def test_shouldNotDeletePendingTransitionWhenDeleted(self): content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) meta1 = TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0) BasicTestModelObjectFactory(workflow=workflow) TransitionApproval.objects.filter(workflow=workflow).update( status=PENDING) assert_that(TransitionApproval.objects.filter(workflow=workflow), has_length(1)) meta1.delete() assert_that(TransitionApproval.objects.filter(workflow=workflow), has_length(0))
def test_shouldReturnAnApprovalWhenUserIsAuthorizedWithAUserGroup(self): authorized_user_group = GroupObjectFactory() authorized_user = UserObjectFactory(groups=[authorized_user_group]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") approval_meta = TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0) approval_meta.groups.add(authorized_user_group) workflow_object = BasicTestModelObjectFactory() available_approvals = BasicTestModel.river.my_field.get_available_approvals( as_user=authorized_user) assert_that(available_approvals, has_length(1)) assert_that( list(available_approvals), has_item( all_of(has_property("workflow_object", workflow_object.model), has_property("workflow", workflow), has_property("source_state", state1), has_property("destination_state", state2))))
def test_shouldAssesFinalStateProperlyWhenThereIsOnlyOne(self): state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0 ) assert_that(BasicTestModel.river.my_field.final_states, has_length(1)) assert_that(list(BasicTestModel.river.my_field.final_states), has_item(state2))
def setUp(self): self.field_name = "my_field" authorized_permission = PermissionObjectFactory() state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") content_type = ContentType.objects.get_for_model(BasicTestModel) workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) app_config.HOOKING_BACKEND_CLASS = 'river.hooking.backends.memory.MemoryHookingBackend' self.handler_backend = callback_backend self.handler_backend.callbacks = {}
def initialize_circular_scenario(self): StateObjectFactory.reset_sequence(0) TransitionApprovalMetaFactory.reset_sequence(0) content_type = ContentType.objects.get_for_model(TestModel) permissions = PermissionObjectFactory.create_batch(4) self.user1 = UserObjectFactory(user_permissions=[permissions[0]]) self.user2 = UserObjectFactory(user_permissions=[permissions[1]]) self.user3 = UserObjectFactory(user_permissions=[permissions[2]]) self.user4 = UserObjectFactory(user_permissions=[permissions[3]]) self.open_state = StateObjectFactory(label='open') self.in_progress_state = StateObjectFactory(label='in-progress') self.resolved_state = StateObjectFactory(label='resolved') self.re_opened_state = StateObjectFactory(label='re-opened') self.closed_state = StateObjectFactory(label='closed') t1 = TransitionApprovalMetaFactory.create( field_name="my_field", content_type=content_type, source_state=self.open_state, destination_state=self.in_progress_state, ) t1.permissions.add(permissions[0]) t2 = TransitionApprovalMetaFactory.create( field_name="my_field", content_type=content_type, source_state=self.in_progress_state, destination_state=self.resolved_state, ) t2.permissions.add(permissions[1]) t3 = TransitionApprovalMetaFactory.create( field_name="my_field", content_type=content_type, source_state=self.resolved_state, destination_state=self.re_opened_state, ) t3.permissions.add(permissions[2]) t4 = TransitionApprovalMetaFactory.create( field_name="my_field", content_type=content_type, source_state=self.resolved_state, destination_state=self.closed_state, ) t4.permissions.add(permissions[3]) t5 = TransitionApprovalMetaFactory.create( field_name="my_field", content_type=content_type, source_state=self.re_opened_state, destination_state=self.in_progress_state, ) t5.permissions.add(permissions[0])
def authorization_rule_with_groups(context, source_state_label, destination_state_label, group_names, priority): from django.contrib.auth.models import Group from river.models.factories import TransitionApprovalMetaFactory transition_identifier = source_state_label + destination_state_label transition_meta = getattr(context, "transitions", {})[transition_identifier] transition_approval_meta = TransitionApprovalMetaFactory.create( workflow=transition_meta.workflow, transition_meta=transition_meta, priority=priority, ) transition_approval_meta.groups.set(Group.objects.filter(name__in=group_names))
def test_shouldNotAllowUnauthorizedUserToProceedToNextState(self): unauthorized_user = UserObjectFactory() state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") authorized_permission = PermissionObjectFactory() workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that( calling(workflow_object.model.river.my_field.approve).with_args(as_user=unauthorized_user), raises(RiverException, "There is no available approval for the user") )
def test_shouldAllowAuthorizedUserToProceedToNextState(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory(user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that(workflow_object.model.my_field, equal_to(state1)) workflow_object.model.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.model.my_field, equal_to(state2))
def test_shouldNotAcceptANextStateWhichIsNotAmongPossibleNextStates(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory(user_permissions=[authorized_permission]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") state3 = StateObjectFactory(label="state3") invalid_state = StateObjectFactory(label="state4") workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, priority=0, permissions=[authorized_permission] ) TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state3, priority=0, permissions=[authorized_permission] ) workflow_object = BasicTestModelObjectFactory() assert_that( calling(workflow_object.model.river.my_field.approve).with_args(as_user=authorized_user, next_state=invalid_state), raises(RiverException, "Invalid state is given\(%s\). Valid states is\(are\) (%s|%s)" % ( invalid_state.label, ",".join([state2.label, state3.label]), ",".join([state3.label, state2.label])) ) )
def build(self): workflow = None states = {} transition_metas = [] transitions_approval_metas = [] workflow_objects = [] for additional_raw_state in self.additional_raw_states: state, _ = State.objects.get_or_create(label=additional_raw_state.label) states[state.label] = state for raw_transition in self.raw_transitions: source_state, _ = State.objects.get_or_create(label=raw_transition.source_state.label) if not workflow: workflow = Workflow.objects.create(field_name=self.field_name, content_type=self.content_type, initial_state=source_state) destination_state, _ = State.objects.get_or_create(label=raw_transition.destination_state.label) states[source_state.label] = source_state states[destination_state.label] = destination_state transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=source_state, destination_state=destination_state, ) transition_metas.append(transition_meta) if raw_transition.authorization_policies: for authorization_policy in raw_transition.authorization_policies: transition_approval_meta = TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=authorization_policy.priority, permissions=authorization_policy.permissions, ) transition_approval_meta.groups.set(authorization_policy.groups) transitions_approval_metas.append(transition_approval_meta) for i in range(self.objects_count): workflow_objects.append(self.object_factory()) return Flow(workflow, states, transition_metas, transitions_approval_metas, workflow_objects)