示例#1
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))
示例#2
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))))
示例#3
0
    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))
示例#4
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))
示例#5
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))
示例#6
0
    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"
            ))
示例#7
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))
示例#8
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()))))
示例#9
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")
        )
    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_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)))
示例#12
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))
示例#13
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")
        )
示例#14
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))
        ))
示例#15
0
    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())))
示例#16
0
def transition(context, source_state_label, destination_state_label, workflow_identifier):
    from river.models import State
    from django.contrib.contenttypes.models import ContentType
    from river.models.factories import TransitionMetaFactory
    from river.tests.models import BasicTestModel
    from river.models.factories import WorkflowFactory

    source_state, _ = State.objects.get_or_create(label=source_state_label)
    workflow = getattr(context, "workflows", {}).get(workflow_identifier, None)
    if not workflow:
        content_type = ContentType.objects.get_for_model(BasicTestModel)
        workflow = WorkflowFactory(initial_state=source_state, content_type=content_type, field_name="my_field")
        if "workflows" not in context:
            context.workflows = {}
        context.workflows[workflow_identifier] = workflow

    destination_state, _ = State.objects.get_or_create(label=destination_state_label)
    transition = TransitionMetaFactory.create(
        workflow=workflow,
        source_state=source_state,
        destination_state=destination_state,
    )
    identifier = source_state_label + destination_state_label
    transitions = getattr(context, "transitions", {})
    transitions[identifier] = transition
    context.transitions = transitions
示例#17
0
    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)))
示例#18
0
def workflow(context, identifier, initial_state_label):
    from django.contrib.contenttypes.models import ContentType
    from river.tests.models import BasicTestModel
    from river.models import State
    from river.models.factories import WorkflowFactory

    content_type = ContentType.objects.get_for_model(BasicTestModel)
    initial_state = State.objects.get(label=initial_state_label)
    workflow = WorkflowFactory(initial_state=initial_state, content_type=content_type, field_name="my_field")
    workflows = getattr(context, "workflows", {})
    workflows[identifier] = workflow
    context.workflows = workflows
示例#19
0
    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")))
示例#21
0
    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))
示例#22
0
    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))
示例#23
0
    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 = {}
示例#24
0
    def test_shouldNotAllowUnauthorizedUserToProceedToNextState(self):
        unauthorized_user = UserObjectFactory()

        state1 = StateObjectFactory(label="state1")
        state2 = StateObjectFactory(label="state2")
        authorized_permission = PermissionObjectFactory()

        workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field")
        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
            priority=0,
            permissions=[authorized_permission]
        )

        workflow_object = BasicTestModelObjectFactory()

        assert_that(
            calling(workflow_object.model.river.my_field.approve).with_args(as_user=unauthorized_user),
            raises(RiverException, "There is no available approval for the user")
        )
示例#25
0
    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))
示例#26
0
    def test_shouldNotAcceptANextStateWhichIsNotAmongPossibleNextStates(self):
        authorized_permission = PermissionObjectFactory()

        authorized_user = UserObjectFactory(user_permissions=[authorized_permission])

        state1 = StateObjectFactory(label="state1")
        state2 = StateObjectFactory(label="state2")
        state3 = StateObjectFactory(label="state3")
        invalid_state = StateObjectFactory(label="state4")

        workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field")
        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
            priority=0,
            permissions=[authorized_permission]
        )

        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state3,
            priority=0,
            permissions=[authorized_permission]
        )

        workflow_object = BasicTestModelObjectFactory()

        assert_that(
            calling(workflow_object.model.river.my_field.approve).with_args(as_user=authorized_user, next_state=invalid_state),
            raises(RiverException,
                   "Invalid state is given\(%s\). Valid states is\(are\) (%s|%s)" % (
                       invalid_state.label,
                       ",".join([state2.label, state3.label]),
                       ",".join([state3.label, state2.label]))
                   )
        )
