Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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'])
Пример #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)
Пример #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())
Пример #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())
Пример #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'])
Пример #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
Пример #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
Пример #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
Пример #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())
Пример #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())
Пример #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())
Пример #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())