Exemplo n.º 1
0
 def _test_required_field(self, field_name):
     u"""
     Check that the form does not validate without a given field.
     """
     del self.form_data[field_name]
     form = BaseContactForm(self.form_data)
     self.assertFalse(form.is_valid())
     self.assertIn(field_name, form.errors)
Exemplo n.º 2
0
 def _test_required_field(self, field_name):
     u"""
     Check that the form does not validate without a given field.
     """
     del self.form_data[field_name]
     form = BaseContactForm(self.form_data)
     self.assertFalse(form.is_valid())
     self.assertIn(field_name, form.errors)
Exemplo n.º 3
0
 def test_get_context(self):
     u"""
     get_context() returns a copy of form's cleaned_data.
     """
     form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
     self.assertTrue(form.is_valid())
     context = form.get_context()
     self.assertEqual(context, form.cleaned_data)
     self.assertFalse(context is form.cleaned_data)
Exemplo n.º 4
0
 def test_get_context(self):
     u"""
     get_context() returns a copy of form's cleaned_data.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     context = form.get_context()
     self.assertEqual(context, form.cleaned_data)
     self.assertFalse(context is form.cleaned_data)
Exemplo n.º 5
0
 def test_get_context(self):
     u"""
     get_context() returns a copy of form's cleaned_data.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     context = form.get_context()
     self.assertEqual(context, form.cleaned_data)
     self.assertFalse(context is form.cleaned_data)
Exemplo n.º 6
0
 def test_save_smtp_error(self):
     """
     If the email backend raised an error, the message is not sent.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     with patch('django.core.mail.EmailMessage') as mock_message:
         mock_message.return_value.send.side_effect = SMTPException
         result = form.save()
         self.assertFalse(result)
Exemplo n.º 7
0
 def test_save_smtp_error(self):
     u"""
     If the email backend raised an error, the message is not sent.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     with patch.object(mail.EmailMessage, "send", side_effect=SMTPException):
         result = form.save()
         self.assertFalse(result)
         self.assertEqual(len(mail.outbox), 0)
Exemplo n.º 8
0
 def test_save(self):
     u"""
     A call to save() on a valid form sends the message.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     result = form.save()
     self.assertTrue(result)
     self.assertEqual(len(mail.outbox), 1)
     self.assertIn(self.form_data['subject'], mail.outbox[0].subject)
Exemplo n.º 9
0
 def test_save(self):
     u"""
     A call to save() on a valid form sends the message.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     result = form.save()
     self.assertTrue(result)
     self.assertEqual(len(mail.outbox), 1)
     self.assertIn(self.form_data["subject"], mail.outbox[0].subject)
Exemplo n.º 10
0
    def test_save(self):
        u"""
        A call to save() on a valid form sends the message.
        """
        form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')

        self.assertTrue(form.is_valid())
        result = form.save()
        self.assertTrue(result)
        self.assertEqual(len(mail.outbox), 1)
        self.assertIn(self.form_data['subject'], mail.outbox[0].subject)
Exemplo n.º 11
0
 def test_save(self):
     """
     A call to save() on a valid form sends the message.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     with patch('django.core.mail.EmailMessage') as mock_message:
         mock_message.return_value.send.return_value = True
         result = form.save()
         self.assertTrue(result)
         args, kwargs = mock_message.call_args
         self.assertTrue(self.form_data['subject'] in kwargs['subject'])
Exemplo n.º 12
0
 def test_send(self):
     u"""
     send() is deprecated, but still sends the message.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
     with warnings.catch_warnings(record=True) as warns:
         warnings.filterwarnings("always", category=DeprecationWarning)
         result = form.send()
         self.assertEqual(len(warns), 1)
     self.assertTrue(result)
     self.assertEqual(len(mail.outbox), 1)
     self.assertIn(self.form_data['subject'], mail.outbox[0].subject)
Exemplo n.º 13
0
 def test_init_attr_override(self):
     u"""
     Attributes can be overridden on __init__()
     """
     overrides = {
         "subject_intro": "New subject style: ",
         "from_email": "*****@*****.**",
         "email_recipients": ["*****@*****.**"],
     }
     form = BaseContactForm(self.form_data, **overrides)
     form.is_valid()
     form.save()
     self.assertIn(overrides["subject_intro"], mail.outbox[0].subject)
     self.assertIn(overrides["from_email"], mail.outbox[0].from_email)
     self.assertIn(overrides["email_recipients"][0], mail.outbox[0].recipients())