示例#27
0
    def test_shouldInvokeCallbackThatIsRegisteredWithoutInstanceWhenAnApprovingHappens(
            self):
        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")

        transition_meta = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
        )

        meta1 = TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta,
            priority=0,
            permissions=[authorized_permission])

        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta,
            priority=1,
            permissions=[authorized_permission])

        workflow_object = BasicTestModelObjectFactory(workflow=workflow)

        self.hook_pre_approve(workflow, meta1)

        assert_that(self.get_output(), 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))

        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.model)),
                    has_entry(
                        equal_to("transition_approval"),
                        equal_to(
                            meta1.transition_approvals.filter(
                                priority=0).first())))))

        workflow_object.model.river.my_field.approve(as_user=authorized_user)
        assert_that(workflow_object.model.my_field, equal_to(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.model)),
                    has_entry(
                        equal_to("transition_approval"),
                        equal_to(
                            meta1.transition_approvals.filter(
                                priority=0).first())))))
示例#28
0
    def test_shouldInvokeCallbackForTheOnlyGivenApproval(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")

        transition_meta_1 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
        )

        transition_meta_2 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state2,
            destination_state=state3,
        )

        transition_meta_3 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state3,
            destination_state=state1,
        )

        meta1 = TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta_1,
            priority=0,
            permissions=[authorized_permission])

        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta_2,
            priority=0,
            permissions=[authorized_permission])

        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta_3,
            priority=0,
            permissions=[authorized_permission])

        workflow_object = BasicTestModelObjectFactory(workflow=workflow)
        workflow_object.model.river.my_field.approve(as_user=authorized_user)
        workflow_object.model.river.my_field.approve(as_user=authorized_user)
        workflow_object.model.river.my_field.approve(as_user=authorized_user)

        assert_that(TransitionApproval.objects.filter(meta=meta1),
                    has_length(2))
        first_approval = TransitionApproval.objects.filter(
            meta=meta1, transition__iteration=0).first()
        assert_that(first_approval, is_not(none()))

        self.hook_pre_approve(workflow,
                              meta1,
                              transition_approval=first_approval)

        output = self.get_output()
        assert_that(output, none())

        workflow_object.model.river.my_field.approve(as_user=authorized_user)

        output = self.get_output()
        assert_that(output, none())
    def test_shouldInvokeCallbackThatIsRegisteredWithoutInstanceWhenTransitionHappens(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")

        transition_meta_1 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
        )

        transition_meta_2 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state2,
            destination_state=state3,
        )

        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta_1,
            priority=0,
            permissions=[authorized_permission]
        )

        TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_meta_2,
            priority=0,
            permissions=[authorized_permission]
        )

        workflow_object = BasicTestModelObjectFactory()

        self.hook_post_transition(workflow, transition_meta_2)

        assert_that(self.get_output(), 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.get_output(), none())

        workflow_object.model.river.my_field.approve(as_user=authorized_user)
        assert_that(workflow_object.model.my_field, equal_to(state3))

        last_approval = TransitionApproval.objects.get(object_id=workflow_object.model.pk, transition__source_state=state2, transition__destination_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.model)),
                has_entry(equal_to("transition_approval"), equal_to(last_approval))

            )
        ))
