def _run_mail_cron_job(self, status_code=200, response={}, delete_settings=(), **override_settings): overrides = { u'EMAIL_OUTBOUND_TRANSPORT': u'poleno.mail.transports.mandrill.MandrillTransport', u'EMAIL_INBOUND_TRANSPORT': None, u'MANDRILL_API_KEY': u'default_testing_api_key', u'MANDRILL_API_URL': u'https://defaulttestinghost/api' } overrides.update(override_settings) requests = mock.Mock() requests.post.return_value.status_code = status_code requests.post.return_value.text = u'Response text' requests.post.return_value.json.return_value = response with self.settings(**overrides): for name in delete_settings: delattr(settings, name) with mock.patch( u'poleno.mail.transports.mandrill.transport.requests', requests): with override_signals(message_sent, message_received): mail_cron_job().do() posts = [ Bunch(url=call[0][0], data=json.loads(call[1][u'data'])) for call in requests.post.call_args_list ] return posts
def _run_mail_cron_job(self, transport=None, ssl_transport=None, mails=[], delete_settings=(), **override_settings): overrides = { u'EMAIL_OUTBOUND_TRANSPORT': None, u'EMAIL_INBOUND_TRANSPORT': u'poleno.mail.transports.imap.ImapTransport', u'IMAP_SSL': False, u'IMAP_HOST': u'defaulttestinghost', u'IMAP_PORT': 1234, u'IMAP_USERNAME': u'defaulttestingusername', u'IMAP_PASSWORD': u'defaulttestingsecret', } overrides.update(override_settings) transport = mock.Mock() transport.return_value.search.return_value = [None, [u' '.join(str(k) for k in range(len(mails)))]] transport.return_value.fetch.side_effect = lambda k, _: [None, [[None, mails[int(k)]]]] imap4 = transport if not overrides[u'IMAP_SSL'] else None imap4ssl = transport if overrides[u'IMAP_SSL'] else None with self.settings(**overrides): for name in delete_settings: delattr(settings, name) with mock.patch.multiple(u'poleno.mail.transports.imap', IMAP4=imap4, IMAP4_SSL=imap4ssl): with override_signals(message_sent, message_received): mail_cron_job().do() return transport
def _run_mail_cron_job(self): overrides = { u'EMAIL_OUTBOUND_TRANSPORT': u'poleno.mail.transports.dummy.DummyTransport', u'EMAIL_INBOUND_TRANSPORT': u'poleno.mail.transports.dummy.DummyTransport', } with self.settings(**overrides): with override_signals(message_sent, message_received): mail_cron_job().do()
def _run_mail_cron_job(self): connection = mock.Mock() with self.settings(EMAIL_OUTBOUND_TRANSPORT=u'poleno.mail.transports.smtp.SmtpTransport', EMAIL_INBOUND_TRANSPORT=None): with mock.patch(u'poleno.mail.transports.smtp.get_connection', return_value=connection): with override_signals(message_sent, message_received): mail_cron_job().do() res = [] for call in connection.send_messages.call_args_list: for mail in call[0][0]: as_bytes = mail.message().as_bytes() headers, body = as_bytes.split(u'\n\n', 1) headers = self._parse_headers(headers) res.append(Bunch(headers=headers, body=body, as_bytes=as_bytes)) return res
def _overrides(self, delete_settings=(), **override_settings): overrides = { u'MANDRILL_WEBHOOK_SECRET': u'default_testing_secret', u'MANDRILL_WEBHOOK_SECRET_NAME': u'default_testing_secret_name', u'MANDRILL_WEBHOOK_KEYS': [u'default_testing_api_key'], u'MANDRILL_WEBHOOK_URL': u'https://defaulttestinghost/', } overrides.update(override_settings) with self.settings(**overrides): for name in delete_settings: delattr(settings, name) with override_signals(webhook_event): yield
def _run_mail_cron_job(self, outbound=False, inbound=False, send_message_method=mock.DEFAULT, message_sent_receiver=None, get_messages_method=mock.DEFAULT, message_received_receiver=None): u""" Mocks mail transport, overrides ``message_sent`` and ``message_received`` signals, calls ``mail`` cron job and eats any stdout prited by the called job. """ transport = u'poleno.mail.transports.base.BaseTransport' outbound_transport = transport if outbound else None inbound_transport = transport if inbound else None with self.settings(EMAIL_OUTBOUND_TRANSPORT=outbound_transport, EMAIL_INBOUND_TRANSPORT=inbound_transport): with mock.patch.multiple(transport, send_message=send_message_method, get_messages=get_messages_method): with override_signals(message_sent, message_received): if message_sent_receiver is not None: message_sent.connect(message_sent_receiver) if message_received_receiver is not None: message_received.connect(message_received_receiver) mail_cron_job().do()
def _run_mail_cron_job(self): connection = mock.Mock() with self.settings(EMAIL_OUTBOUND_TRANSPORT= u'poleno.mail.transports.smtp.SmtpTransport', EMAIL_INBOUND_TRANSPORT=None): with mock.patch(u'poleno.mail.transports.smtp.get_connection', return_value=connection): with override_signals(message_sent, message_received): mail_cron_job().do() res = [] for call in connection.send_messages.call_args_list: for mail in call[0][0]: as_bytes = mail.message().as_bytes() headers, body = as_bytes.split(u'\n\n', 1) headers = self._parse_headers(headers) res.append(Bunch(headers=headers, body=body, as_bytes=as_bytes)) return res
def _run_mail_cron_job(self, status_code=200, response={}, delete_settings=(), **override_settings): overrides = { u'EMAIL_OUTBOUND_TRANSPORT': u'poleno.mail.transports.mandrill.MandrillTransport', u'EMAIL_INBOUND_TRANSPORT': None, u'MANDRILL_API_KEY': u'default_testing_api_key', u'MANDRILL_API_URL': u'https://defaulttestinghost/api' } overrides.update(override_settings) requests = mock.Mock() requests.post.return_value.status_code = status_code requests.post.return_value.text = u'Response text' requests.post.return_value.json.return_value = response with self.settings(**overrides): for name in delete_settings: delattr(settings, name) with mock.patch(u'poleno.mail.transports.mandrill.transport.requests', requests): with override_signals(message_sent, message_received): mail_cron_job().do() posts = [Bunch(url=call[0][0], data=json.loads(call[1][u'data'])) for call in requests.post.call_args_list] return posts