"""
$URL: svn+ssh://sting.mems-exchange.org/repos/trunk/DurusWorks/qp/mail/test/utest_send.py $
$Id: utest_send.py 33719 2011-04-11 01:03:35Z dbinger $
"""
from os import environ
from qp.mail.send import Email, msg_part
from qp.pub.common import clear_publisher, get_publisher
from qp.pub.publish import Publisher
from sancho.utest import UTest


class Test(UTest):

    def _pre(self):
        Publisher()

    def _post(self):
        clear_publisher()

    def a(self):
        e = Email()
        webmaster = get_publisher().get_webmaster_address()
        assert e.format_headers() == ('From: %(webmaster)s\n'
                                      'Subject: \n'
                                      'To: %(webmaster)s\n\n') % locals()
        e.set_to(['al', 'bo', 'ci'])
        e.set_subject('the subject')
        assert e.format_headers() == ('From: %(webmaster)s\n'
                                      'Subject: the subject\n'
                                      'To: al,\n'
                                      '    bo,\n'
                                      '    ci\n'
                                      'Precedence: junk\n'
                                      'X-No-Archive: yes\n\n') % locals()
        e.set_extra_headers(['Spam: yes', 'Ham: no'])
        e.set_reply_to([webmaster])
        assert e.format_headers() == ('From: %(webmaster)s\n'
                                      'Subject: the subject\n'
                                      'To: al,\n'
                                      '    bo,\n'
                                      '    ci\n'
                                      'Reply-To: %(webmaster)s\n'
                                      'Spam: yes\n'
                                      'Ham: no\n'
                                      'Precedence: junk\n'
                                      'X-No-Archive: yes\n\n') % locals()

    def b(self):
        e = Email()
        e.set_to([str(x) for x in range(30)])
        e.set_cc('a')
        webmaster = get_publisher().get_webmaster_address()
        assert e.format_headers() == ('From: %(webmaster)s\n'
                                      'Subject: \n'
                                      'To: (long recipient list suppressed) : ;\n'
                                      'Cc: a\n'
                                      'Precedence: junk\n'
                                      'X-No-Archive: yes\n\n') % locals()
    def c(self):
        e = Email()
        e.set_to([str(x) for x in range(30)])
        e.set_bcc('a')
        webmaster = get_publisher().get_webmaster_address()
        assert e.format_headers() == ('From: %(webmaster)s\n'
                                      'Subject: \n'
                                      'To: (long recipient list suppressed) : ;\n'
                                      'Bcc: a\n'
                                      'Precedence: junk\n'
                                      'X-No-Archive: yes\n\n') % locals()

    def d(self):
        if not environ.get('TESTEMAIL'):
            return
        user = environ.get('TESTEMAIL')
        e = Email()
        e.set_to([user])
        assert get_publisher().is_email_enabled() == False
        if not environ.get('TESTEMAIL'):
            return
        assert e.send() == False
        def is_email_enabled():
            return True
        get_publisher().is_email_enabled = is_email_enabled
        def get_webmaster_address():
            return user
        get_publisher().get_webmaster_address = get_webmaster_address
        def is_live_host():
            return True
        get_publisher().is_live_host = is_live_host
        e.set_body('This was sent when %s ran utest_send.py' % user)
        e.set_subject('utest_send.py')
        assert e.send() == True
        def get_debug_address():
            return None
        get_publisher().get_debug_address = get_debug_address
        f = Email()
        f.set_to([user])
        f.set_subject('utest_send.py')
        f.set_body('This was also sent when %s ran utest_send.py '
                   '\n(with a debug_address).\n' %
                   user)
        assert f.send() == True

    def e(self):
        if not environ.get('TESTEMAIL'):
            return
        user = environ.get('TESTEMAIL')
        e = Email()
        e.set_to([user])
        assert get_publisher().is_email_enabled() == False
        assert e.send() == False
        def is_email_enabled():
            return True
        get_publisher().is_email_enabled = is_email_enabled
        def get_webmaster_address():
            return user
        get_publisher().get_webmaster_address = get_webmaster_address
        def is_live_host():
            return True
        get_publisher().is_live_host = is_live_host
        e.set_mime_type('multipart/mixed')
        e.set_body([
            msg_part(body='This was sent when %s ran utest_send.py' % user),
            msg_part(mime_type="text/html", body='<h1>HELLO</h1>')
            ])
        e.set_subject('utest_send.py')
        assert e.send() == True
        def get_debug_address():
            return None
        get_publisher().get_debug_address = get_debug_address
        f = Email()
        f.set_to([user])
        f.set_subject('utest_send.py')
        f.set_body('This was also sent when %s ran utest_send.py '
                   '\n(with a debug_address).\n' %
                   user)
        assert f.send() == True

if __name__ == '__main__':
    Test()
