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 | |
a855e92a | 24 | from nose.tools import assert_equal, assert_true |
e75d4a0d | 25 | from pkg_resources import resource_filename |
1975b5dd | 26 | |
deea3f66 | 27 | from mediagoblin.tests.tools import get_test_app, \ |
9754802d | 28 | fixture_add_user |
1975b5dd | 29 | from mediagoblin import mg_globals |
deea3f66 | 30 | from mediagoblin.tools import template |
5f8b4ae8 | 31 | from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER |
1975b5dd | 32 | |
e75d4a0d BS |
33 | def resource(filename): |
34 | return resource_filename('mediagoblin.tests', 'test_submission/' + filename) | |
35 | ||
deea3f66 | 36 | |
e75d4a0d BS |
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') | |
6573573d | 42 | BIG_BLUE = resource('bigblue.png') |
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 CM |
51 | class TestSubmission: |
52 | def setUp(self): | |
0a78be3e CM |
53 | self.test_app = get_test_app() |
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}) | |
c16b8196 BS |
89 | assert_equal(count, len(list(comments))) |
90 | ||
75ce65cf | 91 | def test_missing_fields(self): |
ad35dd49 CM |
92 | # Test blank form |
93 | # --------------- | |
31dd6013 | 94 | response, form = self.do_post({}, *FORM_CONTEXT) |
d1f52dc7 | 95 | assert_equal(form.file.errors, [u'You must provide a file.']) |
ad35dd49 CM |
96 | |
97 | # Test blank file | |
98 | # --------------- | |
5bd0adeb | 99 | response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT) |
d1f52dc7 | 100 | assert_equal(form.file.errors, [u'You must provide a file.']) |
ad35dd49 | 101 | |
c0e87ec9 BS |
102 | def check_url(self, response, path): |
103 | assert_equal(urlparse.urlsplit(response.location)[2], path) | |
104 | ||
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)) | |
deea3f66 | 109 | assert_true('mediagoblin/user_pages/user.html' in context) |
c2d6792d | 110 | # Make sure the media view is at least reachable, logged in... |
c0e87ec9 BS |
111 | url = '/u/{0}/m/{1}/'.format(self.test_user.username, |
112 | title.lower().replace(' ', '-')) | |
113 | self.test_app.get(url) | |
c2d6792d E |
114 | # ... and logged out too. |
115 | self.logout() | |
c0e87ec9 | 116 | self.test_app.get(url) |
c2d6792d | 117 | |
c0e87ec9 | 118 | def test_normal_jpg(self): |
5bd0adeb | 119 | self.check_normal_upload(u'Normal upload 1', GOOD_JPG) |
c0e87ec9 BS |
120 | |
121 | def test_normal_png(self): | |
5bd0adeb | 122 | self.check_normal_upload(u'Normal upload 2', GOOD_PNG) |
75ce65cf | 123 | |
77445d13 BS |
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) | |
128 | if count == 0: | |
129 | return | |
130 | return media[0] | |
131 | ||
8ff4dec7 CFD |
132 | def test_tags(self): |
133 | # Good tag string | |
134 | # -------- | |
5bd0adeb | 135 | response, request = self.do_post({'title': u'Balanced Goblin', |
31dd6013 BS |
136 | 'tags': GOOD_TAG_STRING}, |
137 | *REQUEST_CONTEXT, do_follow=True, | |
138 | **self.upload_data(GOOD_JPG)) | |
5bd0adeb | 139 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) |
38877794 CAW |
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' | |
8ff4dec7 CFD |
145 | |
146 | # Test tags that are too long | |
147 | # --------------- | |
5bd0adeb | 148 | response, form = self.do_post({'title': u'Balanced Goblin', |
31dd6013 BS |
149 | 'tags': BAD_TAG_STRING}, |
150 | *FORM_CONTEXT, | |
151 | **self.upload_data(GOOD_JPG)) | |
d1f52dc7 BS |
152 | assert_equal(form.tags.errors, [ |
153 | u'Tags must be shorter than 50 characters. ' \ | |
154 | 'Tags that are too long: ' \ | |
155 | 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']) | |
75ce65cf | 156 | |
afe4e513 | 157 | def test_delete(self): |
5bd0adeb | 158 | response, request = self.do_post({'title': u'Balanced Goblin'}, |
31dd6013 BS |
159 | *REQUEST_CONTEXT, do_follow=True, |
160 | **self.upload_data(GOOD_JPG)) | |
5bd0adeb | 161 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) |
e089b66b | 162 | media_id = media.id |
afe4e513 | 163 | |
a0a7f87f | 164 | # Add a comment, so we can test for its deletion later. |
e089b66b | 165 | self.check_comments(request, media_id, 0) |
c16b8196 BS |
166 | comment_url = request.urlgen( |
167 | 'mediagoblin.user_pages.media_post_comment', | |
e089b66b | 168 | user=self.test_user.username, media=media_id) |
c16b8196 BS |
169 | response = self.do_post({'comment_content': 'i love this test'}, |
170 | url=comment_url, do_follow=True)[0] | |
e089b66b | 171 | self.check_comments(request, media_id, 1) |
a0a7f87f | 172 | |
afe4e513 JW |
173 | # Do not confirm deletion |
174 | # --------------------------------------------------- | |
31dd6013 BS |
175 | delete_url = request.urlgen( |
176 | 'mediagoblin.user_pages.media_confirm_delete', | |
e089b66b | 177 | user=self.test_user.username, media=media_id) |
31dd6013 BS |
178 | # Empty data means don't confirm |
179 | response = self.do_post({}, do_follow=True, url=delete_url)[0] | |
5bd0adeb | 180 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) |
e089b66b | 181 | media_id = media.id |
afe4e513 JW |
182 | |
183 | # Confirm deletion | |
184 | # --------------------------------------------------- | |
31dd6013 BS |
185 | response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT, |
186 | do_follow=True, url=delete_url) | |
e089b66b CAW |
187 | self.check_media(request, {'_id': media_id}, 0) |
188 | self.check_comments(request, media_id, 0) | |
afe4e513 | 189 | |
c3739034 | 190 | def test_evil_file(self): |
ad35dd49 CM |
191 | # Test non-suppoerted file with non-supported extension |
192 | # ----------------------------------------------------- | |
5bd0adeb | 193 | response, form = self.do_post({'title': u'Malicious Upload 1'}, |
31dd6013 BS |
194 | *FORM_CONTEXT, |
195 | **self.upload_data(EVIL_FILE)) | |
d1f52dc7 | 196 | assert_equal(len(form.file.errors), 1) |
0308958b JW |
197 | assert 'Sorry, I don\'t support that file type :(' == \ |
198 | str(form.file.errors[0]) | |
ad35dd49 | 199 | |
5f8b4ae8 SS |
200 | |
201 | def test_get_media_manager(self): | |
202 | """Test if the get_media_manger function returns sensible things | |
203 | """ | |
204 | response, request = self.do_post({'title': u'Balanced Goblin'}, | |
205 | *REQUEST_CONTEXT, do_follow=True, | |
206 | **self.upload_data(GOOD_JPG)) | |
207 | media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) | |
208 | ||
209 | assert_equal(media.media_type, u'mediagoblin.media_types.image') | |
210 | assert_equal(media.media_manager, img_MEDIA_MANAGER) | |
211 | ||
212 | ||
a9d84d4c JW |
213 | def test_sniffing(self): |
214 | ''' | |
215 | Test sniffing mechanism to assert that regular uploads work as intended | |
216 | ''' | |
217 | template.clear_test_template_context() | |
218 | response = self.test_app.post( | |
219 | '/submit/', { | |
5bd0adeb | 220 | 'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE' |
a9d84d4c JW |
221 | }, upload_files=[( |
222 | 'file', GOOD_JPG)]) | |
223 | ||
224 | response.follow() | |
225 | ||
226 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html'] | |
227 | ||
228 | request = context['request'] | |
229 | ||
230 | media = request.db.MediaEntry.find_one({ | |
231 | u'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'}) | |
232 | ||
233 | assert media.media_type == 'mediagoblin.media_types.image' | |
234 | ||
c3739034 | 235 | def check_false_image(self, title, filename): |
68f3ffbe CAW |
236 | # NOTE: The following 2 tests will ultimately fail, but they |
237 | # *will* pass the initial form submission step. Instead, | |
238 | # they'll be caught as failures during the processing step. | |
c3739034 BS |
239 | response, context = self.do_post({'title': title}, do_follow=True, |
240 | **self.upload_data(filename)) | |
241 | self.check_url(response, '/u/{0}/'.format(self.test_user.username)) | |
242 | entry = mg_globals.database.MediaEntry.find_one({'title': title}) | |
243 | assert_equal(entry.state, 'failed') | |
244 | assert_equal(entry.fail_error, u'mediagoblin.processing:BadMediaFail') | |
9df37e8a | 245 | |
c3739034 | 246 | def test_evil_jpg(self): |
75ce65cf | 247 | # Test non-supported file with .jpg extension |
ad35dd49 | 248 | # ------------------------------------------- |
ed3ff88e | 249 | self.check_false_image(u'Malicious Upload 2', EVIL_JPG) |
ad35dd49 | 250 | |
c3739034 | 251 | def test_evil_png(self): |
75ce65cf | 252 | # Test non-supported file with .png extension |
ad35dd49 | 253 | # ------------------------------------------- |
ed3ff88e | 254 | self.check_false_image(u'Malicious Upload 3', EVIL_PNG) |
6573573d BS |
255 | |
256 | def test_processing(self): | |
5bd0adeb | 257 | data = {'title': u'Big Blue'} |
6573573d BS |
258 | response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True, |
259 | **self.upload_data(BIG_BLUE)) | |
260 | media = self.check_media(request, data, 1) | |
261 | last_size = 1024 ** 3 # Needs to be larger than bigblue.png | |
262 | for key, basename in (('original', 'bigblue.png'), | |
263 | ('medium', 'bigblue.medium.png'), | |
264 | ('thumb', 'bigblue.thumbnail.png')): | |
265 | # Does the processed image have a good filename? | |
266 | filename = resource_filename( | |
267 | 'mediagoblin.tests', | |
268 | os.path.join('test_user_dev/media/public', | |
e089b66b | 269 | *media.media_files.get(key, []))) |
6573573d BS |
270 | assert_true(filename.endswith('_' + basename)) |
271 | # Is it smaller than the last processed image we looked at? | |
272 | size = os.stat(filename).st_size | |
273 | assert_true(last_size > size) | |
274 | last_size = size |