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/>.
19 if six
.PY2
: # this hack only work in Python 2
22 sys
.setdefaultencoding('utf-8')
27 import six
.moves
.urllib
.parse
as urlparse
29 # this gst initialization stuff is really required here
31 gi
.require_version('Gst', '1.0')
32 from gi
.repository
import Gst
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
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
47 GOOD_TAG_STRING
= u
'yin,yang'
48 BAD_TAG_STRING
= six
.text_type('rage,' + 'f' * 26 + 'u' * 26)
50 FORM_CONTEXT
= ['mediagoblin/submit/start.html', 'submit_form']
51 REQUEST_CONTEXT
= ['mediagoblin/user_pages/user.html', 'request']
55 @pytest.fixture(autouse
=True)
56 def setup(self
, test_app
):
57 self
.test_app
= test_app
59 # TODO: Possibly abstract into a decorator like:
60 # @as_authenticated_user('chris')
61 fixture_add_user(privileges
=[u
'active',u
'uploader', u
'commenter'])
67 Fetch the user we're submitting with. Every .get() or .post()
68 invalidates the session; this is a hacky workaround.
70 #### FIXME: Pytest collects this as a test and runs this.
71 #### ... it shouldn't. At least it passes, but that's
73 #### Also if we found a way to make this run it should be a
75 return User
.query
.filter(User
.username
==u
'chris').first()
84 self
.test_app
.get('/auth/logout/')
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
)
93 context_data
= template
.TEMPLATE_TEST_CONTEXT
94 for key
in context_keys
:
95 context_data
= context_data
[key
]
96 return response
, context_data
98 def upload_data(self
, filename
):
99 return {'upload_files': [('file', filename
)]}
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
))
105 def test_missing_fields(self
):
108 response
, form
= self
.do_post({}, *FORM_CONTEXT
)
109 assert form
.file.errors
== [u
'You must provide a file.']
113 response
, form
= self
.do_post({'title': u
'test title'}, *FORM_CONTEXT
)
114 assert form
.file.errors
== [u
'You must provide a file.']
116 def check_url(self
, response
, path
):
117 assert urlparse
.urlsplit(response
.location
)[2] == path
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.
130 self
.test_app
.get(url
)
132 def user_upload_limits(self
, uploaded
=None, upload_limit
=None):
133 our_user
= self
.our_user()
136 our_user
.uploaded
= uploaded
138 our_user
.upload_limit
= upload_limit
141 Session
.expunge(our_user
)
143 def test_normal_jpg(self
):
144 # User uploaded should be 0
145 assert self
.our_user().uploaded
== 0
147 self
.check_normal_upload(u
'Normal upload 1', GOOD_JPG
)
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
))
154 assert self
.our_user().uploaded
== file_size
156 def test_normal_png(self
):
157 self
.check_normal_upload(u
'Normal upload 2', GOOD_PNG
)
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)'},
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
167 def test_default_upload_limits(self
):
168 self
.user_upload_limits(uploaded
=500)
170 # User uploaded should be 500
171 assert self
.our_user().uploaded
== 500
173 response
, context
= self
.do_post({'title': u
'Normal upload 4'},
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
179 # Shouldn't have uploaded
180 assert self
.our_user().uploaded
== 500
182 def test_user_upload_limit(self
):
183 self
.user_upload_limits(uploaded
=25, upload_limit
=25)
185 # User uploaded should be 25
186 assert self
.our_user().uploaded
== 25
188 response
, context
= self
.do_post({'title': u
'Normal upload 5'},
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
194 # Shouldn't have uploaded
195 assert self
.our_user().uploaded
== 25
197 def test_user_under_limit(self
):
198 self
.user_upload_limits(uploaded
=499)
200 # User uploaded should be 499
201 assert self
.our_user().uploaded
== 499
203 response
, context
= self
.do_post({'title': u
'Normal upload 6'},
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.']
210 # Shouldn't have uploaded
211 assert self
.our_user().uploaded
== 499
213 def test_big_file(self
):
214 response
, context
= self
.do_post({'title': u
'Normal upload 7'},
216 **self
.upload_data(BIG_PNG
))
218 form
= context
['mediagoblin/submit/start.html']['submit_form']
219 assert form
.file.errors
== [u
'Sorry, the file size is too big.']
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
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'
240 assert media
.tags
[1]['name'] == u
'yang'
241 assert media
.tags
[1]['slug'] == u
'yang'
243 # Test tags that are too long
245 response
, form
= self
.do_post({'title': u
'Balanced Goblin 2',
246 'tags': BAD_TAG_STRING
},
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']
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)
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",
271 media
= self
.check_media(request
, {'title': u
'Balanced Goblin'}, 1)
272 assert media
.slug
== u
"balanced-goblin"
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)
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)
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)
300 # Check that user.uploaded is the same as before the upload
301 assert self
.our_user().uploaded
== 50
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'},
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])
314 def test_get_media_manager(self
):
315 """Test if the get_media_manger function returns sensible things
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)
322 assert media
.media_type
== u
'mediagoblin.media_types.image'
323 assert isinstance(media
.media_manager
, ImageMediaManager
)
324 assert media
.media_manager
.entry
== media
327 def test_sniffing(self
):
329 Test sniffing mechanism to assert that regular uploads work as intended
331 template
.clear_test_template_context()
332 response
= self
.test_app
.post(
334 'title': u
'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
340 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/user_pages/user.html']
342 request
= context
['request']
344 media
= request
.db
.MediaEntry
.query
.filter_by(
345 title
=u
'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE').first()
347 assert media
.media_type
== 'mediagoblin.media_types.image'
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'
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
)
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
)
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
375 def test_audio(self
):
376 with
create_av(make_audio
=True) as path
:
377 self
.check_normal_upload('Audio', path
)
379 def test_video(self
):
380 with
create_av(make_video
=True) as path
:
381 self
.check_normal_upload('Video', path
)
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
)
387 def test_processing(self
):
388 public_store_dir
= mg_globals
.global_config
[
389 'storage:publicstore']['base_dir']
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(
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