def test_update_draft(self): mail = InputMail.from_dict(test_helper.mail_dict()) when(self.mail_store).add_mail("DRAFTS", mail.raw).thenReturn(defer.succeed(LeapMail("id", "DRAFTS"))) self.draft_service.update_draft(mail.ident, mail) inorder.verify(self.mail_store).add_mail("DRAFTS", mail.raw) inorder.verify(self.mail_store).delete_mail(mail.ident)
def test_update_draft(self): mail = InputMail.from_dict(test_helper.mail_dict()) when(self.drafts_mailbox).add(mail).thenReturn(mail) self.draft_service.update_draft(mail.ident, mail) inorder.verify(self.drafts_mailbox).add(mail) inorder.verify(self.drafts_mailbox).remove(mail.ident)
def test_update_draft(self): mail = InputMail.from_dict(test_helper.mail_dict(), from_address='pixelated@org') when(self.mail_store).delete_mail(mail.ident).thenReturn(defer.succeed(True)) when(self.mail_store).add_mail('DRAFTS', mail.raw).thenReturn(defer.succeed(LeapMail('id', 'DRAFTS'))) self.draft_service.update_draft(mail.ident, mail) inorder.verify(self.mail_store).delete_mail(mail.ident) inorder.verify(self.mail_store).add_mail('DRAFTS', mail.raw)
def test_iterates_over_recipients(self): input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message(any(), TwistedSmtpUserCapture(recipient))
def test_iterates_over_recipients(self): sender = MailSender(self._smtp_config, self._keymanager_mock) input_mail = InputMail.from_dict(mail_dict()) when(OutgoingMail).send_message(any(), any()).thenAnswer(lambda: defer.succeed(None)) yield sender.sendmail(input_mail) for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message(any(), TwistedSmtpUserCapture(recipient))
def test_iterates_over_recipients_and_send_whitout_bcc_field(self): input_mail = InputMail.from_dict(mail_dict()) bccs = input_mail.bcc when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message(MailToSmtpFormatCapture(recipient, bccs), TwistedSmtpUserCapture(recipient))
def test_problem_with_email_raises_exception(self): input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') when(OutgoingMail).send_message(any(), any()).thenReturn(defer.fail(Exception('pretend something went wrong'))) try: yield self.sender.sendmail(input_mail) self.fail('Exception expected!') except MailSenderException, e: for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map)
def test_senmail_returns_deffered(self): when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) input_mail = InputMail.from_dict(mail_dict()) mail_sender = MailSender('*****@*****.**', self.smtp) deferred = mail_sender.sendmail(input_mail) self.assertIsNotNone(deferred) self.assertTrue(isinstance(deferred, Deferred)) return self._succeed(deferred)
def test_send_mail(self): when(InputMail).from_dict(ANY()).thenReturn('inputmail') when(self.mail_sender).sendmail(ANY()).thenReturn(defer.Deferred()) sent_deferred = self.mail_service.send_mail(mail_dict()) verify(self.mail_sender).sendmail("inputmail") sent_deferred.callback('Assume sending mail succeeded') return sent_deferred
def test_send_leaves_mail_in_tact(self): input_mail_dict = mail_dict() input_mail = InputMail.from_dict(input_mail_dict, from_address='pixelated@org') when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) self.assertEqual(input_mail.to, input_mail_dict["header"]["to"]) self.assertEqual(input_mail.cc, input_mail_dict["header"]["cc"]) self.assertEqual(input_mail.bcc, input_mail_dict["header"]["bcc"]) self.assertEqual(input_mail.subject, input_mail_dict["header"]["subject"])
def test_problem_with_email_raises_exception(self): sender = MailSender(self._smtp_config, self._keymanager_mock) input_mail = InputMail.from_dict(mail_dict()) when(OutgoingMail).send_message(any(), any()).thenAnswer(lambda: defer.fail(Exception('pretend something went wrong'))) try: yield sender.sendmail(input_mail) self.fail('Exception expected!') except MailSenderException, e: for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map)
def test_senmail_returns_deffered(self): when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) input_mail = InputMail.from_dict(mail_dict()) mail_sender = MailSender('*****@*****.**', self.ensure_smtp_is_running_cb) deferred = mail_sender.sendmail(input_mail) self.assertIsNotNone(deferred) self.assertTrue(isinstance(deferred, Deferred)) return self._succeed(deferred)
def test_sendmail_uses_twisted(self): when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) input_mail = InputMail.from_dict(mail_dict()) mail_sender = MailSender('*****@*****.**', self.smtp) sent_deferred = mail_sender.sendmail(input_mail) verify(reactor).connectTCP('localhost', 4650, any()) return self._succeed(sent_deferred)
def test_iterates_over_recipients(self): input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) for recipient in flatten( [input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message( any(), TwistedSmtpUserCapture(recipient))
def test_iterates_over_recipients(self): sender = MailSender(self._smtp_config, self._keymanager_mock) input_mail = InputMail.from_dict(mail_dict()) when(OutgoingMail).send_message( any(), any()).thenAnswer(lambda: defer.succeed(None)) yield sender.sendmail(input_mail) for recipient in flatten( [input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message( any(), TwistedSmtpUserCapture(recipient))
def test_problem_with_email_raises_exception(self): sender = MailSender(self._smtp_config, self._keymanager_mock) input_mail = InputMail.from_dict(mail_dict()) when(OutgoingMail).send_message(any(), any()).thenAnswer( lambda: defer.fail(Exception('pretend something went wrong'))) try: yield sender.sendmail(input_mail) self.fail('Exception expected!') except MailSenderException, e: for recipient in flatten( [input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map)
def test_problem_with_email_raises_exception(self): input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') when(OutgoingMail).send_message(any(), any()).thenReturn( defer.fail(Exception('pretend something went wrong'))) try: yield self.sender.sendmail(input_mail) self.fail('Exception expected!') except MailSenderException, e: for recipient in flatten( [input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map)
def test_send_mail_does_not_delete_draft_on_error(self): when(InputMail).from_dict(any()).thenReturn('inputmail') when(self.mail_sender).sendmail(any()).thenReturn(Deferred()) send_deferred = self.mail_service.send_mail(mail_dict()) verify(self.mail_sender).sendmail("inputmail") def assert_not_removed_from_drafts(_): verifyNoMoreInteractions(self.drafts) send_deferred.addErrback(assert_not_removed_from_drafts) send_deferred.errback(Exception('Assume sending mail failed')) return send_deferred
def test_keymanager_encrypt_problem_raises_exception(self): input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') when(OutgoingMail)._maybe_attach_key(any(), any(), any()).thenReturn( defer.succeed(None)) when(OutgoingMail)._fix_headers(any(), any(), any()).thenReturn( defer.succeed((None, mock()))) when(self._keymanager_mock).encrypt(any(), any(), sign=any(), fetch_remote=any()).thenReturn(defer.fail(Exception('pretend key expired'))) with patch('leap.bitmask.mail.outgoing.service.emit_async'): try: yield self.sender.sendmail(input_mail) self.fail('Exception expected!') except MailSenderException, e: for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map)
def test_send_mail_removes_draft(self): mail_ident = 'Some ident' mail = mail_dict() mail['ident'] = mail_ident when(InputMail).from_dict(any()).thenReturn('inputmail') deferred = Deferred() when(self.mail_sender).sendmail(any()).thenReturn(deferred) sent_deferred = self.mail_service.send_mail(mail) verify(self.mail_sender).sendmail("inputmail") def assert_removed_from_drafts(_): verify(self.drafts).remove(any()) sent_deferred.addCallback(assert_removed_from_drafts) sent_deferred.callback('Assume sending mail succeeded') return sent_deferred
def test_sendmail(self): when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) input_mail = InputMail.from_dict(mail_dict()) mail_sender = MailSender('*****@*****.**', self.ensure_smtp_is_running_cb) return self._succeed(mail_sender.sendmail(input_mail))