<commit_before>from django.test import TestCase
from django.test import override_settings

from mailer.models import MailNotifier
from mailer.models import MailLog


# Ensure that the events exposed as choices for the
# data model match expectations
class EventChoicesTestCase(TestCase):
    def test_correct_events_exposed(self):
        from mailer.models import EVENT_CHOICES as event_choices
        expected_choices = (
            ('account_created_from_request', 'account_created_from_request'),
            ('account_request_received', 'account_request_received'),
        )
        self.assertEquals(event_choices,expected_choices)

# This test case covers MailNotifier functionality.
class MailNotifierTestCase(TestCase):
    def setUp(self):
        self.ctx = {
            'username':'testuser',
            'email':'<EMAIL>',
        }
        mn_dict = {
            'name':'account_request_received',
            'event':'account_request_received',
            'from_address':'<EMAIL>',
            'mailto':'<EMAIL>,{{ email }},,',
            'cc':'<EMAIL>,{{ email }},,',
            'bcc':'<EMAIL>,{{ email }},,',
            'subject':'Hi, {{ username }}!',
            'body':'You, {{ username }}, did it!!!!',
        }
        self.mn = MailNotifier.objects.create(**mn_dict)
    
    def test_make_body(self):
        body = self.mn.make_body(self.ctx)
        
        self.assertEquals(self.mn.body,'You, {{ username }}, did it!!!!')
        self.assertEquals(body,'You, testuser, did it!!!!')
        
        # Test with no context
        body = self.mn.make_body()
        self.assertEquals(body,'You, , did it!!!!')
    
    def test_make_subject(self):
        subj = self.mn.make_subject(self.ctx)
        
        self.assertEquals(self.mn.subject,'Hi, {{ username }}!')
        self.assertEquals(subj,'Hi, testuser!')
        
        # Test with no context
        subj = self.mn.make_subject()
        self.assertEquals(subj,'Hi, !')
    
    def test_make_mailto_list(self):
        mailto = self.mn.make_mailto_list(self.ctx)
        
        self.assertEquals(
            mailto,
            ['<EMAIL>','<EMAIL>']
        )
        self.assertEquals(self.mn.mailto,'<EMAIL>,{{ email }},,')
        
        # Test with no context
        mailto = self.mn.make_mailto_list()
        self.assertEquals(mailto,['<EMAIL>'])
    
    def test_make_cc_list(self):
        cc = self.mn.make_cc_list(self.ctx)
        
        self.assertEquals(
            cc,
            ['<EMAIL>','<EMAIL>']
        )
        self.assertEquals(self.mn.cc,'<EMAIL>,{{ email }},,')
        
        # Test with no context
        cc = self.mn.make_cc_list()
        self.assertEquals(cc,['<EMAIL>'])
    
    def test_make_bcc_list(self):
        bcc = self.mn.make_bcc_list(self.ctx)
        
        self.assertEquals(
            bcc,
            ['<EMAIL>','<EMAIL>']
        )
        self.assertEquals(self.mn.bcc,'<EMAIL>,{{ email }},,')
        
        # Test with no context
        bcc = self.mn.make_bcc_list()
        self.assertEquals(bcc,['<EMAIL>'])
    
    def test_make_email(self):
        email = self.mn.make_email(self.ctx)
        
        import django.core.mail.message
        self.assertIsInstance(email,django.core.mail.message.EmailMessage)
        
        self.assertEquals(email.subject,'Hi, testuser!')
        self.assertEquals(email.body,'You, testuser, did it!!!!')
        self.assertEquals(email.from_email,'<EMAIL>')
        self.assertEquals(email.to,['<EMAIL>','<EMAIL>'])
        self.assertEquals(email.cc,['<EMAIL>','<EMAIL>'])
        self.assertEquals(email.bcc,['<EMAIL>','<EMAIL>'])
    
    @override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
    def test_send(self):
        msg = self.mn.send(self.ctx)
        
        import django.core.mail.message
        self.assertIsInstance(msg,django.core.mail.message.SafeMIMEText)
        
        from django.core.mail import outbox
        self.assertEquals(len(outbox),1)
        
        ml = MailLog.objects.get()
        self.assertEquals(ml.recipient_emails,'<EMAIL>,<EMAIL>')<commit_msg>Completed test cases for mailer app.<commit_after>from django.test import TestCase
