def test_get_for_save_adds_from(self): InputMail.FROM_EMAIL_ADDRESS = "*****@*****.**" headers = {"Subject": "The subject", "Date": str(datetime.now()), "To": "*****@*****.**"} input_mail = InputMail() input_mail.headers = headers self.assertEqual("*****@*****.**", input_mail.get_for_save(1, "SENT")[1][HEADERS_KEY]["From"])
def test_get_for_save_adds_from(self): InputMail.FROM_EMAIL_ADDRESS = '*****@*****.**' headers = {'Subject': 'The subject', 'Date': str(datetime.now()), 'To': '*****@*****.**'} input_mail = InputMail() input_mail.headers = headers self.assertEqual('*****@*****.**', input_mail.get_for_save(1, 'SENT')[1][fields.HEADERS_KEY]['From'])
def render_PUT(self, request): content_dict = json.loads(request.content.read()) _mail = InputMail.from_dict(content_dict) draft_id = content_dict.get("ident") def defer_response(deferred): deferred.addCallback(lambda pixelated_mail: respond_json_deferred({"ident": pixelated_mail.ident}, request)) if draft_id: deferred_check = self._mail_service.mail_exists(draft_id) def handleDuplicatedDraftException(error): respond_json_deferred("", request, status_code=422) def return422otherwise(mail_exists): if not mail_exists: respond_json_deferred("", request, status_code=422) else: new_draft = self._draft_service.update_draft(draft_id, _mail) new_draft.addErrback(handleDuplicatedDraftException) defer_response(new_draft) deferred_check.addCallback(return422otherwise) else: defer_response(self._draft_service.create_draft(_mail)) return server.NOT_DONE_YET
def test_charset_utf8(self): mail_file = pkg_resources.resource_filename('test.unit.fixtures', 'mail.utf8') with open(mail_file) as utf8_mail: mail = message_from_file(utf8_mail) input_mail = InputMail.from_python_mail(mail) body = u'utf8 é çñ\n' self.assertEqual(body, input_mail.body)
def test_charset_latin1(self): mail_file = pkg_resources.resource_filename('test.unit.fixtures', 'mail.latin1') with open(mail_file) as latin1_mail: mail = message_from_file(latin1_mail) input_mail = InputMail.from_python_mail(mail) body = u'latin1 é çñ\n' self.assertEqual(body, input_mail.body)
def send_mail(self, content_dict): mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') yield self.mail_sender.sendmail(mail) sent_mail = yield self.move_to_sent(draft_id, mail) defer.returnValue(sent_mail)
def render_PUT(self, request): content_dict = json.loads(request.content.read()) _mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') def defer_response(deferred): deferred.addCallback(lambda pixelated_mail: respond_json_deferred( {'ident': pixelated_mail.ident}, request)) if draft_id: deferred_check = self._mail_service.mail_exists(draft_id) def return422otherwise(mail_exists): if not mail_exists: respond_json_deferred("", request, status_code=422) else: defer_response( self._draft_service.update_draft(draft_id, _mail)) deferred_check.addCallback(return422otherwise) else: print '\nCreating draft\n' defer_response(self._draft_service.create_draft(_mail)) return server.NOT_DONE_YET
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))
def send_mail(self, content_dict): mail = InputMail.from_dict(content_dict, self.account_email) draft_id = content_dict.get('ident') yield self.mail_sender.sendmail(mail) sent_mail = yield self.move_to_sent(draft_id, mail) defer.returnValue(sent_mail)
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_to_mime_multipart_handles_alternative_bodies(self): mime_multipart = InputMail.from_dict(multipart_mail_dict()).to_mime_multipart() part_one = 'Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\nHello world!' part_two = 'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n<p>Hello html world!</p>' self.assertRegexpMatches(mime_multipart.as_string(), part_one) self.assertRegexpMatches(mime_multipart.as_string(), part_two)
def add_welcome_mail(mail_store): welcome_mail = pkg_resources.resource_filename('pixelated.assets', 'welcome.mail') with open(welcome_mail) as mail_template_file: mail_template = message_from_file(mail_template_file) input_mail = InputMail.from_python_mail(mail_template) mail_store.add_mail('INBOX', input_mail.raw)
def add_welcome_mail(mail_store): current_path = os.path.dirname(os.path.abspath(__file__)) welcome_mail = os.path.join(current_path, 'assets', 'welcome.mail') with open(welcome_mail) as mail_template_file: mail_template = message_from_file(mail_template_file) input_mail = InputMail.from_python_mail(mail_template) mail_store.add_mail('INBOX', input_mail.raw)
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 add_welcome_mail(mail_store): current_path = os.path.dirname(os.path.abspath(__file__)) welcome_mail = os.path.join(current_path, "assets", "welcome.mail") with open(welcome_mail) as mail_template_file: mail_template = message_from_file(mail_template_file) input_mail = InputMail.from_python_mail(mail_template) mail_store.add_mail("INBOX", input_mail.raw)
def test_if_recipient_doubled_in_fields_send_only_in_bcc(self): mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') yield self.mail_service._deduplicate_recipients(mail) self.assertIn('*****@*****.**', mail.to) self.assertNotIn('*****@*****.**', mail.to) self.assertIn('*****@*****.**', mail.bcc)
def test_if_deduplicates_when_recipient_repeated_in_field(self): mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') yield self.mail_service._deduplicate_recipients(mail) self.assertItemsEqual(['*****@*****.**', '*****@*****.**'], mail.bcc) self.assertItemsEqual(['*****@*****.**', '*****@*****.**'], mail.to) self.assertItemsEqual(['*****@*****.**'], mail.cc)
def _get_welcome_mail(self): current_path = os.path.dirname(os.path.abspath(__file__)) with open( os.path.join(current_path, '..', '..', 'pixelated', 'assets', 'welcome.mail')) as mail_template_file: mail_template = message_from_file(mail_template_file) return InputMail.from_python_mail(mail_template)
def send_mail(self, content_dict): mail = InputMail.from_dict(content_dict, self.account_email) draft_id = content_dict.get('ident') self._deduplicate_recipients(mail) yield self.mail_sender.sendmail(mail) sent_mail = yield self.move_to_sent(draft_id, mail) defer.returnValue(sent_mail)
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_single_recipient(self): mail_single_recipient = { "body": "", "header": {"to": ["*****@*****.**"], "cc": [""], "bcc": [""], "subject": "Oi"}, } result = InputMail.from_dict(mail_single_recipient).raw self.assertRegexpMatches(result, "To: [email protected]")
def test_to_mime_multipart_with_special_chars(self): mail_dict = simple_mail_dict() mail_dict['header']['to'] = u'"Älbert Übrö \xF0\x9F\x92\xA9" <äüö@example.mail>' pixelated.support.date.mail_date_now = lambda: 'date now' mime_multipart = InputMail.from_dict(mail_dict).to_mime_multipart() expected_part_of_encoded_to = 'Iiwgw4QsIGwsIGIsIGUsIHIsIHQsICAsIMOcLCBiLCByLCDDtiwgICwgw7As' self.assertRegexpMatches(mime_multipart.as_string(), expected_part_of_encoded_to)
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 send_mail(self, content_dict): mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') def move_to_sent(_): return self.move_to_sent(draft_id, mail) deferred = self.mail_sender.sendmail(mail) deferred.addCallback(move_to_sent) return deferred
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 _get_welcome_mail(self): current_path = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(current_path, '..', '..', 'pixelated', 'assets', 'welcome.mail')) as mail_template_file: mail_template = message_from_file(mail_template_file) return InputMail.from_python_mail(mail_template)
def test_to_mime_multipart(self): pixelated.support.date.iso_now = lambda: 'date now' mime_multipart = InputMail.from_dict(simple_mail_dict()).to_mime_multipart() self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: [email protected], [email protected]\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: [email protected], [email protected]\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: [email protected], [email protected]\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nDate: date now\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n") self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(simple_mail_dict()['body']))
def test_to_mime_multipart(self): pixelated.support.date.mail_date_now = lambda: 'date now' mime_multipart = InputMail.from_dict(simple_mail_dict()).to_mime_multipart() self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: [email protected], [email protected]\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: [email protected], [email protected]\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: [email protected], [email protected]\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nDate: date now\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n") self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(simple_mail_dict()['body']))
def test_to_mime_multipart_with_special_chars(self): mail_dict = simple_mail_dict() mail_dict['header'][ 'to'] = u'"Älbert Übrö \xF0\x9F\x92\xA9" <äüö@example.mail>' pixelated.support.date.mail_date_now = lambda: 'date now' mime_multipart = InputMail.from_dict(mail_dict).to_mime_multipart() expected_part_of_encoded_to = 'Iiwgw4QsIGwsIGIsIGUsIHIsIHQsICAsIMOcLCBiLCByLCDDtiwgICwgw7As' self.assertRegexpMatches(mime_multipart.as_string(), expected_part_of_encoded_to)
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_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 add_welcome_mail(mail_store): current_path = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(current_path, '..', 'assets', 'welcome.mail')) as mail_template_file: mail_template = message_from_file(mail_template_file) input_mail = InputMail.from_python_mail(mail_template) logging.getLogger('pixelated.config.leap').info('Adding the welcome mail') mail_store.add_mail('INBOX', input_mail.raw)
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_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_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_send_mail(self): input_mail = InputMail() when(InputMail).from_dict(ANY(), ANY()).thenReturn(input_mail) when(self.mail_sender).sendmail(ANY()).thenReturn(defer.Deferred()) sent_deferred = self.mail_service.send_mail(mail_dict()) verify(self.mail_sender).sendmail(input_mail) sent_deferred.callback('Assume sending mail succeeded') return 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 render_POST(self, request): content_dict = json.loads(request.content.read()) _mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') self._mail_service.send(_mail) sent_mail = self._mail_service.move_to_send(draft_id, _mail) self._search_engine.index_mail(sent_mail) if draft_id: self._search_engine.remove_from_index(draft_id) return respond_json(sent_mail.as_dict(), request)
def render_PUT(self, request): content_dict = json.loads(request.content.read()) _mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') if draft_id: if not self._mail_service.mail_exists(draft_id): return respond_json("", request, status_code=422) pixelated_mail = self._draft_service.update_draft(draft_id, _mail) else: pixelated_mail = self._draft_service.create_draft(_mail) return respond_json({'ident': pixelated_mail.ident}, request)
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_raw_with_attachment_data(self): input_mail = InputMail.from_dict(with_attachment_mail_dict(), from_address='pixelated@org') attachment = MIMENonMultipart('text', 'plain', Content_Disposition='attachment; filename=ayoyo.txt') attachment.set_payload('Hello World') mail = MIMEMultipart() mail.attach(attachment) part_one = 'Content-Type: text/plain\nMIME-Version: 1.0\nContent-Disposition: attachment; filename="ayoyo.txt"\nContent-Transfer-Encoding: base64\n\n' part_two = 'Content-Type: text/html\nMIME-Version: 1.0\nContent-Disposition: attachment; filename="hello.html"\nContent-Transfer-Encoding: base64\n\n' self.assertRegexpMatches(input_mail.raw, part_one) self.assertRegexpMatches(input_mail.raw, part_two)
def test_send_mail_does_not_delete_draft_on_error(self): input_mail = InputMail() when(InputMail).from_dict(ANY(), ANY()).thenReturn(input_mail) deferred_failure = defer.fail(Exception("Assume sending mail failed")) when(self.mail_sender).sendmail(ANY()).thenReturn(deferred_failure) try: yield self.mail_service.send_mail({'ident': '12'}) self.fail("send_mail is expected to raise if underlying call fails") except: verify(self.mail_sender).sendmail(input_mail) verifyNoMoreInteractions(self.drafts)
def _handle_put(self, request): _draft_service = self.draft_service(request) _mail_service = self.mail_service(request) content_dict = json.loads(request.content.read()) with_attachment_content = yield self._fetch_attachment_contents(content_dict, _mail_service) _mail = InputMail.from_dict(with_attachment_content, from_address=_mail_service.account_email) draft_id = content_dict.get('ident') pixelated_mail = yield _draft_service.process_draft(draft_id, _mail) if not pixelated_mail: respond_json_deferred("", request, status_code=422) else: respond_json_deferred({'ident': pixelated_mail.ident}, request)
def test_single_recipient(self): mail_single_recipient = { 'body': '', 'header': { 'to': ['*****@*****.**'], 'cc': [''], 'bcc': [''], 'subject': 'Oi' } } result = InputMail.from_dict(mail_single_recipient).raw self.assertRegexpMatches(result, 'To: [email protected]')
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_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_to_mime_multipart_should_add_blank_fields(self): pixelated.support.date.iso_now = lambda: 'date now' mail_dict = simple_mail_dict() mail_dict['header']['to'] = '' mail_dict['header']['bcc'] = '' mail_dict['header']['cc'] = '' mail_dict['header']['subject'] = '' mime_multipart = InputMail.from_dict(mail_dict).to_mime_multipart() self.assertNotRegexpMatches(mime_multipart.as_string(), "\nTo: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nBcc: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nCc: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nSubject: \n")