def test_false_condition_node(config, mongo): ''' conditional node won't be executed if its condition is false ''' # test setup handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('condition.2018-05-17.xml', 'start_node') execution = ptr.proxy.execution.get() channel = MagicMock() mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': Xml.load(config, execution.process_name).get_state(), }) handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('mistery', [ { 'name': 'password', 'type': 'text', 'value': '123456', 'value_caption': '123456', }, ]) ], }, channel) # assertions assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'condition1' # rabbit called channel.basic_publish.assert_called_once() args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('condition1', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call handler.call(rabbit_call, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'condition2'
def test_ifelifelse_else(config, mongo): ''' else will be executed if preceding condition is false''' # test setup handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('else.2018-07-10.xml', 'start_node') channel = MagicMock() mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': ptr.proxy.execution.get().id, 'state': Xml.load(config, 'else').get_state(), }) handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('secret01', [ { 'name': 'password', 'type': 'text', 'value': 'cuca', 'value_caption': 'cuca', }, ]) ], }, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'condition01' # rabbit called channel.basic_publish.assert_called_once() args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('condition01', [ { 'name': 'condition', 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call channel = MagicMock() handler.call(rabbit_call, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'elif01' # rabbit called channel.basic_publish.assert_called_once() args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('elif01', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call channel = MagicMock() handler.call(rabbit_call, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'else01' # rabbit called channel.basic_publish.assert_called_once() args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('else01', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': True, 'value_caption': 'True', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call channel = MagicMock() handler.call(rabbit_call, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'action03' # rabbit called to notify the user channel.basic_publish.assert_called_once() args = channel.basic_publish.call_args[1] assert args['exchange'] == 'charpe_notify' channel = MagicMock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form01', [ { 'name': 'answer', 'value': 'answer', 'value_caption': 'answer', }, ]) ], }, channel) # execution finished assert len(Pointer.get_all()) == 0 assert len(Execution.get_all()) == 0
def test_invalidated_conditional(config, mongo): ''' a condiitonal depends on an invalidated field, if it changes during the second response it must take the second value ''' # test setup handler = Handler(config) user = make_user('juan', 'Juan') process_filename = 'condition_invalidated.2019-10-08.xml' ptr = make_pointer(process_filename, 'start_node') execution = ptr.proxy.execution.get() mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': Xml.load(config, execution.process_name).get_state(), 'values': { '_execution': [{ 'name': '', 'description': '', }], }, }) # initial rabbit call channel = MagicMock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form1', [ { 'name': 'value', 'type': 'int', 'value': 3, 'value_caption': '3', }, ]) ], }, channel) channel.basic_publish.assert_called_once() # arrives to if_node ptr = Pointer.get_all()[0] assert ptr.node_id == 'if_node' # if_node's condition is True args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('if_node', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': True, 'value_caption': 'True', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call # if rabbit call channel.reset_mock() handler.call(rabbit_call, channel) channel.basic_publish.assert_called_once() # arrives to if_validation ptr = Pointer.get_all()[0] assert ptr.node_id == 'if_validation_node' # if's call to validation channel.reset_mock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('if_validation_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'I do not like it', 'value_caption': 'I do not like it', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0:form1.value', }], 'value_caption': '', }, ]) ], }, channel) channel.basic_publish.assert_called_once() # returns to start_node ptr = Pointer.get_all()[0] assert ptr.node_id == 'start_node' # second lap channel.reset_mock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form1', [ { 'name': 'value', 'type': 'int', 'value': -3, 'value_caption': '-3', }, ]) ], }, channel) channel.basic_publish.assert_called_once() # arrives to if_node again ptr = Pointer.get_all()[0] assert ptr.node_id == 'if_node' args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('if_node', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call # if second rabbit call channel.reset_mock() handler.call(rabbit_call, channel) channel.basic_publish.assert_called_once() # arrives to elif_node ptr = Pointer.get_all()[0] assert ptr.node_id == 'elif_node' # elif node's condition is true args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('elif_node', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': True, 'value_caption': 'True', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call # elif rabbit call channel.reset_mock() handler.call(rabbit_call, channel) channel.basic_publish.assert_called_once() # arrives to elif_validation ptr = Pointer.get_all()[0] assert ptr.node_id == 'elif_validation_node' # elif's call to validation channel.reset_mock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('elif_validation_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'Ugly... nope', 'value_caption': 'Ugly... nope', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0:form1.value', }], 'value_caption': '', }, ]) ], }, channel) channel.basic_publish.assert_called_once() # returns to start_node ptr = Pointer.get_all()[0] assert ptr.node_id == 'start_node' # third lap channel.reset_mock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form1', [ { 'name': 'value', 'type': 'int', 'value': 0, 'value_caption': '0', }, ]) ], }, channel) channel.basic_publish.assert_called_once() # arrives to if_node again again ptr = Pointer.get_all()[0] assert ptr.node_id == 'if_node' args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('if_node', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call # if third rabbit call channel.reset_mock() handler.call(rabbit_call, channel) channel.basic_publish.assert_called_once() # arrives to elif_node again ptr = Pointer.get_all()[0] assert ptr.node_id == 'elif_node' args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('elif_node', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call # elif second rabbit call channel.reset_mock() handler.call(rabbit_call, channel) channel.basic_publish.assert_called_once() # arrives to else_node ptr = Pointer.get_all()[0] assert ptr.node_id == 'else_node' # else node's condition is true args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('else_node', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': True, 'value_caption': 'True', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call # else rabbit call channel.reset_mock() handler.call(rabbit_call, channel) channel.basic_publish.assert_called_once() # arrives to if_validation ptr = Pointer.get_all()[0] assert ptr.node_id == 'else_validation_node' # else's call to validation channel.reset_mock() handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('else_validation_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'What? No!', 'value_caption': 'What? No!', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0:form1.value', }], 'value_caption': '', }, ]) ], }, channel) channel.basic_publish.assert_called_once() # returns to start_node ptr = Pointer.get_all()[0] assert ptr.node_id == 'start_node' # state is coherent state = next(mongo[config["EXECUTION_COLLECTION"]].find({ 'id': execution.id, })) del state['_id'] assert state == { '_type': 'execution', 'id': execution.id, 'name': '', 'description': '', 'state': { '_type': ':sorted_map', 'items': { 'start_node': { '_type': 'node', 'type': 'action', 'id': 'start_node', 'state': 'ongoing', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json( 'form1', [ { 'name': 'value', 'type': 'int', 'value': 0, 'value_caption': '0', 'state': 'invalid', }, ], state='invalid', ) ], 'state': 'invalid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Node 1', 'description': 'the value subject to inspection', }, 'if_node': { '_type': 'node', 'type': 'if', 'id': 'if_node', 'state': 'invalid', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { '__system__': { '_type': 'actor', 'forms': [ Form.state_json( 'if_node', [ { 'name': 'condition', 'value': False, 'value_caption': 'False', 'type': 'bool', 'state': 'invalid', }, ], state='invalid', ) ], 'state': 'invalid', 'user': { '_type': 'user', 'identifier': '__system__', 'fullname': 'System', }, }, }, }, 'milestone': False, 'name': 'IF if_node', 'description': 'IF if_node', }, 'if_validation_node': { '_type': 'node', 'type': 'validation', 'id': 'if_validation_node', 'state': 'invalid', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json( 'if_validation_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', 'state': 'invalid', }, { 'name': 'comment', 'value': 'I do not like it', 'value_caption': ('I do not like it'), }, { 'name': 'inputs', 'value': [{ 'ref': ('start_node.juan.0:form1' '.value'), }], 'value_caption': '', }, ], state='invalid', ) ], 'state': 'invalid', 'user': { '_type': 'user', 'fullname': 'Juan', 'identifier': 'juan' }, } } }, 'name': 'The validation', 'description': 'This node invalidates the original value', 'milestone': False, }, 'elif_node': { '_type': 'node', 'type': 'elif', 'id': 'elif_node', 'state': 'invalid', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { '__system__': { '_type': 'actor', 'state': 'invalid', 'user': { '_type': 'user', 'fullname': 'System', 'identifier': '__system__' }, 'forms': [ Form.state_json( 'elif_node', [ { 'name': 'condition', 'value': False, 'value_caption': 'False', 'type': 'bool', 'state': 'invalid', }, ], state='invalid', ) ], } } }, 'name': 'ELIF elif_node', 'description': 'ELIF elif_node', 'milestone': False, }, 'elif_validation_node': { '_type': 'node', 'type': 'validation', 'id': 'elif_validation_node', 'state': 'invalid', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'state': 'invalid', 'user': { '_type': 'user', 'fullname': 'Juan', 'identifier': 'juan' }, 'forms': [ Form.state_json( 'elif_validation_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', 'state': 'invalid', }, { 'name': 'comment', 'value': 'Ugly... nope', 'value_caption': ('Ugly... nope'), }, { 'name': 'inputs', 'value': [{ 'ref': ('start_node.juan.0:form1' '.value'), }], 'value_caption': '', }, ], state='invalid', ) ], } } }, 'name': 'The validation', 'description': ('This node also invalidates the original value'), 'milestone': False, }, 'else_node': { '_type': 'node', 'type': 'else', 'id': 'else_node', 'state': 'invalid', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { '__system__': { '_type': 'actor', 'state': 'invalid', 'user': { '_type': 'user', 'fullname': 'System', 'identifier': '__system__' }, 'forms': [ Form.state_json( 'else_node', [ { 'name': 'condition', 'value': True, 'value_caption': 'True', 'type': 'bool', 'state': 'invalid', }, ], state='invalid', ) ], } } }, 'name': 'ELSE else_node', 'description': 'ELSE else_node', 'milestone': False, }, 'else_validation_node': { '_type': 'node', 'type': 'validation', 'id': 'else_validation_node', 'state': 'invalid', 'comment': 'What? No!', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'state': 'invalid', 'user': { '_type': 'user', 'fullname': 'Juan', 'identifier': 'juan' }, 'forms': [ Form.state_json('else_validation_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', 'state': 'invalid', }, { 'name': 'comment', 'value': 'What? No!', 'value_caption': 'What? No!', }, { 'name': 'inputs', 'value': [{ 'ref': ('start_node.juan.0:form1' '.value'), }], 'value_caption': '', }, ], state='invalid'), ], } } }, 'name': 'The validation', 'description': ('This node invalidates the original value, too'), 'milestone': False, } }, 'item_order': [ 'start_node', 'if_node', 'if_validation_node', 'elif_node', 'elif_validation_node', 'else_node', 'else_validation_node' ], }, 'values': { '_execution': [{ 'name': '', 'description': '', }], 'form1': [{ 'value': 0, }], 'if_node': [{ 'condition': False, }], 'if_validation_node': [ { 'response': 'reject', 'comment': 'I do not like it', 'inputs': [ { 'ref': 'start_node.juan.0:form1.value' }, ], }, ], 'elif_node': [ { 'condition': False, }, ], 'elif_validation_node': [ { 'response': 'reject', 'comment': 'Ugly... nope', 'inputs': [ { 'ref': 'start_node.juan.0:form1.value', }, ], }, ], 'else_node': [ { 'condition': True, }, ], 'else_validation_node': [ { 'response': 'reject', 'comment': 'What? No!', 'inputs': [ { 'ref': 'start_node.juan.0:form1.value', }, ], }, ], }, 'actors': { 'start_node': 'juan', 'if_node': '__system__', 'if_validation_node': 'juan', 'elif_node': '__system__', 'elif_validation_node': 'juan', 'else_node': '__system__', 'else_validation_node': 'juan', }, 'actor_list': [{ 'node': 'start_node', 'identifier': 'juan', }, { 'node': 'if_node', 'identifier': '__system__', }, { 'node': 'if_validation_node', 'identifier': 'juan', }, { 'node': 'elif_node', 'identifier': '__system__', }, { 'node': 'elif_validation_node', 'identifier': 'juan', }, { 'node': 'else_node', 'identifier': '__system__', }, { 'node': 'else_validation_node', 'identifier': 'juan', }], }
def test_reject_with_dependencies(config, mongo): handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('validation-reloaded.2018-05-17.xml', 'node1') channel = MagicMock() execution = ptr.proxy.execution.get() mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': Xml.load(config, 'validation-reloaded').get_state(), 'values': { '_execution': [{ 'name': '', 'description': '', }], }, }) # first call to node1 handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form1', [ { 'name': 'task', 'value': '1', 'value_caption': '1', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node2' # first call to node2 handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form2', [ { 'name': 'task', 'value': '1', 'value_caption': '1', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node3' # first call to node3 handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form3', [ { 'name': 'task', 'value': '1', 'value_caption': '1', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node4' # first call to validation handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('node4', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'I do not like it', 'value_caption': 'I do not like it', }, { 'name': 'inputs', 'value': [{ 'ref': 'node1.juan.0:form1.task', }], 'value_caption': '', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node1' # second call to node1 handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form1', [ { 'name': 'task', 'value': '2', 'value_caption': '2', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node2' # second call to node2 handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form2', [ { 'name': 'task', 'value': '2', 'value_caption': '2', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node4' # second call to validation handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('node4', [ { 'name': 'response', 'value': 'accept', 'value_caption': 'accept', }, { 'name': 'comment', 'value': 'I like it', 'value_caption': 'I like it', }, { 'name': 'inputs', 'value': None, 'value_caption': 'None', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'node5' # first call to last node handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form5', [ { 'name': 'task', 'value': '1', 'value_caption': '1', }, ]) ], }, channel) assert Pointer.get_all() == [] # state is coherent state = next(mongo[config["EXECUTION_COLLECTION"]].find({ 'id': execution.id, })) del state['_id'] del state['finished_at'] assert state == { '_type': 'execution', 'id': execution.id, 'name': '', 'description': '', 'state': { '_type': ':sorted_map', 'items': { 'node1': { '_type': 'node', 'type': 'action', 'id': 'node1', 'state': 'valid', 'comment': 'I do not like it', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('form1', [ { 'name': 'task', 'value': '2', 'value_caption': '2', }, ]) ], 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Primer paso', 'description': 'información original', }, 'node2': { '_type': 'node', 'type': 'action', 'id': 'node2', 'state': 'valid', 'comment': 'I do not like it', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('form2', [ { 'name': 'task', 'value': '2', 'value_caption': '2', }, ]) ], 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Segundo paso', 'description': 'depender de la info', }, 'node3': { '_type': 'node', 'type': 'action', 'id': 'node3', 'state': 'valid', 'comment': '', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('form3', [ { 'name': 'task', 'value': '1', 'value_caption': '1', }, ]) ], 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Tercer paso', 'description': 'no depender de nada', }, 'node4': { '_type': 'node', 'type': 'validation', 'id': 'node4', 'state': 'valid', 'comment': 'I do not like it', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('node4', [ { 'name': 'response', 'value': 'accept', 'value_caption': 'accept', }, { 'name': 'comment', 'value': 'I like it', 'value_caption': 'I like it', }, { 'name': 'inputs', 'value': None, 'value_caption': 'None', }, ]) ], 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Cuarto paso', 'description': 'validar', }, 'node5': { '_type': 'node', 'type': 'action', 'id': 'node5', 'state': 'valid', 'comment': '', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('form5', [ { 'name': 'task', 'value': '1', 'value_caption': '1', }, ]) ], 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Quinto paso', 'description': 'terminar', }, }, 'item_order': ['node1', 'node2', 'node3', 'node4', 'node5'], }, 'status': 'finished', 'values': { '_execution': [{ 'name': '', 'description': '', }], 'node4': [{ 'comment': 'I like it', 'inputs': None, 'response': 'accept', }], 'form1': [{ 'task': '2' }], 'form2': [{ 'task': '2' }], 'form3': [{ 'task': '1' }], 'form5': [{ 'task': '1' }], }, 'actors': { 'node1': 'juan', 'node2': 'juan', 'node3': 'juan', 'node4': 'juan', 'node5': 'juan', }, 'actor_list': [ { 'node': 'node1', 'identifier': 'juan', }, { 'node': 'node2', 'identifier': 'juan', }, { 'node': 'node3', 'identifier': 'juan', }, { 'node': 'node4', 'identifier': 'juan', }, { 'node': 'node5', 'identifier': 'juan', }, ], }
def test_anidated_conditions(config, mongo): ''' conditional node won't be executed if its condition is false ''' # test setup handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('anidated-conditions.2018-05-17.xml', 'a') channel = MagicMock() mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': ptr.proxy.execution.get().id, 'state': Xml.load(config, 'anidated-conditions').get_state(), }) handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('a', [ { 'name': 'a', 'value': '1', 'value_caption': '1', }, ]) ], }, channel) # assertions assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'outer' # rabbit called args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('outer', [ { 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': True, 'value_caption': 'True', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call handler.call(rabbit_call, channel) # assertions assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'b' handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('b', [ { 'name': 'b', 'value': '-1', 'value_caption': '-1', }, ]) ], }, channel) # assertions assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'inner1' # rabbit called args = channel.basic_publish.call_args[1] rabbit_call = { 'command': 'step', 'pointer_id': ptr.id, 'input': [ Form.state_json('inner1', [ { 'name': 'condition', 'name': 'condition', 'state': 'valid', 'type': 'bool', 'value': False, 'value_caption': 'False', }, ]) ], 'user_identifier': '__system__', } assert json.loads(args['body']) == rabbit_call handler.call(rabbit_call, channel) # assertions assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'f' handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('f', [ { 'name': 'f', 'value': '-1', 'value_caption': '-1', }, ]) ], }, channel) ptr = Pointer.get_all()[0] assert ptr.node_id == 'g'
def test_approve(config, mongo): ''' tests that a validation node can go forward on approval ''' # test setup handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('validation.2018-05-09.xml', 'approval_node') channel = MagicMock() mongo[config["POINTER_COLLECTION"]].insert_one({ 'id': ptr.id, 'started_at': datetime(2018, 4, 1, 21, 45), 'finished_at': None, 'execution': { 'id': ptr.proxy.execution.get().id, }, 'node': { 'id': 'approval_node', }, 'actors': { '_type': ':map', 'items': {}, }, }) mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': ptr.proxy.execution.get().id, 'state': Xml.load(config, 'validation.2018-05-09').get_state(), 'actors': { 'start_node': 'juan', }, 'values': { '_execution': [{ 'name': '', 'description': '', }], }, }) # thing to test handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('approval_node', [ { 'name': 'response', 'value': 'accept', 'value_caption': 'accept', }, { 'name': 'comment', 'value': 'I like it', 'value_caption': 'I like it', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0.task', }], 'value_caption': '', }, ]) ], }, channel) # assertions assert Pointer.get(ptr.id) is None new_ptr = Pointer.get_all()[0] assert new_ptr.node_id == 'final_node' reg = next(mongo[config["POINTER_COLLECTION"]].find()) assert reg['started_at'] == datetime(2018, 4, 1, 21, 45) assert_near_date(reg['finished_at']) assert reg['execution']['id'] == ptr.execution assert reg['node']['id'] == 'approval_node' assert reg['actors'] == { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, 'forms': [ Form.state_json('approval_node', [ { 'name': 'response', 'name': 'response', 'value': 'accept', 'value_caption': 'accept', }, { 'name': 'comment', 'name': 'comment', 'value': 'I like it', 'value_caption': 'I like it', }, { 'name': 'inputs', 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0.task', }], 'value_caption': '', }, ]) ], }, }, }
def test_reject(config, mongo): ''' tests that a rejection moves the pointer to a backward position ''' # test setup handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('validation.2018-05-09.xml', 'approval_node') channel = MagicMock() execution = ptr.proxy.execution.get() mongo[config["POINTER_COLLECTION"]].insert_one({ 'id': ptr.id, 'started_at': datetime(2018, 4, 1, 21, 45), 'finished_at': None, 'execution': { 'id': execution.id, }, 'node': { 'id': 'approval_node', }, 'actors': { '_type': ':map', 'items': {}, }, }) state = Xml.load(config, 'validation.2018-05-09').get_state() state['items']['start_node']['state'] = 'valid' state['items']['start_node']['actors']['items']['juan'] = { '_type': 'actor', 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, 'forms': [ Form.state_json('work', [ { 'name': 'task', '_type': 'field', 'state': 'valid', 'value': '2', }, ]) ], } mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': state, 'values': { '_execution': [{ 'name': '', 'description': '', }], }, }) # will teardown the approval node handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('approval_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'I do not like it', 'value_caption': 'I do not like it', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0:work.task', }], 'value_caption': '', }, ]) ], }, channel) # assertions assert Pointer.get(ptr.id) is None new_ptr = Pointer.get_all()[0] assert new_ptr.node_id == 'start_node' assert new_ptr in user.proxy.tasks # data is invalidated state = next(mongo[config["EXECUTION_COLLECTION"]].find({ 'id': execution.id, })) del state['_id'] assert state == { '_type': 'execution', 'id': execution.id, 'name': '', 'description': '', 'state': { '_type': ':sorted_map', 'items': { 'start_node': { '_type': 'node', 'type': 'action', 'id': 'start_node', 'state': 'ongoing', 'comment': 'I do not like it', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('work', [ { 'name': 'task', '_type': 'field', 'state': 'invalid', 'value': '2', }, ], state='invalid') ], 'state': 'invalid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Primer paso', 'description': 'Resolver una tarea', }, 'approval_node': { '_type': 'node', 'type': 'validation', 'id': 'approval_node', 'state': 'invalid', 'comment': 'I do not like it', 'actors': { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('approval_node', [ { 'name': 'response', 'state': 'invalid', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'I do not like it', 'value_caption': 'I do not like it', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.' 'juan.0:work.task', }], 'value_caption': '', }, ], state='invalid') ], 'state': 'invalid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }, 'milestone': False, 'name': 'Aprobación gerente reserva', 'description': 'aprobar reserva', }, 'final_node': { '_type': 'node', 'type': 'action', 'id': 'final_node', 'state': 'unfilled', 'comment': '', 'actors': { '_type': ':map', 'items': {}, }, 'milestone': False, 'name': '', 'description': '', }, }, 'item_order': ['start_node', 'approval_node', 'final_node'], }, 'values': { '_execution': [{ 'name': '', 'description': '', }], 'approval_node': [{ 'comment': 'I do not like it', 'response': 'reject', 'inputs': [{ 'ref': 'start_node.juan.0:work.task' }], }], }, 'actors': { 'approval_node': 'juan', }, 'actor_list': [{ 'node': 'approval_node', 'identifier': 'juan', }], } # mongo has the data reg = next(mongo[config["POINTER_COLLECTION"]].find()) assert reg['started_at'] == datetime(2018, 4, 1, 21, 45) assert (reg['finished_at'] - datetime.now()).total_seconds() < 2 assert reg['execution']['id'] == ptr.execution assert reg['node']['id'] == 'approval_node' assert reg['actors'] == { '_type': ':map', 'items': { 'juan': { '_type': 'actor', 'forms': [ Form.state_json('approval_node', [ { 'name': 'response', 'value': 'reject', 'value_caption': 'reject', }, { 'name': 'comment', 'value': 'I do not like it', 'value_caption': 'I do not like it', }, { 'name': 'inputs', 'value': [{ 'ref': 'start_node.juan.0:work.task', }], 'value_caption': '', }, ]) ], 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'juan', 'fullname': 'Juan', }, }, }, }
def test_wakeup(config, mongo): ''' the first stage in a node's lifecycle ''' # setup stuff handler = Handler(config) pointer = make_pointer('simple.2018-02-19.xml', 'start_node') execution = pointer.proxy.execution.get() juan = User(identifier='juan').save() manager = User( identifier='juan_manager', email='*****@*****.**' ).save() mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': Xml.load(config, execution.process_name).get_state(), 'actors': {'start_node': 'juan'}, }) channel = MagicMock() # will wakeup the second node handler.call({ 'command': 'step', 'pointer_id': pointer.id, 'user_identifier': juan.identifier, 'input': [], }, channel) # test manager is notified channel.basic_publish.assert_called_once() channel.exchange_declare.assert_called_once() args = channel.basic_publish.call_args[1] assert args['exchange'] == config['RABBIT_NOTIFY_EXCHANGE'] assert args['routing_key'] == 'email' assert json.loads(args['body']) == { 'recipient': '*****@*****.**', 'subject': '[procesos] Tarea asignada', 'template': 'assigned-task.html', 'data': { 'pointer': Pointer.get_all()[0].to_json( include=['*', 'execution'] ), 'cacahuate_url': config['GUI_URL'], }, } # pointer collection updated reg = next(mongo[config["POINTER_COLLECTION"]].find()) assert_near_date(reg['started_at']) assert reg['finished_at'] is None assert reg['execution']['id'] == execution.id assert reg['node'] == { 'id': 'mid_node', 'type': 'action', 'description': 'añadir información', 'name': 'Segundo paso', } assert reg['actors'] == { '_type': ':map', 'items': {}, } assert reg['notified_users'] == [manager.to_json()] assert reg['state'] == 'ongoing' # execution collection updated reg = next(mongo[config["EXECUTION_COLLECTION"]].find()) assert reg['state']['items']['mid_node']['state'] == 'ongoing' # tasks where asigned assert manager.proxy.tasks.count() == 1 task = manager.proxy.tasks.get()[0] assert isinstance(task, Pointer) assert task.node_id == 'mid_node' assert task.proxy.execution.get().id == execution.id
def test_teardown(config, mongo): ''' second and last stage of a node's lifecycle ''' # test setup handler = Handler(config) p_0 = make_pointer('simple.2018-02-19.xml', 'mid_node') execution = p_0.proxy.execution.get() User(identifier='juan').save() manager = User(identifier='manager').save() manager2 = User(identifier='manager2').save() assert manager not in execution.proxy.actors.get() assert execution not in manager.proxy.activities.get() manager.proxy.tasks.set([p_0]) manager2.proxy.tasks.set([p_0]) state = Xml.load(config, execution.process_name).get_state() state['items']['start_node']['state'] = 'valid' mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': state, 'values': { '_execution': [{ 'name': '', 'description': '', }], }, 'actors': { 'start_node': 'juan', }, }) mongo[config["POINTER_COLLECTION"]].insert_one({ 'id': p_0.id, 'started_at': datetime(2018, 4, 1, 21, 45), 'finished_at': None, 'execution': { 'id': execution.id, }, 'node': { 'id': p_0.node_id, }, 'actors': { '_type': ':map', 'items': {}, }, }) channel = MagicMock() # will teardown mid_node handler.call({ 'command': 'step', 'pointer_id': p_0.id, 'user_identifier': manager.identifier, 'input': [Form.state_json('mid_form', [ { '_type': 'field', 'state': 'valid', 'value': 'yes', 'value_caption': 'yes', 'name': 'data', }, ])], }, channel) # assertions assert Pointer.get(p_0.id) is None assert Pointer.count() == 1 assert Pointer.get_all()[0].node_id == 'final_node' # mongo has a registry reg = next(mongo[config["POINTER_COLLECTION"]].find()) assert reg['started_at'] == datetime(2018, 4, 1, 21, 45) assert_near_date(reg['finished_at']) assert reg['execution']['id'] == execution.id assert reg['node']['id'] == p_0.node_id assert reg['actors'] == { '_type': ':map', 'items': { 'manager': { '_type': 'actor', 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'manager', 'fullname': None, }, 'forms': [Form.state_json('mid_form', [ { '_type': 'field', 'state': 'valid', 'value': 'yes', 'value_caption': 'yes', 'name': 'data', }, ])], }, }, } # tasks where deleted from user assert manager.proxy.tasks.count() == 0 assert manager2.proxy.tasks.count() == 0 # state reg = next(mongo[config["EXECUTION_COLLECTION"]].find()) assert reg['state'] == { '_type': ':sorted_map', 'items': { 'start_node': { '_type': 'node', 'type': 'action', 'id': 'start_node', 'state': 'valid', 'comment': '', 'actors': { '_type': ':map', 'items': {}, }, 'milestone': False, 'name': 'Primer paso', 'description': 'Resolver una tarea', }, 'mid_node': { '_type': 'node', 'type': 'action', 'id': 'mid_node', 'state': 'valid', 'comment': '', 'actors': { '_type': ':map', 'items': { 'manager': { '_type': 'actor', 'state': 'valid', 'user': { '_type': 'user', 'identifier': 'manager', 'fullname': None, }, 'forms': [Form.state_json('mid_form', [ { '_type': 'field', 'state': 'valid', 'value': 'yes', 'value_caption': 'yes', 'name': 'data', }, ])], }, }, }, 'milestone': False, 'name': 'Segundo paso', 'description': 'añadir información', }, 'final_node': { '_type': 'node', 'type': 'action', 'id': 'final_node', 'state': 'ongoing', 'comment': '', 'actors': { '_type': ':map', 'items': {}, }, 'milestone': False, 'name': '', 'description': '', }, }, 'item_order': [ 'start_node', 'mid_node', 'final_node', ], } assert reg['values'] == { '_execution': [{ 'name': '', 'description': '', }], 'mid_form': [{ 'data': 'yes', }], } assert reg['actors'] == { 'start_node': 'juan', 'mid_node': 'manager', } assert manager in execution.proxy.actors assert execution in manager.proxy.activities
def test_send_request_multiple(config, mongo, mocker): ''' Tests that values are stored in such a way that they can be iterated in a jinja template. Specifically in this test they'll be used as part of a request node, thus also testing that all of the values can be used ''' # test setup class ResponseMock: status_code = 200 text = 'request response' mock = MagicMock(return_value=ResponseMock()) mocker.patch('requests.request', new=mock) handler = Handler(config) user = make_user('juan', 'Juan') ptr = make_pointer('request-multiple.2019-11-14.xml', 'start_node') execution = ptr.proxy.execution.get() channel = MagicMock() names = [random_string() for _ in '123'] mongo[config["EXECUTION_COLLECTION"]].insert_one({ '_type': 'execution', 'id': execution.id, 'state': Xml.load(config, execution.process_name).get_state(), }) handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form1', [ { 'name': 'name', 'type': 'text', 'value': names[0], 'value_caption': names[0], }, ]), Form.state_json('form1', [ { 'name': 'name', 'type': 'text', 'value': names[1], 'value_caption': names[1], }, ]), Form.state_json('form1', [ { 'name': 'name', 'type': 'text', 'value': names[2], 'value_caption': names[2], }, ]), ], }, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'request_node' # request is made with correct data requests.request.assert_called_once() args, kwargs = requests.request.call_args assert args[0] == 'POST' assert args[1] == 'http://localhost/' assert kwargs['data'] == '{{"names": ["{}","{}","{}"]}}'.format(*names) assert kwargs['headers'] == { 'content-type': 'application/json', }
def test_variable_proc_name_mix(config, mongo): ''' Test where the name is related to multiple forms in diferent nodes of the execution''' handler = Handler(config) user = make_user('juan', 'Juan') channel = MagicMock() xml = Xml.load(config, 'variable_name_mix.2020-01-28.xml') xmliter = iter(xml) node = make_node(next(xmliter), xmliter) input = [ Form.state_json('form01', [ { 'name': 'data01', 'type': 'text', 'value': '1', 'value_caption': '1', }, ]) ] execution = xml.start(node, input, mongo, channel, user.identifier) ptr = execution.proxy.pointers.get()[0] handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': input, }, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'node02' execution.reload() assert execution.name == 'Variable name process in step 10' assert execution.description == 'Description is also variable: 1, , ' handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form02', [ { 'name': 'data02', 'type': 'text', 'value': '2', 'value_caption': '2', }, ]) ], }, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'node03' execution.reload() assert execution.name == 'Variable name process in step 210' assert execution.description == 'Description is also variable: 1, 2, ' handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form03', [ { 'name': 'data03', 'type': 'text', 'value': '3', 'value_caption': '3', }, ]) ], }, channel)
def test_variable_proc_name_pointers(config, mongo): ''' Test pointer name's update''' handler = Handler(config) user = make_user('juan', 'Juan') channel = MagicMock() xml = Xml.load(config, 'variable_name_mix.2020-01-28.xml') xmliter = iter(xml) node = make_node(next(xmliter), xmliter) input = [ Form.state_json('form01', [ { 'name': 'data01', 'type': 'text', 'value': '1', 'value_caption': '1', }, ]) ] execution = xml.start(node, input, mongo, channel, user.identifier) ptr = execution.proxy.pointers.get()[0] handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': input, }, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'node02' execution.reload() assert execution.name == 'Variable name process in step 10' assert execution.description == 'Description is also variable: 1, , ' handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form02', [ { 'name': 'data02', 'type': 'text', 'value': '2', 'value_caption': '2', }, ]) ], }, channel) # pointer moved assert Pointer.get(ptr.id) is None ptr = Pointer.get_all()[0] assert ptr.node_id == 'node03' execution.reload() assert execution.name == 'Variable name process in step 210' assert execution.description == 'Description is also variable: 1, 2, ' handler.call( { 'command': 'step', 'pointer_id': ptr.id, 'user_identifier': user.identifier, 'input': [ Form.state_json('form03', [ { 'name': 'data03', 'type': 'text', 'value': '3', 'value_caption': '3', }, ]) ], }, channel) # now check pointers last state cursor = mongo[config["POINTER_COLLECTION"]].find({ 'execution.id': execution.id, }) assert cursor.count() == 3 expected_name = 'Variable name process in step 3210' expected_desc = 'Description is also variable: 1, 2, 3' for item in cursor: assert item['execution']['name'] == expected_name assert item['execution']['description'] == expected_desc