test_util.py 6.5 KB

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