def test_get_next_step_error_with_next_steps(self): flag1 = [ Flag(action='mod1_flag2', args={'arg1': '3'}), Flag(action='mod1_flag2', args={'arg1': '-1'}) ] flag2 = [ Flag(action='mod1_flag2', args={'arg1': '-1'}), Flag(action='mod1_flag2', args={'arg1': '3'}) ] next_steps = [ NextStep(flags=flag1, name='name1'), NextStep(name='name2') ] error_steps = [ NextStep(flags=flag2, name='error1'), NextStep(name='error2') ] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps, errors=error_steps) step.output = 2 self.assertEqual(step.get_next_step({}, error=True), 'error2') step.output = 1 self.assertEqual(step.get_next_step({}, error=True), 'error1')
def test_from_json(self): next_step_names = ['next1', 'next2'] error_names = ['error1', 'error2'] inputs = [{'name': 'name1', 'action': 'action1', 'app': 'app1', 'device': 'dev1', 'next': [], 'error': []}, {'name': 'name2', 'action': 'action2', 'app': 'app2', 'device': 'opt2', 'next': next_step_names, 'error': []}, {'name': 'name3', 'action': 'action3', 'app': 'app3', 'device': 'opt3', 'next': [], 'error': error_names}, {'name': 'name4', 'action': 'action4', 'app': 'app4', 'device': 'dev4', 'next': next_step_names, 'error': error_names}] for input_params in inputs: step = Step(name=input_params['name'], action=input_params['action'], app=input_params['app'], device=input_params['device']) step.conditionals = [NextStep(name=name, parent_name=step.name, ancestry=list(step.ancestry)) for name in input_params['next']] step.errors = [NextStep(name=name, parent_name=step.name, ancestry=list(step.ancestry)) for name in input_params['error']] step_json = step.as_json() derived_step = Step.from_json(step_json, parent_name=step.parent_name, ancestry=list(step.ancestry)[:-1]) self.assertDictEqual(derived_step.as_json(), step_json) self.assertEqual(step.parent_name, derived_step.parent_name) self.assertListEqual(step.ancestry, derived_step.ancestry) # check the ancestry of the next_steps original_next_step_ancestries = [list(next_step.ancestry) for next_step in step.conditionals] derived_next_step_ancestries = [list(next_step.ancestry) for next_step in derived_step.conditionals] self.assertEqual(len(original_next_step_ancestries), len(derived_next_step_ancestries)) for original_next_step_ancestry, derived_next_step_ancestry in zip(original_next_step_ancestries, derived_next_step_ancestries): self.assertListEqual(derived_next_step_ancestry, original_next_step_ancestry) # check the ancestry of the next_steps original_error_ancestries = [list(error.ancestry) for error in step.errors] derived_error_ancestries = [list(error.ancestry) for error in derived_step.errors] self.assertEqual(len(original_error_ancestries), len(derived_error_ancestries)) for original_error_ancestry, derived_error_ancestry in zip(original_error_ancestries, derived_error_ancestries): self.assertListEqual(derived_error_ancestry, original_error_ancestry)
def test_to_xml_error(self): flags = [Flag(action='Top Flag'), Flag(action='mod1_flag1')] next_step = NextStep(name='name', flags=flags) xml = next_step.to_xml(tag='error') self.assertEqual(xml.tag, 'error') self.assertEqual(xml.get('step'), 'name') self.assertEqual(len(xml.findall('flag')), 2)
def test_to_from_json(self): filter_params = ['test_filter_action', ''] flags_params = [('', []), ('test_action', []), ('test_action', filter_params)] input_params = [('', '', None, []), ('test_name', '', None, []), ('test_name', 'test_parent', None, []), ('test_name', 'test_parent', ['a', 'b'], []), ('test_name', 'test_parent', ['a', 'b'], flags_params)] for (name, parent_name, ancestry, flag_params) in input_params: next_step = NextStep(name=name, parent_name=parent_name, ancestry=ancestry) if flag_params: flags = [] for flag_action, flag_filter_params in flag_params: flag = Flag(action=flag_action, parent_name=next_step.name, ancestry=next_step.ancestry) if filter_params: flag.filters = [ Filter(action=flag_action, parent_name=flag.name, ancestry=flag.ancestry) for flag_action in flag_filter_params ] flags.append(flag) next_step.flags = flags next_step_json = next_step.as_json() derived_next_step = NextStep.from_json(next_step_json, parent_name=parent_name, ancestry=ancestry) self.assertDictEqual(derived_next_step.as_json(), next_step_json) self.assertEqual(next_step.parent_name, derived_next_step.parent_name) self.assertListEqual(next_step.ancestry, derived_next_step.ancestry) derived_json_without_children = next_step_json derived_json_without_children['flags'] = [ flag['action'] for flag in derived_json_without_children['flags'] ] self.assertDictEqual( derived_next_step.as_json(with_children=False), derived_json_without_children) # check the ancestry of the flags original_flag_ancestries = [ list(flag.ancestry) for flag in next_step.flags ] derived_flag_ancestries = [ list(flag.ancestry) for flag in derived_next_step.flags ] self.assertEqual(len(original_flag_ancestries), len(derived_flag_ancestries)) for original_flag_ancestry, derived_flag_ancestry in zip( original_flag_ancestries, derived_flag_ancestries): self.assertListEqual(derived_flag_ancestry, original_flag_ancestry)
def test_to_from_xml_with_next_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] self.__assert_xml_is_convertible( Step(app='HelloWorld', action='helloWorld', next_steps=next_steps))
def test_get_children_next_not_found(self): next_steps = [NextStep(name='name1'), NextStep(name='name2')] error_steps = [NextStep(name='error1'), NextStep(name='error2')] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps, errors=error_steps) self.assertIsNone(step.get_children(['invalid']))
def test_get_children_in_next_step(self): next_steps = [NextStep(name='name1'), NextStep(name='name2')] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps) self.assertDictEqual(step.get_children(['name1']), next_steps[0].as_json(with_children=False)) self.assertDictEqual(step.get_children(['name2']), next_steps[1].as_json(with_children=False))
def test_to_xml_with_status(self): next_step = NextStep(name='name', status='test_status') xml = next_step.to_xml() self.assertEqual(xml.tag, 'next') self.assertEqual(xml.get('step'), 'name') status_xml = xml.findall('status') self.assertEqual(len(status_xml), 1) self.assertEqual(status_xml[0].text, 'test_status') self.assertEqual(len(xml.findall('flag')), 0)
def test_get_children_duplicate_in_both_next_steps_and_error(self): next_steps = [NextStep(name='name1'), NextStep(name='name2')] error_steps = [NextStep(name='name1'), NextStep(name='error2')] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps, errors=error_steps) self.assertDictEqual(step.get_children(['name1']), next_steps[0].as_json(with_children=False))
def test_name_parent_flag_rename(self): next_step = NextStep(ancestry=['nextstep_parent'], name='nextstep') flag = Flag(action="Top Flag", ancestry=next_step.ancestry) next_step.flags = [flag] new_ancestry = ["nextstep_parent_update"] next_step.reconstruct_ancestry(new_ancestry) new_ancestry.append("nextstep") new_ancestry.append("Top Flag") self.assertListEqual(new_ancestry, next_step.flags[0].ancestry)
def test_to_xml_with_next_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps) self.__check_xml(step, next_steps=next_steps)
def test_init_with_error_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', errors=next_steps) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], [step.as_json() for step in next_steps], ['', ''], [])
def test_as_json_with_children_with_error_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', errors=next_steps) self.basic_json['errors'] = [ next_step.as_json() for next_step in next_steps ] self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_from_json_with_next_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] next_steps_json = [next_step.as_json() for next_step in next_steps] self.basic_input_json['next'] = next_steps_json step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, next_steps_json, ['', ''], [])
def test_as_json_without_children_with_next_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps) self.basic_json['next'] = [next_step.name for next_step in next_steps] self.assertDictEqual(step.as_json(with_children=False), self.basic_json)
def from_json(json_in, position, parent_name='', ancestry=None): """Forms a Step object from the provided JSON object. Args: json (JSON object): The JSON object to convert from. parent_name (str, optional): The name of the parent for ancestry purposes. Defaults to an empty string. ancestry (list[str], optional): The ancestry for the new Step object. Defaults to None. Returns: The Step object parsed from the JSON object. """ device = json_in['device'] if ('device' in json_in and json_in['device'] is not None and json_in['device'] != 'None') else '' risk = json_in['risk'] if 'risk' in json_in else 0 widgets = [] if 'widgets' in json_in: widgets = [(widget['app'], widget['name']) for widget in json_in['widgets'] if ('app' in widget and 'name' in widget)] step = Step(name=json_in['name'], action=json_in['action'], app=json_in['app'], device=device, risk=risk, inputs={ arg_name: arguments.Argument.from_json(arg_element) for arg_name, arg_element in json_in['input'].items() }, parent_name=parent_name, position=position, widgets=widgets, ancestry=ancestry) if json_in['next']: step.conditionals = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['next'] if next_step ] if json_in['errors']: step.errors = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['errors'] if next_step ] return step
def test_create_flag(self): def test_help(_next_step, _expected): self.assertEqual(len(_next_step.flags), len(_expected)) self.assertListEqual([flag.action for flag in _next_step.flags], [flag['action'] for flag in _expected]) for flag, expected_flag in zip(_next_step.flags, _expected): self.assertDictEqual(flag.as_json(), expected_flag) self.assertEqual(flag.parent_name, 'name') expected_ancestry = list(_next_step.ancestry) expected_ancestry.append(flag.name) self.assertListEqual(flag.ancestry, expected_ancestry) next_step = NextStep(name='name') next_step.create_flag('1') expected = [Flag(action='1').as_json()] test_help(next_step, expected) filters = [Filter(action='test_filter_action'), Filter()] next_step.create_flag('2', filters=filters) expected.append(Flag(action='2', filters=filters).as_json()) test_help(next_step, expected) args = {'arg1': 'a', 'arg2': 3, 'arg3': u'abc'} args = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in args.items() } next_step.create_flag('3', filters=filters, args=args) expected.append(Flag(action='3', filters=filters, args=args).as_json()) test_help(next_step, expected)
def test_name_parent_multiple_flag_rename(self): next_step = NextStep(ancestry=['nextstep_parent'], name='mod1_flag1') flag_one = Flag(action="Top Flag", ancestry=next_step.ancestry) flag_two = Flag(action="mod1_flag1", ancestry=next_step.ancestry) next_step.flags = [flag_one, flag_two] new_ancestry = ["nextstep_parent_update"] next_step.reconstruct_ancestry(new_ancestry) new_ancestry.append("mod1_flag1") new_ancestry.append("Top Flag") self.assertListEqual(new_ancestry, next_step.flags[0].ancestry) new_ancestry.remove("Top Flag") new_ancestry.append("mod1_flag1") self.assertListEqual(new_ancestry, next_step.flags[1].ancestry)
def from_json(json, parent_name='', ancestry=None): step = Step(name=json['name'], action=json['action'], app=json['app'], device=json['device'], input={arg_name: arguments.Argument.from_json(arg_element) for arg_name, arg_element in json['input'].items()}, parent_name=parent_name, ancestry=ancestry) step.conditionals = [NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json['next']] step.errors = [NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json['errors']] return step
def createNext(self, nextStep="", flags=None): flags = flags if flags is not None else [] new_conditional = NextStep(parent_name=self.name, name=nextStep, flags=flags, ancestry=list(self.ancestry)) if any(conditional == new_conditional for conditional in self.conditionals): return False self.conditionals.append(new_conditional) return True
def test_eq(self): flags = [Flag(action='mod1_flag1'), Flag(action='Top Flag')] next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=flags, ancestry=['a', 'b']) ] for i in range(len(next_steps)): for j in range(len(next_steps)): if i == j: self.assertEqual(next_steps[i], next_steps[j]) else: self.assertNotEqual(next_steps[i], next_steps[j])
def test_as_json_without_children_with_status(self): self.assertDictEqual( NextStep(status='test_status').as_json(with_children=False), { 'name': '', 'status': 'test_status', 'flags': [] })
def test_as_json_without_children_with_name(self): self.assertDictEqual( NextStep(name='name1').as_json(with_children=False), { 'name': 'name1', 'status': 'Success', 'flags': [] })
def test_get_next_step(self): flag1 = [ Flag(action='mod1_flag2', args={'arg1': '3'}), Flag(action='mod1_flag2', args={'arg1': '-1'}) ] next_steps = [ NextStep(flags=flag1, name='name1'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps) step.output = ActionResult(2, 'Success') self.assertEqual(step.get_next_step({}), 'name2') step.output = ActionResult(1, 'Success') self.assertEqual(step.get_next_step({}), 'name1')
def test_from_json_with_status(self): json_in = {'name': 'name1', 'status': 'test_status', 'flags': []} next_step = NextStep.from_json(json_in) self.__compare_init(next_step, 'name1', '', [], ['', 'name1'], status='test_status')
def test_from_json_with_parent_and_ancestry(self): json_in = {'name': 'name1', 'flags': []} next_step = NextStep.from_json(json_in, parent_name='parent', ancestry=['a', 'b']) self.__compare_init(next_step, 'name1', 'parent', [], ['a', 'b', 'name1'])
def test_init_with_empty_flags(self): next_step = NextStep(name='name', parent_name='parent', flags=[], ancestry=['a', 'b']) self.__compare_init(next_step, 'name', 'parent', [], ['a', 'b', 'name'])
def test_as_json_without_children_full(self): flags = [Flag(action='Top Flag'), Flag(action='mod1_flag1')] expected_flag_json = ['Top Flag', 'mod1_flag1'] self.assertDictEqual( NextStep(name='name1', flags=flags).as_json(with_children=False), { 'name': 'name1', 'flags': expected_flag_json })
def test_name_parent_multiple_nextstep_rename(self): step = Step(ancestry=['step_parent'], name='step') nextstepOne = NextStep(name="test_nextstep_one", ancestry=step.ancestry) nextstepTwo = NextStep(name="test_nextstep_two", ancestry=step.ancestry) step.conditionals = [nextstepOne] step.errors = [nextstepTwo] new_ancestry = ["step_parent_update"] step.reconstruct_ancestry(new_ancestry) new_ancestry.append("step") new_ancestry.append("test_nextstep_one") self.assertListEqual(new_ancestry, step.conditionals[0].ancestry) new_ancestry.remove("test_nextstep_one") new_ancestry.append("test_nextstep_two") self.assertListEqual(new_ancestry, step.errors[0].ancestry)
def test_name_parent_nextstep_rename_error(self): step = Step(ancestry=['step_parent'], name='step') nextstep = NextStep(name="test_nextstep", ancestry=step.ancestry) step.errors = [nextstep] new_ancestry = ["step_parent_update"] step.reconstruct_ancestry(new_ancestry) new_ancestry.append("step") new_ancestry.append("test_nextstep") self.assertListEqual(new_ancestry, step.errors[0].ancestry)