GOOD_TAG_STRING = 'yin,yang'
BAD_TAG_STRING = 'rage,' + 'f' * 26 + 'u' * 26
+FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form']
+REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request']
class TestSubmission:
def setUp(self):
def logout(self):
self.test_app.get('/auth/logout/')
+ def do_post(self, data, *context_keys, **kwargs):
+ url = kwargs.pop('url', '/submit/')
+ do_follow = kwargs.pop('do_follow', False)
+ template.clear_test_template_context()
+ response = self.test_app.post(url, data, **kwargs)
+ if do_follow:
+ response.follow()
+ context_data = template.TEMPLATE_TEST_CONTEXT
+ for key in context_keys:
+ context_data = context_data[key]
+ return response, context_data
+
+ def upload_data(self, filename):
+ return {'upload_files': [('file', filename)]}
+
def test_missing_fields(self):
# Test blank form
# ---------------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {})
- context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
- form = context['submit_form']
+ response, form = self.do_post({}, *FORM_CONTEXT)
assert form.file.errors == [u'You must provide a file.']
# Test blank file
# ---------------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'test title'})
- context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
- form = context['submit_form']
+ response, form = self.do_post({'title': 'test title'}, *FORM_CONTEXT)
assert form.file.errors == [u'You must provide a file.']
def test_normal_uploads(self):
# Test JPG
# --------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Normal upload 1'
- }, upload_files=[(
- 'file', GOOD_JPG)])
-
- # User should be redirected
- response.follow()
+ response, context = self.do_post({'title': 'Normal upload 1'},
+ do_follow=True,
+ **self.upload_data(GOOD_JPG))
assert_equal(
urlparse.urlsplit(response.location)[2],
'/u/chris/')
# Test PNG
# --------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Normal upload 2'
- }, upload_files=[(
- 'file', GOOD_PNG)])
-
- response.follow()
+ response, context = self.do_post({'title': 'Normal upload 2'},
+ do_follow=True,
+ **self.upload_data(GOOD_PNG))
assert_equal(
urlparse.urlsplit(response.location)[2],
'/u/chris/')
def test_tags(self):
# Good tag string
# --------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Balanced Goblin',
- 'tags': GOOD_TAG_STRING
- }, upload_files=[(
- 'file', GOOD_JPG)])
-
- # New media entry with correct tags should be created
- response.follow()
- context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html']
- request = context['request']
+ response, request = self.do_post({'title': 'Balanced Goblin',
+ 'tags': GOOD_TAG_STRING},
+ *REQUEST_CONTEXT, do_follow=True,
+ **self.upload_data(GOOD_JPG))
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
assert_equal(media.tags,
[{'name': u'yin', 'slug': u'yin'},
# Test tags that are too long
# ---------------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Balanced Goblin',
- 'tags': BAD_TAG_STRING
- }, upload_files=[(
- 'file', GOOD_JPG)])
-
- # Too long error should be raised
- context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
- form = context['submit_form']
+ response, form = self.do_post({'title': 'Balanced Goblin',
+ 'tags': BAD_TAG_STRING},
+ *FORM_CONTEXT,
+ **self.upload_data(GOOD_JPG))
assert form.tags.errors == [
u'Tags must be shorter than 50 characters. Tags that are too long'\
': ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
def test_delete(self):
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Balanced Goblin',
- }, upload_files=[(
- 'file', GOOD_JPG)])
-
- # Post image
- response.follow()
-
- request = template.TEMPLATE_TEST_CONTEXT[
- 'mediagoblin/user_pages/user.html']['request']
-
+ response, request = self.do_post({'title': 'Balanced Goblin'},
+ *REQUEST_CONTEXT, do_follow=True,
+ **self.upload_data(GOOD_JPG))
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
# Does media entry exist?
# Do not confirm deletion
# ---------------------------------------------------
- response = self.test_app.post(
- request.urlgen('mediagoblin.user_pages.media_confirm_delete',
- # No work: user=media.uploader().username,
- user=self.test_user.username,
- media=media._id),
- # no value means no confirm
- {})
-
- response.follow()
-
- request = template.TEMPLATE_TEST_CONTEXT[
- 'mediagoblin/user_pages/user.html']['request']
-
+ delete_url = request.urlgen(
+ 'mediagoblin.user_pages.media_confirm_delete',
+ user=self.test_user.username, media=media._id)
+ # Empty data means don't confirm
+ response = self.do_post({}, do_follow=True, url=delete_url)[0]
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
# Does media entry still exist?
# Confirm deletion
# ---------------------------------------------------
- response = self.test_app.post(
- request.urlgen('mediagoblin.user_pages.media_confirm_delete',
- # No work: user=media.uploader().username,
- user=self.test_user.username,
- media=media._id),
- {'confirm': 'y'})
-
- response.follow()
-
- request = template.TEMPLATE_TEST_CONTEXT[
- 'mediagoblin/user_pages/user.html']['request']
-
+ response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
+ do_follow=True, url=delete_url)
# Does media entry still exist?
assert_false(
request.db.MediaEntry.find(
def test_malicious_uploads(self):
# Test non-suppoerted file with non-supported extension
# -----------------------------------------------------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Malicious Upload 1'
- }, upload_files=[(
- 'file', EVIL_FILE)])
-
- context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
- form = context['submit_form']
+ response, form = self.do_post({'title': 'Malicious Upload 1'},
+ *FORM_CONTEXT,
+ **self.upload_data(EVIL_FILE))
assert re.match(r'^Could not extract any file extension from ".*?"$', str(form.file.errors[0]))
assert len(form.file.errors) == 1
# Test non-supported file with .jpg extension
# -------------------------------------------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Malicious Upload 2'
- }, upload_files=[(
- 'file', EVIL_JPG)])
- response.follow()
+ response, context = self.do_post({'title': 'Malicious Upload 2'},
+ do_follow=True,
+ **self.upload_data(EVIL_JPG))
assert_equal(
urlparse.urlsplit(response.location)[2],
'/u/chris/')
# Test non-supported file with .png extension
# -------------------------------------------
- template.clear_test_template_context()
- response = self.test_app.post(
- '/submit/', {
- 'title': 'Malicious Upload 3'
- }, upload_files=[(
- 'file', EVIL_PNG)])
- response.follow()
+ response, context = self.do_post({'title': 'Malicious Upload 3'},
+ do_follow=True,
+ **self.upload_data(EVIL_PNG))
assert_equal(
urlparse.urlsplit(response.location)[2],
'/u/chris/')