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