Merge branch 'thumbnail', fixes #592.
[mediagoblin.git] / mediagoblin / tests / test_util.py
CommitLineData
cb8ea0fe 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
cb8ea0fe
CAW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
fa3f46d7
CAW
17try:
18 import mock
19except ImportError:
20 import unittest.mock as mock
4d4f6050 21import email
caee9aa2 22import socket
7ffd4cf4
JT
23import pytest
24import smtplib
25import pkg_resources
4d4f6050 26
e49b7e02
BP
27import six
28
7ffd4cf4 29from mediagoblin.tests.tools import get_app
caee9aa2 30from mediagoblin import mg_globals
152a3bfa 31from mediagoblin.tools import common, url, translate, mail, text, testing
cb8ea0fe 32
152a3bfa 33testing._activate_testing()
4d4f6050
CAW
34
35
cb8ea0fe
CAW
36def _import_component_testing_method(silly_string):
37 # Just for the sake of testing that our component importer works.
38 return u"'%s' is the silliest string I've ever seen" % silly_string
39
40
41def test_import_component():
152a3bfa 42 imported_func = common.import_component(
cb8ea0fe
CAW
43 'mediagoblin.tests.test_util:_import_component_testing_method')
44 result = imported_func('hooobaladoobala')
45 expected = u"'hooobaladoobala' is the silliest string I've ever seen"
46 assert result == expected
4d4f6050
CAW
47
48
49def test_send_email():
152a3bfa 50 mail._clear_test_inboxes()
4d4f6050
CAW
51
52 # send the email
152a3bfa 53 mail.send_email(
4d4f6050
CAW
54 "sender@mediagoblin.example.org",
55 ["amanda@example.org", "akila@example.org"],
56 "Testing is so much fun!",
57 """HAYYY GUYS!
58
59I hope you like unit tests JUST AS MUCH AS I DO!""")
60
61 # check the main inbox
152a3bfa
AW
62 assert len(mail.EMAIL_TEST_INBOX) == 1
63 message = mail.EMAIL_TEST_INBOX.pop()
4d4f6050
CAW
64 assert message['From'] == "sender@mediagoblin.example.org"
65 assert message['To'] == "amanda@example.org, akila@example.org"
66 assert message['Subject'] == "Testing is so much fun!"
a7e1d882 67 assert message.get_payload(decode=True) == b"""HAYYY GUYS!
4d4f6050
CAW
68
69I hope you like unit tests JUST AS MUCH AS I DO!"""
70
71 # Check everything that the FakeMhost.sendmail() method got is correct
152a3bfa
AW
72 assert len(mail.EMAIL_TEST_MBOX_INBOX) == 1
73 mbox_dict = mail.EMAIL_TEST_MBOX_INBOX.pop()
4d4f6050
CAW
74 assert mbox_dict['from'] == "sender@mediagoblin.example.org"
75 assert mbox_dict['to'] == ["amanda@example.org", "akila@example.org"]
76 mbox_message = email.message_from_string(mbox_dict['message'])
77 assert mbox_message['From'] == "sender@mediagoblin.example.org"
78 assert mbox_message['To'] == "amanda@example.org, akila@example.org"
79 assert mbox_message['Subject'] == "Testing is so much fun!"
a7e1d882 80 assert mbox_message.get_payload(decode=True) == b"""HAYYY GUYS!
4d4f6050
CAW
81
82I hope you like unit tests JUST AS MUCH AS I DO!"""
8b28bee4 83
7ffd4cf4
JT
84@pytest.fixture()
85def starttls_enabled_app(request):
86 return get_app(
87 request,
88 mgoblin_config=pkg_resources.resource_filename(
89 "mediagoblin.tests",
90 "starttls_config.ini"
91 )
92 )
93
94def test_email_force_starttls(starttls_enabled_app):
95 common.TESTS_ENABLED = False
96 SMTP = lambda *args, **kwargs: mail.FakeMhost()
97 with mock.patch('smtplib.SMTP', SMTP):
98 with pytest.raises(smtplib.SMTPException):
99 mail.send_email(
100 from_addr="notices@my.test.instance.com",
101 to_addrs="someone@someplace.com",
102 subject="Testing is so much fun!",
103 message_body="Ohai ^_^"
104 )
105
0546833c 106def test_slugify():
20884259
CAW
107 assert url.slugify(u'a walk in the park') == u'a-walk-in-the-park'
108 assert url.slugify(u'A Walk in the Park') == u'a-walk-in-the-park'
109 assert url.slugify(u'a walk in the park') == u'a-walk-in-the-park'
110 assert url.slugify(u'a walk in-the-park') == u'a-walk-in-the-park'
111 assert url.slugify(u'a w@lk in the park?') == u'a-w-lk-in-the-park'
112 assert url.slugify(u'a walk in the par\u0107') == u'a-walk-in-the-parc'
113 assert url.slugify(u'\u00E0\u0042\u00E7\u010F\u00EB\u0066') == u'abcdef'
2636dddf
BB
114 # Russian
115 assert url.slugify(u'\u043f\u0440\u043e\u0433\u0443\u043b\u043a\u0430 '
116 u'\u0432 \u043f\u0430\u0440\u043a\u0435') == u'progulka-v-parke'
117 # Korean
118 assert (url.slugify(u'\uacf5\uc6d0\uc5d0\uc11c \uc0b0\ucc45') ==
119 u'gongweoneseo-sancaeg')
8b28bee4
CAW
120
121def test_locale_to_lower_upper():
122 """
123 Test cc.i18n.util.locale_to_lower_upper()
124 """
ae3bc7fa
AW
125 assert translate.locale_to_lower_upper('en') == 'en'
126 assert translate.locale_to_lower_upper('en_US') == 'en_US'
127 assert translate.locale_to_lower_upper('en-us') == 'en_US'
8b28bee4
CAW
128
129 # crazy renditions. Useful?
ae3bc7fa
AW
130 assert translate.locale_to_lower_upper('en-US') == 'en_US'
131 assert translate.locale_to_lower_upper('en_us') == 'en_US'
8b28bee4
CAW
132
133
134def test_locale_to_lower_lower():
135 """
136 Test cc.i18n.util.locale_to_lower_lower()
137 """
ae3bc7fa
AW
138 assert translate.locale_to_lower_lower('en') == 'en'
139 assert translate.locale_to_lower_lower('en_US') == 'en-us'
140 assert translate.locale_to_lower_lower('en-us') == 'en-us'
8b28bee4
CAW
141
142 # crazy renditions. Useful?
ae3bc7fa
AW
143 assert translate.locale_to_lower_lower('en-US') == 'en-us'
144 assert translate.locale_to_lower_lower('en_us') == 'en-us'
a68ee555
CAW
145
146
d8919664
E
147def test_gettext_lazy_proxy():
148 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
149 from mediagoblin.tools.translate import pass_to_ugettext, set_thread_locale
150 proxy = _(u"Password")
151 orig = u"Password"
152
153 set_thread_locale("es")
e49b7e02 154 p1 = six.text_type(proxy)
d8919664
E
155 p1_should = pass_to_ugettext(orig)
156 assert p1_should != orig, "Test useless, string not translated"
157 assert p1 == p1_should
158
159 set_thread_locale("sv")
e49b7e02 160 p2 = six.text_type(proxy)
d8919664
E
161 p2_should = pass_to_ugettext(orig)
162 assert p2_should != orig, "Test broken, string not translated"
163 assert p2 == p2_should
164
165 assert p1_should != p2_should, "Test broken, same translated string"
166 assert p1 != p2
167
168
a68ee555
CAW
169def test_html_cleaner():
170 # Remove images
152a3bfa 171 result = text.clean_html(
a68ee555
CAW
172 '<p>Hi everybody! '
173 '<img src="http://example.org/huge-purple-barney.png" /></p>\n'
174 '<p>:)</p>')
175 assert result == (
176 '<div>'
177 '<p>Hi everybody! </p>\n'
178 '<p>:)</p>'
179 '</div>')
180
181 # Remove evil javascript
152a3bfa 182 result = text.clean_html(
a68ee555
CAW
183 '<p><a href="javascript:nasty_surprise">innocent link!</a></p>')
184 assert result == (
185 '<p><a href="">innocent link!</a></p>')
caee9aa2
JS
186
187
9d37bcd0
JS
188class TestMail(object):
189 """ Test mediagoblin's mail tool """
190 def test_no_mail_server(self):
191 """ Tests that no smtp server is available """
192 with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock:
193 smtp_mock.side_effect = socket.error
194 mg_globals.app_config = {
195 "email_debug_mode": False,
196 "email_smtp_use_ssl": False,
197 "email_smtp_host": "127.0.0.1",
198 "email_smtp_port": 0}
199 common.TESTS_ENABLED = False
200 mail.send_email("", "", "", "")
201
202 def test_no_smtp_host(self):
203 """ Empty email_smtp_host """
204 with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock:
205 smtp_mock.return_value.connect.side_effect = socket.error
206 mg_globals.app_config = {
207 "email_debug_mode": False,
208 "email_smtp_use_ssl": False,
209 "email_smtp_host": "",
210 "email_smtp_port": 0}
211 common.TESTS_ENABLED = False
212 mail.send_email("", "", "", "")