def test_apply_new_approve_definition(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) self.assertEqual(0, TransitionApprovement.objects.filter(object_id=5001, content_type=ct).count()) ObjectService.register_object(ct.pk, 5001, field.pk) self.assertEqual(1, TransitionApprovement.objects.filter(object_id=5001, content_type=ct).count()) transition = Transition.objects.create(content_type=ct, field=field, source_state=State.objects.get(pk=3002), destination_state=State.objects.get(pk=3003)) cursor = connection.cursor() cursor.execute(''' INSERT INTO %(transition_approve_definition_table)s('transition_id','permission_id','order') VALUES (%(transition_id)s,1001,0) ''' % {'transition_approve_definition_table': TransitionApproveDefinition._meta.db_table, 'transition_id': transition.pk}) transition_approve_definition = TransitionApproveDefinition.objects.get(~Q(pk=6001)) self.assertEqual(1, TransitionApprovement.objects.filter(object_id=5001, field=field, content_type=ct).count()) ApproveDefinitionService.apply_new_approve_definition(transition_approve_definition) self.assertEqual(2, TransitionApprovement.objects.filter(object_id=5001, field=field, content_type=ct).count()) transition_approve_definition.save() self.assertEqual(2, TransitionApprovement.objects.filter(object_id=5001, field=field, content_type=ct).count())
def test_get_approvements_object_waiting_for_approval_without_skip(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) middleware.get_user = MagicMock(return_value=User.objects.get(pk=2101)) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 1, [Object.objects.get(object_id=5001).state], include_user=True) self.assertEqual(1, approvements.count()) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 2, [Object.objects.get(object_id=5001).state], include_user=False) self.assertEqual(1, approvements.count()) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 2, [Object.objects.get(object_id=5001).state], include_user=True) self.assertEqual(0, approvements.count()) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 3, [Object.objects.get(object_id=5001).state], include_user=False) self.assertEqual(1, approvements.count()) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 3, [Object.objects.get(object_id=5001).state], include_user=True) self.assertEqual(0, approvements.count()) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 4, [Object.objects.get(object_id=5001).state], include_user=False) self.assertEqual(1, approvements.count()) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5001, field.pk, 4, [Object.objects.get(object_id=5001).state], include_user=True) self.assertEqual(0, approvements.count())
def test_get_object_count_waiting_for_approval(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) result = self.client.get(reverse('riverio.apps.web.views.restful.get_object_count_waiting_for_approval', args=[ct.pk, field.pk, 1]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) self.assertEqual(OK, result.status_code) content = json.loads(result.content) self.assertIn('count', content) self.assertEqual(2, content['count'])
def test_get_available_states(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) middleware.get_user = MagicMock(return_value=User.objects.get(pk=2101)) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 2, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 2, include_user=True) self.assertEqual(0, available_states.count()) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 2) self.assertEqual(0, available_states.count()) obj = TestModel.objects.get(pk=5001) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 3, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 3, include_user=True) self.assertEqual(0, available_states.count()) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 3) self.assertEqual(0, available_states.count()) obj = TestModel.objects.get(pk=5001) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 4, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 4, include_user=True) self.assertEqual(0, available_states.count()) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 4) self.assertEqual(0, available_states.count()) obj = TestModel.objects.get(pk=5001) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 1, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 1, include_user=True) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 1) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0])
def test_init(self): test_obj = TestModel.objects.get(pk=5001) ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) obj = Object.objects.get_object_or_None(object_id=test_obj.pk) self.assertIsNone(obj) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) obj = Object.objects.get_object_or_None(object_id=test_obj.pk) self.assertIsNotNone(obj) initial_state = StateService.get_init_state(ct.pk, field.pk) self.assertEqual(initial_state, obj.state) self.assertEqual(18, TransitionApprovement.objects.count())
def test_approve_transition(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) # #################### # STATE 1 - STATE 2 # Only User1(2001) can approve and after his approve state must be changed to STATE 2 # ################### # Approved by user has no required permission for this transition middleware.get_user = MagicMock(return_value=User.objects.get(pk=2101)) obj = Object.objects.get(object_id=5001) try: TransitionService.approve_transition(ct.pk, 5001, field.pk, 2) self.fail('Exception was expected') except Exception, e: self.assertEqual(e.message, 'There is no available state for destination for the user.')
def test_get_objects_waiting_for_approval_for_user(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) middleware.get_user = MagicMock(return_value=User.objects.get(pk=2101)) on_approval_objects = ObjectService.get_objects_waiting_for_approval(ct.pk, field.pk, 1) self.assertEqual(0, on_approval_objects.count()) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) on_approval_objects = ObjectService.get_objects_waiting_for_approval(ct.pk, field.pk, 1) self.assertEqual(2, on_approval_objects.count()) self.assertEqual(5001, on_approval_objects[0].object_id) on_approval_objects = ObjectService.get_objects_waiting_for_approval(ct.pk, field.pk, 2) self.assertEqual(0, on_approval_objects.count()) on_approval_objects = ObjectService.get_objects_waiting_for_approval(ct.pk, field.pk, 3) self.assertEqual(0, on_approval_objects.count()) on_approval_objects = ObjectService.get_objects_waiting_for_approval(ct.pk, field.pk, 4) self.assertEqual(0, on_approval_objects.count())
def test_get_approvements_object_waiting_for_approval_with_skip(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) middleware.get_user = MagicMock(return_value=User.objects.get(pk=2101)) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s2'), approvements[0].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s2'), approvements[0].approve_definition.transition.destination_state) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state=State.objects.get(label='s2') ).update(skip=True) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s3'), approvements[0].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s3'), approvements[0].approve_definition.transition.destination_state) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state=State.objects.get(label='s3') ).update(skip=True) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(2, approvements.count()) self.assertEqual(State.objects.get(label='s4'), approvements[0].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5'), approvements[1].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(2, approvements.count()) self.assertEqual(State.objects.get(label='s4'), approvements[0].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5'), approvements[1].approve_definition.transition.destination_state) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state=State.objects.get(label='s4') ).update(skip=True) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s5'), approvements[0].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s5'), approvements[0].approve_definition.transition.destination_state) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state=State.objects.get(label='s4') ).update(skip=False) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state=State.objects.get(label='s5') ).update(skip=True) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s4'), approvements[0].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(1, approvements.count()) self.assertEqual(State.objects.get(label='s4'), approvements[0].approve_definition.transition.destination_state) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state__in=State.objects.filter(label__in=['s4', 's5']) ).update(skip=True) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(4, approvements.count()) self.assertEqual(State.objects.get(label='s4.1'), approvements[0].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s4.2'), approvements[1].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5.1'), approvements[2].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5.2'), approvements[3].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(4, approvements.count()) self.assertEqual(State.objects.get(label='s4.1'), approvements[0].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s4.2'), approvements[1].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5.1'), approvements[2].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5.2'), approvements[3].approve_definition.transition.destination_state) TransitionApprovement.objects.filter( content_type=ct, field=field, object_id=5002, approve_definition__transition__destination_state__in=State.objects.filter(label__in=['s4.1', 's5.1']) ).update(skip=True) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=False) self.assertEqual(2, approvements.count()) self.assertEqual(State.objects.get(label='s4.2'), approvements[0].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5.2'), approvements[1].approve_definition.transition.destination_state) approvements = ApprovementService.get_approvements_object_waiting_for_approval(ct.pk, 5002, field.pk, 1, [Object.objects.get(object_id=5002).state], include_user=True) self.assertEqual(2, approvements.count()) self.assertEqual(State.objects.get(label='s4.2'), approvements[0].approve_definition.transition.destination_state) self.assertEqual(State.objects.get(label='s5.2'), approvements[1].approve_definition.transition.destination_state)
def test_approve_transition(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) # #################### # STATE 1 - STATE 2 # Only User1(2001) can approve and after his approve state must be changed to STATE 2 # ################### # Approved by user has no required permission for this transition obj = Object.objects.get(object_id=5001) result = self.client.post(reverse('riverio.apps.web.views.restful.approve_transition', args=[ct.pk, obj.object_id, field.pk, 2]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) self.assertEqual(HttpResponseBadRequest.status_code, result.status_code) content = json.loads(result.content) self.assertIn('error_code', content) self.assertIn('error_message', content) self.assertEqual(None, content['error_code']) self.assertEqual('There is no available state for destination for the user.', content['error_message']) # Approved by user has no required permission for this transition result = self.client.post(reverse('riverio.apps.web.views.restful.approve_transition', args=[ct.pk, obj.object_id, field.pk, 3]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) self.assertEqual(HttpResponseBadRequest.status_code, result.status_code) content = json.loads(result.content) self.assertIn('error_code', content) self.assertIn('error_message', content) self.assertEqual(None, content['error_code']) self.assertEqual('There is no available state for destination for the user.', content['error_message']) # Approved by user has no required permission for this transition result = self.client.post(reverse('riverio.apps.web.views.restful.approve_transition', args=[ct.pk, obj.object_id, field.pk, 4]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) self.assertEqual(HttpResponseBadRequest.status_code, result.status_code) content = json.loads(result.content) self.assertIn('error_code', content) self.assertIn('error_message', content) self.assertEqual(None, content['error_code']) self.assertEqual('There is no available state for destination for the user.', content['error_message']) # Approved by user has required permission for this transition self.assertEqual(State.objects.get(label='s1'), obj.state) result = self.client.post(reverse('riverio.apps.web.views.restful.approve_transition', args=[ct.pk, obj.object_id, field.pk, 1]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) self.assertEqual(OK, result.status_code) content = json.loads(result.content) self.assertNotIn('error_code', content) self.assertNotIn('error_message', content) obj = Object.objects.get(object_id=5001) self.assertEqual(State.objects.get(label='s2'), obj.state) approvements = TransitionApprovement.objects.filter( content_type=ct, object_id=obj.object_id, field=field, status=APPROVED, approve_definition__transition__source_state__label='s1', approve_definition__transition__destination_state__label='s2' ) self.assertEqual(1, approvements.count()) self.assertIsNotNone(approvements[0].transactioner) self.assertEqual(ExternalUser.objects.get(pk=2001), approvements[0].transactioner) self.assertIsNotNone(approvements[0].transaction_date) result = self.client.post(reverse('riverio.apps.web.views.restful.approve_transition', args=[ct.pk, obj.object_id, field.pk, 1]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) self.assertEqual(HttpResponseBadRequest.status_code, result.status_code) content = json.loads(result.content) self.assertIn('error_code', content) self.assertIn('error_message', content) self.assertEqual(None, content['error_code']) self.assertEqual('There is no available state for destination for the user.', content['error_message'])
def get_object_count_waiting_for_approval(request, content_type_id, field_id, user_id): return "count", ObjectService.get_object_count_waiting_for_approval(content_type_id, field_id, user_id)
def register_object(request, content_type_id, object_id, field_id): return ObjectService.register_object(content_type_id, object_id, field_id)