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 sys
.setdefaultencoding('utf-8')
24 from nose
.tools
import assert_equal
, assert_true
25 from pkg_resources
import resource_filename
27 from mediagoblin
.tests
.tools
import get_test_app
, \
29 from mediagoblin
import mg_globals
30 from mediagoblin
.tools
import template
31 from mediagoblin
.media_types
.image
import MEDIA_MANAGER
as img_MEDIA_MANAGER
33 def resource(filename
):
34 return resource_filename('mediagoblin.tests', 'test_submission/' + filename
)
37 GOOD_JPG
= resource('good.jpg')
38 GOOD_PNG
= resource('good.png')
39 EVIL_FILE
= resource('evil')
40 EVIL_JPG
= resource('evil.jpg')
41 EVIL_PNG
= resource('evil.png')
42 BIG_BLUE
= resource('bigblue.png')
44 GOOD_TAG_STRING
= u
'yin,yang'
45 BAD_TAG_STRING
= unicode('rage,' + 'f' * 26 + 'u' * 26)
47 FORM_CONTEXT
= ['mediagoblin/submit/start.html', 'submit_form']
48 REQUEST_CONTEXT
= ['mediagoblin/user_pages/user.html', 'request']
53 self
.test_app
= get_test_app(dump_old_app
=False)
55 # TODO: Possibly abstract into a decorator like:
56 # @as_authenticated_user('chris')
57 test_user
= fixture_add_user()
59 self
.test_user
= test_user
70 self
.test_app
.get('/auth/logout/')
72 def do_post(self
, data
, *context_keys
, **kwargs
):
73 url
= kwargs
.pop('url', '/submit/')
74 do_follow
= kwargs
.pop('do_follow', False)
75 template
.clear_test_template_context()
76 response
= self
.test_app
.post(url
, data
, **kwargs
)
79 context_data
= template
.TEMPLATE_TEST_CONTEXT
80 for key
in context_keys
:
81 context_data
= context_data
[key
]
82 return response
, context_data
84 def upload_data(self
, filename
):
85 return {'upload_files': [('file', filename
)]}
87 def check_comments(self
, request
, media_id
, count
):
88 comments
= request
.db
.MediaComment
.find({'media_entry': media_id
})
89 assert_equal(count
, len(list(comments
)))
91 def test_missing_fields(self
):
94 response
, form
= self
.do_post({}, *FORM_CONTEXT
)
95 assert_equal(form
.file.errors
, [u
'You must provide a file.'])
99 response
, form
= self
.do_post({'title': u
'test title'}, *FORM_CONTEXT
)
100 assert_equal(form
.file.errors
, [u
'You must provide a file.'])
102 def check_url(self
, response
, path
):
103 assert_equal(urlparse
.urlsplit(response
.location
)[2], path
)
105 def check_normal_upload(self
, title
, filename
):
106 response
, context
= self
.do_post({'title': title
}, do_follow
=True,
107 **self
.upload_data(filename
))
108 self
.check_url(response
, '/u/{0}/'.format(self
.test_user
.username
))
109 assert_true('mediagoblin/user_pages/user.html' in context
)
110 # Make sure the media view is at least reachable, logged in...
111 url
= '/u/{0}/m/{1}/'.format(self
.test_user
.username
,
112 title
.lower().replace(' ', '-'))
113 self
.test_app
.get(url
)
114 # ... and logged out too.
116 self
.test_app
.get(url
)
118 def test_normal_jpg(self
):
119 self
.check_normal_upload(u
'Normal upload 1', GOOD_JPG
)
121 def test_normal_png(self
):
122 self
.check_normal_upload(u
'Normal upload 2', GOOD_PNG
)
124 def check_media(self
, request
, find_data
, count
=None):
125 media
= request
.db
.MediaEntry
.find(find_data
)
126 if count
is not None:
127 assert_equal(media
.count(), count
)
135 response
, request
= self
.do_post({'title': u
'Balanced Goblin 2',
136 'tags': GOOD_TAG_STRING
},
137 *REQUEST_CONTEXT
, do_follow
=True,
138 **self
.upload_data(GOOD_JPG
))
139 media
= self
.check_media(request
, {'title': u
'Balanced Goblin 2'}, 1)
140 assert media
.tags
[0]['name'] == u
'yin'
141 assert media
.tags
[0]['slug'] == u
'yin'
143 assert media
.tags
[1]['name'] == u
'yang'
144 assert media
.tags
[1]['slug'] == u
'yang'
146 # Test tags that are too long
148 response
, form
= self
.do_post({'title': u
'Balanced Goblin 2',
149 'tags': BAD_TAG_STRING
},
151 **self
.upload_data(GOOD_JPG
))
152 assert_equal(form
.tags
.errors
, [
153 u
'Tags must be shorter than 50 characters. ' \
154 'Tags that are too long: ' \
155 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu'])
157 def test_delete(self
):
158 response
, request
= self
.do_post({'title': u
'Balanced Goblin'},
159 *REQUEST_CONTEXT
, do_follow
=True,
160 **self
.upload_data(GOOD_JPG
))
161 media
= self
.check_media(request
, {'title': u
'Balanced Goblin'}, 1)
164 # At least render the edit page
165 edit_url
= request
.urlgen(
166 'mediagoblin.edit.edit_media',
167 user
=self
.test_user
.username
, media_id
=media_id
)
168 self
.test_app
.get(edit_url
)
170 # Add a comment, so we can test for its deletion later.
171 self
.check_comments(request
, media_id
, 0)
172 comment_url
= request
.urlgen(
173 'mediagoblin.user_pages.media_post_comment',
174 user
=self
.test_user
.username
, media_id
=media_id
)
175 response
= self
.do_post({'comment_content': 'i love this test'},
176 url
=comment_url
, do_follow
=True)[0]
177 self
.check_comments(request
, media_id
, 1)
179 # Do not confirm deletion
180 # ---------------------------------------------------
181 delete_url
= request
.urlgen(
182 'mediagoblin.user_pages.media_confirm_delete',
183 user
=self
.test_user
.username
, media_id
=media_id
)
184 # Empty data means don't confirm
185 response
= self
.do_post({}, do_follow
=True, url
=delete_url
)[0]
186 media
= self
.check_media(request
, {'title': u
'Balanced Goblin'}, 1)
190 # ---------------------------------------------------
191 response
, request
= self
.do_post({'confirm': 'y'}, *REQUEST_CONTEXT
,
192 do_follow
=True, url
=delete_url
)
193 self
.check_media(request
, {'id': media_id
}, 0)
194 self
.check_comments(request
, media_id
, 0)
196 def test_evil_file(self
):
197 # Test non-suppoerted file with non-supported extension
198 # -----------------------------------------------------
199 response
, form
= self
.do_post({'title': u
'Malicious Upload 1'},
201 **self
.upload_data(EVIL_FILE
))
202 assert_equal(len(form
.file.errors
), 1)
203 assert 'Sorry, I don\'t support that file type :(' == \
204 str(form
.file.errors
[0])
207 def test_get_media_manager(self
):
208 """Test if the get_media_manger function returns sensible things
210 response
, request
= self
.do_post({'title': u
'Balanced Goblin'},
211 *REQUEST_CONTEXT
, do_follow
=True,
212 **self
.upload_data(GOOD_JPG
))
213 media
= self
.check_media(request
, {'title': u
'Balanced Goblin'}, 1)
215 assert_equal(media
.media_type
, u
'mediagoblin.media_types.image')
216 assert_equal(media
.media_manager
, img_MEDIA_MANAGER
)
219 def test_sniffing(self
):
221 Test sniffing mechanism to assert that regular uploads work as intended
223 template
.clear_test_template_context()
224 response
= self
.test_app
.post(
226 'title': u
'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
232 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/user_pages/user.html']
234 request
= context
['request']
236 media
= request
.db
.MediaEntry
.find_one({
237 u
'title': u
'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'})
239 assert media
.media_type
== 'mediagoblin.media_types.image'
241 def check_false_image(self
, title
, filename
):
242 # NOTE: The following 2 tests will ultimately fail, but they
243 # *will* pass the initial form submission step. Instead,
244 # they'll be caught as failures during the processing step.
245 response
, context
= self
.do_post({'title': title
}, do_follow
=True,
246 **self
.upload_data(filename
))
247 self
.check_url(response
, '/u/{0}/'.format(self
.test_user
.username
))
248 entry
= mg_globals
.database
.MediaEntry
.find_one({'title': title
})
249 assert_equal(entry
.state
, 'failed')
250 assert_equal(entry
.fail_error
, u
'mediagoblin.processing:BadMediaFail')
252 def test_evil_jpg(self
):
253 # Test non-supported file with .jpg extension
254 # -------------------------------------------
255 self
.check_false_image(u
'Malicious Upload 2', EVIL_JPG
)
257 def test_evil_png(self
):
258 # Test non-supported file with .png extension
259 # -------------------------------------------
260 self
.check_false_image(u
'Malicious Upload 3', EVIL_PNG
)
262 def test_processing(self
):
263 data
= {'title': u
'Big Blue'}
264 response
, request
= self
.do_post(data
, *REQUEST_CONTEXT
, do_follow
=True,
265 **self
.upload_data(BIG_BLUE
))
266 media
= self
.check_media(request
, data
, 1)
267 last_size
= 1024 ** 3 # Needs to be larger than bigblue.png
268 for key
, basename
in (('original', 'bigblue.png'),
269 ('medium', 'bigblue.medium.png'),
270 ('thumb', 'bigblue.thumbnail.png')):
271 # Does the processed image have a good filename?
272 filename
= resource_filename(
274 os
.path
.join('test_user_dev/media/public',
275 *media
.media_files
.get(key
, [])))
276 assert_true(filename
.endswith('_' + basename
))
277 # Is it smaller than the last processed image we looked at?
278 size
= os
.stat(filename
).st_size
279 assert_true(last_size
> size
)