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
, LocalUser
, Privilege
, \
26 from mediagoblin
.tests
import MGClientTestCase
27 from mediagoblin
.tests
.tools
import fixture_add_user
, fixture_media_entry
, \
33 import unittest
.mock
as mock
37 class FakeUUID(object):
38 hex = 'testtest-test-test-test-testtesttest'
40 UUID_MOCK
= mock
.Mock(return_value
=FakeUUID())
42 REQUEST_CONTEXT
= ['mediagoblin/root.html', 'request']
45 class TestMediaEntrySlugs(object):
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")
53 def _insert_media_entry_fixture(self
, title
=None, slug
=None, this_id
=None,
54 uploader
=None, save
=True):
56 entry
.title
= title
or u
"Some title"
59 entry
.actor
= uploader
or self
.chris_user
.id
60 entry
.media_type
= u
'image'
67 def test_unique_slug_from_title(self
, test_app
):
70 entry
= self
._insert
_media
_entry
_fixture
(u
"Totally unique slug!", save
=False)
72 assert entry
.slug
== u
'totally-unique-slug'
74 def test_old_good_unique_slug(self
, test_app
):
77 entry
= self
._insert
_media
_entry
_fixture
(
78 u
"A title here", u
"a-different-slug-there", save
=False)
80 assert entry
.slug
== u
"a-different-slug-there"
82 def test_old_weird_slug(self
, test_app
):
85 entry
= self
._insert
_media
_entry
_fixture
(
86 slug
=u
"wowee!!!!!", save
=False)
88 assert entry
.slug
== u
"wowee"
90 def test_existing_slug_use_id(self
, test_app
):
93 entry
= self
._insert
_media
_entry
_fixture
(
94 u
"Beware, I exist!!", this_id
=9000, save
=False)
96 assert entry
.slug
== u
"beware-i-exist-9000"
98 def test_existing_slug_cant_use_id(self
, test_app
):
101 # Getting tired of dealing with test_app and this mock.patch
102 # thing conflicting, getting lazy.
103 @mock.patch('uuid.uuid4', UUID_MOCK
)
105 # This one grabs the nine thousand slug
106 self
._insert
_media
_entry
_fixture
(
107 slug
=u
"beware-i-exist-9000")
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"
116 def test_existing_slug_cant_use_id_extra_junk(self
, test_app
):
119 # Getting tired of dealing with test_app and this mock.patch
120 # thing conflicting, getting lazy.
121 @mock.patch('uuid.uuid4', UUID_MOCK
)
123 # This one grabs the nine thousand slug
124 self
._insert
_media
_entry
_fixture
(
125 slug
=u
"beware-i-exist-9000")
127 # This one grabs makes sure the annoyance doesn't stop
128 self
._insert
_media
_entry
_fixture
(
129 slug
=u
"beware-i-exist-test")
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"
138 def test_garbage_slug(self
, test_app
):
140 Titles that sound totally like Q*Bert shouldn't have slugs at
141 all. We'll just reference them by id.
160 qbert_entry
= self
._insert
_media
_entry
_fixture
(
161 u
"@!#?@!", save
=False)
162 qbert_entry
.generate_slug()
163 assert qbert_entry
.slug
is None
165 class TestUserHasPrivilege
:
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()
176 def test_privilege_added_correctly(self
, test_app
):
178 admin
= Privilege
.query
.filter(
179 Privilege
.privilege_name
== u
'admin').one()
180 # first make sure the privileges were added successfully
182 assert admin
in self
.natalie_user
.all_privileges
183 assert admin
not in self
.aeva_user
.all_privileges
185 def test_user_has_privilege_one(self
, test_app
):
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')
192 def test_allow_admin(self
, test_app
):
195 # This should work because she is an admin.
196 assert self
.natalie_user
.has_privilege(u
'commenter')
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)
201 def test_media_data_init(test_app
):
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
210 for obj
in Session():
213 assert obj_in_session
== 0
216 class TestUserUrlForSelf(MGClientTestCase
):
218 usernames
= [(u
'lindsay', dict(privileges
=[u
'active']))]
220 def test_url_for_self(self
):
221 _
, request
= self
.do_get('/', *REQUEST_CONTEXT
)
223 assert self
.user(u
'lindsay').url_for_self(request
.urlgen
) == '/u/lindsay/'
225 def test_url_for_self_not_callable(self
):
226 _
, request
= self
.do_get('/', *REQUEST_CONTEXT
)
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
)