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