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