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_shouldReturnAnApprovalWhenUserIsAuthorizedWithAUserGroup(self): authorized_user_group = GroupObjectFactory() authorized_user = UserObjectFactory(groups=[authorized_user_group]) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") authorization_policies = [ AuthorizationPolicyBuilder().with_group( authorized_user_group).build() ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] 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), has_property("workflow", flow.workflow), has_property( "transition", flow.transitions_metas[0].transitions.first()))))
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_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__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_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") TransitionApprovalMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, 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("source_state", state1), has_property("destination_state", state2))))
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_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_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_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") transition_meta = TransitionMetaFactory.create( workflow=workflow, source_state=state1, destination_state=state2, ) approval_meta = TransitionApprovalMetaFactory.create( workflow=workflow, transition_meta=transition_meta, priority=0) approval_meta.groups.add(authorized_user_group) workflow_object = BasicTestModelObjectFactory(workflow=workflow) 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_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_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 initialize_circular_scenario(self): from river.models.factories import \ TransitionObjectFactory, \ UserObjectFactory, \ PermissionObjectFactory, \ ProceedingMetaObjectFactory, \ StateObjectFactory TransitionObjectFactory.reset_sequence(0) ProceedingMetaObjectFactory.reset_sequence(0) StateObjectFactory.reset_sequence(0) TestModel.objects.all().delete() self.content_type = ContentType.objects.get_for_model(TestModel) self.permissions = PermissionObjectFactory.create_batch(4) self.user1 = UserObjectFactory(user_permissions=[self.permissions[0]]) self.user2 = UserObjectFactory(user_permissions=[self.permissions[1]]) self.user3 = UserObjectFactory(user_permissions=[self.permissions[2]]) self.user4 = UserObjectFactory(user_permissions=[self.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' ) self.transitions = [ TransitionObjectFactory(source_state=self.open_state, destination_state=self.in_progress_state), TransitionObjectFactory(source_state=self.in_progress_state, destination_state=self.resolved_state), TransitionObjectFactory(source_state=self.resolved_state, destination_state=self.re_opened_state), TransitionObjectFactory(source_state=self.resolved_state, destination_state=self.closed_state), TransitionObjectFactory(source_state=self.re_opened_state, destination_state=self.in_progress_state)] self.proceeding_metas = ProceedingMetaObjectFactory.create_batch( 5, content_type=self.content_type, transition=factory.Sequence(lambda n: self.transitions[n]), order=0 ) for n, proceeding_meta in enumerate(self.proceeding_metas): proceeding_meta.permissions.add(self.permissions[n] if n < len(self.permissions) else self.permissions[0]) self.objects = TestModelObjectFactory.create_batch(2)
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__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 setUp(self): from river.models.factories import ProceedingMetaObjectFactory, StateObjectFactory self.state1 = StateObjectFactory() self.state2 = StateObjectFactory() self.state3 = StateObjectFactory() self.content_type = ContentType.objects.get_for_model(TestModel) self.proceeding_meta = ProceedingMetaObjectFactory( content_type=self.content_type, transition__source_state=self.state1, transition__destination_state=self.state2) self.object = TestModelObjectFactory().model
def setUp(self): from river.models.factories import ApprovementMetaObjectFactory, StateObjectFactory self.state1 = StateObjectFactory() self.state2 = StateObjectFactory() self.state3 = StateObjectFactory() self.content_type = ContentType.objects.get_for_model(TestModel) self.approvement_meta = ApprovementMetaObjectFactory( transition__content_type=self.content_type, transition__source_state=self.state1, transition__destination_state=self.state2) self.object = TestModelObjectFactory().model self.field = "my_field"
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 setUp(self): from river.models.factories import \ TransitionObjectFactory, \ UserObjectFactory, \ PermissionObjectFactory, \ ApprovementMetaObjectFactory, \ StateObjectFactory TransitionObjectFactory.reset_sequence(0) ApprovementMetaObjectFactory.reset_sequence(0) StateObjectFactory.reset_sequence(0) TestModel.objects.all().delete() self.content_type = ContentType.objects.get_for_model(TestModel) self.permissions = PermissionObjectFactory.create_batch(4) self.user1 = UserObjectFactory(user_permissions=[self.permissions[0]]) self.user2 = UserObjectFactory(user_permissions=[self.permissions[1]]) self.user3 = UserObjectFactory(user_permissions=[self.permissions[2]]) self.user4 = UserObjectFactory(user_permissions=[self.permissions[3]]) self.field = 'my_field' self.states = StateObjectFactory.create_batch( 9, label=factory.Sequence(lambda n: "s%s" % str(n + 1) if n <= 4 else ("s4.%s" % str(n - 4) if n <= 6 else "s5.%s" % str(n - 6)))) self.transitions = TransitionObjectFactory.create_batch( 8, content_type=self.content_type, field=self.field, source_state=factory.Sequence(lambda n: self.states[ n] if n <= 2 else (self.states[n - 1]) if n <= 4 else ( self.states[n - 2] if n <= 6 else self.states[4])), destination_state=factory.Sequence(lambda n: self.states[n + 1])) self.approvement_metas = ApprovementMetaObjectFactory.create_batch( 9, transition=factory.Sequence(lambda n: self.transitions[n] if n <= 1 else self.transitions[n - 1]), order=factory.Sequence(lambda n: 1 if n == 2 else 0)) for n, approvement_meta in enumerate(self.approvement_metas): approvement_meta.permissions.add( self.permissions[n] if n <= 3 else self.permissions[3]) self.objects = TestModelObjectFactory.create_batch(2)
def initialize_normal_scenario(self): from river.models.factories import \ TransitionObjectFactory, \ UserObjectFactory, \ PermissionObjectFactory, \ ProceedingMetaObjectFactory, \ StateObjectFactory TransitionObjectFactory.reset_sequence(0) ProceedingMetaObjectFactory.reset_sequence(0) StateObjectFactory.reset_sequence(0) TestModel.objects.all().delete() self.content_type = ContentType.objects.get_for_model(TestModel) self.permissions = PermissionObjectFactory.create_batch(4) self.user1 = UserObjectFactory(user_permissions=[self.permissions[0]]) self.user2 = UserObjectFactory(user_permissions=[self.permissions[1]]) self.user3 = UserObjectFactory(user_permissions=[self.permissions[2]]) self.user4 = UserObjectFactory(user_permissions=[self.permissions[3]]) self.states = StateObjectFactory.create_batch( 9, label=factory.Sequence( lambda n: "s%s" % str(n + 1) if n <= 4 else ("s4.%s" % str(n - 4) if n <= 6 else "s5.%s" % str(n - 6))) ) self.transitions = TransitionObjectFactory.create_batch(8, source_state=factory.Sequence( lambda n: self.states[n] if n <= 2 else ( self.states[n - 1]) if n <= 4 else ( self.states[n - 2] if n <= 6 else self.states[ 4])), destination_state=factory.Sequence( lambda n: self.states[n + 1])) self.proceeding_metas = ProceedingMetaObjectFactory.create_batch( 9, content_type=self.content_type, transition=factory.Sequence(lambda n: self.transitions[n] if n <= 1 else self.transitions[n - 1]), order=factory.Sequence(lambda n: 1 if n == 2 else 0) ) for n, proceeding_meta in enumerate(self.proceeding_metas): proceeding_meta.permissions.add(self.permissions[n] if n <= 3 else self.permissions[3]) self.objects = TestModelObjectFactory.create_batch(2)
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 test_shouldInvokeCallbackThatIsRegisteredWithoutInstanceWhenFlowIsComplete( self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = StateObjectFactory(label="state1") state2 = StateObjectFactory(label="state2") state3 = StateObjectFactory(label="state3") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state2, state3, authorization_policies) \ .build() workflow_object = flow.objects[0] self.hook_post_complete(flow.workflow) assert_that(self.get_output(), none()) assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(state2))) assert_that(self.get_output(), none()) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(state3))) output = self.get_output() assert_that(output, has_length(1)) assert_that(output[0], has_key("hook")) assert_that(output[0]["hook"], has_entry("type", "on-complete")) assert_that(output[0]["hook"], has_entry("when", AFTER)) assert_that( output[0]["hook"], has_entry( "payload", has_entry(equal_to("workflow_object"), equal_to(workflow_object))))
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 initialize_circular_scenario(self): from river.models.factories import \ TransitionObjectFactory, \ UserObjectFactory, \ PermissionObjectFactory, \ ProceedingMetaObjectFactory, \ StateObjectFactory TransitionObjectFactory.reset_sequence(0) ProceedingMetaObjectFactory.reset_sequence(0) StateObjectFactory.reset_sequence(0) TestModel.objects.all().delete() self.content_type = ContentType.objects.get_for_model(TestModel) self.permissions = PermissionObjectFactory.create_batch(4) self.user1 = UserObjectFactory(user_permissions=[self.permissions[0]]) self.user2 = UserObjectFactory(user_permissions=[self.permissions[1]]) self.user3 = UserObjectFactory(user_permissions=[self.permissions[2]]) self.user4 = UserObjectFactory(user_permissions=[self.permissions[3]]) self.field = 'my_field' 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') self.transitions = [ TransitionObjectFactory(source_state=self.open_state, destination_state=self.in_progress_state), TransitionObjectFactory(source_state=self.in_progress_state, destination_state=self.resolved_state), TransitionObjectFactory(source_state=self.resolved_state, destination_state=self.re_opened_state), TransitionObjectFactory(source_state=self.resolved_state, destination_state=self.closed_state), TransitionObjectFactory(source_state=self.re_opened_state, destination_state=self.in_progress_state) ] self.proceeding_metas = ProceedingMetaObjectFactory.create_batch( 5, content_type=self.content_type, field=self.field, transition=factory.Sequence(lambda n: self.transitions[n]), order=0) for n, proceeding_meta in enumerate(self.proceeding_metas): proceeding_meta.permissions.add(self.permissions[n] if n < len( self.permissions) else self.permissions[0]) self.objects = TestModelObjectFactory.create_batch(2)
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))