def test_send_email_default(self, mock_validate_email): """ Tests that the form is sent to the correct default address when the 'send_to' field is set to an empty string. Returns true if the form has been sent to [email protected] Errors out if unsuccessful """ builder = EnvironBuilder(method='POST', data={'name': 'Valid Guy', 'email': '*****@*****.**', 'send_to': '', 'last_name': '', 'token': conf.TOKEN, 'redirect': 'http://www.example.com'}) env = builder.get_environ() req = Request(env) # Construct message for assertion msg = handler.create_msg(req) msg_send = MIMEText(str(msg)) msg_subj = handler.set_mail_subject(msg) msg_send['Subject'] = msg_subj msg_send['To'] = conf.EMAIL['default'] # Mock sendmail function smtplib.SMTP.sendmail = Mock('smtplib.SMTP.sendmail') # Call send_email and assert sendmail was correctly called handler.send_email(msg, msg_subj, send_to_email='default') smtplib.SMTP.sendmail.assert_called_with(conf.FROM, conf.EMAIL['default'], msg_send.as_string())
def test_send_email(self): """ Tests send_email send_email returns True when it successfully sends an email to a default address and errors out when unsuccessful. """ # Build test environment builder = EnvironBuilder(method='POST', data={'name': 'Valid Guy', 'email': '*****@*****.**', 'last_name': '', 'token': conf.TOKEN, 'redirect': 'http://www.example.com'}) env = builder.get_environ() req = Request(env) # Construct message for assertion msg = handler.create_msg(req) msg_send = MIMEText(str(msg)) msg_subj = handler.set_mail_subject(msg) msg_send['Subject'] = msg_subj msg_send['To'] = conf.EMAIL['default'] # Mock sendmail function so it doesn't send an actual email smtplib.SMTP.sendmail = Mock('smtplib.SMTP.sendmail') # Call send_email and assert sendmail was called correctly handler.create_app() handler.send_email(msg, msg_subj) smtplib.SMTP.sendmail.assert_called_with(conf.FROM, conf.EMAIL['default'], msg_send.as_string())