added a/v submission testing
[mediagoblin.git] / mediagoblin / tests / test_submission.py
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 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')
23
24 import os
25 import pytest
26
27 import six.moves.urllib.parse as urlparse
28
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
35 from mediagoblin.tests.tools import fixture_add_user
36 from .media_tools import create_av
37 from mediagoblin import mg_globals
38 from mediagoblin.db.models import MediaEntry, User
39 from mediagoblin.db.base import Session
40 from mediagoblin.tools import template
41 from mediagoblin.media_types.image import ImageMediaManager
42 from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites
43
44 from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \
45 BIG_BLUE, GOOD_PDF, GPS_JPG, MED_PNG, BIG_PNG
46
47 GOOD_TAG_STRING = u'yin,yang'
48 BAD_TAG_STRING = six.text_type('rage,' + 'f' * 26 + 'u' * 26)
49
50 FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form']
51 REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request']
52
53
54 class TestSubmission:
55 @pytest.fixture(autouse=True)
56 def setup(self, test_app):
57 self.test_app = test_app
58
59 # TODO: Possibly abstract into a decorator like:
60 # @as_authenticated_user('chris')
61 fixture_add_user(privileges=[u'active',u'uploader', u'commenter'])
62
63 self.login()
64
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
77 def login(self):
78 self.test_app.post(
79 '/auth/login/', {
80 'username': u'chris',
81 'password': 'toast'})
82
83 def logout(self):
84 self.test_app.get('/auth/logout/')
85
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
97
98 def upload_data(self, filename):
99 return {'upload_files': [('file', filename)]}
100
101 def check_comments(self, request, media_id, count):
102 comments = request.db.MediaComment.query.filter_by(media_entry=media_id)
103 assert count == len(list(comments))
104
105 def test_missing_fields(self):
106 # Test blank form
107 # ---------------
108 response, form = self.do_post({}, *FORM_CONTEXT)
109 assert form.file.errors == [u'You must provide a file.']
110
111 # Test blank file
112 # ---------------
113 response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT)
114 assert form.file.errors == [u'You must provide a file.']
115
116 def check_url(self, response, path):
117 assert urlparse.urlsplit(response.location)[2] == path
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))
122 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
123 assert 'mediagoblin/user_pages/user.html' in context
124 # Make sure the media view is at least reachable, logged in...
125 url = '/u/{0}/m/{1}/'.format(self.our_user().username,
126 title.lower().replace(' ', '-'))
127 self.test_app.get(url)
128 # ... and logged out too.
129 self.logout()
130 self.test_app.get(url)
131
132 def user_upload_limits(self, uploaded=None, upload_limit=None):
133 our_user = self.our_user()
134
135 if uploaded:
136 our_user.uploaded = uploaded
137 if upload_limit:
138 our_user.upload_limit = upload_limit
139
140 our_user.save()
141 Session.expunge(our_user)
142
143 def test_normal_jpg(self):
144 # User uploaded should be 0
145 assert self.our_user().uploaded == 0
146
147 self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
148
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
154 assert self.our_user().uploaded == file_size
155
156 def test_normal_png(self):
157 self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
158
159 @pytest.mark.skipif("not os.path.exists(GOOD_PDF) or not pdf_check_prerequisites()")
160 def test_normal_pdf(self):
161 response, context = self.do_post({'title': u'Normal upload 3 (pdf)'},
162 do_follow=True,
163 **self.upload_data(GOOD_PDF))
164 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
165 assert 'mediagoblin/user_pages/user.html' in context
166
167 def test_default_upload_limits(self):
168 self.user_upload_limits(uploaded=500)
169
170 # User uploaded should be 500
171 assert self.our_user().uploaded == 500
172
173 response, context = self.do_post({'title': u'Normal upload 4'},
174 do_follow=True,
175 **self.upload_data(GOOD_JPG))
176 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
177 assert 'mediagoblin/user_pages/user.html' in context
178
179 # Shouldn't have uploaded
180 assert self.our_user().uploaded == 500
181
182 def test_user_upload_limit(self):
183 self.user_upload_limits(uploaded=25, upload_limit=25)
184
185 # User uploaded should be 25
186 assert self.our_user().uploaded == 25
187
188 response, context = self.do_post({'title': u'Normal upload 5'},
189 do_follow=True,
190 **self.upload_data(GOOD_JPG))
191 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
192 assert 'mediagoblin/user_pages/user.html' in context
193
194 # Shouldn't have uploaded
195 assert self.our_user().uploaded == 25
196
197 def test_user_under_limit(self):
198 self.user_upload_limits(uploaded=499)
199
200 # User uploaded should be 499
201 assert self.our_user().uploaded == 499
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
210 # Shouldn't have uploaded
211 assert self.our_user().uploaded == 499
212
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))
217
218 form = context['mediagoblin/submit/start.html']['submit_form']
219 assert form.file.errors == [u'Sorry, the file size is too big.']
220
221 def check_media(self, request, find_data, count=None):
222 media = MediaEntry.query.filter_by(**find_data)
223 if count is not None:
224 assert media.count() == count
225 if count == 0:
226 return
227 return media[0]
228
229 def test_tags(self):
230 # Good tag string
231 # --------
232 response, request = self.do_post({'title': u'Balanced Goblin 2',
233 'tags': GOOD_TAG_STRING},
234 *REQUEST_CONTEXT, do_follow=True,
235 **self.upload_data(GOOD_JPG))
236 media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1)
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'
242
243 # Test tags that are too long
244 # ---------------
245 response, form = self.do_post({'title': u'Balanced Goblin 2',
246 'tags': BAD_TAG_STRING},
247 *FORM_CONTEXT,
248 **self.upload_data(GOOD_JPG))
249 assert form.tags.errors == [
250 u'Tags must be shorter than 50 characters. ' \
251 'Tags that are too long: ' \
252 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
253
254 def test_delete(self):
255 self.user_upload_limits(uploaded=50)
256 response, request = self.do_post({'title': u'Balanced Goblin'},
257 *REQUEST_CONTEXT, do_follow=True,
258 **self.upload_data(GOOD_JPG))
259 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
260 media_id = media.id
261
262 # render and post to the edit page.
263 edit_url = request.urlgen(
264 'mediagoblin.edit.edit_media',
265 user=self.our_user().username, media_id=media_id)
266 self.test_app.get(edit_url)
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)
272 assert media.slug == u"balanced-goblin"
273
274 # Add a comment, so we can test for its deletion later.
275 self.check_comments(request, media_id, 0)
276 comment_url = request.urlgen(
277 'mediagoblin.user_pages.media_post_comment',
278 user=self.our_user().username, media_id=media_id)
279 response = self.do_post({'comment_content': 'i love this test'},
280 url=comment_url, do_follow=True)[0]
281 self.check_comments(request, media_id, 1)
282
283 # Do not confirm deletion
284 # ---------------------------------------------------
285 delete_url = request.urlgen(
286 'mediagoblin.user_pages.media_confirm_delete',
287 user=self.our_user().username, media_id=media_id)
288 # Empty data means don't confirm
289 response = self.do_post({}, do_follow=True, url=delete_url)[0]
290 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
291 media_id = media.id
292
293 # Confirm deletion
294 # ---------------------------------------------------
295 response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
296 do_follow=True, url=delete_url)
297 self.check_media(request, {'id': media_id}, 0)
298 self.check_comments(request, media_id, 0)
299
300 # Check that user.uploaded is the same as before the upload
301 assert self.our_user().uploaded == 50
302
303 def test_evil_file(self):
304 # Test non-suppoerted file with non-supported extension
305 # -----------------------------------------------------
306 response, form = self.do_post({'title': u'Malicious Upload 1'},
307 *FORM_CONTEXT,
308 **self.upload_data(EVIL_FILE))
309 assert len(form.file.errors) == 1
310 assert 'Sorry, I don\'t support that file type :(' == \
311 str(form.file.errors[0])
312
313
314 def test_get_media_manager(self):
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
322 assert media.media_type == u'mediagoblin.media_types.image'
323 assert isinstance(media.media_manager, ImageMediaManager)
324 assert media.media_manager.entry == media
325
326
327 def test_sniffing(self):
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/', {
334 'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
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
344 media = request.db.MediaEntry.query.filter_by(
345 title=u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE').first()
346
347 assert media.media_type == 'mediagoblin.media_types.image'
348
349 def check_false_image(self, title, filename):
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.
353 response, context = self.do_post({'title': title}, do_follow=True,
354 **self.upload_data(filename))
355 self.check_url(response, '/u/{0}/'.format(self.our_user().username))
356 entry = mg_globals.database.MediaEntry.query.filter_by(title=title).first()
357 assert entry.state == 'failed'
358 assert entry.fail_error == u'mediagoblin.processing:BadMediaFail'
359
360 def test_evil_jpg(self):
361 # Test non-supported file with .jpg extension
362 # -------------------------------------------
363 self.check_false_image(u'Malicious Upload 2', EVIL_JPG)
364
365 def test_evil_png(self):
366 # Test non-supported file with .png extension
367 # -------------------------------------------
368 self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
369
370 def test_media_data(self):
371 self.check_normal_upload(u"With GPS data", GPS_JPG)
372 media = self.check_media(None, {"title": u"With GPS data"}, 1)
373 assert media.get_location.position["latitude"] == 59.336666666666666
374
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
387 def test_processing(self):
388 public_store_dir = mg_globals.global_config[
389 'storage:publicstore']['base_dir']
390
391 data = {'title': u'Big Blue'}
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?
400 filename = os.path.join(
401 public_store_dir,
402 *media.media_files[key])
403 assert filename.endswith('_' + basename)
404 # Is it smaller than the last processed image we looked at?
405 size = os.stat(filename).st_size
406 assert last_size > size
407 last_size = size