Exemplo n.º 14
0
 def test_init_attr_override(self):
     """
     Attributes can be overridden on __init__()
     """
     overrides = {
         'subject_intro': 'New subject style: ',
         'from_email': '*****@*****.**',
         'email_recipients': ['*****@*****.**'],
     }
     form = BaseContactForm(self.form_data, **overrides)
     form.is_valid()
     form.save()
     self.assertIn(overrides['subject_intro'], mail.outbox[0].subject)
     self.assertIn(overrides['from_email'], mail.outbox[0].from_email)
     self.assertIn(overrides['email_recipients'][0], mail.outbox[0].recipients())
Exemplo n.º 15
0
 def test_init_attr_override(self):
     """
     Attributes can be overridden on __init__()
     """
     overrides = {
         'subject_intro': 'New subject style: ',
         'from_email': '*****@*****.**',
         'email_recipients': ['*****@*****.**'],
     }
     form = BaseContactForm(self.form_data, **overrides)
     form.is_valid()
     with patch('django.core.mail.EmailMessage') as mock_message:
         mock_message.return_value.send.return_value = True
         form.save()
         args, kwargs = mock_message.call_args
         self.assertTrue(overrides['subject_intro'] in kwargs['subject'])
         self.assertTrue(overrides['from_email'] in kwargs['from_email'])
         self.assertTrue(overrides['email_recipients'][0] in kwargs['to'])
Exemplo n.º 16
0
    def test_save_smtp_error(self):
        u"""
        If the email backend raised an error, the message is not sent.
        """
        form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
        self.assertTrue(form.is_valid())
        old_send_mail = mail.EmailMessage

        def new_send_mail(*args, **kwargs):
            raise SMTPException

        try:
            mail.EmailMessage = new_send_mail
            result = form.save()
            self.assertFalse(result)
            self.assertEqual(len(mail.outbox), 0)
        finally:
            mail.EmailMessage = old_send_mail
Exemplo n.º 17
0
    def test_save_smtp_error(self):
        u"""
        If the email backend raised an error, the message is not sent.
        """
        form = BaseContactForm(self.form_data)
        self.assertTrue(form.is_valid())
        old_send_mail = mail.send_mail

        def new_send_mail(*args):
            raise SMTPException

        try:
            mail.send_mail = new_send_mail
            result = form.save()
            self.assertFalse(result)
            self.assertEqual(len(mail.outbox), 0)
        finally:
            mail.send_mail = old_send_mail
Exemplo n.º 18
0
    def test_save_smtp_error(self):
        u"""
        If the email backend raised an error, the message is not sent.
        """
        form = BaseContactForm(self.form_data)
        self.assertTrue(form.is_valid())
        old_send_mail = mail.send_mail

        def new_send_mail(*args):
            raise SMTPException

        try:
            mail.send_mail = new_send_mail
            result = form.save()
            self.assertFalse(result)
            self.assertEqual(len(mail.outbox), 0)
        finally:
            mail.send_mail = old_send_mail
Exemplo n.º 19
0
 def test_init_attr_override(self):
     u"""
     Attributes can be overridden on __init__()
     """
     overrides = {
         'subject_intro': 'New subject style: ',
         'from_email': '*****@*****.**',
         'email_recipients': ['*****@*****.**'],
     }
     form = BaseContactForm(self.form_data, **overrides)
     form.is_valid()
     form.save()
     self.assertIn(overrides['subject_intro'], mail.outbox[0].subject)
     self.assertIn(overrides['from_email'], mail.outbox[0].from_email)
     self.assertIn(overrides['email_recipients'][0],
                   mail.outbox[0].recipients())
Exemplo n.º 20
0
 def test_all_fields_valid(self):
     u"""
     When all required fields are supplied, the form is valid.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())
Exemplo n.º 21
0
 def test_all_fields_valid(self):
     u"""
     When all required fields are supplied, the form is valid.
     """
     form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
     self.assertTrue(form.is_valid())
Exemplo n.º 22
0
 def test_all_fields_valid(self):
     u"""
     When all required fields are supplied, the form is valid.
     """
     form = BaseContactForm(self.form_data)
     self.assertTrue(form.is_valid())