test_modelmethods.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. # Maybe not every model needs a test, but some models have special
  17. # methods, and so it makes sense to test them here.
  18. from __future__ import print_function
  19. from mediagoblin.db.base import Session
  20. from mediagoblin.db.models import MediaEntry, User, Privilege, Activity, \
  21. Generator
  22. from mediagoblin.tests import MGClientTestCase
  23. from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry, \
  24. fixture_add_activity
  25. try:
  26. import mock
  27. except ImportError:
  28. import unittest.mock as mock
  29. import pytest
  30. class FakeUUID(object):
  31. hex = 'testtest-test-test-test-testtesttest'
  32. UUID_MOCK = mock.Mock(return_value=FakeUUID())
  33. REQUEST_CONTEXT = ['mediagoblin/root.html', 'request']
  34. class TestMediaEntrySlugs(object):
  35. def _setup(self):
  36. self.chris_user = fixture_add_user(u'chris')
  37. self.emily_user = fixture_add_user(u'emily')
  38. self.existing_entry = self._insert_media_entry_fixture(
  39. title=u"Beware, I exist!",
  40. slug=u"beware-i-exist")
  41. def _insert_media_entry_fixture(self, title=None, slug=None, this_id=None,
  42. uploader=None, save=True):
  43. entry = MediaEntry()
  44. entry.title = title or u"Some title"
  45. entry.slug = slug
  46. entry.id = this_id
  47. entry.uploader = uploader or self.chris_user.id
  48. entry.media_type = u'image'
  49. if save:
  50. entry.save()
  51. return entry
  52. def test_unique_slug_from_title(self, test_app):
  53. self._setup()
  54. entry = self._insert_media_entry_fixture(u"Totally unique slug!", save=False)
  55. entry.generate_slug()
  56. assert entry.slug == u'totally-unique-slug'
  57. def test_old_good_unique_slug(self, test_app):
  58. self._setup()
  59. entry = self._insert_media_entry_fixture(
  60. u"A title here", u"a-different-slug-there", save=False)
  61. entry.generate_slug()
  62. assert entry.slug == u"a-different-slug-there"
  63. def test_old_weird_slug(self, test_app):
  64. self._setup()
  65. entry = self._insert_media_entry_fixture(
  66. slug=u"wowee!!!!!", save=False)
  67. entry.generate_slug()
  68. assert entry.slug == u"wowee"
  69. def test_existing_slug_use_id(self, test_app):
  70. self._setup()
  71. entry = self._insert_media_entry_fixture(
  72. u"Beware, I exist!!", this_id=9000, save=False)
  73. entry.generate_slug()
  74. assert entry.slug == u"beware-i-exist-9000"
  75. def test_existing_slug_cant_use_id(self, test_app):
  76. self._setup()
  77. # Getting tired of dealing with test_app and this mock.patch
  78. # thing conflicting, getting lazy.
  79. @mock.patch('uuid.uuid4', UUID_MOCK)
  80. def _real_test():
  81. # This one grabs the nine thousand slug
  82. self._insert_media_entry_fixture(
  83. slug=u"beware-i-exist-9000")
  84. entry = self._insert_media_entry_fixture(
  85. u"Beware, I exist!!", this_id=9000, save=False)
  86. entry.generate_slug()
  87. assert entry.slug == u"beware-i-exist-test"
  88. _real_test()
  89. def test_existing_slug_cant_use_id_extra_junk(self, test_app):
  90. self._setup()
  91. # Getting tired of dealing with test_app and this mock.patch
  92. # thing conflicting, getting lazy.
  93. @mock.patch('uuid.uuid4', UUID_MOCK)
  94. def _real_test():
  95. # This one grabs the nine thousand slug
  96. self._insert_media_entry_fixture(
  97. slug=u"beware-i-exist-9000")
  98. # This one grabs makes sure the annoyance doesn't stop
  99. self._insert_media_entry_fixture(
  100. slug=u"beware-i-exist-test")
  101. entry = self._insert_media_entry_fixture(
  102. u"Beware, I exist!!", this_id=9000, save=False)
  103. entry.generate_slug()
  104. assert entry.slug == u"beware-i-exist-testtest"
  105. _real_test()
  106. def test_garbage_slug(self, test_app):
  107. """
  108. Titles that sound totally like Q*Bert shouldn't have slugs at
  109. all. We'll just reference them by id.
  110. ,
  111. / \ (@!#?@!)
  112. |\,/| ,-, /
  113. | |#| ( ")~
  114. / \|/ \ L L
  115. |\,/|\,/|
  116. | |#, |#|
  117. / \|/ \|/ \
  118. |\,/|\,/|\,/|
  119. | |#| |#| |#|
  120. / \|/ \|/ \|/ \
  121. |\,/|\,/|\,/|\,/|
  122. | |#| |#| |#| |#|
  123. \|/ \|/ \|/ \|/
  124. """
  125. self._setup()
  126. qbert_entry = self._insert_media_entry_fixture(
  127. u"@!#?@!", save=False)
  128. qbert_entry.generate_slug()
  129. assert qbert_entry.slug is None
  130. class TestUserHasPrivilege:
  131. def _setup(self):
  132. fixture_add_user(u'natalie',
  133. privileges=[u'admin',u'moderator',u'active'])
  134. fixture_add_user(u'aeva',
  135. privileges=[u'moderator',u'active'])
  136. self.natalie_user = User.query.filter(
  137. User.username==u'natalie').first()
  138. self.aeva_user = User.query.filter(
  139. User.username==u'aeva').first()
  140. def test_privilege_added_correctly(self, test_app):
  141. self._setup()
  142. admin = Privilege.query.filter(
  143. Privilege.privilege_name == u'admin').one()
  144. # first make sure the privileges were added successfully
  145. assert admin in self.natalie_user.all_privileges
  146. assert admin not in self.aeva_user.all_privileges
  147. def test_user_has_privilege_one(self, test_app):
  148. self._setup()
  149. # then test out the user.has_privilege method for one privilege
  150. assert not self.aeva_user.has_privilege(u'admin')
  151. assert self.natalie_user.has_privilege(u'active')
  152. def test_allow_admin(self, test_app):
  153. self._setup()
  154. # This should work because she is an admin.
  155. assert self.natalie_user.has_privilege(u'commenter')
  156. # Test that we can look this out ignoring that she's an admin
  157. assert not self.natalie_user.has_privilege(u'commenter', allow_admin=False)
  158. def test_media_data_init(test_app):
  159. Session.rollback()
  160. Session.remove()
  161. media = MediaEntry()
  162. media.media_type = u"mediagoblin.media_types.image"
  163. assert media.media_data is None
  164. media.media_data_init()
  165. assert media.media_data is not None
  166. obj_in_session = 0
  167. for obj in Session():
  168. obj_in_session += 1
  169. print(repr(obj))
  170. assert obj_in_session == 0
  171. class TestUserUrlForSelf(MGClientTestCase):
  172. usernames = [(u'lindsay', dict(privileges=[u'active']))]
  173. def test_url_for_self(self):
  174. _, request = self.do_get('/', *REQUEST_CONTEXT)
  175. assert self.user(u'lindsay').url_for_self(request.urlgen) == '/u/lindsay/'
  176. def test_url_for_self_not_callable(self):
  177. _, request = self.do_get('/', *REQUEST_CONTEXT)
  178. def fake_urlgen():
  179. pass
  180. with pytest.raises(TypeError) as excinfo:
  181. self.user(u'lindsay').url_for_self(fake_urlgen())
  182. assert excinfo.errisinstance(TypeError)
  183. assert 'object is not callable' in str(excinfo)