add pdf media type
[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
1975b5dd 21import urlparse
6573573d 22import os
1975b5dd 23
e75d4a0d 24from pkg_resources import resource_filename
1975b5dd 25
5c2ece74 26from mediagoblin.tests.tools import fixture_add_user
1975b5dd 27from mediagoblin import mg_globals
d728c636 28from mediagoblin.db.models import MediaEntry
deea3f66 29from mediagoblin.tools import template
5f8b4ae8 30from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER
a80ebf3b 31from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites
1975b5dd 32
e75d4a0d
BS
33def resource(filename):
34 return resource_filename('mediagoblin.tests', 'test_submission/' + filename)
35
deea3f66 36
e75d4a0d
BS
37GOOD_JPG = resource('good.jpg')
38GOOD_PNG = resource('good.png')
39EVIL_FILE = resource('evil')
40EVIL_JPG = resource('evil.jpg')
41EVIL_PNG = resource('evil.png')
6573573d 42BIG_BLUE = resource('bigblue.png')
a80ebf3b
AL
43GOOD_PDF = resource('good.pdf')
44
d728c636 45from .test_exif import GPS_JPG
75ce65cf 46
5bd0adeb
BS
47GOOD_TAG_STRING = u'yin,yang'
48BAD_TAG_STRING = unicode('rage,' + 'f' * 26 + 'u' * 26)
8ff4dec7 49
31dd6013
BS
50FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form']
51REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request']
1975b5dd 52
1975b5dd 53
1975b5dd 54class TestSubmission:
5c2ece74
CAW
55 def _setup(self, test_app):
56 self.test_app = test_app
0a78be3e 57
75ce65cf
CM
58 # TODO: Possibly abstract into a decorator like:
59 # @as_authenticated_user('chris')
9754802d 60 test_user = fixture_add_user()
1975b5dd 61
afe4e513
JW
62 self.test_user = test_user
63
c2d6792d
E
64 self.login()
65
66 def login(self):
75ce65cf
CM
67 self.test_app.post(
68 '/auth/login/', {
69 'username': u'chris',
70 'password': 'toast'})
71
c2d6792d
E
72 def logout(self):
73 self.test_app.get('/auth/logout/')
74
31dd6013
BS
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
deea3f66 86
31dd6013
BS
87 def upload_data(self, filename):
88 return {'upload_files': [('file', filename)]}
89
e089b66b
CAW
90 def check_comments(self, request, media_id, count):
91 comments = request.db.MediaComment.find({'media_entry': media_id})
7d503a89 92 assert count == len(list(comments))
c16b8196 93
5c2ece74
CAW
94 def test_missing_fields(self, test_app):
95 self._setup(test_app)
96
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))
113 self.check_url(response, '/u/{0}/'.format(self.test_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...
c0e87ec9
BS
116 url = '/u/{0}/m/{1}/'.format(self.test_user.username,
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
5c2ece74
CAW
123 def test_normal_jpg(self, test_app):
124 self._setup(test_app)
5bd0adeb 125 self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
c0e87ec9 126
5c2ece74
CAW
127 def test_normal_png(self, test_app):
128 self._setup(test_app)
5bd0adeb 129 self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
75ce65cf 130
a80ebf3b
AL
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
77445d13 141 def check_media(self, request, find_data, count=None):
d728c636 142 media = MediaEntry.find(find_data)
77445d13 143 if count is not None:
7d503a89 144 assert media.count() == count
77445d13
BS
145 if count == 0:
146 return
147 return media[0]
148
5c2ece74
CAW
149 def test_tags(self, test_app):
150 self._setup(test_app)
151
8ff4dec7
CFD
152 # Good tag string
153 # --------
40cec2b4 154 response, request = self.do_post({'title': u'Balanced Goblin 2',
31dd6013
BS
155 'tags': GOOD_TAG_STRING},
156 *REQUEST_CONTEXT, do_follow=True,
157 **self.upload_data(GOOD_JPG))
40cec2b4 158 media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1)
38877794
CAW
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'
8ff4dec7
CFD
164
165 # Test tags that are too long
166 # ---------------
40cec2b4 167 response, form = self.do_post({'title': u'Balanced Goblin 2',
31dd6013
BS
168 'tags': BAD_TAG_STRING},
169 *FORM_CONTEXT,
170 **self.upload_data(GOOD_JPG))
7d503a89 171 assert form.tags.errors == [
d1f52dc7
BS
172 u'Tags must be shorter than 50 characters. ' \
173 'Tags that are too long: ' \
7d503a89 174 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
75ce65cf 175
5c2ece74
CAW
176 def test_delete(self, test_app):
177 self._setup(test_app)
178
5bd0adeb 179 response, request = self.do_post({'title': u'Balanced Goblin'},
31dd6013
BS
180 *REQUEST_CONTEXT, do_follow=True,
181 **self.upload_data(GOOD_JPG))
5bd0adeb 182 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
e089b66b 183 media_id = media.id
afe4e513 184
d92fbdc4 185 # render and post to the edit page.
461dd971
E
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)
d92fbdc4
E
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)
7d503a89 195 assert media.slug == u"balanced-goblin"
461dd971 196
a0a7f87f 197 # Add a comment, so we can test for its deletion later.
e089b66b 198 self.check_comments(request, media_id, 0)
c16b8196
BS
199 comment_url = request.urlgen(
200 'mediagoblin.user_pages.media_post_comment',
461dd971 201 user=self.test_user.username, media_id=media_id)
c16b8196
BS
202 response = self.do_post({'comment_content': 'i love this test'},
203 url=comment_url, do_follow=True)[0]
e089b66b 204 self.check_comments(request, media_id, 1)
a0a7f87f 205
afe4e513
JW
206 # Do not confirm deletion
207 # ---------------------------------------------------
31dd6013
BS
208 delete_url = request.urlgen(
209 'mediagoblin.user_pages.media_confirm_delete',
461dd971 210 user=self.test_user.username, media_id=media_id)
31dd6013
BS
211 # Empty data means don't confirm
212 response = self.do_post({}, do_follow=True, url=delete_url)[0]
5bd0adeb 213 media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
e089b66b 214 media_id = media.id
afe4e513
JW
215
216 # Confirm deletion
217 # ---------------------------------------------------
31dd6013
BS
218 response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
219 do_follow=True, url=delete_url)
5c2b8486 220 self.check_media(request, {'id': media_id}, 0)
e089b66b 221 self.check_comments(request, media_id, 0)
afe4e513 222
5c2ece74
CAW
223 def test_evil_file(self, test_app):
224 self._setup(test_app)
225
ad35dd49
CM
226 # Test non-suppoerted file with non-supported extension
227 # -----------------------------------------------------
5bd0adeb 228 response, form = self.do_post({'title': u'Malicious Upload 1'},
31dd6013
BS
229 *FORM_CONTEXT,
230 **self.upload_data(EVIL_FILE))
7d503a89 231 assert len(form.file.errors) == 1
0308958b
JW
232 assert 'Sorry, I don\'t support that file type :(' == \
233 str(form.file.errors[0])
ad35dd49 234
5f8b4ae8 235
5c2ece74 236 def test_get_media_manager(self, test_app):
5f8b4ae8
SS
237 """Test if the get_media_manger function returns sensible things
238 """
5c2ece74
CAW
239 self._setup(test_app)
240
5f8b4ae8
SS
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
7d503a89
CAW
246 assert media.media_type == u'mediagoblin.media_types.image'
247 assert media.media_manager == img_MEDIA_MANAGER
5f8b4ae8
SS
248
249
5c2ece74 250 def test_sniffing(self, test_app):
a9d84d4c
JW
251 '''
252 Test sniffing mechanism to assert that regular uploads work as intended
253 '''
5c2ece74
CAW
254 self._setup(test_app)
255
a9d84d4c
JW
256 template.clear_test_template_context()
257 response = self.test_app.post(
258 '/submit/', {
5bd0adeb 259 'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
a9d84d4c
JW
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
c3739034 274 def check_false_image(self, title, filename):
68f3ffbe
CAW
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.
c3739034
BS
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})
7d503a89
CAW
282 assert entry.state == 'failed'
283 assert entry.fail_error == u'mediagoblin.processing:BadMediaFail'
9df37e8a 284
5c2ece74
CAW
285 def test_evil_jpg(self, test_app):
286 self._setup(test_app)
287
75ce65cf 288 # Test non-supported file with .jpg extension
ad35dd49 289 # -------------------------------------------
ed3ff88e 290 self.check_false_image(u'Malicious Upload 2', EVIL_JPG)
ad35dd49 291
5c2ece74
CAW
292 def test_evil_png(self, test_app):
293 self._setup(test_app)
294
75ce65cf 295 # Test non-supported file with .png extension
ad35dd49 296 # -------------------------------------------
ed3ff88e 297 self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
6573573d 298
5c2ece74
CAW
299 def test_media_data(self, test_app):
300 self._setup(test_app)
301
d728c636
E
302 self.check_normal_upload(u"With GPS data", GPS_JPG)
303 media = self.check_media(None, {"title": u"With GPS data"}, 1)
7d503a89 304 assert media.media_data.gps_latitude == 59.336666666666666
d728c636 305
5c2ece74
CAW
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
5bd0adeb 312 data = {'title': u'Big Blue'}
6573573d
BS
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?
5c2ece74
CAW
321 filename = os.path.join(
322 public_store_dir,
323 *media.media_files.get(key, []))
7d503a89 324 assert filename.endswith('_' + basename)
6573573d
BS
325 # Is it smaller than the last processed image we looked at?
326 size = os.stat(filename).st_size
7d503a89 327 assert last_size > size
6573573d 328 last_size = size