def test_shouldJumpToASpecificState(self): authorized_permission = PermissionObjectFactory() state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state2, state3, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) approvals = TransitionApproval.objects.filter( workflow_object=workflow_object) assert_that(approvals, has_approval(state1, state2, PENDING)) assert_that(approvals, has_approval(state2, state3, PENDING)) workflow_object.river.my_field.jump_to(flow.get_state(state3)) assert_that(workflow_object.my_field, equal_to(flow.get_state(state3))) approvals = TransitionApproval.objects.filter( workflow_object=workflow_object) assert_that(approvals, has_approval(state1, state2, JUMPED)) assert_that(approvals, has_approval(state2, state3, JUMPED))
def test_shouldNotAcceptANextStateWhichIsNotAmongPossibleNextStates(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") invalid_state = RawState("state4") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state1, state3, authorization_policies) \ .with_additional_state(invalid_state) \ .build() workflow_object = flow.objects[0] assert_that( calling(workflow_object.river.my_field.approve).with_args( as_user=authorized_user, next_state=flow.get_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 test_shouldNotTransitToNextStateWhenThereAreMultipleApprovalsToBeApproved( self): manager_permission = PermissionObjectFactory() team_leader_permission = PermissionObjectFactory() team_leader = UserObjectFactory( user_permissions=[team_leader_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_priority(0).with_permission( team_leader_permission).build(), AuthorizationPolicyBuilder().with_priority(1).with_permission( manager_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) workflow_object.river.my_field.approve(team_leader) assert_that(workflow_object.my_field, equal_to(flow.get_state(state1)))
def test_shouldNotJumpBackToAPreviousState(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] workflow_object.river.my_field.approve( as_user=authorized_user, next_state=flow.get_state(state2)) assert_that( calling(workflow_object.river.my_field.jump_to).with_args( flow.get_state(state1)), raises( RiverException, "This state is not available to be jumped in the future of this object" ))
def test_shouldTransitToTheGivenNextStateWhenThereAreMultipleNextStates( self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state1, state3, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) workflow_object.river.my_field.approve( as_user=authorized_user, next_state=flow.get_state(state3)) assert_that(workflow_object.my_field, equal_to(flow.get_state(state3)))
def test_shouldNotCrashWhenAModelObjectWithStringPrimaryKeyIsApproved( self): content_type = ContentType.objects.get_for_model( ModelWithStringPrimaryKey) authorized_permission = PermissionObjectFactory( content_type=content_type) authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("status", content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_object_factory(lambda: ModelWithStringPrimaryKey.objects.create()) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.status, equal_to(flow.get_state(state1))) workflow_object.river.status.approve(as_user=authorized_user) assert_that(workflow_object.status, equal_to(flow.get_state(state2)))
def test_shouldNotCancelDescendantsThatCanBeTransitedInTheFuture(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state_1") state2 = RawState("state_2") state3 = RawState("state_3") final_state = RawState("final_state") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state1, state3, authorization_policies) \ .with_transition(state2, state3, authorization_policies) \ .with_transition(state3, final_state, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) workflow_object.river.my_field.approve( as_user=authorized_user, next_state=flow.get_state(state2)) assert_that(workflow_object.my_field, equal_to(flow.get_state(state2))) approvals = TransitionApproval.objects.filter( workflow=flow.workflow, workflow_object=workflow_object) assert_that(approvals, has_approval(state3, final_state, PENDING))
def test_shouldAssesInitialStateProperly(self): state1 = RawState("state1") state2 = RawState("state2") flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, []) \ .build() assert_that(BasicTestModel.river.my_field.initial_state, equal_to(flow.get_state(state1)))
def test_shouldAssesFinalStateProperlyWhenThereIsOnlyOne(self): state1 = RawState("state1") state2 = RawState("state2") flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, []) \ .build() assert_that(BasicTestModel.river.my_field.final_states, has_length(1)) assert_that(list(BasicTestModel.river.my_field.final_states), has_item(flow.get_state(state2)))
def test__shouldHandleUndefinedSecondWorkflowCase(self): state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [] flow = FlowBuilder("status1", ContentType.objects.get_for_model(ModelWithTwoStateFields)) \ .with_object_factory(lambda: ModelWithTwoStateFieldsObjectFactory().model) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.status1, equal_to(flow.get_state(state1))) assert_that(workflow_object.status2, none())
def test_shouldNotLetUserWhosePriorityComesLaterApproveProceed(self): manager_permission = PermissionObjectFactory() team_leader_permission = PermissionObjectFactory() manager = UserObjectFactory(user_permissions=[manager_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_priority(0).with_permission( team_leader_permission).build(), AuthorizationPolicyBuilder().with_priority(1).with_permission( manager_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that( calling(workflow_object.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_shouldNotReturnOtherObjectsApprovalsForTheAuthorizedUser(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build() ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_objects(2) \ .build() workflow_object1 = flow.objects[0] workflow_object2 = flow.objects[1] available_approvals = workflow_object1.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))) assert_that( list(available_approvals), has_item(is_not(has_property("workflow_object", workflow_object2))))
def test_shouldDictatePassingNextStateWhenThereAreMultiple(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state1, state3, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that( calling(workflow_object.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_shouldNotAllowTheStateToBeDeletedWhenThereIsATransitionApprovalThatIsUsedAsDestination(self): content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = RawState("state_1") state2 = RawState("state_2") state3 = RawState("state_3") authorization_policies = [] flow = FlowBuilder("my_field", content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state2, state3, authorization_policies) \ .build() assert_that( calling(flow.get_state(state3).delete), raises(ProtectedError, "Cannot delete some instances of model 'State' because they are referenced through a protected foreign key") )
def test_shouldInvokeCallbackThatIsRegisteredWithInstanceWhenFlowIsComplete( self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("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, workflow_object=workflow_object) 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 test_shouldCancelAllOtherStateTransitionDescendants(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") state4 = RawState("state4") state5 = RawState("state5") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state1, state3, authorization_policies) \ .with_transition(state1, state4, authorization_policies) \ .with_transition(state4, state5, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) workflow_object.river.my_field.approve( as_user=authorized_user, next_state=flow.get_state(state3)) assert_that(workflow_object.my_field, equal_to(flow.get_state(state3))) assert_that( flow.transitions_approval_metas[0].transition_approvals.all(), all_of(has_length(1), has_item(has_property("status", CANCELLED)))) assert_that( flow.transitions_approval_metas[1].transition_approvals.all(), all_of(has_length(1), has_item(has_property("status", APPROVED)))) assert_that( flow.transitions_approval_metas[2].transition_approvals.all(), all_of(has_length(1), has_item(has_property("status", CANCELLED)))) assert_that( flow.transitions_approval_metas[3].transition_approvals.all(), all_of(has_length(1), has_item(has_property("status", CANCELLED))))
def test_shouldAssesFinalStateProperlyWhenThereAreMultiple(self): state1 = RawState("state1") state21 = RawState("state21") state22 = RawState("state22") state31 = RawState("state31") state32 = RawState("state32") flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state21, []) \ .with_transition(state1, state22, []) \ .with_transition(state1, state31, []) \ .with_transition(state1, state32, []) \ .build() assert_that(BasicTestModel.river.my_field.final_states, has_length(4)) assert_that( list(BasicTestModel.river.my_field.final_states), has_items(flow.get_state(state21), flow.get_state(state22), flow.get_state(state31), flow.get_state(state32)))
def test_shouldAllowAuthorizedUserToProceedToNextState(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build() ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] 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)))
def test_shouldInvokeCallbackThatIsRegisteredWithoutInstanceWhenTransitionHappens( self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("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_transition(flow.workflow, flow.transitions_metas[1]) 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))) last_approval = TransitionApproval.objects.get( object_id=workflow_object.pk, transition__source_state=flow.get_state(state2), transition__destination_state=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-transit")) assert_that(output[0]["hook"], has_entry("when", AFTER)) assert_that( output[0]["hook"], has_entry( "payload", all_of( has_entry(equal_to("workflow_object"), equal_to(workflow_object)), has_entry(equal_to("transition_approval"), equal_to(last_approval)))))
def test__shouldReturnNextApprovals(self): state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") authorization_policies = [AuthorizationPolicyBuilder().build()] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_transition(state1, state3, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(state1))) next_approvals = workflow_object.river.my_field.next_approvals assert_that(next_approvals, has_length(2)) assert_that( next_approvals, has_item(flow.transitions_approval_metas[0].transition_approvals. first())) assert_that( next_approvals, has_item(flow.transitions_approval_metas[1].transition_approvals. first()))
def test_shouldNotAllowWorkflowToBeDeletedWhenThereIsATransitionApproval( self): content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = RawState("state_1") state2 = RawState("state_2") authorization_policies = [] flow = FlowBuilder("my_field", content_type).with_transition( state1, state2, authorization_policies).build() assert_that( calling(flow.workflow.delete), raises( ProtectedError, "Cannot delete some instances of model 'Workflow' because they are referenced through .*" ))
def test_shouldInvokeCallbackForTheOnlyGivenTransition(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("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) \ .with_transition(state3, state1, authorization_policies) \ .build() workflow_object = flow.objects[0] workflow_object.river.my_field.approve(as_user=authorized_user) workflow_object.river.my_field.approve(as_user=authorized_user) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that( TransitionApproval.objects.filter( meta=flow.transitions_approval_metas[0]), has_length(2)) first_approval = TransitionApproval.objects.filter( meta=flow.transitions_approval_metas[0], transition__iteration=0).first() assert_that(first_approval, is_not(none())) self.hook_pre_transition( flow.workflow, flow.transitions_metas[0], transition=flow.transitions_metas[0].transitions.first()) output = self.get_output() assert_that(output, none()) workflow_object.river.my_field.approve(as_user=authorized_user) output = self.get_output() assert_that(output, none())
def test_shouldReturnNoApprovalWhenUserIsUnAuthorized(self): unauthorized_user = UserObjectFactory() authorized_permission = PermissionObjectFactory() state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build() ] FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() available_approvals = BasicTestModel.river.my_field.get_available_approvals( as_user=unauthorized_user) assert_that(available_approvals, has_length(0))
def test_shouldAllowMultipleCyclicTransitions(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) initial_state = RawState("initial_state") cycle_state_1 = RawState("cycle_state_1") cycle_state_2 = RawState("cycle_state_2") off_the_cycle_state = RawState("off_the_cycle_state") cycle_state_3 = RawState("cycle_state_3") cycle_state_4 = RawState("cycle_state_4") final_state = RawState("final_state") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(initial_state, cycle_state_1, authorization_policies) \ .with_transition(cycle_state_1, cycle_state_2, authorization_policies) \ .with_transition(cycle_state_2, cycle_state_1, authorization_policies) \ .with_transition(cycle_state_1, off_the_cycle_state, authorization_policies) \ .with_transition(off_the_cycle_state, cycle_state_3, authorization_policies) \ .with_transition(cycle_state_3, cycle_state_4, authorization_policies) \ .with_transition(cycle_state_4, cycle_state_3, authorization_policies) \ .with_transition(cycle_state_3, final_state, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(initial_state))) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_1))) workflow_object.river.my_field.approve( as_user=authorized_user, next_state=flow.get_state(cycle_state_2)) assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_2))) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_1)))
def test_shouldNotAllowUnauthorizedUserToProceedToNextState(self): unauthorized_user = UserObjectFactory() authorized_permission = PermissionObjectFactory() state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build() ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that( calling(workflow_object.river.my_field.approve).with_args( as_user=unauthorized_user), raises(RiverException, "There is no available approval for the user"))
def test_shouldAssessIterationsCorrectly(self): authorized_permission1 = PermissionObjectFactory() authorized_permission2 = PermissionObjectFactory() state1 = RawState("state1") state2 = RawState("state2") state3 = RawState("state3") flow = FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, [AuthorizationPolicyBuilder().with_permission(authorized_permission1).build()]) \ .with_transition(state2, state3, [ AuthorizationPolicyBuilder().with_permission(authorized_permission1).build(), AuthorizationPolicyBuilder().with_priority(1).with_permission(authorized_permission2).build(), ]) \ .build() workflow_object = flow.objects[0] approvals = TransitionApproval.objects.filter( workflow=flow.workflow, workflow_object=workflow_object) assert_that(approvals, has_length(3)) assert_that(approvals, has_approval(state1, state2, PENDING, iteration=0)) assert_that( approvals, has_approval(state2, state3, PENDING, iteration=1, permissions=[authorized_permission1])) assert_that( approvals, has_approval(state2, state3, PENDING, iteration=1, permissions=[authorized_permission2]))
def test__shouldReturnApprovalsOnTimeWhenTooManyWorkflowObject(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build() ] FlowBuilder("my_field", self.content_type) \ .with_transition(state1, state2, authorization_policies) \ .with_objects(250) \ .build() before = datetime.now() approvals = BasicTestModel.river.my_field.get_on_approval_objects( as_user=authorized_user) after = datetime.now() assert_that(after - before, less_than(timedelta(milliseconds=200))) assert_that(approvals, has_length(250)) print("Time taken %s" % str(after - before))
def test_shouldInvokeCallbackThatIsRegisteredWithoutInstanceWhenAnApprovingHappens( self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) content_type = ContentType.objects.get_for_model(BasicTestModel) state1 = RawState("state1") state2 = RawState("state2") authorization_policies = [ AuthorizationPolicyBuilder().with_priority(0).with_permission( authorized_permission).build(), AuthorizationPolicyBuilder().with_priority(1).with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", content_type) \ .with_transition(state1, state2, authorization_policies) \ .build() workflow_object = flow.objects[0] self.hook_pre_approve(flow.workflow, flow.transitions_approval_metas[0]) 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(state1))) 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-approved")) assert_that(output[0]["hook"], has_entry("when", BEFORE)) assert_that( output[0]["hook"], has_entry( "payload", all_of( has_entry(equal_to("workflow_object"), equal_to(workflow_object)), has_entry( equal_to("transition_approval"), equal_to(flow.transitions_approval_metas[0]. transition_approvals.all().first()))))) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(state2))) 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-approved")) assert_that(output[0]["hook"], has_entry("when", BEFORE)) assert_that( output[0]["hook"], has_entry( "payload", all_of( has_entry(equal_to("workflow_object"), equal_to(workflow_object)), has_entry( equal_to("transition_approval"), equal_to(flow.transitions_approval_metas[0]. transition_approvals.all().first())))))
def test_shouldAssessIterationsCorrectlyWhenCycled(self): authorized_permission = PermissionObjectFactory() authorized_user = UserObjectFactory( user_permissions=[authorized_permission]) cycle_state_1 = RawState("cycle_state_1") cycle_state_2 = RawState("cycle_state_2") cycle_state_3 = RawState("cycle_state_3") off_the_cycle_state = RawState("off_the_cycle_state") final_state = RawState("final_state") authorization_policies = [ AuthorizationPolicyBuilder().with_permission( authorized_permission).build(), ] flow = FlowBuilder("my_field", self.content_type) \ .with_transition(cycle_state_1, cycle_state_2, authorization_policies) \ .with_transition(cycle_state_2, cycle_state_3, authorization_policies) \ .with_transition(cycle_state_3, cycle_state_1, authorization_policies) \ .with_transition(cycle_state_3, off_the_cycle_state, authorization_policies) \ .with_transition(off_the_cycle_state, final_state, authorization_policies) \ .build() workflow_object = flow.objects[0] assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_1))) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_2))) workflow_object.river.my_field.approve(as_user=authorized_user) assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_3))) approvals = TransitionApproval.objects.filter( workflow=flow.workflow, workflow_object=workflow_object) assert_that(approvals, has_length(5)) workflow_object.river.my_field.approve( as_user=authorized_user, next_state=flow.get_state(cycle_state_1)) assert_that(workflow_object.my_field, equal_to(flow.get_state(cycle_state_1))) approvals = TransitionApproval.objects.filter( workflow=flow.workflow, workflow_object=workflow_object) assert_that(approvals, has_length(10)) assert_that( approvals, has_approval(cycle_state_1, cycle_state_2, APPROVED, iteration=0)) assert_that( approvals, has_approval(cycle_state_2, cycle_state_3, APPROVED, iteration=1)) assert_that( approvals, has_approval(cycle_state_3, cycle_state_1, APPROVED, iteration=2)) assert_that( approvals, has_approval(cycle_state_3, off_the_cycle_state, CANCELLED, iteration=2)) assert_that( approvals, has_approval(off_the_cycle_state, final_state, CANCELLED, iteration=3)) assert_that( approvals, has_approval(cycle_state_1, cycle_state_2, PENDING, iteration=3)) assert_that( approvals, has_approval(cycle_state_2, cycle_state_3, PENDING, iteration=4)) assert_that( approvals, has_approval(cycle_state_3, cycle_state_1, PENDING, iteration=5)) assert_that( approvals, has_approval(cycle_state_3, off_the_cycle_state, PENDING, iteration=5)) assert_that( approvals, has_approval(off_the_cycle_state, final_state, PENDING, iteration=6))