"""This module holds the tests backend for the ``apps.common.notifications`` module."""

from django.core import mail

from pinax.notifications.models import NoticeType

from mtp.apps.common.notifications.backends.email import MultipartEmailBackend
from mtp.apps.common.tests.test_cases import MTPTestCase


class MultipartEmailBackendTestcase(MTPTestCase):
    """Test case for ``MultipartEmailBackend``"""

    def test_email_goes_out_ok(self):
        """Test we can send an email with this class"""

        NoticeType.create(
            'test_notice',
            'Title',
            'Description',
        )
        notice_type = NoticeType.objects.get(label='test_notice')

        # attach a file to the email
        context = {
            'attachments': [('file.txt', 'content', 'text/plain')]
        }

        backend = MultipartEmailBackend('email')
        backend.deliver(
            self.trainee,
            None,
            notice_type,
            context
        )

        # one mail has been sent
        self.assertEqual(len(mail.outbox), 1)
        sent_mail = mail.outbox[0]

        # attachment is set
        self.assertEqual(
            sent_mail.attachments,
            context['attachments']
        )

        # txt content is the one of the NoticeType (default pinax)
        self.assertEqual(sent_mail.body, '\n')

        # we have some html attached
        self.assertTrue(len(sent_mail.alternatives) > 0)
        self.assertIn('doctype html', sent_mail.alternatives[0][0])

        # default pinax subject is empty
        self.assertEqual(sent_mail.subject, '')
