def test__message_handler__MessageHandle__handle__populatesAndSendsTemplate__whenCalledWithValidMessage( self, mock_send): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) message = { 'first_name': 'Jane', 'surname': 'Doe', 'title': 'Mrs', 'subject': 'test', 'recipient_email_address': '*****@*****.**', 'mobile_number': '', 'content': 'dummy_content', 'onetime_pass': '******' } templated_message = message templated_message['content'] = 'Dear Mrs Jane Doe\n ' \ 'Welcome to NDOP your one time passcode is:\n ' \ 'timmy_the_password\n\n ' \ 'Many Thanks,\n ' \ 'Your mates at NDOP' handler.handle(message) mock_send.assert_called_with(templated_message)
class StubReader(QueueReader): def __init__(self, sender): self.handler = MessageHandler(sender) self.watch_queue() def watch_queue(self): for i in range(0, 3): message = self.get_message() self.handler.handle(message) def get_message(self): message = { MESSAGE_FIELD_FIRST_NAME: 'Jane', MESSAGE_FIELD_SURNAME: 'Doe', MESSAGE_FIELD_TITLE: 'Mrs', MESSAGE_FIELD_SUBJECT: 'test', MESSAGE_FIELD_EMAIL: '*****@*****.**', MESSAGE_FIELD_MOBILE: '', MESSAGE_FIELD_CONTENT: 'dummy_content', MESSAGE_FIELD_ONETIME_PASS: '******' } return message
def test__message_handler__MessageHandler__send__sendsMessage__whenCalled( self): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) dummy_message = {} handler.send(dummy_message) handler.sender.send_message.assert_called_with(dummy_message)
def test__message_handler__MessageHandler__handle__logsError__whenSMTPRecipientsRefusedIsRaised( self, mock_templater, mock_send, mock_logger): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) mock_send.side_effect = SMTPRecipientsRefused(['some_recipients']) handler.handle({}) assert mock_logger.called
def test__message_handler__MessageHandler__send__logsError__whenSMTPServerDisconnectedIsRaised( self, mock_logger): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) dummy_message = {} handler.sender.send_message.side_effect = SMTPServerDisconnected handler.send(dummy_message) assert mock_logger.called
def test__message_handler__MessageHandler__send__logsError__whenGetAddressInfoErrorIsRaised( self, mock_logger): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) dummy_message = {} handler.sender.send_message.side_effect = gaierror handler.send(dummy_message) assert mock_logger.called
def test__message_handler__MessageHandler__send__logsError__whenSMTPAuthenticationErrorIsRaised( self, mock_logger): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) dummy_message = {} handler.sender.send_message.side_effect = SMTPAuthenticationError( 1, 'some_message') handler.send(dummy_message) assert mock_logger.called
def test__message_handler__MessageHandler____init____exposesInjectedSender( self): mock_sender = MagicMock(spec=NHSMailEmailSender).return_value handler = MessageHandler(mock_sender) self.assertEqual(mock_sender, handler.sender)
def _on_webhook(self): update = request.get_json() if "message" in update: # message request MessageHandler(self._bot, update["message"]).handle() elif "inline_query" in update: # inline request InlineMessageNoThreadHandler(self._bot, update["inline_query"]) \ .handle() return "OK"
class SingleReader(QueueReader): def __init__(self, sender, getParams, templateFile): self.handler = MessageHandler(sender) message = self.get_message(getParams, templateFile) self.handler.handle(message) def get_message(self, getParams, templateFile): message = { MESSAGE_FIELD_ONETIME_PASS: getParams['code'], MESSAGE_FIELD_SUBJECT: 'ODS One Time Password', MESSAGE_FIELD_EMAIL: getParams['destination'], MESSAGE_FIELD_GUID: getParams['guid'], MESSAGE_TEMPLATE: templateFile } return message def watch_queue(self): #Not used for single message, this is only used when reading from a queue return False
def _handle_update(self, update): if "update_id" in update: if not self._should_process_update(update["update_id"]): return if "message" in update: # message request MessageHandler(self._bot, update["message"], self._make_session_class()).handle() elif "callback_query" in update: # inline request CallbackQueryHandler(self._bot, update["callback_query"], self._make_session_class()).handle()
def _on_message(self, msg): MessageHandler(self._bot, msg, self._make_session_class()).handle()
def __init__(self, sender, getParams, templateFile): self.handler = MessageHandler(sender) message = self.get_message(getParams, templateFile) self.handler.handle(message)
def __init__(self, sender): self.handler = MessageHandler(sender) self.watch_queue()
def _on_message(self, msg): MessageHandler(self._bot, msg).handle()