def test_should_create_new_command_with_passed_arguments(self): factory = CommandFactory factory.commandMapping = {"status": CommandMock} command = CommandFactory.create_command("status2") self.assertIsNotNone(command) self.assertIsInstance(command, UnknownCommand) self.assertEqual({"message_text": "Unknown command"}, command.process("reer"))
def check_updates(self, data): # Message gets accepted if # (1) Sender is authorized # (2) Correct recipient for update in data: self.offset = update['update_id'] from_id = update['message']['chat']['id'] name = update['message']['chat']['first_name'] if from_id not in self.configuration.allowed_access: LoggingService.info("Unauthorized access") return self.send_text(from_id, UnauthorizedCommand(None).process(from_id)) continue message = update['message']['text'] parameters = (self.offset, name, from_id, message) LoggingService.info('Message (id%s) from %s (id%s): "%s"' % parameters) body = self.check_recipient_params(message) if body is None: LoggingService.info("Different recipient") continue command = CommandFactory.create_command(body) try: return_message = command.process(from_id) self.send_message(from_id, return_message) except Exception as inst: LoggingService.exception(inst) return self.send_text(from_id, str(inst))
def check_updates(self, data): # Message gets accepted if # (1) Sender is authorized # (2) Correct recipient for update in data: self.offset = update['update_id'] from_id = update['message']['chat']['id'] name = update['message']['chat']['first_name'] if from_id not in self.configuration.allowed_access: LoggingService.info("Unauthorized access") return self.send_text( from_id, UnauthorizedCommand(None).process(from_id)) continue message = update['message']['text'] parameters = (self.offset, name, from_id, message) LoggingService.info('Message (id%s) from %s (id%s): "%s"' % parameters) body = self.check_recipient_params(message) if body is None: LoggingService.info("Different recipient") continue command = CommandFactory.create_command(body) try: return_message = command.process(from_id) self.send_message(from_id, return_message) except Exception as inst: LoggingService.exception(inst) return self.send_text(from_id, str(inst))
def test_should_create_new_command_with_passed_arguments(self): factory = CommandFactory factory.commandMapping = {"status": CommandMock} command = CommandFactory.create_command("status2") self.assertIsNotNone(command) self.assertIsInstance(command, UnknownCommand) self.assertEqual({"message_text": "Unknown command"}, command.process("reer"))
def create_command(arguments): """This method creates the right command. """ try: command_type = arguments.parser created_command = CommandFactory.create(command_type, **vars(arguments)) return created_command except Exception as exp: raise Exception("failed to create command of type: {0} with exception: {1}".format(command_type, exp))
class ShellTransformerTest(unittest.TestCase): command_factory = CommandFactory(Environment()) def setUp(self) -> None: self.parser = ShellParser(ShellTransformerTest.command_factory) def parse(self, string): return self.parser.parse(string) def testEmpty(self): result = self.parse('') self.assertEqual(0, len(result)) def testEcho(self): result = self.parse('echo 123 | echo "hey" | echo 7') self.assertEqual(3, len(result)) args = ['123', 'hey', '7'] for command, arg in zip(result, args): self.assertTrue(isinstance(command, EchoCommand)) self.assertEqual([arg], command.args) def testExit(self): result = self.parse('exit') self.assertEqual(1, len(result)) command = result[0] self.assertTrue(isinstance(command, ExitCommand)) @parameterized.expand([ ('echo', 'echo 123', EchoCommand, ['123']), ('exit', 'exit', ExitCommand, []), ('cat', 'cat main.py', CatCommand, ['main.py']), ('custom command', '/bin/sh main.sh', CustomCommand, ['/bin/sh', 'main.sh']), ('equality', 'x = 3', AssignmentCommand, ['x', '3']), ('pwd', 'pwd', PwdCommand, []), ('wc', 'wc main.py', WcCommand, ['main.py']), ]) def test(self, _, string, command_type, args): result = self.parse(string) self.assertEqual(1, len(result)) command = result[0] self.assertTrue(isinstance(command, command_type)) self.assertEqual(args, command.args)
def __init__(self, environment: Environment): super().__init__(environment) self.parser = ShellParser(CommandFactory(environment))
def __init__(self, **kwargs): """QueryCommand initialization. """ self.device_name = kwargs[cs.UI_ARG_DEVICE] self.vHCAid = kwargs[cs.UI_ARG_VHCAID] self.data = None def get_data(self): """call the QueryData for getting the menu data. """ self.data = QueryData.get_query(self.device_name, self.vHCAid) def print_data(self): """call the data printer with the right configuration for print the menu to screen. """ DataPrinter.print_query_data(self.data) def validate(self): """call the capability validator and check if the core dump supported by the FW. """ if CapabilityValidator.validate(): return True else: raise Exception( "Resource Dump register is not supported by the FW") CommandFactory.register(constants.RESOURCE_DUMP_COMMAND_TYPE_QUERY, QueryCommand)
def test_should_create_new_command(self): factory = CommandFactory factory.commandMapping = {"status": CommandMock, "status2": None} command = CommandFactory.create_command("status") self.assertIsNotNone(command) self.assertEqual(None, command.params)
def test_should_create_new_command_with_passed_arguments(self): factory = CommandFactory factory.commandMapping = {"status": CommandMock} command = CommandFactory.create_command("status param1 param2") self.assertIsNotNone(command) self.assertEqual("param1 param2", command.params)
def test_should_create_new_command(self): factory = CommandFactory factory.commandMapping = {"status": CommandMock, "status2": None} command = CommandFactory.create_command("status") self.assertIsNotNone(command) self.assertEqual(None, command.params)
def test_should_create_new_command_with_passed_arguments(self): factory = CommandFactory factory.commandMapping = {"status": CommandMock} command = CommandFactory.create_command("status param1 param2") self.assertIsNotNone(command) self.assertEqual("param1 param2", command.params)
def setUp(self) -> None: self.parser = ShellParser(CommandFactory(Environment()))
self.data = DumpData.get_dump(device_name=self.device_name, segment=self.segment, vHCAid=self.vHCAid, index1=self.index1, index2=self.index2, numOfObj1=self.numOfObj1, numOfObj2=self.numOfObj2, depth=self.depth, bin=self.bin) def print_data(self): """call the data printer with the right configuration for print the dump data to screen or in a binary format (choosed by the user). """ DataPrinter.print_dump_data(self.data, self.bin) def validate(self): """call the capability validator and check if the core dump supported by the FW. """ validation_status = False if CapabilityValidator.validate(): validation_status = True else: print("resource dump register is not supported by FW") return validation_status CommandFactory.register(cs.RESOURCE_DUMP_COMMAND_TYPE_DUMP, DumpCommand)