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 mediagoblin
.db
.base
import Session
21 from mediagoblin
.db
.models
import MediaEntry
23 from mediagoblin
.tests
.tools
import fixture_add_user
28 class FakeUUID(object):
29 hex = 'testtest-test-test-test-testtesttest'
31 UUID_MOCK
= mock
.Mock(return_value
=FakeUUID())
34 class TestMediaEntrySlugs(object):
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")
42 def _insert_media_entry_fixture(self
, title
=None, slug
=None, this_id
=None,
43 uploader
=None, save
=True):
45 entry
.title
= title
or u
"Some title"
48 entry
.uploader
= uploader
or self
.chris_user
.id
49 entry
.media_type
= u
'image'
56 def test_unique_slug_from_title(self
, test_app
):
59 entry
= self
._insert
_media
_entry
_fixture
(u
"Totally unique slug!", save
=False)
61 assert entry
.slug
== u
'totally-unique-slug'
63 def test_old_good_unique_slug(self
, test_app
):
66 entry
= self
._insert
_media
_entry
_fixture
(
67 u
"A title here", u
"a-different-slug-there", save
=False)
69 assert entry
.slug
== u
"a-different-slug-there"
71 def test_old_weird_slug(self
, test_app
):
74 entry
= self
._insert
_media
_entry
_fixture
(
75 slug
=u
"wowee!!!!!", save
=False)
77 assert entry
.slug
== u
"wowee"
79 def test_existing_slug_use_id(self
, test_app
):
82 entry
= self
._insert
_media
_entry
_fixture
(
83 u
"Beware, I exist!!", this_id
=9000, save
=False)
85 assert entry
.slug
== u
"beware-i-exist-9000"
87 def test_existing_slug_cant_use_id(self
, test_app
):
90 # Getting tired of dealing with test_app and this mock.patch
91 # thing conflicting, getting lazy.
92 @mock.patch('uuid.uuid4', UUID_MOCK
)
94 # This one grabs the nine thousand slug
95 self
._insert
_media
_entry
_fixture
(
96 slug
=u
"beware-i-exist-9000")
98 entry
= self
._insert
_media
_entry
_fixture
(
99 u
"Beware, I exist!!", this_id
=9000, save
=False)
100 entry
.generate_slug()
101 assert entry
.slug
== u
"beware-i-exist-test"
105 def test_existing_slug_cant_use_id_extra_junk(self
, test_app
):
108 # Getting tired of dealing with test_app and this mock.patch
109 # thing conflicting, getting lazy.
110 @mock.patch('uuid.uuid4', UUID_MOCK
)
112 # This one grabs the nine thousand slug
113 self
._insert
_media
_entry
_fixture
(
114 slug
=u
"beware-i-exist-9000")
116 # This one grabs makes sure the annoyance doesn't stop
117 self
._insert
_media
_entry
_fixture
(
118 slug
=u
"beware-i-exist-test")
120 entry
= self
._insert
_media
_entry
_fixture
(
121 u
"Beware, I exist!!", this_id
=9000, save
=False)
122 entry
.generate_slug()
123 assert entry
.slug
== u
"beware-i-exist-testtest"
127 def test_garbage_slug(self
, test_app
):
129 Titles that sound totally like Q*Bert shouldn't have slugs at
130 all. We'll just reference them by id.
149 qbert_entry
= self
._insert
_media
_entry
_fixture
(
150 u
"@!#?@!", save
=False)
151 qbert_entry
.generate_slug()
152 assert qbert_entry
.slug
is None
155 def test_media_data_init(test_app
):
159 media
.media_type
= u
"mediagoblin.media_types.image"
160 assert media
.media_data
is None
161 media
.media_data_init()
162 assert media
.media_data
is not None
164 for obj
in Session():
167 assert obj_in_session
== 0