<s> from __future__ import with_statement <EOL> import unittest <EOL> import mailbox <EOL> from email import encoders <EOL> from flask import Flask , g <EOL> from flaskext . mail import Mail , Message , BadHeaderError , Attachment <EOL> from nose . tools import assert_equal <EOL> class TestCase ( unittest . TestCase ) : <EOL> TESTING = True <EOL> DEFAULT_MAIL_SENDER = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> self . app = Flask ( __name__ ) <EOL> self . app . config . from_object ( self ) <EOL> assert self . app . testing <EOL> self . mail = Mail ( self . app ) <EOL> self . ctx = self . app . test_request_context ( ) <EOL> self . ctx . push ( ) <EOL> def tearDown ( self ) : <EOL> self . ctx . pop ( ) <EOL> class TestMessage ( TestCase ) : <EOL> def test_initialize ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] ) <EOL> assert msg . sender == "<STR_LIT>" <EOL> assert msg . recipients == [ "<STR_LIT>" ] <EOL> def test_recipients_properly_initialized ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" ) <EOL> assert msg . recipients == [ ] <EOL> msg2 = Message ( subject = "<STR_LIT>" ) <EOL> msg2 . add_recipient ( "<STR_LIT>" ) <EOL> assert len ( msg . recipients ) == <NUM_LIT:0> <EOL> msg3 = Message ( subject = "<STR_LIT>" ) <EOL> msg3 . add_recipient ( "<STR_LIT>" ) <EOL> assert len ( msg . recipients ) == <NUM_LIT:0> <EOL> def test_add_recipient ( self ) : <EOL> msg = Message ( "<STR_LIT>" ) <EOL> msg . add_recipient ( "<STR_LIT>" ) <EOL> assert msg . recipients == [ "<STR_LIT>" ] <EOL> def test_sender_as_tuple ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> sender = ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def test_send_without_sender ( self ) : <EOL> del self . app . config [ '<STR_LIT>' ] <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> self . assertRaises ( AssertionError , self . mail . send , msg ) <EOL> def test_send_without_recipients ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ ] , <EOL> body = "<STR_LIT>" ) <EOL> self . assertRaises ( AssertionError , self . mail . send , msg ) <EOL> def test_send_without_body ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] ) <EOL> self . assertRaises ( AssertionError , self . mail . send , msg ) <EOL> msg . html = "<STR_LIT>" <EOL> self . mail . send ( msg ) <EOL> def test_normal_send ( self ) : <EOL> """<STR_LIT>""" <EOL> self . app . config [ '<STR_LIT>' ] = False <EOL> self . mail . init_app ( self . app ) <EOL> with self . mail . record_messages ( ) as outbox : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> self . mail . send ( msg ) <EOL> assert len ( outbox ) == <NUM_LIT:1> <EOL> self . app . config [ '<STR_LIT>' ] = True <EOL> def test_attach ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> msg . attach ( data = "<STR_LIT>" , <EOL> content_type = "<STR_LIT>" ) <EOL> a = msg . attachments [ <NUM_LIT:0> ] <EOL> assert a . filename is None <EOL> assert a . disposition == '<STR_LIT>' <EOL> assert a . content_type == "<STR_LIT>" <EOL> assert a . data == "<STR_LIT>" <EOL> def test_bad_header_subject ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> sender = "<STR_LIT>" , <EOL> body = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] ) <EOL> self . assertRaises ( BadHeaderError , self . mail . send , msg ) <EOL> def test_bad_header_sender ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> sender = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> self . assertRaises ( BadHeaderError , self . mail . send , msg ) <EOL> def test_bad_header_recipient ( self ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> sender = "<STR_LIT>" , <EOL> recipients = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> self . assertRaises ( BadHeaderError , self . mail . send , msg ) <EOL> class TestMail ( TestCase ) : <EOL> def test_send ( self ) : <EOL> with self . mail . record_messages ( ) as outbox : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT:test>" ) <EOL> self . mail . send ( msg ) <EOL> assert len ( outbox ) == <NUM_LIT:1> <EOL> def test_send_message ( self ) : <EOL> with self . mail . record_messages ( ) as outbox : <EOL> self . mail . send_message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT:test>" ) <EOL> assert len ( outbox ) == <NUM_LIT:1> <EOL> msg = outbox [ <NUM_LIT:0> ] <EOL> assert msg . subject == "<STR_LIT>" <EOL> assert msg . recipients == [ "<STR_LIT>" ] <EOL> assert msg . body == "<STR_LIT:test>" <EOL> class TestConnection ( TestCase ) : <EOL> def test_send_message ( self ) : <EOL> with self . mail . record_messages ( ) as outbox : <EOL> with self . mail . connect ( ) as conn : <EOL> conn . send_message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> assert len ( outbox ) == <NUM_LIT:1> <EOL> def test_send_single ( self ) : <EOL> with self . mail . record_messages ( ) as outbox : <EOL> with self . mail . connect ( ) as conn : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> conn . send ( msg ) <EOL> assert len ( outbox ) == <NUM_LIT:1> <EOL> def test_send_many ( self ) : <EOL> messages = [ ] <EOL> with self . mail . record_messages ( ) as outbox : <EOL> with self . mail . connect ( ) as conn : <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> conn . send ( msg ) <EOL> assert len ( outbox ) == <NUM_LIT:100> <EOL> def test_max_emails ( self ) : <EOL> messages = [ ] <EOL> with self . mail . record_messages ( ) as outbox : <EOL> with self . mail . connect ( max_emails = <NUM_LIT:10> ) as conn : <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> msg = Message ( subject = "<STR_LIT>" , <EOL> recipients = [ "<STR_LIT>" ] , <EOL> body = "<STR_LIT>" ) <EOL> conn . send ( msg ) <EOL> print conn . num_emails <EOL> if i % <NUM_LIT:10> == <NUM_LIT:0> : <EOL> assert conn . num_emails == <NUM_LIT:1> <EOL> assert len ( outbox ) == <NUM_LIT:100> </s>
