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)
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)
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)
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)
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)
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)
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)
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)
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'])
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)
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())
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())
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'])
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
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
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())
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())
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())