import pytest
from mailthon.headers import Headers, cc, bcc, to, sender
from .mimetest import blank


class TestNotResentHeaders:
    @pytest.fixture
    def headers(self):
        return Headers([
            ('From', 'from@mail.com'),
            sender('sender@mail.com'),
            to('to@mail.com'),
            cc('cc1@mail.com', 'cc2@mail.com'),
            bcc('bcc1@mail.com', 'bcc2@mail.com'),
        ])

    def test_getitem(self, headers):
        assert headers['Sender'] == 'sender@mail.com'
        assert headers['To'] == 'to@mail.com'

    def test_sender(self, headers):
        assert headers.sender == 'sender@mail.com'

    def test_receivers(self, headers):
        assert set(headers.receivers) == set([
            'to@mail.com',
            'cc1@mail.com',
            'cc2@mail.com',
            'bcc1@mail.com',
            'bcc2@mail.com',
            ])

    def test_resent(self, headers):
        assert not headers.resent

    def test_prepare(self, headers):
        mime = blank()
        headers.prepare(mime)

        assert not mime['Bcc']
        assert mime['Cc'] == 'cc1@mail.com, cc2@mail.com'
        assert mime['To'] == 'to@mail.com'
        assert mime['Sender'] == 'sender@mail.com'
        assert mime['From'] == 'from@mail.com'


class TestResentHeaders(TestNotResentHeaders):
    @pytest.fixture
    def headers(self):
        head = TestNotResentHeaders.headers(self)
        head.update({
            'Resent-Date': 'Today',
            'Resent-From': 'rfrom@mail.com',
            'Resent-To': 'rto@mail.com',
            'Resent-Cc': 'rcc@mail.com',
            'Resent-Bcc': 'rbcc1@mail.com, rbcc2@mail.com'
        })
        return head

    def test_sender(self, headers):
        assert headers.sender == 'rfrom@mail.com'

    def test_prefers_resent_sender(self, headers):
        headers['Resent-Sender'] = 'rsender@mail.com'
        assert headers.sender == 'rsender@mail.com'

    def test_resent_sender_without_senders(self, headers):
        del headers['Resent-From']
        assert headers.sender is None

    def test_receivers(self, headers):
        assert set(headers.receivers) == set([
            'rto@mail.com',
            'rcc@mail.com',
            'rbcc1@mail.com',
            'rbcc2@mail.com',
        ])

    def test_resent(self, headers):
        assert headers.resent

    def test_resent_date_removed(self, headers):
        headers.pop('Resent-Date')
        assert not headers.resent

    def test_prepare(self, headers):
        mime = blank()
        headers.prepare(mime)

        assert not mime['Resent-Bcc']
        assert not mime['Bcc']


@pytest.mark.parametrize('function', [to, cc, bcc])
def test_tuple_headers(function):
    _, value = function(
        ('Sender', 'sender@mail.com'),
        'Me <me@mail.com>',
    )
    expected = 'Sender <sender@mail.com>, Me <me@mail.com>'
    assert value == expected


@pytest.mark.parametrize('argtype', [str, tuple])
def test_sender_tuple(argtype):
    param = ('name', 'mail@mail.com')
    if argtype is str:
        param = '{0} <{1}>'.format(*param)
    _, value = sender(param)
    assert value == 'name <mail@mail.com>'

@@ -7,16 +7,68 @@
  pytest%0A
+from mock import Mock, call%0Aimport mailthon.headers%0A
 from mai
@@ -104,24 +104,42 @@
  cc,
+ to,
  bcc, 
-to, 
 sender
+, message_id, date
 %0Afro
@@ -3221,20 +3221,574 @@
 me %3Cmail@mail.com%3E'%0A
+%0A%0Adef test_message_id():%0A    def msgid(thing=None):%0A        return thing%0A%0A    mailthon.headers.make_msgid = Mock(side_effect=msgid)%0A    assert tuple(message_id()) == ('Message-ID', None)%0A    assert tuple(message_id('string')) == ('Message-ID', 'string')%0A    assert tuple(message_id(idstring=1)) == ('Message-ID', 1)%0A%0A%0Adef test_date():%0A    formatdate = mailthon.headers.formatdate = Mock(return_value=1)%0A    assert tuple(date()) == ('Date', 1)%0A    assert formatdate.mock_calls == %5Bcall(localtime=True)%5D%0A%0A    assert tuple(date('time')) == ('Date', 'time')%0A

test date and message id functions