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