Commit | Line | Data |
---|---|---|
a81082fc CAW |
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 | ||
9459fa3c BP |
20 | from __future__ import print_function |
21 | ||
d728c636 | 22 | from mediagoblin.db.base import Session |
e1561d04 | 23 | from mediagoblin.db.models import MediaEntry, User, Privilege |
a81082fc | 24 | |
de6a313c | 25 | from mediagoblin.tests import MGClientTestCase |
5c2ece74 | 26 | from mediagoblin.tests.tools import fixture_add_user |
a81082fc | 27 | |
9459fa3c BP |
28 | try: |
29 | import mock | |
30 | except ImportError: | |
31 | import unittest.mock as mock | |
de6a313c | 32 | import pytest |
a81082fc CAW |
33 | |
34 | ||
35 | class FakeUUID(object): | |
36 | hex = 'testtest-test-test-test-testtesttest' | |
37 | ||
38 | UUID_MOCK = mock.Mock(return_value=FakeUUID()) | |
39 | ||
de6a313c BP |
40 | REQUEST_CONTEXT = ['mediagoblin/root.html', 'request'] |
41 | ||
a81082fc CAW |
42 | |
43 | class TestMediaEntrySlugs(object): | |
5c2ece74 | 44 | def _setup(self): |
a81082fc CAW |
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") | |
50 | ||
51 | def _insert_media_entry_fixture(self, title=None, slug=None, this_id=None, | |
52 | uploader=None, save=True): | |
53 | entry = MediaEntry() | |
54 | entry.title = title or u"Some title" | |
55 | entry.slug = slug | |
56 | entry.id = this_id | |
57 | entry.uploader = uploader or self.chris_user.id | |
58 | entry.media_type = u'image' | |
dc31cd1b | 59 | |
a81082fc CAW |
60 | if save: |
61 | entry.save() | |
62 | ||
63 | return entry | |
64 | ||
5c2ece74 CAW |
65 | def test_unique_slug_from_title(self, test_app): |
66 | self._setup() | |
67 | ||
a81082fc CAW |
68 | entry = self._insert_media_entry_fixture(u"Totally unique slug!", save=False) |
69 | entry.generate_slug() | |
70 | assert entry.slug == u'totally-unique-slug' | |
71 | ||
5c2ece74 CAW |
72 | def test_old_good_unique_slug(self, test_app): |
73 | self._setup() | |
74 | ||
a81082fc CAW |
75 | entry = self._insert_media_entry_fixture( |
76 | u"A title here", u"a-different-slug-there", save=False) | |
77 | entry.generate_slug() | |
78 | assert entry.slug == u"a-different-slug-there" | |
79 | ||
5c2ece74 CAW |
80 | def test_old_weird_slug(self, test_app): |
81 | self._setup() | |
82 | ||
a81082fc CAW |
83 | entry = self._insert_media_entry_fixture( |
84 | slug=u"wowee!!!!!", save=False) | |
85 | entry.generate_slug() | |
86 | assert entry.slug == u"wowee" | |
87 | ||
5c2ece74 CAW |
88 | def test_existing_slug_use_id(self, test_app): |
89 | self._setup() | |
a81082fc CAW |
90 | |
91 | entry = self._insert_media_entry_fixture( | |
92 | u"Beware, I exist!!", this_id=9000, save=False) | |
93 | entry.generate_slug() | |
5c2ece74 | 94 | assert entry.slug == u"beware-i-exist-9000" |
a81082fc | 95 | |
5c2ece74 CAW |
96 | def test_existing_slug_cant_use_id(self, test_app): |
97 | self._setup() | |
98 | ||
99 | # Getting tired of dealing with test_app and this mock.patch | |
100 | # thing conflicting, getting lazy. | |
101 | @mock.patch('uuid.uuid4', UUID_MOCK) | |
102 | def _real_test(): | |
103 | # This one grabs the nine thousand slug | |
104 | self._insert_media_entry_fixture( | |
105 | slug=u"beware-i-exist-9000") | |
106 | ||
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" | |
dc31cd1b | 111 | |
5c2ece74 CAW |
112 | _real_test() |
113 | ||
114 | def test_existing_slug_cant_use_id_extra_junk(self, test_app): | |
115 | self._setup() | |
116 | ||
117 | # Getting tired of dealing with test_app and this mock.patch | |
118 | # thing conflicting, getting lazy. | |
119 | @mock.patch('uuid.uuid4', UUID_MOCK) | |
120 | def _real_test(): | |
121 | # This one grabs the nine thousand slug | |
122 | self._insert_media_entry_fixture( | |
123 | slug=u"beware-i-exist-9000") | |
124 | ||
125 | # This one grabs makes sure the annoyance doesn't stop | |
126 | self._insert_media_entry_fixture( | |
127 | slug=u"beware-i-exist-test") | |
128 | ||
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" | |
133 | ||
134 | _real_test() | |
135 | ||
136 | def test_garbage_slug(self, test_app): | |
a81082fc CAW |
137 | """ |
138 | Titles that sound totally like Q*Bert shouldn't have slugs at | |
139 | all. We'll just reference them by id. | |
140 | ||
141 | , | |
142 | / \ (@!#?@!) | |
143 | |\,/| ,-, / | |
144 | | |#| ( ")~ | |
145 | / \|/ \ L L | |
146 | |\,/|\,/| | |
147 | | |#, |#| | |
148 | / \|/ \|/ \ | |
149 | |\,/|\,/|\,/| | |
150 | | |#| |#| |#| | |
151 | / \|/ \|/ \|/ \ | |
152 | |\,/|\,/|\,/|\,/| | |
153 | | |#| |#| |#| |#| | |
154 | \|/ \|/ \|/ \|/ | |
155 | """ | |
5c2ece74 CAW |
156 | self._setup() |
157 | ||
a81082fc CAW |
158 | qbert_entry = self._insert_media_entry_fixture( |
159 | u"@!#?@!", save=False) | |
160 | qbert_entry.generate_slug() | |
161 | assert qbert_entry.slug is None | |
d728c636 | 162 | |
e1561d04 | 163 | class TestUserHasPrivilege: |
164 | def _setup(self): | |
e46fb71c | 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() | |
e1561d04 | 173 | |
174 | def test_privilege_added_correctly(self, test_app): | |
175 | self._setup() | |
176 | admin = Privilege.query.filter( | |
177 | Privilege.privilege_name == u'admin').one() | |
178 | # first make sure the privileges were added successfully | |
179 | ||
180 | assert admin in self.natalie_user.all_privileges | |
181 | assert admin not in self.aeva_user.all_privileges | |
182 | ||
183 | def test_user_has_privilege_one(self, test_app): | |
184 | self._setup() | |
185 | ||
186 | # then test out the user.has_privilege method for one privilege | |
7bfc81b2 JT |
187 | assert not self.aeva_user.has_privilege(u'admin') |
188 | assert self.natalie_user.has_privilege(u'active') | |
e1561d04 | 189 | |
7bfc81b2 | 190 | def test_allow_admin(self, test_app): |
e1561d04 | 191 | self._setup() |
192 | ||
7bfc81b2 JT |
193 | # This should work because she is an admin. |
194 | assert self.natalie_user.has_privilege(u'commenter') | |
e1561d04 | 195 | |
7bfc81b2 JT |
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) | |
d728c636 | 198 | |
5c2ece74 | 199 | def test_media_data_init(test_app): |
d728c636 E |
200 | Session.rollback() |
201 | Session.remove() | |
202 | media = MediaEntry() | |
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 | |
207 | obj_in_session = 0 | |
208 | for obj in Session(): | |
209 | obj_in_session += 1 | |
9459fa3c | 210 | print(repr(obj)) |
7d503a89 | 211 | assert obj_in_session == 0 |
e1561d04 | 212 | |
de6a313c BP |
213 | |
214 | class TestUserUrlForSelf(MGClientTestCase): | |
215 | ||
216 | usernames = [(u'lindsay', dict(privileges=[u'active']))] | |
217 | ||
218 | def test_url_for_self(self): | |
219 | _, request = self.do_get('/', *REQUEST_CONTEXT) | |
220 | ||
221 | assert self.user(u'lindsay').url_for_self(request.urlgen) == '/u/lindsay/' | |
222 | ||
223 | def test_url_for_self_not_callable(self): | |
224 | _, request = self.do_get('/', *REQUEST_CONTEXT) | |
225 | ||
226 | def fake_urlgen(): | |
227 | pass | |
228 | ||
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) |