Add new tests to check for email failures

This commit is contained in:
Baptiste Jonglez 2020-05-18 13:33:57 +02:00
parent 3d7e6b212e
commit 6fc9ddeaee

View file

@ -5,6 +5,8 @@ import io
import json
import os
import re
import smtplib
import socket
from time import sleep
import unittest
from unittest.mock import MagicMock, patch
@ -2288,6 +2290,78 @@ class ModelsTestCase(IhatemoneyTestCase):
self.assertEqual(bill.pay_each(), pay_each_expected)
class EmailFailureTestCase(IhatemoneyTestCase):
def test_creation_email_failure_smtp(self):
self.login("raclette")
with patch.object(
self.app.mail, "send", MagicMock(side_effect=smtplib.SMTPException)
):
resp = self.post_project("raclette", follow_redirects=True)
# Check that an error message is displayed
self.assertIn(
"We tried to send you an reminder email, but there was an error",
resp.data.decode("utf-8"),
)
# Check that we are redirected anyway
self.assertIn(
'You probably want to <a href="/raclette/members/add"',
resp.data.decode("utf-8"),
)
def test_creation_email_failure_socket(self):
self.login("raclette")
with patch.object(self.app.mail, "send", MagicMock(side_effect=socket.error)):
resp = self.post_project("raclette", follow_redirects=True)
# Check that an error message is displayed
self.assertIn(
"We tried to send you an reminder email, but there was an error",
resp.data.decode("utf-8"),
)
# Check that we are redirected anyway
self.assertIn(
'You probably want to <a href="/raclette/members/add"',
resp.data.decode("utf-8"),
)
def test_password_reset_email_failure(self):
self.create_project("raclette")
for exception in (smtplib.SMTPException, socket.error):
with patch.object(self.app.mail, "send", MagicMock(side_effect=exception)):
resp = self.client.post(
"/password-reminder", data={"id": "raclette"}, follow_redirects=True
)
# Check that an error message is displayed
self.assertIn(
"there was an error while sending you an email",
resp.data.decode("utf-8"),
)
# Check that we were not redirected to the success page
self.assertNotIn(
"A link to reset your password has been sent to you",
resp.data.decode("utf-8"),
)
def test_invitation_email_failure(self):
self.login("raclette")
self.post_project("raclette")
for exception in (smtplib.SMTPException, socket.error):
with patch.object(self.app.mail, "send", MagicMock(side_effect=exception)):
resp = self.client.post(
"/raclette/invite",
data={"emails": "toto@notmyidea.org"},
follow_redirects=True,
)
# Check that an error message is displayed
self.assertIn(
"there was an error while trying to send the invitation emails",
resp.data.decode("utf-8"),
)
# Check that we are still on the same page (no redirection)
self.assertIn(
"Invite people to join this project", resp.data.decode("utf-8"),
)
def em_surround(string, regex_escape=False):
if regex_escape:
return r'<em class="font-italic">%s<\/em>' % string