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.
22 from mediagoblin
.db
.base
import Session
23 from mediagoblin
.db
.models
import MediaEntry
25 from mediagoblin
.tests
.tools
import fixture_add_user
26 from mediagoblin
.tools
.testing
import _activate_testing
32 class FakeUUID(object):
33 hex = 'testtest-test-test-test-testtesttest'
35 UUID_MOCK
= mock
.Mock(return_value
=FakeUUID())
38 class TestMediaEntrySlugs(object):
40 self
.chris_user
= fixture_add_user(u
'chris')
41 self
.emily_user
= fixture_add_user(u
'emily')
42 self
.existing_entry
= self
._insert
_media
_entry
_fixture
(
43 title
=u
"Beware, I exist!",
44 slug
=u
"beware-i-exist")
46 def _insert_media_entry_fixture(self
, title
=None, slug
=None, this_id
=None,
47 uploader
=None, save
=True):
49 entry
.title
= title
or u
"Some title"
52 entry
.uploader
= uploader
or self
.chris_user
.id
53 entry
.media_type
= u
'image'
60 def test_unique_slug_from_title(self
, test_app
):
63 entry
= self
._insert
_media
_entry
_fixture
(u
"Totally unique slug!", save
=False)
65 assert entry
.slug
== u
'totally-unique-slug'
67 def test_old_good_unique_slug(self
, test_app
):
70 entry
= self
._insert
_media
_entry
_fixture
(
71 u
"A title here", u
"a-different-slug-there", save
=False)
73 assert entry
.slug
== u
"a-different-slug-there"
75 def test_old_weird_slug(self
, test_app
):
78 entry
= self
._insert
_media
_entry
_fixture
(
79 slug
=u
"wowee!!!!!", save
=False)
81 assert entry
.slug
== u
"wowee"
83 def test_existing_slug_use_id(self
, test_app
):
86 entry
= self
._insert
_media
_entry
_fixture
(
87 u
"Beware, I exist!!", this_id
=9000, save
=False)
89 assert entry
.slug
== u
"beware-i-exist-9000"
91 def test_existing_slug_cant_use_id(self
, test_app
):
94 # Getting tired of dealing with test_app and this mock.patch
95 # thing conflicting, getting lazy.
96 @mock.patch('uuid.uuid4', UUID_MOCK
)
98 # This one grabs the nine thousand slug
99 self
._insert
_media
_entry
_fixture
(
100 slug
=u
"beware-i-exist-9000")
102 entry
= self
._insert
_media
_entry
_fixture
(
103 u
"Beware, I exist!!", this_id
=9000, save
=False)
104 entry
.generate_slug()
105 assert entry
.slug
== u
"beware-i-exist-test"
109 def test_existing_slug_cant_use_id_extra_junk(self
, test_app
):
112 # Getting tired of dealing with test_app and this mock.patch
113 # thing conflicting, getting lazy.
114 @mock.patch('uuid.uuid4', UUID_MOCK
)
116 # This one grabs the nine thousand slug
117 self
._insert
_media
_entry
_fixture
(
118 slug
=u
"beware-i-exist-9000")
120 # This one grabs makes sure the annoyance doesn't stop
121 self
._insert
_media
_entry
_fixture
(
122 slug
=u
"beware-i-exist-test")
124 entry
= self
._insert
_media
_entry
_fixture
(
125 u
"Beware, I exist!!", this_id
=9000, save
=False)
126 entry
.generate_slug()
127 assert entry
.slug
== u
"beware-i-exist-testtest"
131 def test_garbage_slug(self
, test_app
):
133 Titles that sound totally like Q*Bert shouldn't have slugs at
134 all. We'll just reference them by id.
153 qbert_entry
= self
._insert
_media
_entry
_fixture
(
154 u
"@!#?@!", save
=False)
155 qbert_entry
.generate_slug()
156 assert qbert_entry
.slug
is None
159 def test_media_data_init(test_app
):
163 media
.media_type
= u
"mediagoblin.media_types.image"
164 assert media
.media_data
is None
165 media
.media_data_init()
166 assert media
.media_data
is not None
168 for obj
in Session():
171 assert obj_in_session
== 0