<commit_before>from django.core import mail
from django.core.exceptions import ValidationError
from django.template import TemplateDoesNotExist

from django.test import TestCase
from django.test.utils import override_settings

from ..models import Email, STATUS, PRIORITY, EmailTemplate
from ..utils import send_mail, send_queued_mail, get_email_template
from ..validators import validate_email_with_name


@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
class UtilsTest(TestCase):

    def test_mail_status(self):
        """
        Check that send_mail assigns the right status field to Email instances
        """
        send_mail('subject', 'message', '<EMAIL>', ['<EMAIL>'],
                  priority=PRIORITY.medium)
        email = Email.objects.latest('id')
        self.assertEqual(email.status, STATUS.queued)

        # Emails sent with "now" priority don't get sent right away
        send_mail('subject', 'message', '<EMAIL>', ['<EMAIL>'],
                  priority=PRIORITY.now)
        email = Email.objects.latest('id')
        self.assertEqual(email.status, STATUS.sent)

    def test_send_queued_mail(self):
        """
        Check that only queued messages are sent.
        """
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Test', message='Message', status=STATUS.sent)
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Test', message='Message', status=STATUS.failed)
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Test', message='Message', status=None)

        # This should be the only email that gets sent
        email = Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Queued', message='Message', status=STATUS.queued)
        send_queued_mail()
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Queued')

    def test_email_validator(self):
        # These should validate
        validate_email_with_name('<EMAIL>')
        validate_email_with_name('<NAME> <<EMAIL>>')
        Email.objects.create(to='<EMAIL>', from_email='Alice <<EMAIL>>',
            subject='Test', message='Message', status=STATUS.sent)

        # Should also support international domains
        validate_email_with_name('<NAME> <<EMAIL>>')

        # These should raise ValidationError
        self.assertRaises(ValidationError, validate_email_with_name, 'invalid_mail')
        self.assertRaises(ValidationError, validate_email_with_name, 'Alice <invalid_mail>')

    def test_get_template_email(self):
        template_name = 'customer/en/happy-holidays'
        self.assertRaises(EmailTemplate.DoesNotExist, get_email_template, template_name)
        email_template = EmailTemplate.objects.create(name=template_name, content='Happy Holiday!')
        self.assertEqual(email_template, get_email_template(template_name))<commit_msg>Added test for sending templated email<commit_after>from django.core import mail
from django.core.exceptions import ValidationError
from django.template import TemplateDoesNotExist

from django.test import TestCase
from django.test.utils import override_settings

from ..models import Email, STATUS, PRIORITY, EmailTemplate
from ..utils import send_mail, send_queued_mail, get_email_template, send_templated_mail
from ..validators import validate_email_with_name


@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
class UtilsTest(TestCase):

    def test_mail_status(self):
        """
        Check that send_mail assigns the right status field to Email instances
        """
        send_mail('subject', 'message', '<EMAIL>', ['<EMAIL>'],
                  priority=PRIORITY.medium)
        email = Email.objects.latest('id')
        self.assertEqual(email.status, STATUS.queued)

        # Emails sent with "now" priority don't get sent right away
        send_mail('subject', 'message', '<EMAIL>', ['<EMAIL>'],
                  priority=PRIORITY.now)
        email = Email.objects.latest('id')
        self.assertEqual(email.status, STATUS.sent)

    def test_send_queued_mail(self):
        """
        Check that only queued messages are sent.
        """
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Test', message='Message', status=STATUS.sent)
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Test', message='Message', status=STATUS.failed)
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Test', message='Message', status=None)

        # This should be the only email that gets sent
        Email.objects.create(to='<EMAIL>', from_email='<EMAIL>',
            subject='Queued', message='Message', status=STATUS.queued)
        send_queued_mail()
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Queued')

    def test_email_validator(self):
        # These should validate
        validate_email_with_name('<EMAIL>')
        validate_email_with_name('<NAME> <<EMAIL>>')
        Email.objects.create(to='<EMAIL>', from_email='Alice <<EMAIL>>',
            subject='Test', message='Message', status=STATUS.sent)

        # Should also support international domains
        validate_email_with_name('<NAME> <<EMAIL>>')

        # These should raise ValidationError
        self.assertRaises(ValidationError, validate_email_with_name, 'invalid_mail')
        self.assertRaises(ValidationError, validate_email_with_name, 'Alice <invalid_mail>')

    def test_get_template_email(self):
        template_name = 'customer/en/happy-holidays'
        self.assertRaises(EmailTemplate.DoesNotExist, get_email_template, template_name)
        email_template = EmailTemplate.objects.create(name=template_name, content='Happy Holiday!')
        self.assertEqual(email_template, get_email_template(template_name))

    def test_send_templated_email(self):
        template_name = 'customer/en/happy-holidays'
        EmailTemplate.objects.create(name=template_name,
            content='Hi {{name}}', html_content='<p>Hi {{name}}</p>',
            subject='Happy Holidays!')
        send_templated_mail(template_name, '<EMAIL>',
            ['<EMAIL>', '<EMAIL>'],
            priority=PRIORITY.medium, context={'name': 'AwesomeBoy'})
        send_queued_mail()
        self.assertEqual(len(mail.outbox), 2)

        for email in mail.outbox:
            self.assertEqual(email.subject, 'Happy Holidays!')
            self.assertEqual(email.body, 'Hi AwesomeBoy')
            self.assertEqual(email.alternatives, [('<p>Hi AwesomeBoy</p>', 'text/html')])
{'fraction_non_alphanumeric': 0.12473919523099851, 'fraction_numerical': 0.0007451564828614009, 'mean_word_length': 3.5498305084745763, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5494506', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1912, 'n_words': 379}