Commit | Line | Data |
---|---|---|
1975b5dd | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
1975b5dd CM |
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 | ||
9459fa3c BP |
17 | import six |
18 | ||
19 | if six.PY2: # this hack only work in Python 2 | |
20 | import sys | |
21 | reload(sys) | |
22 | sys.setdefaultencoding('utf-8') | |
ed3ff88e | 23 | |
6573573d | 24 | import os |
54b3113a | 25 | import pytest |
1975b5dd | 26 | |
fd19da34 BP |
27 | import six.moves.urllib.parse as urlparse |
28 | ||
c41705bf BB |
29 | # this gst initialization stuff is really required here |
30 | import gi | |
31 | gi.require_version('Gst', '1.0') | |
32 | from gi.repository import Gst | |
33 | Gst.init(None) | |
34 | ||
5c2ece74 | 35 | from mediagoblin.tests.tools import fixture_add_user |
c41705bf | 36 | from .media_tools import create_av |
1975b5dd | 37 | from mediagoblin import mg_globals |
c3cce756 RE |
38 | from mediagoblin.db.models import MediaEntry, User |
39 | from mediagoblin.db.base import Session | |
deea3f66 | 40 | from mediagoblin.tools import template |
60eeb456 | 41 | from mediagoblin.media_types.image import ImageMediaManager |
a80ebf3b | 42 | from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites |
1975b5dd | 43 | |
b698c94d | 44 | from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \ |
53cf5b45 | 45 | BIG_BLUE, GOOD_PDF, GPS_JPG, MED_PNG, BIG_PNG |
75ce65cf | 46 | |
5bd0adeb | 47 | GOOD_TAG_STRING = u'yin,yang' |
e49b7e02 | 48 | BAD_TAG_STRING = six.text_type('rage,' + 'f' * 26 + 'u' * 26) |
8ff4dec7 | 49 | |
31dd6013 BS |
50 | FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form'] |
51 | REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request'] | |
1975b5dd | 52 | |
1975b5dd | 53 | |
1975b5dd | 54 | class TestSubmission: |
0f346701 CAW |
55 | @pytest.fixture(autouse=True) |
56 | def setup(self, test_app): | |
5c2ece74 | 57 | self.test_app = test_app |
0a78be3e | 58 | |
75ce65cf CM |
59 | # TODO: Possibly abstract into a decorator like: |
60 | # @as_authenticated_user('chris') | |
8e91df87 | 61 | fixture_add_user(privileges=[u'active',u'uploader', u'commenter']) |
afe4e513 | 62 | |
c2d6792d E |
63 | self.login() |
64 | ||
dfd66b78 | 65 | def our_user(self): |
66 | """ | |
67 | Fetch the user we're submitting with. Every .get() or .post() | |
68 | invalidates the session; this is a hacky workaround. | |
69 | """ | |
70 | #### FIXME: Pytest collects this as a test and runs this. | |
71 | #### ... it shouldn't. At least it passes, but that's | |
72 | #### totally stupid. | |
73 | #### Also if we found a way to make this run it should be a | |
74 | #### property. | |
75 | return User.query.filter(User.username==u'chris').first() | |
76 | ||
c2d6792d | 77 | def login(self): |
75ce65cf CM |
78 | self.test_app.post( |
79 | '/auth/login/', { | |
80 | 'username': u'chris', | |
81 | 'password': 'toast'}) | |
82 | ||
c2d6792d E |
83 | def logout(self): |
84 | self.test_app.get('/auth/logout/') | |
85 | ||
31dd6013 BS |
86 | def do_post(self, data, *context_keys, **kwargs): |
87 | url = kwargs.pop('url', '/submit/') | |
88 | do_follow = kwargs.pop('do_follow', False) | |
89 | template.clear_test_template_context() | |
90 | response = self.test_app.post(url, data, **kwargs) | |
91 | if do_follow: | |
92 | response.follow() | |
93 | context_data = template.TEMPLATE_TEST_CONTEXT | |
94 | for key in context_keys: | |
95 | context_data = context_data[key] | |
96 | return response, context_data | |
deea3f66 | 97 | |
31dd6013 BS |
98 | def upload_data(self, filename): |
99 | return {'upload_files': [('file', filename)]} | |
100 | ||
e089b66b | 101 | def check_comments(self, request, media_id, count): |
44082b12 | 102 | comments = request.db.MediaComment.query.filter_by(media_entry=media_id) |
7d503a89 | 103 | assert count == len(list(comments)) |
c16b8196 | 104 | |
0f346701 | 105 | def test_missing_fields(self): |
ad35dd49 CM |
106 | # Test blank form |
107 | # --------------- | |
31dd6013 | 108 | response, form = self.do_post({}, *FORM_CONTEXT) |
7d503a89 | 109 | assert form.file.errors == [u'You must provide a file.'] |
ad35dd49 CM |
110 | |
111 | # Test blank file | |
112 | # --------------- | |
5bd0adeb | 113 | response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT) |
7d503a89 | 114 | assert form.file.errors == [u'You must provide a file.'] |
ad35dd49 | 115 | |
c0e87ec9 | 116 | def check_url(self, response, path): |
7d503a89 | 117 | assert urlparse.urlsplit(response.location)[2] == path |
c0e87ec9 BS |
118 | |
119 | def check_normal_upload(self, title, filename): | |
120 | response, context = self.do_post({'title': title}, do_follow=True, | |
121 | **self.upload_data(filename)) | |
dfd66b78 | 122 | self.check_url(response, '/u/{0}/'.format(self.our_user().username)) |
7d503a89 | 123 | assert 'mediagoblin/user_pages/user.html' in context |
c2d6792d | 124 | # Make sure the media view is at least reachable, logged in... |
dfd66b78 | 125 | url = '/u/{0}/m/{1}/'.format(self.our_user().username, |
c0e87ec9 BS |
126 | title.lower().replace(' ', '-')) |
127 | self.test_app.get(url) | |
c2d6792d E |
128 | # ... and logged out too. |
129 | self.logout() | |
c0e87ec9 | 130 | self.test_app.get(url) |
c2d6792d | 131 | |
c3cce756 | 132 | def user_upload_limits(self, uploaded=None, upload_limit=None): |
fce8e969 CAW |
133 | our_user = self.our_user() |
134 | ||
c3cce756 | 135 | if uploaded: |
fce8e969 | 136 | our_user.uploaded = uploaded |
c3cce756 | 137 | if upload_limit: |
fce8e969 | 138 | our_user.upload_limit = upload_limit |
c3cce756 | 139 | |
fce8e969 CAW |
140 | our_user.save() |
141 | Session.expunge(our_user) | |
c3cce756 | 142 | |
0f346701 | 143 | def test_normal_jpg(self): |
c3cce756 | 144 | # User uploaded should be 0 |
fce8e969 | 145 | assert self.our_user().uploaded == 0 |
c3cce756 | 146 | |
5bd0adeb | 147 | self.check_normal_upload(u'Normal upload 1', GOOD_JPG) |
c0e87ec9 | 148 | |
c3cce756 RE |
149 | # User uploaded should be the same as GOOD_JPG size in Mb |
150 | file_size = os.stat(GOOD_JPG).st_size / (1024.0 * 1024) | |
151 | file_size = float('{0:.2f}'.format(file_size)) | |
152 | ||
153 | # Reload user | |
fce8e969 | 154 | assert self.our_user().uploaded == file_size |
c3cce756 | 155 | |
0f346701 | 156 | def test_normal_png(self): |
5bd0adeb | 157 | self.check_normal_upload(u'Normal upload 2', GOOD_PNG) |
75ce65cf | 158 | |
becd1607 | 159 | @pytest.mark.skipif("not os.path.exists(GOOD_PDF) or not pdf_check_prerequisites()") |
0f346701 | 160 | def test_normal_pdf(self): |
a80ebf3b AL |
161 | response, context = self.do_post({'title': u'Normal upload 3 (pdf)'}, |
162 | do_follow=True, | |
163 | **self.upload_data(GOOD_PDF)) | |
dfd66b78 | 164 | self.check_url(response, '/u/{0}/'.format(self.our_user().username)) |
a80ebf3b AL |
165 | assert 'mediagoblin/user_pages/user.html' in context |
166 | ||
c3cce756 RE |
167 | def test_default_upload_limits(self): |
168 | self.user_upload_limits(uploaded=500) | |
169 | ||
001a50a8 | 170 | # User uploaded should be 500 |
fce8e969 | 171 | assert self.our_user().uploaded == 500 |
001a50a8 | 172 | |
c3cce756 RE |
173 | response, context = self.do_post({'title': u'Normal upload 4'}, |
174 | do_follow=True, | |
175 | **self.upload_data(GOOD_JPG)) | |
fce8e969 | 176 | self.check_url(response, '/u/{0}/'.format(self.our_user().username)) |
c3cce756 RE |
177 | assert 'mediagoblin/user_pages/user.html' in context |
178 | ||
001a50a8 | 179 | # Shouldn't have uploaded |
fce8e969 | 180 | assert self.our_user().uploaded == 500 |
001a50a8 | 181 | |
c3cce756 RE |
182 | def test_user_upload_limit(self): |
183 | self.user_upload_limits(uploaded=25, upload_limit=25) | |
184 | ||
001a50a8 | 185 | # User uploaded should be 25 |
fce8e969 | 186 | assert self.our_user().uploaded == 25 |
001a50a8 | 187 | |
53cf5b45 | 188 | response, context = self.do_post({'title': u'Normal upload 5'}, |
c3cce756 RE |
189 | do_follow=True, |
190 | **self.upload_data(GOOD_JPG)) | |
fce8e969 | 191 | self.check_url(response, '/u/{0}/'.format(self.our_user().username)) |
c3cce756 RE |
192 | assert 'mediagoblin/user_pages/user.html' in context |
193 | ||
001a50a8 | 194 | # Shouldn't have uploaded |
fce8e969 | 195 | assert self.our_user().uploaded == 25 |
001a50a8 | 196 | |
c3cce756 RE |
197 | def test_user_under_limit(self): |
198 | self.user_upload_limits(uploaded=499) | |
199 | ||
001a50a8 | 200 | # User uploaded should be 499 |
fce8e969 | 201 | assert self.our_user().uploaded == 499 |
001a50a8 RE |
202 | |
203 | response, context = self.do_post({'title': u'Normal upload 6'}, | |
204 | do_follow=False, | |
205 | **self.upload_data(MED_PNG)) | |
206 | form = context['mediagoblin/submit/start.html']['submit_form'] | |
207 | assert form.file.errors == [u'Sorry, uploading this file will put you' | |
208 | ' over your upload limit.'] | |
209 | ||
1cb84a36 | 210 | # Shouldn't have uploaded |
fce8e969 | 211 | assert self.our_user().uploaded == 499 |
1cb84a36 | 212 | |
53cf5b45 RE |
213 | def test_big_file(self): |
214 | response, context = self.do_post({'title': u'Normal upload 7'}, | |
215 | do_follow=False, | |
216 | **self.upload_data(BIG_PNG)) | |
001a50a8 | 217 | |
53cf5b45 RE |
218 | form = context['mediagoblin/submit/start.html']['submit_form'] |
219 | assert form.file.errors == [u'Sorry, the file size is too big.'] | |
c3cce756 | 220 | |
77445d13 | 221 | def check_media(self, request, find_data, count=None): |
44082b12 | 222 | media = MediaEntry.query.filter_by(**find_data) |
77445d13 | 223 | if count is not None: |
7d503a89 | 224 | assert media.count() == count |
77445d13 BS |
225 | if count == 0: |
226 | return | |
227 | return media[0] | |
228 | ||
0f346701 | 229 | def test_tags(self): |
8ff4dec7 CFD |
230 | # Good tag string |
231 | # -------- | |
40cec2b4 | 232 | response, request = self.do_post({'title': u'Balanced Goblin 2', |
31dd6013 BS |
233 | 'tags': GOOD_TAG_STRING}, |
234 | *REQUEST_CONTEXT, do_follow=True, | |
235 | **self.upload_data(GOOD_JPG)) | |
40cec2b4 | 236 | media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1) |
38877794 CAW |
237 | assert media.tags[0]['name'] == u'yin' |
238 | assert media.tags[0]['slug'] == u'yin' | |
239 | ||
240 | assert media.tags[1]['name'] == u'yang' | |
241 | assert media.tags[1]['slug'] == u'yang' | |
8ff4dec7 CFD |
242 | |
243 | # Test tags that are too long | |
244 | # --------------- | |
40cec2b4 | 245 | response, form = self.do_post({'title': u'Balanced Goblin 2', |
31dd6013 BS |
246 | 'tags': BAD_TAG_STRING}, |
247 | *FORM_CONTEXT, | |
248 | **self.upload_data(GOOD_JPG)) | |
7d503a89 | 249 | assert form.tags.errors == [ |
d1f52dc7 BS |
250 | u'Tags must be shorter than 50 characters. ' \ |
251 | 'Tags that are too long: ' \ | |
7d503a89 | 252 | 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu'] |
75ce65cf | 253 | |
0f346701 | 254 | def test_delete(self): |
c3cce756 | 255 | self.user_upload_limits(uploaded=50) |
5bd0adeb | 256 | response, request = self.do_post({'title': u'Balanced Goblin'}, |
31dd6013 BS |
257 | *REQUEST_CONTEXT, do_follow=True, |
258 | **self.upload_data(GOOD_JPG)) | |
5bd0adeb | 259 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) |
e089b66b | 260 | media_id = media.id |
afe4e513 | 261 | |
d92fbdc4 | 262 | # render and post to the edit page. |
461dd971 E |
263 | edit_url = request.urlgen( |
264 | 'mediagoblin.edit.edit_media', | |
dfd66b78 | 265 | user=self.our_user().username, media_id=media_id) |
461dd971 | 266 | self.test_app.get(edit_url) |
d92fbdc4 E |
267 | self.test_app.post(edit_url, |
268 | {'title': u'Balanced Goblin', | |
269 | 'slug': u"Balanced=Goblin", | |
270 | 'tags': u''}) | |
271 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) | |
7d503a89 | 272 | assert media.slug == u"balanced-goblin" |
461dd971 | 273 | |
a0a7f87f | 274 | # Add a comment, so we can test for its deletion later. |
e089b66b | 275 | self.check_comments(request, media_id, 0) |
c16b8196 BS |
276 | comment_url = request.urlgen( |
277 | 'mediagoblin.user_pages.media_post_comment', | |
dfd66b78 | 278 | user=self.our_user().username, media_id=media_id) |
c16b8196 BS |
279 | response = self.do_post({'comment_content': 'i love this test'}, |
280 | url=comment_url, do_follow=True)[0] | |
e089b66b | 281 | self.check_comments(request, media_id, 1) |
a0a7f87f | 282 | |
afe4e513 JW |
283 | # Do not confirm deletion |
284 | # --------------------------------------------------- | |
31dd6013 BS |
285 | delete_url = request.urlgen( |
286 | 'mediagoblin.user_pages.media_confirm_delete', | |
dfd66b78 | 287 | user=self.our_user().username, media_id=media_id) |
31dd6013 BS |
288 | # Empty data means don't confirm |
289 | response = self.do_post({}, do_follow=True, url=delete_url)[0] | |
5bd0adeb | 290 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) |
e089b66b | 291 | media_id = media.id |
afe4e513 JW |
292 | |
293 | # Confirm deletion | |
294 | # --------------------------------------------------- | |
31dd6013 BS |
295 | response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT, |
296 | do_follow=True, url=delete_url) | |
5c2b8486 | 297 | self.check_media(request, {'id': media_id}, 0) |
e089b66b | 298 | self.check_comments(request, media_id, 0) |
afe4e513 | 299 | |
c3cce756 | 300 | # Check that user.uploaded is the same as before the upload |
fce8e969 | 301 | assert self.our_user().uploaded == 50 |
c3cce756 | 302 | |
0f346701 | 303 | def test_evil_file(self): |
ad35dd49 CM |
304 | # Test non-suppoerted file with non-supported extension |
305 | # ----------------------------------------------------- | |
5bd0adeb | 306 | response, form = self.do_post({'title': u'Malicious Upload 1'}, |
31dd6013 BS |
307 | *FORM_CONTEXT, |
308 | **self.upload_data(EVIL_FILE)) | |
7d503a89 | 309 | assert len(form.file.errors) == 1 |
0308958b JW |
310 | assert 'Sorry, I don\'t support that file type :(' == \ |
311 | str(form.file.errors[0]) | |
ad35dd49 | 312 | |
5f8b4ae8 | 313 | |
0f346701 | 314 | def test_get_media_manager(self): |
5f8b4ae8 SS |
315 | """Test if the get_media_manger function returns sensible things |
316 | """ | |
317 | response, request = self.do_post({'title': u'Balanced Goblin'}, | |
318 | *REQUEST_CONTEXT, do_follow=True, | |
319 | **self.upload_data(GOOD_JPG)) | |
320 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) | |
321 | ||
7d503a89 | 322 | assert media.media_type == u'mediagoblin.media_types.image' |
60eeb456 | 323 | assert isinstance(media.media_manager, ImageMediaManager) |
2077d6ed | 324 | assert media.media_manager.entry == media |
5f8b4ae8 SS |
325 | |
326 | ||
0f346701 | 327 | def test_sniffing(self): |
a9d84d4c JW |
328 | ''' |
329 | Test sniffing mechanism to assert that regular uploads work as intended | |
330 | ''' | |
331 | template.clear_test_template_context() | |
332 | response = self.test_app.post( | |
333 | '/submit/', { | |
5bd0adeb | 334 | 'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE' |
a9d84d4c JW |
335 | }, upload_files=[( |
336 | 'file', GOOD_JPG)]) | |
337 | ||
338 | response.follow() | |
339 | ||
340 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html'] | |
341 | ||
342 | request = context['request'] | |
343 | ||
44082b12 RE |
344 | media = request.db.MediaEntry.query.filter_by( |
345 | title=u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE').first() | |
a9d84d4c JW |
346 | |
347 | assert media.media_type == 'mediagoblin.media_types.image' | |
348 | ||
c3739034 | 349 | def check_false_image(self, title, filename): |
68f3ffbe CAW |
350 | # NOTE: The following 2 tests will ultimately fail, but they |
351 | # *will* pass the initial form submission step. Instead, | |
352 | # they'll be caught as failures during the processing step. | |
c3739034 BS |
353 | response, context = self.do_post({'title': title}, do_follow=True, |
354 | **self.upload_data(filename)) | |
dfd66b78 | 355 | self.check_url(response, '/u/{0}/'.format(self.our_user().username)) |
44082b12 | 356 | entry = mg_globals.database.MediaEntry.query.filter_by(title=title).first() |
7d503a89 CAW |
357 | assert entry.state == 'failed' |
358 | assert entry.fail_error == u'mediagoblin.processing:BadMediaFail' | |
9df37e8a | 359 | |
0f346701 | 360 | def test_evil_jpg(self): |
75ce65cf | 361 | # Test non-supported file with .jpg extension |
ad35dd49 | 362 | # ------------------------------------------- |
ed3ff88e | 363 | self.check_false_image(u'Malicious Upload 2', EVIL_JPG) |
ad35dd49 | 364 | |
0f346701 | 365 | def test_evil_png(self): |
75ce65cf | 366 | # Test non-supported file with .png extension |
ad35dd49 | 367 | # ------------------------------------------- |
ed3ff88e | 368 | self.check_false_image(u'Malicious Upload 3', EVIL_PNG) |
6573573d | 369 | |
0f346701 | 370 | def test_media_data(self): |
d728c636 E |
371 | self.check_normal_upload(u"With GPS data", GPS_JPG) |
372 | media = self.check_media(None, {"title": u"With GPS data"}, 1) | |
c0434db4 | 373 | assert media.get_location.position["latitude"] == 59.336666666666666 |
d728c636 | 374 | |
c41705bf BB |
375 | def test_audio(self): |
376 | with create_av(make_audio=True) as path: | |
377 | self.check_normal_upload('Audio', path) | |
378 | ||
379 | def test_video(self): | |
380 | with create_av(make_video=True) as path: | |
381 | self.check_normal_upload('Video', path) | |
382 | ||
383 | def test_audio_and_video(self): | |
384 | with create_av(make_audio=True, make_video=True) as path: | |
385 | self.check_normal_upload('Audio and Video', path) | |
386 | ||
0f346701 | 387 | def test_processing(self): |
5c2ece74 CAW |
388 | public_store_dir = mg_globals.global_config[ |
389 | 'storage:publicstore']['base_dir'] | |
390 | ||
5bd0adeb | 391 | data = {'title': u'Big Blue'} |
6573573d BS |
392 | response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True, |
393 | **self.upload_data(BIG_BLUE)) | |
394 | media = self.check_media(request, data, 1) | |
395 | last_size = 1024 ** 3 # Needs to be larger than bigblue.png | |
396 | for key, basename in (('original', 'bigblue.png'), | |
397 | ('medium', 'bigblue.medium.png'), | |
398 | ('thumb', 'bigblue.thumbnail.png')): | |
399 | # Does the processed image have a good filename? | |
5c2ece74 CAW |
400 | filename = os.path.join( |
401 | public_store_dir, | |
3b359ddd | 402 | *media.media_files[key]) |
7d503a89 | 403 | assert filename.endswith('_' + basename) |
6573573d BS |
404 | # Is it smaller than the last processed image we looked at? |
405 | size = os.stat(filename).st_size | |
7d503a89 | 406 | assert last_size > size |
6573573d | 407 | last_size = size |