from django.test import override_settings


from mailer.models import MailNotifier
from mailer.models import MailLog
from mailer import receivers
from mailer.signals import *


# Ensure that the events exposed as choices for the
# data model match expectations
class EventChoicesTestCase(TestCase):
    def test_correct_events_exposed(self):
        from mailer.models import EVENT_CHOICES as event_choices
        expected_choices = (
            ('account_created_from_request', 'account_created_from_request'),
            ('account_request_received', 'account_request_received'),
        )
        self.assertEquals(event_choices,expected_choices)

# This test case covers MailNotifier functionality.
class MailNotifierTestCase(TestCase):
    def setUp(self):
        self.ctx = {
            'username':'testuser',
            'email':'<EMAIL>',
        }
        mn_dict = {
            'name':'account_request_received',
            'event':'account_request_received',
            'from_address':'<EMAIL>',
            'mailto':'<EMAIL>,{{ email }},,',
            'cc':'<EMAIL>,{{ email }},,',
            'bcc':'<EMAIL>,{{ email }},,',
            'subject':'Hi, {{ username }}!',
            'body':'You, {{ username }}, did it!!!!',
        }
        self.mn = MailNotifier.objects.create(**mn_dict)
    
    def test_make_body(self):
        body = self.mn.make_body(self.ctx)
        
        self.assertEquals(self.mn.body,'You, {{ username }}, did it!!!!')
        self.assertEquals(body,'You, testuser, did it!!!!')
        
        # Test with no context
        body = self.mn.make_body()
        self.assertEquals(body,'You, , did it!!!!')
    
    def test_make_subject(self):
        subj = self.mn.make_subject(self.ctx)
        
        self.assertEquals(self.mn.subject,'Hi, {{ username }}!')
        self.assertEquals(subj,'Hi, testuser!')
        
        # Test with no context
        subj = self.mn.make_subject()
        self.assertEquals(subj,'Hi, !')
    
    def test_make_mailto_list(self):
        mailto = self.mn.make_mailto_list(self.ctx)
        
        self.assertEquals(
            mailto,
            ['<EMAIL>','<EMAIL>']
        )
        self.assertEquals(self.mn.mailto,'<EMAIL>,{{ email }},,')
        
        # Test with no context
        mailto = self.mn.make_mailto_list()
        self.assertEquals(mailto,['<EMAIL>'])
    
    def test_make_cc_list(self):
        cc = self.mn.make_cc_list(self.ctx)
        
        self.assertEquals(
            cc,
            ['<EMAIL>','<EMAIL>']
        )
        self.assertEquals(self.mn.cc,'<EMAIL>,{{ email }},,')
        
        # Test with no context
        cc = self.mn.make_cc_list()
        self.assertEquals(cc,['<EMAIL>'])
    
    def test_make_bcc_list(self):
        bcc = self.mn.make_bcc_list(self.ctx)
        
        self.assertEquals(
            bcc,
            ['<EMAIL>','<EMAIL>']
        )
        self.assertEquals(self.mn.bcc,'<EMAIL>,{{ email }},,')
        
        # Test with no context
        bcc = self.mn.make_bcc_list()
        self.assertEquals(bcc,['<EMAIL>'])
    
    def test_make_email(self):
        email = self.mn.make_email(self.ctx)
        
        import django.core.mail.message
        self.assertIsInstance(email,django.core.mail.message.EmailMessage)
        
        self.assertEquals(email.subject,'Hi, testuser!')
        self.assertEquals(email.body,'You, testuser, did it!!!!')
        self.assertEquals(email.from_email,'<EMAIL>')
        self.assertEquals(email.to,['<EMAIL>','<EMAIL>'])
        self.assertEquals(email.cc,['<EMAIL>','<EMAIL>'])
        self.assertEquals(email.bcc,['<EMAIL>','<EMAIL>'])
    
    @override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
    def test_send(self):
        msg = self.mn.send(self.ctx)
        
        import django.core.mail.message
        self.assertIsInstance(msg,django.core.mail.message.SafeMIMEText)
        
        from django.core.mail import outbox
        self.assertEquals(len(outbox),1)
        
        ml = MailLog.objects.get()
        self.assertEquals(ml.recipient_emails,'<EMAIL>,<EMAIL>')

