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