<commit_before>from entity.models import Entity, EntityRelationship
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django_dynamic_fixture import G, N
from mock import patch

from entity_emailer import tasks
from entity_emailer.models import Email, EmailType, EmailTemplate, Unsubscribed


class GetFromEmailAddressTest(TestCase):
    def test_default_from_email(self):
        # settings.DEFAULT_FROM_EMAIL is already set to <EMAIL>
        from_email = tasks.get_from_email_address()
        expected = '<EMAIL>'
        self.assertEqual(from_email, expected)

    def test_entity_emailer_from_email(self):
        with patch('django.conf.settings.ENTITY_EMAILER_FROM_EMAIL', new='<EMAIL>', create=True):
            from_email = tasks.get_from_email_address()
            expected = '<EMAIL>'
            self.assertEqual(from_email, expected)


class GetEmailAddressesTest(TestCase):
    def setUp(self):
        self.ct = ContentType.objects.get_for_model(Email)
        self.ct2 = ContentType.objects.get_for_model(Unsubscribed)
        self.super_entity = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct)
        self.sub_entity_1 = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct)
        self.sub_entity_2 = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct)
        self.sub_entity_3 = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct2)
        G(EntityRelationship, sub_entity=self.sub_entity_1, super_entity=self.super_entity)
        G(EntityRelationship, sub_entity=self.sub_entity_2, super_entity=self.super_entity)
        G(EntityRelationship, sub_entity=self.sub_entity_3, super_entity=self.super_entity)
        self.template = G(EmailTemplate, text_template='Hi!')

    def test_returns_sub_entities_emails(self):
        email = N(Email, send_to=self.super_entity, subentity_type=self.ct, template=self.template, context={})
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>', u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)

    def test_filters_other_entity_types(self):
        email = N(Email, send_to=self.super_entity, subentity_type=self.ct2, template=self.template, context={})
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)

    def test_returns_own_email(self):
        email = N(Email, send_to=self.super_entity, subentity_type=None, template=self.template, context={})
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)

    def test_unsubscription_works(self):
        test_email_type = G(EmailType, name='test_email')
        G(Unsubscribed, entity=self.sub_entity_1, unsubscribed_from=test_email_type)
        email = N(
            Email, send_to=self.super_entity, subentity_type=self.ct,
            email_type=test_email_type, template=self.template, context={}
        )
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)


class RenderTemplatesTest(TestCase):
    @patch('__builtin__.open')
    def test_text_path(self, open_mock):
        open_mock.return_value.__enter__.return_value.read.return_value = 'Hi {{ name }}'
        template = G(EmailTemplate, text_template_path='NotNothing')
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, 'Hi Mr. T')
        self.assertEqual(rendered_html, '')

    def test_text_textfield(self):
        template = G(EmailTemplate, text_template='Hi {{ name }}')
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, 'Hi Mr. T')
        self.assertEqual(rendered_html, '')

    @patch('__builtin__.open')
    def test_html_path(self, open_mock):
        temp = '<html>Hi {{ name }}</html>'
        open_mock.return_value.__enter__.return_value.read.return_value = temp
        template = G(EmailTemplate, html_template_path='NotNothing')
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, '')
        self.assertEqual(rendered_html, '<html>Hi Mr. T</html>')

    def test_test_textfield(self):
        temp = '<html>Hi {{ name }}</html>'
        template = G(EmailTemplate, html_template=temp)
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, '')
        self.assertEqual(rendered_html, '<html>Hi Mr. T</html>')
<commit_msg>Create Email Objects tests.<commit_after>from entity.models import Entity, EntityRelationship
from django.contrib.contenttypes.models import ContentType
from django.core import mail
from django.test import TestCase
from django_dynamic_fixture import G, N
from mock import patch

from entity_emailer import tasks
from entity_emailer.models import Email, EmailType, EmailTemplate, Unsubscribed


class CreateEmailObjectTest(TestCase):
    def test_no_html(self):
        email = tasks.create_email_object(
            ['<EMAIL>'], '<EMAIL>', 'Subject', 'Email Body.', ''
        )
        email.send()
        self.assertEqual(mail.outbox[0].attachments, [])

    def test_html(self):
        email = tasks.create_email_object(
            ['<EMAIL>'], '<EMAIL>', 'Subject', 'Email Body.', '<html>A</html>'
        )
        email.send()
        expected_alternatives = [('<html>A</html>', 'text/html')]
        self.assertEqual(mail.outbox[0].alternatives, expected_alternatives)


