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