Developer Journal: 15 May 2020

Posted on Fri 15 May 2020 in dev-journal

Today I wanted to write a function to send me an email when someone registers for conveyor.dev

To do this I wrote the following test:

@patch('routes.send_reg_notification_email')
@patch('stripe.Customer.create')
@patch('stripe.Subscription.create')
def test_notification_email_is_sent(self, mock_sub, mock_cust, mock_email):
    mock_cust.return_value = stripe_customer
    mock_sub.return_value = stripe_subscription
    self.client.post("/auth/register", data=self.form)
    mock_email.assert_called_once()

Then I wrote the send_reg_notification_email function:

def send_reg_notification_email(user_email):
    body = f"You have a new customer! Welcome them via {user_email}"
    try:
        send_email('You have a new customer!',
                   sender=current_app.config["MAIL_USERNAME"],
                   recipients=["[email protected]"],
                   text_body=body,
                   html_body=body)
    except Exception as e:
        current_app.logger.error('Failed to send email to %s Error: %s',
                                 send_to, e)