class GetFromEmailAddressTest(TestCase):
    def test_default_from_email(self):
        # settings.DEFAULT_FROM_EMAIL is already set to <EMAIL>
        from_email = tasks.get_from_email_address()
        expected = '<EMAIL>'
        self.assertEqual(from_email, expected)

    def test_entity_emailer_from_email(self):
        with patch('django.conf.settings.ENTITY_EMAILER_FROM_EMAIL', new='<EMAIL>', create=True):
            from_email = tasks.get_from_email_address()
            expected = '<EMAIL>'
            self.assertEqual(from_email, expected)


class GetEmailAddressesTest(TestCase):
    def setUp(self):
        self.ct = ContentType.objects.get_for_model(Email)
        self.ct2 = ContentType.objects.get_for_model(Unsubscribed)
        self.super_entity = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct)
        self.sub_entity_1 = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct)
        self.sub_entity_2 = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct)
        self.sub_entity_3 = G(Entity, entity_meta={'email': '<EMAIL>'}, entity_type=self.ct2)
        G(EntityRelationship, sub_entity=self.sub_entity_1, super_entity=self.super_entity)
        G(EntityRelationship, sub_entity=self.sub_entity_2, super_entity=self.super_entity)
        G(EntityRelationship, sub_entity=self.sub_entity_3, super_entity=self.super_entity)
        self.template = G(EmailTemplate, text_template='Hi!')

    def test_returns_sub_entities_emails(self):
        email = N(Email, send_to=self.super_entity, subentity_type=self.ct, template=self.template, context={})
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>', u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)

    def test_filters_other_entity_types(self):
        email = N(Email, send_to=self.super_entity, subentity_type=self.ct2, template=self.template, context={})
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)

    def test_returns_own_email(self):
        email = N(Email, send_to=self.super_entity, subentity_type=None, template=self.template, context={})
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u'<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)

    def test_unsubscription_works(self):
        test_email_type = G(EmailType, name='test_email')
        G(Unsubscribed, entity=self.sub_entity_1, unsubscribed_from=test_email_type)
        email = N(
            Email, send_to=self.super_entity, subentity_type=self.ct,
            email_type=test_email_type, template=self.template, context={}
        )
        addresses = tasks.get_email_addresses(email)
        expected_addresses = {u't<EMAIL>'}
        self.assertEqual(set(addresses), expected_addresses)


class RenderTemplatesTest(TestCase):
    @patch('__builtin__.open')
    def test_text_path(self, open_mock):
        open_mock.return_value.__enter__.return_value.read.return_value = 'Hi {{ name }}'
        template = G(EmailTemplate, text_template_path='NotNothing')
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, 'Hi Mr. T')
        self.assertEqual(rendered_html, '')

    def test_text_textfield(self):
        template = G(EmailTemplate, text_template='Hi {{ name }}')
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, 'Hi Mr. T')
        self.assertEqual(rendered_html, '')

    @patch('__builtin__.open')
    def test_html_path(self, open_mock):
        temp = '<html>Hi {{ name }}</html>'
        open_mock.return_value.__enter__.return_value.read.return_value = temp
        template = G(EmailTemplate, html_template_path='NotNothing')
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, '')
        self.assertEqual(rendered_html, '<html>Hi Mr. T</html>')

    def test_test_textfield(self):
        temp = '<html>Hi {{ name }}</html>'
        template = G(EmailTemplate, html_template=temp)
        email = N(Email, template=template, context={'name': 'Mr. T'})
        rendered_text, rendered_html = tasks.render_templates(email)
        self.assertEqual(rendered_text, '')
        self.assertEqual(rendered_html, '<html>Hi Mr. T</html>')

{'fraction_non_alphanumeric': 0.12229203788802401, 'fraction_numerical': 0.002063209228172184, 'mean_word_length': 3.937037037037037, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, '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': '4021242', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3257, 'n_words': 560}