82cca855ee1772d0042ad07cb00db0338cfa4cb0
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/>.
17 # Maybe not every model needs a test, but some models have special
18 # methods, and so it makes sense to test them here.
20 from __future__
import print_function
22 from mediagoblin
.db
.base
import Session
23 from mediagoblin
.db
.models
import MediaEntry
, User
, Privilege
25 from mediagoblin
.tests
import MGClientTestCase
26 from mediagoblin
.tests
.tools
import fixture_add_user
31 import unittest
.mock
as mock
35 class FakeUUID(object):
36 hex = 'testtest-test-test-test-testtesttest'
38 UUID_MOCK
= mock
.Mock(return_value
=FakeUUID())
40 REQUEST_CONTEXT
= ['mediagoblin/root.html', 'request']
43 class TestMediaEntrySlugs(object):
45 self
.chris_user
= fixture_add_user(u
'chris')
46 self
.emily_user
= fixture_add_user(u
'emily')
47 self
.existing_entry
= self
._insert
_media
_entry
_fixture
(
48 title
=u
"Beware, I exist!",
49 slug
=u
"beware-i-exist")
51 def _insert_media_entry_fixture(self
, title
=None, slug
=None, this_id
=None,
52 uploader
=None, save
=True):
54 entry
.title
= title
or u
"Some title"
57 entry
.uploader
= uploader
or self
.chris_user
.id
58 entry
.media_type
= u
'image'
65 def test_unique_slug_from_title(self
, test_app
):
68 entry
= self
._insert
_media
_entry
_fixture
(u
"Totally unique slug!", save
=False)
70 assert entry
.slug
== u
'totally-unique-slug'
72 def test_old_good_unique_slug(self
, test_app
):
75 entry
= self
._insert
_media
_entry
_fixture
(
76 u
"A title here", u
"a-different-slug-there", save
=False)
78 assert entry
.slug
== u
"a-different-slug-there"
80 def test_old_weird_slug(self
, test_app
):
83 entry
= self
._insert
_media
_entry
_fixture
(
84 slug
=u
"wowee!!!!!", save
=False)
86 assert entry
.slug
== u
"wowee"
88 def test_existing_slug_use_id(self
, test_app
):
91 entry
= self
._insert
_media
_entry
_fixture
(
92 u
"Beware, I exist!!", this_id
=9000, save
=False)
94 assert entry
.slug
== u
"beware-i-exist-9000"
96 def test_existing_slug_cant_use_id(self
, test_app
):
99 # Getting tired of dealing with test_app and this mock.patch
100 # thing conflicting, getting lazy.
101 @mock.patch('uuid.uuid4', UUID_MOCK
)
103 # This one grabs the nine thousand slug
104 self
._insert
_media
_entry
_fixture
(
105 slug
=u
"beware-i-exist-9000")
107 entry
= self
._insert
_media
_entry
_fixture
(
108 u
"Beware, I exist!!", this_id
=9000, save
=False)
109 entry
.generate_slug()
110 assert entry
.slug
== u
"beware-i-exist-test"
114 def test_existing_slug_cant_use_id_extra_junk(self
, test_app
):
117 # Getting tired of dealing with test_app and this mock.patch
118 # thing conflicting, getting lazy.
119 @mock.patch('uuid.uuid4', UUID_MOCK
)
121 # This one grabs the nine thousand slug
122 self
._insert
_media
_entry
_fixture
(
123 slug
=u
"beware-i-exist-9000")
125 # This one grabs makes sure the annoyance doesn't stop
126 self
._insert
_media
_entry
_fixture
(
127 slug
=u
"beware-i-exist-test")
129 entry
= self
._insert
_media
_entry
_fixture
(
130 u
"Beware, I exist!!", this_id
=9000, save
=False)
131 entry
.generate_slug()
132 assert entry
.slug
== u
"beware-i-exist-testtest"
136 def test_garbage_slug(self
, test_app
):
138 Titles that sound totally like Q*Bert shouldn't have slugs at
139 all. We'll just reference them by id.
158 qbert_entry
= self
._insert
_media
_entry
_fixture
(
159 u
"@!#?@!", save
=False)
160 qbert_entry
.generate_slug()
161 assert qbert_entry
.slug
is None
163 class TestUserHasPrivilege
:
165 fixture_add_user(u
'natalie',
166 privileges
=[u
'admin',u
'moderator',u
'active'])
167 fixture_add_user(u
'aeva',
168 privileges
=[u
'moderator',u
'active'])
169 self
.natalie_user
= User
.query
.filter(
170 User
.username
==u
'natalie').first()
171 self
.aeva_user
= User
.query
.filter(
172 User
.username
==u
'aeva').first()
174 def test_privilege_added_correctly(self
, test_app
):
176 admin
= Privilege
.query
.filter(
177 Privilege
.privilege_name
== u
'admin').one()
178 # first make sure the privileges were added successfully
180 assert admin
in self
.natalie_user
.all_privileges
181 assert admin
not in self
.aeva_user
.all_privileges
183 def test_user_has_privilege_one(self
, test_app
):
186 # then test out the user.has_privilege method for one privilege
187 assert not self
.aeva_user
.has_privilege(u
'admin')
188 assert self
.natalie_user
.has_privilege(u
'active')
190 def test_allow_admin(self
, test_app
):
193 # This should work because she is an admin.
194 assert self
.natalie_user
.has_privilege(u
'commenter')
196 # Test that we can look this out ignoring that she's an admin
197 assert not self
.natalie_user
.has_privilege(u
'commenter', allow_admin
=False)
199 def test_media_data_init(test_app
):
203 media
.media_type
= u
"mediagoblin.media_types.image"
204 assert media
.media_data
is None
205 media
.media_data_init()
206 assert media
.media_data
is not None
208 for obj
in Session():
211 assert obj_in_session
== 0
214 class TestUserUrlForSelf(MGClientTestCase
):
216 usernames
= [(u
'lindsay', dict(privileges
=[u
'active']))]
218 def test_url_for_self(self
):
219 _
, request
= self
.do_get('/', *REQUEST_CONTEXT
)
221 assert self
.user(u
'lindsay').url_for_self(request
.urlgen
) == '/u/lindsay/'
223 def test_url_for_self_not_callable(self
):
224 _
, request
= self
.do_get('/', *REQUEST_CONTEXT
)
229 with pytest
.raises(TypeError) as excinfo
:
230 self
.user(u
'lindsay').url_for_self(fake_urlgen())
231 assert excinfo
.errisinstance(TypeError)
232 assert 'object is not callable' in str(excinfo
)