1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
20 import unittest
.mock
as mock
28 from mediagoblin
.tests
.tools
import get_app
29 from mediagoblin
.tools
import common
, url
, translate
, mail
, text
, testing
31 testing
._activate
_testing
()
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
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
47 def test_send_email():
48 mail
._clear
_test
_inboxes
()
52 "sender@mediagoblin.example.org",
53 ["amanda@example.org", "akila@example.org"],
54 "Testing is so much fun!",
57 I hope you like unit tests JUST AS MUCH AS I DO!""")
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!
67 I hope you like unit tests JUST AS MUCH AS I DO!"""
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!
80 I hope you like unit tests JUST AS MUCH AS I DO!"""
83 def starttls_enabled_app(request
):
86 mgoblin_config
=pkg_resources
.resource_filename(
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
):
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 ^_^"
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'
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'
116 assert (url
.slugify(u
'\uacf5\uc6d0\uc5d0\uc11c \uc0b0\ucc45') ==
117 u
'gongweoneseo-sancaeg')
119 def test_locale_to_lower_upper():
121 Test cc.i18n.util.locale_to_lower_upper()
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'
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'
132 def test_locale_to_lower_lower():
134 Test cc.i18n.util.locale_to_lower_lower()
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'
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'
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")
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
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
163 assert p1_should
!= p2_should
, "Test broken, same translated string"
167 def test_html_cleaner():
169 result
= text
.clean_html(
171 '<img src="http://example.org/huge-purple-barney.png" /></p>\n'
175 '<p>Hi everybody! </p>\n'
179 # Remove evil javascript
180 result
= text
.clean_html(
181 '<p><a href="javascript:nasty_surprise">innocent link!</a></p>')
183 '<p><a href="">innocent link!</a></p>')