示例#30
0
    def test__shouldCreateTransitionsAndTransitionMetasOutOfApprovalMetaAndApprovals(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_1 = TransitionMetaFactory.create(workflow=workflow, source_state=state1, destination_state=state2)
        transition_2 = TransitionMetaFactory.create(workflow=workflow, source_state=state2, destination_state=state3)
        transition_3 = TransitionMetaFactory.create(workflow=workflow, source_state=state2, destination_state=state4)

        meta_1 = TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_1,
            priority=0
        )
        meta_2 = TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_2,
            priority=0
        )

        meta_3 = TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_2,
            priority=1
        )

        meta_4 = TransitionApprovalMetaFactory.create(
            workflow=workflow,
            transition_meta=transition_3,
            priority=0
        )

        workflow_object = BasicTestModelObjectFactory()

        with connection.cursor() as cur:
            result = cur.execute("select id, transition_meta_id from river_transitionapprovalmeta;").fetchall()
            assert_that(result, has_length(4))
            assert_that(result, has_item(equal_to((meta_1.pk, transition_1.pk))))
            assert_that(result, has_item(equal_to((meta_2.pk, transition_2.pk))))
            assert_that(result, has_item(equal_to((meta_3.pk, transition_2.pk))))
            assert_that(result, has_item(equal_to((meta_4.pk, transition_3.pk))))

            result = cur.execute("select id, transition_id from river_transitionapproval where object_id='%s';" % workflow_object.model.pk).fetchall()
            assert_that(result, has_length(4))
            assert_that(result, has_item(equal_to((meta_1.transition_approvals.first().pk, transition_1.transitions.first().pk))))
            assert_that(result, has_item(equal_to((meta_2.transition_approvals.first().pk, transition_2.transitions.first().pk))))
            assert_that(result, has_item(equal_to((meta_3.transition_approvals.first().pk, transition_2.transitions.first().pk))))
            assert_that(result, has_item(equal_to((meta_4.transition_approvals.first().pk, transition_3.transitions.first().pk))))

        call_command('migrate', 'river', '0009', stdout=out)

        with connection.cursor() as cur:
            schema = cur.execute("PRAGMA table_info('river_transitionapprovalmeta');").fetchall()
            columns = [column[1] for column in schema]
            assert_that(columns, is_not(has_item("transition_meta_id")))
            assert_that(columns, has_item("source_state_id"))
            assert_that(columns, has_item("destination_state_id"))

            schema = cur.execute("PRAGMA table_info('river_transitionapproval');").fetchall()
            columns = [column[1] for column in schema]
            assert_that(columns, is_not(has_item("transition_id")))
            assert_that(columns, has_item("source_state_id"))
            assert_that(columns, has_item("destination_state_id"))

        call_command('migrate', 'river', '0010', stdout=out)

        with connection.cursor() as cur:
            schema = cur.execute("PRAGMA table_info('river_transitionapprovalmeta');").fetchall()
            columns = six.moves.map(lambda column: column[1], schema)
            assert_that(columns, has_item("transition_meta_id"))
            assert_that(columns, is_not(has_item("source_state_id")))
            assert_that(columns, is_not(has_item("destination_state_id")))

            schema = cur.execute("PRAGMA table_info('river_transitionapproval');").fetchall()
            columns = [column[1] for column in schema]
            assert_that(columns, has_item("transition_id"))
            assert_that(columns, is_not(has_item("source_state_id")))
            assert_that(columns, is_not(has_item("destination_state_id")))

        with connection.cursor() as cur:
            result = cur.execute("select id, transition_meta_id from river_transitionapprovalmeta;").fetchall()
            assert_that(result, has_length(4))
            assert_that(result, has_item(equal_to((meta_1.pk, transition_1.pk))))
            assert_that(result, has_item(equal_to((meta_2.pk, transition_2.pk))))
            assert_that(result, has_item(equal_to((meta_3.pk, transition_2.pk))))
            assert_that(result, has_item(equal_to((meta_4.pk, transition_3.pk))))

            result = cur.execute("select id, transition_id from river_transitionapproval where object_id='%s';" % workflow_object.model.pk).fetchall()
            assert_that(result, has_length(4))
            assert_that(result, has_item(equal_to((meta_1.transition_approvals.first().pk, transition_1.transitions.first().pk))))
            assert_that(result, has_item(equal_to((meta_2.transition_approvals.first().pk, transition_2.transitions.first().pk))))
            assert_that(result, has_item(equal_to((meta_3.transition_approvals.first().pk, transition_2.transitions.first().pk))))
            assert_that(result, has_item(equal_to((meta_4.transition_approvals.first().pk, transition_3.transitions.first().pk))))