Exemple #1
0
    def test_shouldTransitToNextStateWhenAllTheApprovalsAreApproved(self):
        manager_permission = PermissionObjectFactory()
        team_leader_permission = PermissionObjectFactory()

        manager = UserObjectFactory(user_permissions=[manager_permission])
        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)))

        assert_that(workflow_object.my_field, equal_to(flow.get_state(state1)))
        workflow_object.river.my_field.approve(manager)
        assert_that(workflow_object.my_field, equal_to(flow.get_state(state2)))
    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])
Exemple #3
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)
Exemple #4
0
    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()))))
Exemple #5
0
    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")
        )
Exemple #6
0
    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))
Exemple #7
0
    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")
        )
Exemple #8
0
    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))
        ))
Exemple #9
0
    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_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_get_proceedings_object_waiting_for_approval_slow_test_from_fixture(self):

        call_command('loaddata', 'river/fixtures/slow-case.json', verbosity=0)

        user1 = UserObjectFactory(groups=[Group.objects.get(pk=3)])

        objects1 = []
        for i in range(66703, 66723):
            objects1.append(TestModelSlowCase1.objects.create(pk=i))

        objects2 = []
        for i in range(8, 13):
            objects2.append(TestModelSlowCase2.objects.create(pk=i))

        for o in objects1:
            before = datetime.now()
            ProceedingService.get_available_proceedings(o, [o.status], user1)
            after = datetime.now()
            self.assertLess(after - before, timedelta(seconds=0.5))

        for o in objects2:
            before = datetime.now()
            ProceedingService.get_available_proceedings(o, [o.status], user1)
            after = datetime.now()
            self.assertLess(after - before, timedelta(seconds=0.5))
Exemple #12
0
    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)))
Exemple #13
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))
Exemple #14
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")

        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()))))
Exemple #15
0
    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"
            ))
Exemple #16
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))
Exemple #17
0
    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"
            ))
Exemple #18
0
    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))
Exemple #19
0
    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]))))
Exemple #20
0
    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"))
Exemple #21
0
    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))
Exemple #22
0
    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))))
Exemple #23
0
    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 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 test_shouldInvokeCallbackThatIsRegisteredWithInstanceWhenTransitionHappens(
            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],
                                  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)))

        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_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 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())
Exemple #28
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)))
Exemple #29
0
    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))))
Exemple #30
0
    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))