Added documentation for the batchaddmedia gmg tool to the mediagoblin docs.
[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
4d4f6050
CAW
17import email
18
152a3bfa 19from mediagoblin.tools import common, url, translate, mail, text, testing
cb8ea0fe 20
152a3bfa 21testing._activate_testing()
4d4f6050
CAW
22
23
cb8ea0fe
CAW
24def _import_component_testing_method(silly_string):
25 # Just for the sake of testing that our component importer works.
26 return u"'%s' is the silliest string I've ever seen" % silly_string
27
28
29def test_import_component():
152a3bfa 30 imported_func = common.import_component(
cb8ea0fe
CAW
31 'mediagoblin.tests.test_util:_import_component_testing_method')
32 result = imported_func('hooobaladoobala')
33 expected = u"'hooobaladoobala' is the silliest string I've ever seen"
34 assert result == expected
4d4f6050
CAW
35
36
37def test_send_email():
152a3bfa 38 mail._clear_test_inboxes()
4d4f6050
CAW
39
40 # send the email
152a3bfa 41 mail.send_email(
4d4f6050
CAW
42 "sender@mediagoblin.example.org",
43 ["amanda@example.org", "akila@example.org"],
44 "Testing is so much fun!",
45 """HAYYY GUYS!
46
47I hope you like unit tests JUST AS MUCH AS I DO!""")
48
49 # check the main inbox
152a3bfa
AW
50 assert len(mail.EMAIL_TEST_INBOX) == 1
51 message = mail.EMAIL_TEST_INBOX.pop()
4d4f6050
CAW
52 assert message['From'] == "sender@mediagoblin.example.org"
53 assert message['To'] == "amanda@example.org, akila@example.org"
54 assert message['Subject'] == "Testing is so much fun!"
55 assert message.get_payload(decode=True) == """HAYYY GUYS!
56
57I hope you like unit tests JUST AS MUCH AS I DO!"""
58
59 # Check everything that the FakeMhost.sendmail() method got is correct
152a3bfa
AW
60 assert len(mail.EMAIL_TEST_MBOX_INBOX) == 1
61 mbox_dict = mail.EMAIL_TEST_MBOX_INBOX.pop()
4d4f6050
CAW
62 assert mbox_dict['from'] == "sender@mediagoblin.example.org"
63 assert mbox_dict['to'] == ["amanda@example.org", "akila@example.org"]
64 mbox_message = email.message_from_string(mbox_dict['message'])
65 assert mbox_message['From'] == "sender@mediagoblin.example.org"
66 assert mbox_message['To'] == "amanda@example.org, akila@example.org"
67 assert mbox_message['Subject'] == "Testing is so much fun!"
68 assert mbox_message.get_payload(decode=True) == """HAYYY GUYS!
69
70I hope you like unit tests JUST AS MUCH AS I DO!"""
8b28bee4 71
0546833c 72def test_slugify():
20884259
CAW
73 assert url.slugify(u'a walk in the park') == u'a-walk-in-the-park'
74 assert url.slugify(u'A Walk in the Park') == u'a-walk-in-the-park'
75 assert url.slugify(u'a walk in the park') == u'a-walk-in-the-park'
76 assert url.slugify(u'a walk in-the-park') == u'a-walk-in-the-park'
77 assert url.slugify(u'a w@lk in the park?') == u'a-w-lk-in-the-park'
78 assert url.slugify(u'a walk in the par\u0107') == u'a-walk-in-the-parc'
79 assert url.slugify(u'\u00E0\u0042\u00E7\u010F\u00EB\u0066') == u'abcdef'
2636dddf
BB
80 # Russian
81 assert url.slugify(u'\u043f\u0440\u043e\u0433\u0443\u043b\u043a\u0430 '
82 u'\u0432 \u043f\u0430\u0440\u043a\u0435') == u'progulka-v-parke'
83 # Korean
84 assert (url.slugify(u'\uacf5\uc6d0\uc5d0\uc11c \uc0b0\ucc45') ==
85 u'gongweoneseo-sancaeg')
8b28bee4
CAW
86
87def test_locale_to_lower_upper():
88 """
89 Test cc.i18n.util.locale_to_lower_upper()
90 """
ae3bc7fa
AW
91 assert translate.locale_to_lower_upper('en') == 'en'
92 assert translate.locale_to_lower_upper('en_US') == 'en_US'
93 assert translate.locale_to_lower_upper('en-us') == 'en_US'
8b28bee4
CAW
94
95 # crazy renditions. Useful?
ae3bc7fa
AW
96 assert translate.locale_to_lower_upper('en-US') == 'en_US'
97 assert translate.locale_to_lower_upper('en_us') == 'en_US'
8b28bee4
CAW
98
99
100def test_locale_to_lower_lower():
101 """
102 Test cc.i18n.util.locale_to_lower_lower()
103 """
ae3bc7fa
AW
104 assert translate.locale_to_lower_lower('en') == 'en'
105 assert translate.locale_to_lower_lower('en_US') == 'en-us'
106 assert translate.locale_to_lower_lower('en-us') == 'en-us'
8b28bee4
CAW
107
108 # crazy renditions. Useful?
ae3bc7fa
AW
109 assert translate.locale_to_lower_lower('en-US') == 'en-us'
110 assert translate.locale_to_lower_lower('en_us') == 'en-us'
a68ee555
CAW
111
112
d8919664
E
113def test_gettext_lazy_proxy():
114 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
115 from mediagoblin.tools.translate import pass_to_ugettext, set_thread_locale
116 proxy = _(u"Password")
117 orig = u"Password"
118
119 set_thread_locale("es")
120 p1 = unicode(proxy)
121 p1_should = pass_to_ugettext(orig)
122 assert p1_should != orig, "Test useless, string not translated"
123 assert p1 == p1_should
124
125 set_thread_locale("sv")
126 p2 = unicode(proxy)
127 p2_should = pass_to_ugettext(orig)
128 assert p2_should != orig, "Test broken, string not translated"
129 assert p2 == p2_should
130
131 assert p1_should != p2_should, "Test broken, same translated string"
132 assert p1 != p2
133
134
a68ee555
CAW
135def test_html_cleaner():
136 # Remove images
152a3bfa 137 result = text.clean_html(
a68ee555
CAW
138 '<p>Hi everybody! '
139 '<img src="http://example.org/huge-purple-barney.png" /></p>\n'
140 '<p>:)</p>')
141 assert result == (
142 '<div>'
143 '<p>Hi everybody! </p>\n'
144 '<p>:)</p>'
145 '</div>')
146
147 # Remove evil javascript
152a3bfa 148 result = text.clean_html(
a68ee555
CAW
149 '<p><a href="javascript:nasty_surprise">innocent link!</a></p>')
150 assert result == (
151 '<p><a href="">innocent link!</a></p>')