<s> import os <EOL> import pickle <EOL> from django . core import mail <EOL> from django . template import TemplateDoesNotExist <EOL> from django . template . loader import get_template <EOL> from django . test import TestCase <EOL> from django . utils import translation <EOL> from . import send_mail , EmailMessage <EOL> CONTEXT2 = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> SUBJECT2 = '<STR_LIT>' <EOL> BODY2 = '<STR_LIT>' <EOL> class BaseMailTestCase ( TestCase ) : <EOL> def _assertMessage ( self , from_email , to , subject , body , <EOL> res_content_type = '<STR_LIT>' , clean = False ) : <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> message = mail . outbox [ <NUM_LIT:0> ] <EOL> self . assertEqual ( message . from_email , from_email ) <EOL> self . assertEqual ( message . to , to ) <EOL> self . assertEqual ( message . subject , subject ) <EOL> self . assertEqual ( message . body , body ) <EOL> self . assertEqual ( message . content_subtype , res_content_type ) <EOL> self . _assertMessageClean ( message , clean ) <EOL> return message <EOL> def _assertMessageClean ( self , message , clean ) : <EOL> self . assertNotEqual ( hasattr ( message , '<STR_LIT>' ) , clean ) <EOL> self . assertNotEqual ( hasattr ( message , '<STR_LIT>' ) , clean ) <EOL> self . assertNotEqual ( hasattr ( message , '<STR_LIT>' ) , clean ) <EOL> class SendMailTestCase ( BaseMailTestCase ) : <EOL> def _send_mail ( self , template_name , context , from_email , to , <EOL> res_subject , res_body , res_content_type = '<STR_LIT>' , <EOL> * args , ** kwargs ) : <EOL> send_mail ( template_name , context , from_email , to , * args , ** kwargs ) <EOL> return self . _assertMessage ( from_email , to , res_subject , res_body , <EOL> res_content_type , <EOL> clean = kwargs . pop ( '<STR_LIT>' , True ) ) <EOL> def test_plain ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_html ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:html>' ) <EOL> def test_multipart ( self ) : <EOL> message = self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( len ( message . alternatives ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_multilang ( self ) : <EOL> translation . activate ( '<STR_LIT>' ) <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> translation . deactivate ( ) <EOL> def test_alternatives ( self ) : <EOL> message = self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> alternatives = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( len ( message . alternatives ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_multipart_alternatives ( self ) : <EOL> message = self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> alternatives = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( len ( message . alternatives ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_attachment ( self ) : <EOL> file_name = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> with open ( file_name , '<STR_LIT:rb>' ) as f : <EOL> content = f . read ( ) <EOL> attachment = ( '<STR_LIT>' , content , '<STR_LIT>' ) <EOL> message = self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> attachments = [ attachment ] ) <EOL> self . assertEqual ( len ( message . attachments ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( message . attachments [ <NUM_LIT:0> ] , attachment ) <EOL> def test_extended ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_overridden ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_overridden2 ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_whitespaces ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def test_override_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from django . test import override_settings <EOL> except ImportError : <EOL> from unittest import SkipTest <EOL> raise SkipTest ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> tag_format = '<STR_LIT>' <EOL> tag_var_format = '<STR_LIT>' <EOL> with override_settings ( <EOL> MAIL_TEMPLATED_TAG_FORMAT = tag_format , <EOL> MAIL_TEMPLATED_TAG_VAR_FORMAT = tag_var_format ) : <EOL> from mail_templated . conf import app_settings <EOL> self . assertEqual ( app_settings . TAG_FORMAT , tag_format ) <EOL> self . assertEqual ( app_settings . TAG_VAR_FORMAT , tag_var_format ) <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_cleanup ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , clean = True ) <EOL> class EmailMessageTestCase ( BaseMailTestCase ) : <EOL> def _send_mail ( self , template_name , context , from_email , to , <EOL> res_subject , res_body , res_content_type = '<STR_LIT>' , <EOL> * args , ** kwargs ) : <EOL> clean = kwargs . pop ( '<STR_LIT>' , False ) <EOL> message = EmailMessage ( template_name , context , from_email , to , <EOL> * args , ** kwargs ) <EOL> message . send ( clean = clean ) <EOL> return self . _assertMessage ( from_email , to , res_subject , res_body , <EOL> res_content_type , clean = clean ) <EOL> def test_plain ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_defaults ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> subject = '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> def test_overridden_defaults ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> subject = '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> def test_html_defaults ( self ) : <EOL> self . _send_mail ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:html>' , <EOL> subject = '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> def test_late_init ( self ) : <EOL> message = EmailMessage ( ) <EOL> message . template_name = '<STR_LIT>' <EOL> message . context = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> message . from_email = '<STR_LIT>' <EOL> message . to = [ '<STR_LIT>' ] <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_load_template ( self ) : <EOL> message = EmailMessage ( '<STR_LIT>' , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , '<STR_LIT>' , <EOL> [ '<STR_LIT>' ] ) <EOL> message . load_template ( ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_load_template_by_name ( self ) : <EOL> message = EmailMessage ( None , { '<STR_LIT:name>' : '<STR_LIT>' } , '<STR_LIT>' , <EOL> [ '<STR_LIT>' ] ) <EOL> message . load_template ( '<STR_LIT>' ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_manual_load_template ( self ) : <EOL> message = EmailMessage ( None , { '<STR_LIT:name>' : '<STR_LIT>' } , '<STR_LIT>' , <EOL> [ '<STR_LIT>' ] ) <EOL> message . template = get_template ( '<STR_LIT>' ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_attach_alternative ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> message . attach_alternative ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( len ( message . alternatives ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( message . alternatives [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_pickling ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . assertIsNone ( message . template ) <EOL> dumped_message = pickle . dumps ( message ) <EOL> message = pickle . loads ( dumped_message ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_pickling_rendered ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , render = True ) <EOL> self . assertIsNotNone ( message . template ) <EOL> dumped_message = pickle . dumps ( message ) <EOL> message = pickle . loads ( dumped_message ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_attach ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> file_name = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> message . attach_file ( file_name , '<STR_LIT>' ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( len ( message . attachments ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( message . attachments [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertTrue ( len ( message . attachments [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) > <NUM_LIT:0> ) <EOL> self . assertEqual ( message . attachments [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] , '<STR_LIT>' ) <EOL> def test_cleanup ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . _assertMessageClean ( message , False ) <EOL> message . render ( ) <EOL> self . _assertMessageClean ( message , False ) <EOL> message . clean ( ) <EOL> self . _assertMessageClean ( message , True ) <EOL> message . send ( ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , clean = True ) <EOL> def test_cleanup_create ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , render = True , clean = True ) <EOL> self . _assertMessageClean ( message , True ) <EOL> def test_cleanup_render ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . _assertMessageClean ( message , False ) <EOL> message . render ( clean = True ) <EOL> self . _assertMessageClean ( message , True ) <EOL> def test_cleanup_send ( self ) : <EOL> message = EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . _assertMessageClean ( message , False ) <EOL> message . render ( ) <EOL> self . _assertMessageClean ( message , False ) <EOL> message . send ( clean = True ) <EOL> self . _assertMessage ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , clean = True ) <EOL> class RenderTestCase ( BaseMailTestCase ) : <EOL> def _initMessage ( self , * args , ** kwargs ) : <EOL> return EmailMessage ( <EOL> '<STR_LIT>' , { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> * args , ** kwargs ) <EOL> def _assertIsRendered ( self , message , is_rendered , subject = '<STR_LIT>' , <EOL> body = '<STR_LIT>' ) : <EOL> if is_rendered : <EOL> self . assertEqual ( message . subject , subject ) <EOL> self . assertEqual ( message . body , body ) <EOL> self . assertTrue ( message . is_rendered ) <EOL> else : <EOL> self . assertIsNone ( message . subject ) <EOL> self . assertIsNone ( message . body ) <EOL> self . assertFalse ( message . is_rendered ) <EOL> def test_init_render ( self ) : <EOL> message = self . _initMessage ( render = True ) <EOL> self . _assertIsRendered ( message , True ) <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True ) <EOL> def test_init_norender ( self ) : <EOL> message = self . _initMessage ( ) <EOL> self . _assertIsRendered ( message , False ) <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True ) <EOL> def test_init_notrender ( self ) : <EOL> message = self . _initMessage ( render = False ) <EOL> self . _assertIsRendered ( message , False ) <EOL> def test_init_cantrender ( self ) : <EOL> self . assertRaises ( TemplateDoesNotExist , EmailMessage , render = True ) <EOL> def test_manual_render ( self ) : <EOL> message = self . _initMessage ( ) <EOL> message . render ( ) <EOL> self . _assertIsRendered ( message , True ) <EOL> message . context = CONTEXT2 <EOL> message . render ( ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> def test_manual_rerender ( self ) : <EOL> message = self . _initMessage ( render = True ) <EOL> message . context = CONTEXT2 <EOL> message . render ( ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> def test_manual_render_with_context ( self ) : <EOL> message = self . _initMessage ( ) <EOL> message . render ( CONTEXT2 ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> def test_manual_cantrender ( self ) : <EOL> message = EmailMessage ( ) <EOL> self . assertRaises ( TemplateDoesNotExist , message . render ) <EOL> def test_send_notrendered ( self ) : <EOL> message = self . _initMessage ( ) <EOL> message . context = CONTEXT2 <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True , SUBJECT2 , BODY2 ) <EOL> def test_send_rendered ( self ) : <EOL> message = self . _initMessage ( render = True ) <EOL> message . context = CONTEXT2 <EOL> message . send ( ) <EOL> self . _assertIsRendered ( message , True ) </s>
