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