Add collection drop down to submit page.
[larjonas-mediagoblin.git] / mediagoblin / tests / test_submission.py
blobeed7afa3e181c170cb19ed1d1f498b4a4893b5ca
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 import six
19 if six.PY2: # this hack only work in Python 2
20 import sys
21 reload(sys)
22 sys.setdefaultencoding('utf-8')
24 import os
25 import pytest
26 import webtest.forms
28 import six.moves.urllib.parse as urlparse
30 # this gst initialization stuff is really required here
31 import gi
32 gi.require_version('Gst', '1.0')
33 from gi.repository import Gst
34 Gst.init(None)
36 from mediagoblin.tests.tools import fixture_add_user, fixture_add_collection
37 from .media_tools import create_av
38 from mediagoblin import mg_globals
39 from mediagoblin.db.models import MediaEntry, User, LocalUser
40 from mediagoblin.db.base import Session
41 from mediagoblin.tools import template
42 from mediagoblin.media_types.image import ImageMediaManager
43 from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites
45 from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \
46 BIG_BLUE, GOOD_PDF, GPS_JPG, MED_PNG, BIG_PNG
48 GOOD_TAG_STRING = u'yin,yang'
49 BAD_TAG_STRING = six.text_type('rage,' + 'f' * 26 + 'u' * 26)
51 FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form']
52 REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request']
55 class TestSubmission:
56 @pytest.fixture(autouse=True)
57 def setup(self, test_app):
58 self.test_app = test_app
60 # TODO: Possibly abstract into a decorator like:
61 # @as_authenticated_user('chris')
62 fixture_add_user(privileges=[u'active',u'uploader', u'commenter'])
64 self.login()
66 def our_user(self):
67 """
68 Fetch the user we're submitting with. Every .get() or .post()
69 invalidates the session; this is a hacky workaround.
70 """
71 #### FIXME: Pytest collects this as a test and runs this.
72 #### ... it shouldn't. At least it passes, but that's
73 #### totally stupid.
74 #### Also if we found a way to make this run it should be a
75 #### property.
76 return LocalUser.query.filter(LocalUser.username==u'chris').first()
78 def login(self):
79 self.test_app.post(
80 '/auth/login/', {
81 'username': u'chris',
82 'password': 'toast'})
84 def logout(self):
85 self.test_app.get('/auth/logout/')
87 def do_post(self, data, *context_keys, **kwargs):
88 url = kwargs.pop('url', '/submit/')
89 do_follow = kwargs.pop('do_follow', False)
90 template.clear_test_template_context()
91 response = self.test_app.post(url, data, **kwargs)
92 if do_follow:
93 response.follow()
94 context_data = template.TEMPLATE_TEST_CONTEXT
95 for key in context_keys:
96 context_data = context_data[key]
97 return response, context_data
99 def upload_data(self, filename):
100 return {'upload_files': [('file', filename)]}
102 def check_comments(self, request, media_id, count):
103 gmr = request.db.GenericModelReference.query.filter_by(
104 obj_pk=media_id,
105 model_type=request.db.MediaEntry.__tablename__
106 ).first()
107 if gmr is None and count <= 0:
108 return # Yerp it's fine.
109 comments = request.db.Comment.query.filter_by(target_id=gmr.id)
110 assert count == comments.count()
112 def test_missing_fields(self):
113 # Test blank form
114 # ---------------
115 response, form = self.do_post({}, *FORM_CONTEXT)
116 assert form.file.errors == [u'You must provide a file.']
118 # Test blank file
119 # ---------------
120 response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT)
121 assert form.file.errors == [u'You must provide a file.']
123 def check_url(self, response, path):
124 assert urlparse.urlsplit(response.location)[2] == path
126 def check_normal_upload(self, title, filename):
127 response, context = self.do_post({'title': title}, do_follow=True,
128 **self.upload_data(filename))
129 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
130 assert 'mediagoblin/user_pages/user.html' in context
131 # Make sure the media view is at least reachable, logged in...
132 url = '/u/{0}/m/{1}/'.format(self.our_user().username,
133 title.lower().replace(' ', '-'))
134 self.test_app.get(url)
135 # ... and logged out too.
136 self.logout()
137 self.test_app.get(url)
139 def user_upload_limits(self, uploaded=None, upload_limit=None):
140 our_user = self.our_user()
142 if uploaded:
143 our_user.uploaded = uploaded
144 if upload_limit:
145 our_user.upload_limit = upload_limit
147 our_user.save()
148 Session.expunge(our_user)
150 def test_normal_jpg(self):
151 # User uploaded should be 0
152 assert self.our_user().uploaded == 0
154 self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
156 # User uploaded should be the same as GOOD_JPG size in Mb
157 file_size = os.stat(GOOD_JPG).st_size / (1024.0 * 1024)
158 file_size = float('{0:.2f}'.format(file_size))
160 # Reload user
161 assert self.our_user().uploaded == file_size
163 def test_public_id_populated(self):
164 # Upload the image first.
165 response, request = self.do_post({'title': u'Balanced Goblin'},
166 *REQUEST_CONTEXT, do_follow=True,
167 **self.upload_data(GOOD_JPG))
168 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
170 # Now check that the public_id attribute is set.
171 assert media.public_id != None
173 def test_normal_png(self):
174 self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
176 @pytest.mark.skipif("not os.path.exists(GOOD_PDF) or not pdf_check_prerequisites()")
177 def test_normal_pdf(self):
178 response, context = self.do_post({'title': u'Normal upload 3 (pdf)'},
179 do_follow=True,
180 **self.upload_data(GOOD_PDF))
181 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
182 assert 'mediagoblin/user_pages/user.html' in context
184 def test_default_upload_limits(self):
185 self.user_upload_limits(uploaded=500)
187 # User uploaded should be 500
188 assert self.our_user().uploaded == 500
190 response, context = self.do_post({'title': u'Normal upload 4'},
191 do_follow=True,
192 **self.upload_data(GOOD_JPG))
193 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
194 assert 'mediagoblin/user_pages/user.html' in context
196 # Shouldn't have uploaded
197 assert self.our_user().uploaded == 500
199 def test_user_upload_limit(self):
200 self.user_upload_limits(uploaded=25, upload_limit=25)
202 # User uploaded should be 25
203 assert self.our_user().uploaded == 25
205 response, context = self.do_post({'title': u'Normal upload 5'},
206 do_follow=True,
207 **self.upload_data(GOOD_JPG))
208 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
209 assert 'mediagoblin/user_pages/user.html' in context
211 # Shouldn't have uploaded
212 assert self.our_user().uploaded == 25
214 def test_user_under_limit(self):
215 self.user_upload_limits(uploaded=499)
217 # User uploaded should be 499
218 assert self.our_user().uploaded == 499
220 response, context = self.do_post({'title': u'Normal upload 6'},
221 do_follow=False,
222 **self.upload_data(MED_PNG))
223 form = context['mediagoblin/submit/start.html']['submit_form']
224 assert form.file.errors == [u'Sorry, uploading this file will put you'
225 ' over your upload limit.']
227 # Shouldn't have uploaded
228 assert self.our_user().uploaded == 499
230 def test_big_file(self):
231 response, context = self.do_post({'title': u'Normal upload 7'},
232 do_follow=False,
233 **self.upload_data(BIG_PNG))
235 form = context['mediagoblin/submit/start.html']['submit_form']
236 assert form.file.errors == [u'Sorry, the file size is too big.']
238 def check_media(self, request, find_data, count=None):
239 media = MediaEntry.query.filter_by(**find_data)
240 if count is not None:
241 assert media.count() == count
242 if count == 0:
243 return
244 return media[0]
246 def test_tags(self):
247 # Good tag string
248 # --------
249 response, request = self.do_post({'title': u'Balanced Goblin 2',
250 'tags': GOOD_TAG_STRING},
251 *REQUEST_CONTEXT, do_follow=True,
252 **self.upload_data(GOOD_JPG))
253 media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1)
254 assert media.tags[0]['name'] == u'yin'
255 assert media.tags[0]['slug'] == u'yin'
257 assert media.tags[1]['name'] == u'yang'
258 assert media.tags[1]['slug'] == u'yang'
260 # Test tags that are too long
261 # ---------------
262 response, form = self.do_post({'title': u'Balanced Goblin 2',
263 'tags': BAD_TAG_STRING},
264 *FORM_CONTEXT,
265 **self.upload_data(GOOD_JPG))
266 assert form.tags.errors == [
267 u'Tags must be shorter than 50 characters. ' \
268 'Tags that are too long: ' \
269 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
271 def test_delete(self):
272 self.user_upload_limits(uploaded=50)
273 response, request = self.do_post({'title': u'Balanced Goblin'},
274 *REQUEST_CONTEXT, do_follow=True,
275 **self.upload_data(GOOD_JPG))
276 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
277 media_id = media.id
279 # render and post to the edit page.
280 edit_url = request.urlgen(
281 'mediagoblin.edit.edit_media',
282 user=self.our_user().username, media_id=media_id)
283 self.test_app.get(edit_url)
284 self.test_app.post(edit_url,
285 {'title': u'Balanced Goblin',
286 'slug': u"Balanced=Goblin",
287 'tags': u''})
288 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
289 assert media.slug == u"balanced-goblin"
291 # Add a comment, so we can test for its deletion later.
292 self.check_comments(request, media_id, 0)
293 comment_url = request.urlgen(
294 'mediagoblin.user_pages.media_post_comment',
295 user=self.our_user().username, media_id=media_id)
296 response = self.do_post({'comment_content': 'i love this test'},
297 url=comment_url, do_follow=True)[0]
298 self.check_comments(request, media_id, 1)
300 # Do not confirm deletion
301 # ---------------------------------------------------
302 delete_url = request.urlgen(
303 'mediagoblin.user_pages.media_confirm_delete',
304 user=self.our_user().username, media_id=media_id)
305 # Empty data means don't confirm
306 response = self.do_post({}, do_follow=True, url=delete_url)[0]
307 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
308 media_id = media.id
310 # Confirm deletion
311 # ---------------------------------------------------
312 response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
313 do_follow=True, url=delete_url)
314 self.check_media(request, {'id': media_id}, 0)
315 self.check_comments(request, media_id, 0)
317 # Check that user.uploaded is the same as before the upload
318 assert self.our_user().uploaded == 50
320 def test_evil_file(self):
321 # Test non-suppoerted file with non-supported extension
322 # -----------------------------------------------------
323 response, form = self.do_post({'title': u'Malicious Upload 1'},
324 *FORM_CONTEXT,
325 **self.upload_data(EVIL_FILE))
326 assert len(form.file.errors) == 1
327 assert 'Sorry, I don\'t support that file type :(' == \
328 str(form.file.errors[0])
331 def test_get_media_manager(self):
332 """Test if the get_media_manger function returns sensible things
334 response, request = self.do_post({'title': u'Balanced Goblin'},
335 *REQUEST_CONTEXT, do_follow=True,
336 **self.upload_data(GOOD_JPG))
337 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
339 assert media.media_type == u'mediagoblin.media_types.image'
340 assert isinstance(media.media_manager, ImageMediaManager)
341 assert media.media_manager.entry == media
344 def test_sniffing(self):
346 Test sniffing mechanism to assert that regular uploads work as intended
348 template.clear_test_template_context()
349 response = self.test_app.post(
350 '/submit/', {
351 'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
352 }, upload_files=[(
353 'file', GOOD_JPG)])
355 response.follow()
357 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html']
359 request = context['request']
361 media = request.db.MediaEntry.query.filter_by(
362 title=u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE').first()
364 assert media.media_type == 'mediagoblin.media_types.image'
366 def check_false_image(self, title, filename):
367 # NOTE: The following 2 tests will ultimately fail, but they
368 # *will* pass the initial form submission step. Instead,
369 # they'll be caught as failures during the processing step.
370 response, context = self.do_post({'title': title}, do_follow=True,
371 **self.upload_data(filename))
372 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
373 entry = mg_globals.database.MediaEntry.query.filter_by(title=title).first()
374 assert entry.state == 'failed'
375 assert entry.fail_error == u'mediagoblin.processing:BadMediaFail'
377 def test_evil_jpg(self):
378 # Test non-supported file with .jpg extension
379 # -------------------------------------------
380 self.check_false_image(u'Malicious Upload 2', EVIL_JPG)
382 def test_evil_png(self):
383 # Test non-supported file with .png extension
384 # -------------------------------------------
385 self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
387 def test_media_data(self):
388 self.check_normal_upload(u"With GPS data", GPS_JPG)
389 media = self.check_media(None, {"title": u"With GPS data"}, 1)
390 assert media.get_location.position["latitude"] == 59.336666666666666
392 def test_audio(self):
393 with create_av(make_audio=True) as path:
394 self.check_normal_upload('Audio', path)
396 def test_video(self):
397 with create_av(make_video=True) as path:
398 self.check_normal_upload('Video', path)
400 def test_audio_and_video(self):
401 with create_av(make_audio=True, make_video=True) as path:
402 self.check_normal_upload('Audio and Video', path)
404 def test_processing(self):
405 public_store_dir = mg_globals.global_config[
406 'storage:publicstore']['base_dir']
408 data = {'title': u'Big Blue'}
409 response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True,
410 **self.upload_data(BIG_BLUE))
411 media = self.check_media(request, data, 1)
412 last_size = 1024 ** 3 # Needs to be larger than bigblue.png
413 for key, basename in (('original', 'bigblue.png'),
414 ('medium', 'bigblue.medium.png'),
415 ('thumb', 'bigblue.thumbnail.png')):
416 # Does the processed image have a good filename?
417 filename = os.path.join(
418 public_store_dir,
419 *media.media_files[key])
420 assert filename.endswith('_' + basename)
421 # Is it smaller than the last processed image we looked at?
422 size = os.stat(filename).st_size
423 assert last_size > size
424 last_size = size
426 def test_collection_selection(self):
427 """Test the ability to choose a collection when submitting media
429 # Collection option shouldn't be present if the user has no collections
430 response = self.test_app.get('/submit/')
431 assert 'collection' not in response.form.fields
433 upload = webtest.forms.Upload(os.path.join(
434 'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png'))
436 # Check that upload of an image when a user has no collections
437 response.form['file'] = upload
438 no_collection_title = 'no collection'
439 response.form['title'] = no_collection_title
440 response.form.submit()
441 assert MediaEntry.query.filter_by(
442 actor=self.our_user().id
443 ).first().title == no_collection_title
445 # Collection option should be present if the user has collections. It
446 # shouldn't allow other users' collections to be selected.
447 col = fixture_add_collection(user=self.our_user())
448 user = fixture_add_user(username=u'different')
449 fixture_add_collection(user=user, name=u'different')
450 response = self.test_app.get('/submit/')
451 form = response.form
452 assert 'collection' in form.fields
453 # Option length is 2, because of the default "--Select--" option
454 assert len(form['collection'].options) == 2
455 assert form['collection'].options[1][2] == col.title
457 # Test that if we specify a collection then the media entry is added to
458 # the specified collection.
459 form['file'] = upload
460 title = 'new picture'
461 form['title'] = title
462 form['collection'] = form['collection'].options[1][0]
463 form.submit()
464 # The title of the first item in our user's first collection should
465 # match the title of the picture that was just added.
466 col = self.our_user().collections[0]
467 assert col.collection_items[0].get_object().title == title
469 # Test upload succeeds if the user has collection and no collection is
470 # chosen.
471 form['file'] = webtest.forms.Upload(os.path.join(
472 'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png'))
473 title = 'no collection 2'
474 form['title'] = title
475 form['collection'] = form['collection'].options[0][0]
476 form.submit()
477 # The title of the first item in our user's first collection should
478 # match the title of the picture that was just added.
479 assert MediaEntry.query.filter_by(
480 actor=self.our_user().id
481 ).count() == 3