add pdf media type
[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 sys
18 reload(sys)
19 sys.setdefaultencoding('utf-8')
20
21 import urlparse
22 import os
23
24 from pkg_resources import resource_filename
25
26 from mediagoblin.tests.tools import fixture_add_user
27 from mediagoblin import mg_globals
28 from mediagoblin.db.models import MediaEntry
29 from mediagoblin.tools import template
30 from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER
31 from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites
32
33 def resource(filename):
34 return resource_filename('mediagoblin.tests', 'test_submission/' + filename)
35
36
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')
43 GOOD_PDF = resource('good.pdf')
44
45 from .test_exif import GPS_JPG
46
47 GOOD_TAG_STRING = u'yin,yang'
48 BAD_TAG_STRING = unicode('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 def _setup(self, test_app):
56 self.test_app = test_app
57
58 # TODO: Possibly abstract into a decorator like:
59 # @as_authenticated_user('chris')
60 test_user = fixture_add_user()
61
62 self.test_user = test_user
63
64 self.login()
65
66 def login(self):
67 self.test_app.post(
68 '/auth/login/', {
69 'username': u'chris',
70 'password': 'toast'})
71
72 def logout(self):
73 self.test_app.get('/auth/logout/')
74
75 def do_post(self, data, *context_keys, **kwargs):
76 url = kwargs.pop('url', '/submit/')
77 do_follow = kwargs.pop('do_follow', False)
78 template.clear_test_template_context()
79 response = self.test_app.post(url, data, **kwargs)
80 if do_follow:
81 response.follow()
82 context_data = template.TEMPLATE_TEST_CONTEXT
83 for key in context_keys:
84 context_data = context_data[key]
85 return response, context_data
86
87 def upload_data(self, filename):
88 return {'upload_files': [('file', filename)]}
89
90 def check_comments(self, request, media_id, count):
91 comments = request.db.MediaComment.find({'media_entry': media_id})
92 assert count == len(list(comments))
93
94 def test_missing_fields(self, test_app):
95 self._setup(test_app)
96
97 # Test blank form
98 # ---------------
99 response, form = self.do_post({}, *FORM_CONTEXT)
100 assert form.file.errors == [u'You must provide a file.']
101
102 # Test blank file
103 # ---------------
104 response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT)
105 assert form.file.errors == [u'You must provide a file.']
106
107 def check_url(self, response, path):
108 assert urlparse.urlsplit(response.location)[2] == path
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))
113 self.check_url(response, '/u/{0}/'.format(self.test_user.username))
114 assert 'mediagoblin/user_pages/user.html' in context
115 # Make sure the media view is at least reachable, logged in...
116 url = '/u/{0}/m/{1}/'.format(self.test_user.username,
117 title.lower().replace(' ', '-'))
118 self.test_app.get(url)
119 # ... and logged out too.
120 self.logout()
121 self.test_app.get(url)
122
123 def test_normal_jpg(self, test_app):
124 self._setup(test_app)
125 self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
126
127 def test_normal_png(self, test_app):
128 self._setup(test_app)
129 self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
130
131 def test_normal_pdf(self, test_app):
132 if not pdf_check_prerequisites():
133 return
134 self._setup(test_app)
135 response, context = self.do_post({'title': u'Normal upload 3 (pdf)'},
136 do_follow=True,
137 **self.upload_data(GOOD_PDF))
138 self.check_url(response, '/u/{0}/'.format(self.test_user.username))
139 assert 'mediagoblin/user_pages/user.html' in context
140
141 def check_media(self, request, find_data, count=None):
142 media = MediaEntry.find(find_data)
143 if count is not None:
144 assert media.count() == count
145 if count == 0:
146 return
147 return media[0]
148
149 def test_tags(self, test_app):
150 self._setup(test_app)
151
152 # Good tag string
153 # --------
154 response, request = self.do_post({'title': u'Balanced Goblin 2',
155 'tags': GOOD_TAG_STRING},
156 *REQUEST_CONTEXT, do_follow=True,
157 **self.upload_data(GOOD_JPG))
158 media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1)
159 assert media.tags[0]['name'] == u'yin'
160 assert media.tags[0]['slug'] == u'yin'
161
162 assert media.tags[1]['name'] == u'yang'
163 assert media.tags[1]['slug'] == u'yang'
164
165 # Test tags that are too long
166 # ---------------
167 response, form = self.do_post({'title': u'Balanced Goblin 2',
168 'tags': BAD_TAG_STRING},
169 *FORM_CONTEXT,
170 **self.upload_data(GOOD_JPG))
171 assert form.tags.errors == [
172 u'Tags must be shorter than 50 characters. ' \
173 'Tags that are too long: ' \
174 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
175
176 def test_delete(self, test_app):
177 self._setup(test_app)
178
179 response, request = self.do_post({'title': u'Balanced Goblin'},
180 *REQUEST_CONTEXT, do_follow=True,
181 **self.upload_data(GOOD_JPG))
182 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
183 media_id = media.id
184
185 # render and post to the edit page.
186 edit_url = request.urlgen(
187 'mediagoblin.edit.edit_media',
188 user=self.test_user.username, media_id=media_id)
189 self.test_app.get(edit_url)
190 self.test_app.post(edit_url,
191 {'title': u'Balanced Goblin',
192 'slug': u"Balanced=Goblin",
193 'tags': u''})
194 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
195 assert media.slug == u"balanced-goblin"
196
197 # Add a comment, so we can test for its deletion later.
198 self.check_comments(request, media_id, 0)
199 comment_url = request.urlgen(
200 'mediagoblin.user_pages.media_post_comment',
201 user=self.test_user.username, media_id=media_id)
202 response = self.do_post({'comment_content': 'i love this test'},
203 url=comment_url, do_follow=True)[0]
204 self.check_comments(request, media_id, 1)
205
206 # Do not confirm deletion
207 # ---------------------------------------------------
208 delete_url = request.urlgen(
209 'mediagoblin.user_pages.media_confirm_delete',
210 user=self.test_user.username, media_id=media_id)
211 # Empty data means don't confirm
212 response = self.do_post({}, do_follow=True, url=delete_url)[0]
213 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
214 media_id = media.id
215
216 # Confirm deletion
217 # ---------------------------------------------------
218 response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
219 do_follow=True, url=delete_url)
220 self.check_media(request, {'id': media_id}, 0)
221 self.check_comments(request, media_id, 0)
222
223 def test_evil_file(self, test_app):
224 self._setup(test_app)
225
226 # Test non-suppoerted file with non-supported extension
227 # -----------------------------------------------------
228 response, form = self.do_post({'title': u'Malicious Upload 1'},
229 *FORM_CONTEXT,
230 **self.upload_data(EVIL_FILE))
231 assert len(form.file.errors) == 1
232 assert 'Sorry, I don\'t support that file type :(' == \
233 str(form.file.errors[0])
234
235
236 def test_get_media_manager(self, test_app):
237 """Test if the get_media_manger function returns sensible things
238 """
239 self._setup(test_app)
240
241 response, request = self.do_post({'title': u'Balanced Goblin'},
242 *REQUEST_CONTEXT, do_follow=True,
243 **self.upload_data(GOOD_JPG))
244 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
245
246 assert media.media_type == u'mediagoblin.media_types.image'
247 assert media.media_manager == img_MEDIA_MANAGER
248
249
250 def test_sniffing(self, test_app):
251 '''
252 Test sniffing mechanism to assert that regular uploads work as intended
253 '''
254 self._setup(test_app)
255
256 template.clear_test_template_context()
257 response = self.test_app.post(
258 '/submit/', {
259 'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
260 }, upload_files=[(
261 'file', GOOD_JPG)])
262
263 response.follow()
264
265 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html']
266
267 request = context['request']
268
269 media = request.db.MediaEntry.find_one({
270 u'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'})
271
272 assert media.media_type == 'mediagoblin.media_types.image'
273
274 def check_false_image(self, title, filename):
275 # NOTE: The following 2 tests will ultimately fail, but they
276 # *will* pass the initial form submission step. Instead,
277 # they'll be caught as failures during the processing step.
278 response, context = self.do_post({'title': title}, do_follow=True,
279 **self.upload_data(filename))
280 self.check_url(response, '/u/{0}/'.format(self.test_user.username))
281 entry = mg_globals.database.MediaEntry.find_one({'title': title})
282 assert entry.state == 'failed'
283 assert entry.fail_error == u'mediagoblin.processing:BadMediaFail'
284
285 def test_evil_jpg(self, test_app):
286 self._setup(test_app)
287
288 # Test non-supported file with .jpg extension
289 # -------------------------------------------
290 self.check_false_image(u'Malicious Upload 2', EVIL_JPG)
291
292 def test_evil_png(self, test_app):
293 self._setup(test_app)
294
295 # Test non-supported file with .png extension
296 # -------------------------------------------
297 self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
298
299 def test_media_data(self, test_app):
300 self._setup(test_app)
301
302 self.check_normal_upload(u"With GPS data", GPS_JPG)
303 media = self.check_media(None, {"title": u"With GPS data"}, 1)
304 assert media.media_data.gps_latitude == 59.336666666666666
305
306 def test_processing(self, test_app):
307 self._setup(test_app)
308
309 public_store_dir = mg_globals.global_config[
310 'storage:publicstore']['base_dir']
311
312 data = {'title': u'Big Blue'}
313 response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True,
314 **self.upload_data(BIG_BLUE))
315 media = self.check_media(request, data, 1)
316 last_size = 1024 ** 3 # Needs to be larger than bigblue.png
317 for key, basename in (('original', 'bigblue.png'),
318 ('medium', 'bigblue.medium.png'),
319 ('thumb', 'bigblue.thumbnail.png')):
320 # Does the processed image have a good filename?
321 filename = os.path.join(
322 public_store_dir,
323 *media.media_files.get(key, []))
324 assert filename.endswith('_' + basename)
325 # Is it smaller than the last processed image we looked at?
326 size = os.stat(filename).st_size
327 assert last_size > size
328 last_size = size