class ARReceivedTestCase(TestCase):
    def setUp(self):
        self.ctx = {
            'username':'testuser',
            'email':'<EMAIL>',
        }
        mn1_dict = {
            'name':'account_request_received',
            'event':'account_request_received',
            'from_address':'<EMAIL>',
            'mailto':"<EMAIL>,{{ account_request.email }},,",
            'cc':"<EMAIL>,{{ account_request.email }},,",
            'bcc':"<EMAIL>,{{ account_request.email }},,",
            'subject':"Hi, {{ account_request.username }}!",
            'body':"You, {{ account_request.username }}, did it!!!!",
        }
        mn2_dict = {
            'name':'account_request_received2',
            'event':'account_request_received',
            'from_address':'<EMAIL>',
            'mailto':"<EMAIL>,{{ account_request.email }},,",
            'cc':"<EMAIL>,{{ account_request.email }},,",
            'bcc':"<EMAIL>,{{ account_request.email }},,",
            'subject':"Hi, {{ account_request.username }}!",
            'body':"You, {{ account_request.username }}, did it!!!!",
        }
        self.mn1 = MailNotifier.objects.create(**mn1_dict)
        self.mn2 = MailNotifier.objects.create(**mn2_dict)
    
    def test_account_requested_received(self):
        account_request_received.send(
            sender='AccountRequest',
            account_request=self.ctx
        )
        
        from django.core.mail import outbox
        self.assertEquals(len(outbox),2)
        self.assertEquals(
            outbox[0].body,
            self.mn1.make_body({'account_request':self.ctx})
        )
        self.assertEquals(
            outbox[1].body,
            self.mn2.make_body({'account_request':self.ctx})
        )

class ARApprovedTestCase(TestCase):
    def setUp(self):
        self.ctx = {
            'username':'testuser',
            'email':'<EMAIL>',
        }
        mn1_dict = {
            'name':'account_created_from_request',
            'event':'account_created_from_request',
            'from_address':'<EMAIL>',
            'mailto':"<EMAIL>,{{ account.email }},,",
            'cc':"<EMAIL>,{{ account.email }},,",
            'bcc':"<EMAIL>,{{ account.email }},,",
            'subject':"Hi, {{ account.username }}!",
            'body':"You, {{ account.username }}, did it!!!!",
        }
        mn2_dict = {
            'name':'account_created_from_request2',
            'event':'account_created_from_request',
            'from_address':'<EMAIL>',
            'mailto':"<EMAIL>,{{ account.email }},,",
            'cc':"<EMAIL>,{{ account.email }},,",
            'bcc':"<EMAIL>,{{ account.email }},,",
            'subject':"Hi, {{ account.username }}!",
            'body':"You, {{ account.username }}, did it!!!!",
        }
        self.mn1 = MailNotifier.objects.create(**mn1_dict)
        self.mn2 = MailNotifier.objects.create(**mn2_dict)
    
    def test_account_requested_received(self):
        account_created_from_request.send(
            sender='RcLdapUser',
            account=self.ctx
        )
        
        from django.core.mail import outbox
        self.assertEquals(len(outbox),2)
        self.assertEquals(
            outbox[0].body,
            self.mn1.make_body({'account':self.ctx})
        )
        self.assertEquals(
            outbox[1].body,
            self.mn2.make_body({'account':self.ctx})
        )
{'fraction_non_alphanumeric': 0.1520999829960891, 'fraction_numerical': 0.0022105084169358954, 'mean_word_length': 2.2503453992815694, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, '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': '7281415', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3604, 'n_words': 515}