# -*- coding: utf-8 -*-
from watson.mail import messages, Message


class TestRecipients(object):
    def test_assigned_recipients(self):
        recipients = messages.Recipients('<EMAIL>')
        assert '<EMAIL>' in recipients.to
        assert recipients.to[0].formatted == '<EMAIL>'

    def test_cc_address(self):
        recipients = messages.Recipients('<EMAIL>')
        assert '<EMAIL>' in recipients.to
        assert not recipients.bcc
        assert not recipients.cc

    def test_multiple_to_addresses(self):
        recipients = messages.Recipients(
            ['<EMAIL>', '<EMAIL>'])
        assert len(recipients.to) == 2

    def test_named_recipient(self):
        recipients = messages.Recipients(('Test', '<EMAIL>'))
        assert '<EMAIL>' in recipients.to
        assert recipients.to[0].name == 'Test'
        assert recipients.to[0].formatted == 'Test <<EMAIL>>'

    def test_add_remove_recipient(self):
        recipients = messages.Recipients('<EMAIL>')
        assert len(recipients.to) == 1
        recipients.to.remove('<EMAIL>')
        assert len(recipients.to) == 0
        assert '<EMAIL>' not in recipients.to

    def test_ccs(self):
        recipients = messages.Recipients(
            '<EMAIL>',
            cc='<EMAIL>',
            bcc='<EMAIL>')
        assert len(recipients.cc) == 1
        assert len(recipients.bcc) == 1

    def test_add_to(self):
        recipients = messages.Recipients('<EMAIL>')
        assert len(recipients.to) == 1
        recipients.to.add('<EMAIL>')
        assert len(recipients.to) == 2


class TestSenders(object):
    def test_message_from(self):
        message = Message(to='<EMAIL>')
        assert message.senders.from_.email == '<EMAIL>'
        assert '<EMAIL>' in message.senders.reply_to


class TestMailMessage(object):
    def test_message_construction(self):
        message = Message(to='<EMAIL>')
        assert repr(message) == '<watson.mail.messages.Message to:1 cc:0 bcc:0>'

    def test_message_prepared(self):
        message = Message(
            to=['<EMAIL>', ('Test', '<EMAIL>')],
            subject='Testing',
            body='<b>This is a test</b>')
        message_string = message.prepared.as_string()
        assert 'Subject: Testing' in message_string
        assert 'PGI+VGhpcyBpcyBhIHRlc3Q8L2I+' in message_string

    def test_message_raw_prepared(self):
        message = Message(
            to=['<EMAIL>', ('Test', '<EMAIL>')],
            subject='Testing',
            body='<b>This is a test</b>',
            send_as_base64=False)
        message_string = message.prepared.as_string()
        assert 'This is a test' in message_string

    def test_text_message(self):
        message = Message(
            to='<EMAIL>',
            body='<b>This</b> is a test',
            alternative='This is a test')
        message_string = message.prepared.as_string()
        assert 'PGI+VGhpczwvYj4gaXMgYSB0ZXN0' in message_string
        assert 'VGhpcyBpcyBhIHRlc3Q=' in message_string

    def test_text_message_breaks(self):
        message = Message(
            to='<EMAIL>',
            body='This<br>is a test')
        message_string = message.prepared.as_string()
        assert 'VGhpcwppcyBhIHRlc3Q=' in message_string

    def test_attachments(self):
        message = Message(to='<EMAIL>')
        message.attach(__file__)
        assert 'filename="test_mail.py"' in message.prepared.as_string()

    def test_recipients(self):
        message = Message(
            '<EMAIL>',
            cc='<EMAIL>',
            bcc='<EMAIL>')
        message_string = message.prepared.as_string()
        assert 'To: <EMAIL>' in message_string
        assert 'Cc: <EMAIL>' in message_string
        assert 'Bcc: <EMAIL>' in message_string

# -*- coding: utf-8 -*-
from watson.mail import messages, Message


class TestRecipients(object):
    def test_assigned_recipients(self):
        recipients = messages.Recipients('<EMAIL>')
        assert '<EMAIL>' in recipients.to
        assert recipients.to[0].formatted == '<EMAIL>'

    def test_cc_address(self):
        recipients = messages.Recipients('<EMAIL>')
        assert '<EMAIL>' in recipients.to
        assert not recipients.bcc
        assert not recipients.cc

    def test_multiple_to_addresses(self):
        recipients = messages.Recipients(
            ['<EMAIL>', '<EMAIL>'])
        assert len(recipients.to) == 2

    def test_named_recipient(self):
        recipients = messages.Recipients(('Test', '<EMAIL>'))
        assert '<EMAIL>' in recipients.to
        assert recipients.to[0].name == 'Test'
        assert recipients.to[0].formatted == 'Test <<EMAIL>>'

    def test_add_remove_recipient(self):
        recipients = messages.Recipients('<EMAIL>')
        assert len(recipients.to) == 1
        recipients.to.remove('<EMAIL>')
        assert len(recipients.to) == 0
        assert '<EMAIL>' not in recipients.to

    def test_ccs(self):
        recipients = messages.Recipients(
            '<EMAIL>',
            cc='<EMAIL>',
            bcc='<EMAIL>')
        assert len(recipients.cc) == 1
        assert len(recipients.bcc) == 1

    def test_add_to(self):
        recipients = messages.Recipients('<EMAIL>')
        assert len(recipients.to) == 1
        recipients.to.add('<EMAIL>')
        assert len(recipients.to) == 2


class TestSenders(object):
    def test_message_from(self):
        message = Message(to='<EMAIL>')
        assert message.senders.from_.email == '<EMAIL>'
        assert '<EMAIL>' in message.senders.reply_to


class TestMailMessage(object):
    def test_message_construction(self):
        message = Message(to='<EMAIL>')
        assert repr(message) == '<watson.mail.messages.Message to:1 cc:0 bcc:0>'

    def test_message_prepared(self):
        message = Message(
            to=['<EMAIL>', ('Test', '<EMAIL>')],
            subject='Testing',
            body='<b>This is a test</b>')
        message_string = message.prepared.as_string()
        assert 'Subject: Testing' in message_string
        assert 'PGI+VGhpcyBpcyBhIHRlc3Q8L2I+' in message_string

    def test_message_raw_prepared(self):
        message = Message(
            to=['<EMAIL>', ('Test', '<EMAIL>')],
            subject='Testing',
            body='<b>This is a test</b>',
            send_as_base64=False)
        message_string = message.prepared.as_string()
        assert 'This is a test' in message_string

    def test_text_message(self):
        message = Message(
            to='<EMAIL>',
            body='<b>This</b> is a test',
            alternative='This is a test')
        message_string = message.prepared.as_string()
        assert 'PGI+VGhpczwvYj4gaXMgYSB0ZXN0' in message_string
        assert 'VGhpcyBpcyBhIHRlc3Q=' in message_string

    def test_text_message_breaks(self):
        message = Message(
            to='<EMAIL>',
            body='This<br>is a test')
        message_string = message.prepared.as_string()
        assert 'VGhpcwppcyBhIHRlc3Q=' in message_string

    def test_attachments(self):
        message = Message(to='<EMAIL>')
        message.attach(__file__)
        assert 'filename="test_mail.py"' in message.prepared.as_string()

    def test_recipients(self):
        message = Message(
            '<EMAIL>',
            cc='<EMAIL>',
            bcc='<EMAIL>')
        message_string = message.prepared.as_string()
        assert 'To: <EMAIL>' in message_string
        assert 'Cc: <EMAIL>' in message_string
        assert 'Bcc: <EMAIL>' in message_string

# -*- coding: utf-8 -*-