diff options
Diffstat (limited to 'mediagoblin/tests/test_util.py')
-rw-r--r-- | mediagoblin/tests/test_util.py | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index 9d9b1c16..02976405 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -14,8 +14,20 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +try: + import mock +except ImportError: + import unittest.mock as mock import email +import socket +import pytest +import smtplib +import pkg_resources +import six + +from mediagoblin.tests.tools import get_app +from mediagoblin import mg_globals from mediagoblin.tools import common, url, translate, mail, text, testing testing._activate_testing() @@ -52,7 +64,7 @@ I hope you like unit tests JUST AS MUCH AS I DO!""") assert message['From'] == "sender@mediagoblin.example.org" assert message['To'] == "amanda@example.org, akila@example.org" assert message['Subject'] == "Testing is so much fun!" - assert message.get_payload(decode=True) == """HAYYY GUYS! + assert message.get_payload(decode=True) == b"""HAYYY GUYS! I hope you like unit tests JUST AS MUCH AS I DO!""" @@ -65,10 +77,32 @@ I hope you like unit tests JUST AS MUCH AS I DO!""" assert mbox_message['From'] == "sender@mediagoblin.example.org" assert mbox_message['To'] == "amanda@example.org, akila@example.org" assert mbox_message['Subject'] == "Testing is so much fun!" - assert mbox_message.get_payload(decode=True) == """HAYYY GUYS! + assert mbox_message.get_payload(decode=True) == b"""HAYYY GUYS! I hope you like unit tests JUST AS MUCH AS I DO!""" +@pytest.fixture() +def starttls_enabled_app(request): + return get_app( + request, + mgoblin_config=pkg_resources.resource_filename( + "mediagoblin.tests", + "starttls_config.ini" + ) + ) + +def test_email_force_starttls(starttls_enabled_app): + common.TESTS_ENABLED = False + SMTP = lambda *args, **kwargs: mail.FakeMhost() + with mock.patch('smtplib.SMTP', SMTP): + with pytest.raises(smtplib.SMTPException): + mail.send_email( + from_addr="notices@my.test.instance.com", + to_addrs="someone@someplace.com", + subject="Testing is so much fun!", + message_body="Ohai ^_^" + ) + def test_slugify(): assert url.slugify(u'a walk in the park') == u'a-walk-in-the-park' assert url.slugify(u'A Walk in the Park') == u'a-walk-in-the-park' @@ -117,13 +151,13 @@ def test_gettext_lazy_proxy(): orig = u"Password" set_thread_locale("es") - p1 = unicode(proxy) + p1 = six.text_type(proxy) p1_should = pass_to_ugettext(orig) assert p1_should != orig, "Test useless, string not translated" assert p1 == p1_should set_thread_locale("sv") - p2 = unicode(proxy) + p2 = six.text_type(proxy) p2_should = pass_to_ugettext(orig) assert p2_should != orig, "Test broken, string not translated" assert p2 == p2_should @@ -149,3 +183,30 @@ def test_html_cleaner(): '<p><a href="javascript:nasty_surprise">innocent link!</a></p>') assert result == ( '<p><a href="">innocent link!</a></p>') + + +class TestMail(object): + """ Test mediagoblin's mail tool """ + def test_no_mail_server(self): + """ Tests that no smtp server is available """ + with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock: + smtp_mock.side_effect = socket.error + mg_globals.app_config = { + "email_debug_mode": False, + "email_smtp_use_ssl": False, + "email_smtp_host": "127.0.0.1", + "email_smtp_port": 0} + common.TESTS_ENABLED = False + mail.send_email("", "", "", "") + + def test_no_smtp_host(self): + """ Empty email_smtp_host """ + with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock: + smtp_mock.return_value.connect.side_effect = socket.error + mg_globals.app_config = { + "email_debug_mode": False, + "email_smtp_use_ssl": False, + "email_smtp_host": "", + "email_smtp_port": 0} + common.TESTS_ENABLED = False + mail.send_email("", "", "", "") |