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 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_data_received(self): with patch.object(MockClass, 'write', return_value=None) and\ patch.object(MockClass, 'handle_message', return_value=Message()): remuprotocol = RemuProtocol() remuprotocol.transport = MockClass() tcp = RemuTCP(None, False, None, 8003) tcp.connection = MockClass() tcp.parent = MockClass() remuprotocol.factory = RemuProtocolFactory(tcp) remuprotocol.dataReceived(b'{"name": "test", "last": "case"}')
def seedDatabase(db): user1 = User("USER-1", "user1","Name1", ["ROLE-3", "ROLE-5", "ROLE-4"], []) user2 = User("USER-2", "Brad123", "Brad McBradface", ["ROLE-1", "ROLE-4"], []) user3 = User("USER-3", "Team5", "Jon Pedersen", ["ROLE-4"], []) role1 = Role("ROLE-1", "Manager") role2 = Role("ROLE-2", "Doctor") role3 = Role("ROLE-3", "Receptionist") role4 = Role("ROLE-4", "Nurse") role5 = Role("ROLE-5", "Brain surgeron") channel1 = GroupChannel("CHANNEL-1", [], "Intensivavdeling", ["ROLE-1", "ROLE-3"]) channel2 = GroupChannel("CHANNEL-2", [], "Akutmottak", ["ROLE-4"]) channel3 = GroupChannel("CHANNEL-3", [], "Underetg", ["ROLE-1"]) message1 = Message("MESSAGE-1", "USER-1", "CHANNEL-1", 200, False, "IOPFEOPWKAFOPKPO==", datetime.datetime.now()) message2 = Message("MESSAGE-2", "USER-3", "CHANNEL-2", 14, True, "DPOKWFOPAWKKOPFW", datetime.datetime.now()) channel1.publishMessage(message1) channel2.publishMessage(message2) db.addRole(role1) db.addRole(role2) db.addRole(role3) db.addRole(role4) db.addRole(role5) db.addUser(user1) db.addUser(user2) db.addUser(user3) db.addChannel(channel1) db.addChannel(channel2) db.addChannel(channel3) db.addMessage(message1) db.addMessage(message2)
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 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 handleSendMessage(self, session, message): channelId = message.get("channel") payload = message.get("payload") duration = message.get("duration") messageId = message.get("id") emergency = message.get("emergency") channel = self._db.findChannel(channelId) if channel is None: raise Exception(ERROR_CODES.UNKNOWN_CHANNEL) sender = self._db.findUser(session.userName) if not channel.hasAccess(sender): raise Exception(ERROR_CODES.ACCESS_DENIED) newMessage = Message(messageId, sender.id, channelId, duration, emergency, payload, datetime.datetime.now()) result = channel.publishMessage(newMessage) if result == -1: raise Exception(ERROR_CODES.VOICE_MESSAGE_TOO_LONG) if result == 0: raise Exception(ERROR_CODES.VOICE_MESSAGE_LIMIT_EXCEEDED) self._db.addMessage(newMessage) for otherSession in self._sessions: #Broadcasting to other walkies if otherSession.walkie == session.walkie: continue if not (channel.id in otherSession.joinedChannels): continue print("Sending to ", otherSession.userName) self.sendToWalkie( otherSession.walkie, WALKIE_MESSAGE.INCOMING_MESSAGE, { 'id': messageId, 'duration': newMessage.duration, 'emergency': newMessage.isEmergency, 'payload': newMessage.payload, 'timestamp': newMessage.timestamp.strftime("%Y-%m-%d %H:%M:%S") }) self.sendToWalkie(session.walkie, WALKIE_MESSAGE.SEND_MESSAGE, {'id': messageId})
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)
def test_no_data(self): msg = Message() self.assertIsNone(msg.get_data())
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_handling_empty_messages(self): slave = Slave() msg = Message() response = slave.handle_message(msg) self.assertEqual(response.get_field(MessageKeys.response_key), Command.INVALID_COMMAND.value)
def test_set_and_get_field(self): msg = Message() msg.set_field("Test", "Kappa") self.assertEqual(msg.fields["Test"], "Kappa")
def test_to_json(self): json_data = r'{"name": "test", "last": "case"}' msg = Message(json_data) json_data2 = msg.to_json() self.assertEqual(sorted(json_data), sorted(json_data2))
def test_init(self): json_data = r'{"name": "test", "last": "case"}' msg = Message(json_data) self.assertEqual(msg.fields['name'], 'test') self.assertEqual(msg.fields['last'], 'case')
def test_get_field(self): msg = Message() msg.fields["Test"] = "Kappa" self.assertEqual(msg.fields["Test"], msg.get_field("Test"))
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_key(self): msg = Message() self.assertIsNone(msg.get_field("test"))
def test_invalid_command(self): msg = Message() msg.set_field("command", "9999") self.assertEqual(Command.INVALID_COMMAND, msg.get_command())