def test_handling_show_next(self): slave = Slave(Mock(PresentationLayout)) slave.set_presentation(self.createPresentation()) msg = Message() msg.set_field(MessageKeys.type_key, "command") msg.set_field(MessageKeys.command_key, Command.SHOW_NEXT.value) response = slave.handle_message(msg) self.assertEqual(response.get_field(MessageKeys.response_key), Command.SHOW_NEXT.value)
def test_handling_closing_connection(self): slave = Slave() msg = Message() slave.layout = Mock(PresentationLayout) with patch.object(slave.layout, "reset_presentation") as mock: msg.set_field("command", Command.DROP_CONNECTION.value) slave.handle_message(msg) mock.assert_called_with()
def test_handling_ending_presentation(self): slave = Slave() slave.presentation.set_files(["suck", "on", "this"]) msg = Message() slave.layout = Mock(PresentationLayout) with patch.object(slave.layout, "reset_presentation") as mock: msg.set_field("command", Command.END_PRESENTATION.value) response = slave.handle_message(msg) self.assertEqual(response.get_field("responseTo"), Command.END_PRESENTATION.value) mock.assert_called_with()
def create_response(command, metadata=None): """ Creates a instance of Message based on the given command """ resp = Message() resp.set_field(MessageKeys.response_key, command) if metadata is not None: for key, value in metadata.items(): resp.set_field(key, value) return resp
def test_handling_show_next_resetting_presentation(self): slave = Slave() slave.set_presentation(self.createPresentation()) slave.presentation.load() for i in range(0, len(slave.presentation.get_presentation_content())): slave.presentation.get_next() msg = Message() slave.layout = Mock(PresentationLayout) with patch.object(slave.layout, "reset_presentation") as mock: msg.set_field(MessageKeys.command_key, Command.SHOW_NEXT.value) response = slave.handle_message(msg) self.assertEqual(response.get_field(MessageKeys.response_key), Command.SHOW_NEXT.value) mock.assert_called_with()
def handle_message(self, json_msg): """ Makes a Message from the json message and adds a sender field. """ msg = Message(json_msg) msg.set_field("sender", self.connection.getPeer().host) response = self.parent.handle_message(msg) if response: response.set_field("name", self.__get_name()) response.set_field("address", msg.get_field("sender")) Logger.debug("RemuTCP: response to json") Logger.debug("RemuTCP: %s", str(response.fields)) return response.to_json() return None
def send_command(self, command, params=None): """ Private function for sending commands """ if self.connection: msg = Message() Logger.debug("SlaveConnection: %s", str(params)) msg.set_field(MessageKeys.type_key, "command") msg.set_field(MessageKeys.command_key, command) msg.set_field(MessageKeys.params_key, params) self.connection.send_message(msg)
def test_handling_invalid_commands(self): slave = Slave() msg = Message() msg.set_field(MessageKeys.command_key, "SHOWUSWHATYOU'VEGOT") response = slave.handle_message(msg) self.assertEqual(response.get_field(MessageKeys.response_key), Command.INVALID_COMMAND.value)
def test_valid_response(self): msg = Message() msg.set_field("responseTo", 0) self.assertEqual(0, msg.get_response())
def test_invalid_response(self): msg = Message() msg.set_field("responseTo", "9999") self.assertEqual(Command.INVALID_COMMAND, msg.get_response())
def test_data(self): msg = Message() msg.set_field("data", "test") self.assertEqual(msg.get_data(), "test")
def test_invalid_command(self): msg = Message() msg.set_field("command", "9999") self.assertEqual(Command.INVALID_COMMAND, msg.get_command())
def test_set_and_get_field(self): msg = Message() msg.set_field("Test", "Kappa") self.assertEqual(msg.fields["Test"], "Kappa")
def test_handle_invalid_command(self): msg = Message() msg.set_field(MessageKeys.response_key, "LET ME OUT LET ME OUT LET ME OUT") self.sc.handle_message(msg)