Example #1
0
    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())
Example #2
0
    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())
Example #3
0
    def test_set_mail_subject_with_nothing(self):
        """
        set_mail_subject(message) returns the string in message['mail_subject']
        when it is present, otherwise it returns 'Form Submission'
        """

        # Build test environment
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': '*****@*****.**',
                                       'redirect': 'http://www.example.com',
                                       'last_name': '',
                                       'token': conf.TOKEN})
        env = builder.get_environ()
        req = Request(env)
        # Create message from request and call set_mail_subject()
        message = handler.create_msg(req)
        subject = handler.set_mail_subject(message)
        self.assertEqual(subject, 'Form Submission')
Example #4
0
    def test_set_mail_subject_with_subj_key_missing(self):
        """
        set_mail_subject(message) returns the default string 'Form Submission'
        when no configuration fields are available
        """

        # Build test environment
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': '*****@*****.**',
                                       'redirect': 'http://www.example.com',
                                       'last_name': '',
                                       'mail_subject_key': 'project',
                                       'token': conf.TOKEN})
        env = builder.get_environ()
        req = Request(env)
        # Create message from request and call set_mail_subject()
        message = handler.create_msg(req)
        self.assertEqual(handler.set_mail_subject(message), 'Form Submission')
Example #5
0
    def test_set_mail_subject_with_subj_prefix(self):
        """
        set_mail_subject(message) returns the string
        "message['mail_subject_prefix']" when it is the only field available
        """

        # Build test environment
        builder = EnvironBuilder(method='POST',
                                 data={'name': 'Valid Guy',
                                       'email': '*****@*****.**',
                                       'redirect': 'http://www.example.com',
                                       'last_name': '',
                                       'mail_subject_prefix': 'Hosting',
                                       'token': conf.TOKEN})
        env = builder.get_environ()
        req = Request(env)
        # Create message from request and call set_mail_subject()
        message = handler.create_msg(req)
        self.assertEqual(handler.set_mail_subject(message), 'Hosting')