Refactor data posts into one do_post function.
All the data posts in these tests had a lot of common code. Putting all that into a function makes it easier to write more tests (which I'll be doing in a bit) and see what's really being tested.
This commit is contained in:
parent
176f207b63
commit
31dd6013b8
@ -39,6 +39,8 @@ EVIL_PNG = pkg_resources.resource_filename(
|
|||||||
GOOD_TAG_STRING = 'yin,yang'
|
GOOD_TAG_STRING = 'yin,yang'
|
||||||
BAD_TAG_STRING = 'rage,' + 'f' * 26 + 'u' * 26
|
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:
|
class TestSubmission:
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -61,39 +63,39 @@ class TestSubmission:
|
|||||||
def logout(self):
|
def logout(self):
|
||||||
self.test_app.get('/auth/logout/')
|
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):
|
def test_missing_fields(self):
|
||||||
# Test blank form
|
# Test blank form
|
||||||
# ---------------
|
# ---------------
|
||||||
template.clear_test_template_context()
|
response, form = self.do_post({}, *FORM_CONTEXT)
|
||||||
response = self.test_app.post(
|
|
||||||
'/submit/', {})
|
|
||||||
context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
|
|
||||||
form = context['submit_form']
|
|
||||||
assert form.file.errors == [u'You must provide a file.']
|
assert form.file.errors == [u'You must provide a file.']
|
||||||
|
|
||||||
# Test blank file
|
# Test blank file
|
||||||
# ---------------
|
# ---------------
|
||||||
template.clear_test_template_context()
|
response, form = self.do_post({'title': 'test title'}, *FORM_CONTEXT)
|
||||||
response = self.test_app.post(
|
|
||||||
'/submit/', {
|
|
||||||
'title': 'test title'})
|
|
||||||
context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
|
|
||||||
form = context['submit_form']
|
|
||||||
assert form.file.errors == [u'You must provide a file.']
|
assert form.file.errors == [u'You must provide a file.']
|
||||||
|
|
||||||
|
|
||||||
def test_normal_uploads(self):
|
def test_normal_uploads(self):
|
||||||
# Test JPG
|
# Test JPG
|
||||||
# --------
|
# --------
|
||||||
template.clear_test_template_context()
|
response, context = self.do_post({'title': 'Normal upload 1'},
|
||||||
response = self.test_app.post(
|
do_follow=True,
|
||||||
'/submit/', {
|
**self.upload_data(GOOD_JPG))
|
||||||
'title': 'Normal upload 1'
|
|
||||||
}, upload_files=[(
|
|
||||||
'file', GOOD_JPG)])
|
|
||||||
|
|
||||||
# User should be redirected
|
|
||||||
response.follow()
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
urlparse.urlsplit(response.location)[2],
|
urlparse.urlsplit(response.location)[2],
|
||||||
'/u/chris/')
|
'/u/chris/')
|
||||||
@ -110,14 +112,9 @@ class TestSubmission:
|
|||||||
|
|
||||||
# Test PNG
|
# Test PNG
|
||||||
# --------
|
# --------
|
||||||
template.clear_test_template_context()
|
response, context = self.do_post({'title': 'Normal upload 2'},
|
||||||
response = self.test_app.post(
|
do_follow=True,
|
||||||
'/submit/', {
|
**self.upload_data(GOOD_PNG))
|
||||||
'title': 'Normal upload 2'
|
|
||||||
}, upload_files=[(
|
|
||||||
'file', GOOD_PNG)])
|
|
||||||
|
|
||||||
response.follow()
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
urlparse.urlsplit(response.location)[2],
|
urlparse.urlsplit(response.location)[2],
|
||||||
'/u/chris/')
|
'/u/chris/')
|
||||||
@ -127,18 +124,10 @@ class TestSubmission:
|
|||||||
def test_tags(self):
|
def test_tags(self):
|
||||||
# Good tag string
|
# Good tag string
|
||||||
# --------
|
# --------
|
||||||
template.clear_test_template_context()
|
response, request = self.do_post({'title': 'Balanced Goblin',
|
||||||
response = self.test_app.post(
|
'tags': GOOD_TAG_STRING},
|
||||||
'/submit/', {
|
*REQUEST_CONTEXT, do_follow=True,
|
||||||
'title': 'Balanced Goblin',
|
**self.upload_data(GOOD_JPG))
|
||||||
'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']
|
|
||||||
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
|
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
|
||||||
assert_equal(media.tags,
|
assert_equal(media.tags,
|
||||||
[{'name': u'yin', 'slug': u'yin'},
|
[{'name': u'yin', 'slug': u'yin'},
|
||||||
@ -146,35 +135,18 @@ class TestSubmission:
|
|||||||
|
|
||||||
# Test tags that are too long
|
# Test tags that are too long
|
||||||
# ---------------
|
# ---------------
|
||||||
template.clear_test_template_context()
|
response, form = self.do_post({'title': 'Balanced Goblin',
|
||||||
response = self.test_app.post(
|
'tags': BAD_TAG_STRING},
|
||||||
'/submit/', {
|
*FORM_CONTEXT,
|
||||||
'title': 'Balanced Goblin',
|
**self.upload_data(GOOD_JPG))
|
||||||
'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']
|
|
||||||
assert form.tags.errors == [
|
assert form.tags.errors == [
|
||||||
u'Tags must be shorter than 50 characters. Tags that are too long'\
|
u'Tags must be shorter than 50 characters. Tags that are too long'\
|
||||||
': ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
|
': ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
template.clear_test_template_context()
|
response, request = self.do_post({'title': 'Balanced Goblin'},
|
||||||
response = self.test_app.post(
|
*REQUEST_CONTEXT, do_follow=True,
|
||||||
'/submit/', {
|
**self.upload_data(GOOD_JPG))
|
||||||
'title': 'Balanced Goblin',
|
|
||||||
}, upload_files=[(
|
|
||||||
'file', GOOD_JPG)])
|
|
||||||
|
|
||||||
# Post image
|
|
||||||
response.follow()
|
|
||||||
|
|
||||||
request = template.TEMPLATE_TEST_CONTEXT[
|
|
||||||
'mediagoblin/user_pages/user.html']['request']
|
|
||||||
|
|
||||||
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
|
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
|
||||||
|
|
||||||
# Does media entry exist?
|
# Does media entry exist?
|
||||||
@ -182,19 +154,11 @@ class TestSubmission:
|
|||||||
|
|
||||||
# Do not confirm deletion
|
# Do not confirm deletion
|
||||||
# ---------------------------------------------------
|
# ---------------------------------------------------
|
||||||
response = self.test_app.post(
|
delete_url = request.urlgen(
|
||||||
request.urlgen('mediagoblin.user_pages.media_confirm_delete',
|
'mediagoblin.user_pages.media_confirm_delete',
|
||||||
# No work: user=media.uploader().username,
|
user=self.test_user.username, media=media._id)
|
||||||
user=self.test_user.username,
|
# Empty data means don't confirm
|
||||||
media=media._id),
|
response = self.do_post({}, do_follow=True, url=delete_url)[0]
|
||||||
# no value means no confirm
|
|
||||||
{})
|
|
||||||
|
|
||||||
response.follow()
|
|
||||||
|
|
||||||
request = template.TEMPLATE_TEST_CONTEXT[
|
|
||||||
'mediagoblin/user_pages/user.html']['request']
|
|
||||||
|
|
||||||
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
|
media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
|
||||||
|
|
||||||
# Does media entry still exist?
|
# Does media entry still exist?
|
||||||
@ -202,18 +166,8 @@ class TestSubmission:
|
|||||||
|
|
||||||
# Confirm deletion
|
# Confirm deletion
|
||||||
# ---------------------------------------------------
|
# ---------------------------------------------------
|
||||||
response = self.test_app.post(
|
response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
|
||||||
request.urlgen('mediagoblin.user_pages.media_confirm_delete',
|
do_follow=True, url=delete_url)
|
||||||
# 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']
|
|
||||||
|
|
||||||
# Does media entry still exist?
|
# Does media entry still exist?
|
||||||
assert_false(
|
assert_false(
|
||||||
request.db.MediaEntry.find(
|
request.db.MediaEntry.find(
|
||||||
@ -222,15 +176,9 @@ class TestSubmission:
|
|||||||
def test_malicious_uploads(self):
|
def test_malicious_uploads(self):
|
||||||
# Test non-suppoerted file with non-supported extension
|
# Test non-suppoerted file with non-supported extension
|
||||||
# -----------------------------------------------------
|
# -----------------------------------------------------
|
||||||
template.clear_test_template_context()
|
response, form = self.do_post({'title': 'Malicious Upload 1'},
|
||||||
response = self.test_app.post(
|
*FORM_CONTEXT,
|
||||||
'/submit/', {
|
**self.upload_data(EVIL_FILE))
|
||||||
'title': 'Malicious Upload 1'
|
|
||||||
}, upload_files=[(
|
|
||||||
'file', EVIL_FILE)])
|
|
||||||
|
|
||||||
context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
|
|
||||||
form = context['submit_form']
|
|
||||||
assert re.match(r'^Could not extract any file extension from ".*?"$', str(form.file.errors[0]))
|
assert re.match(r'^Could not extract any file extension from ".*?"$', str(form.file.errors[0]))
|
||||||
assert len(form.file.errors) == 1
|
assert len(form.file.errors) == 1
|
||||||
|
|
||||||
@ -240,13 +188,9 @@ class TestSubmission:
|
|||||||
|
|
||||||
# Test non-supported file with .jpg extension
|
# Test non-supported file with .jpg extension
|
||||||
# -------------------------------------------
|
# -------------------------------------------
|
||||||
template.clear_test_template_context()
|
response, context = self.do_post({'title': 'Malicious Upload 2'},
|
||||||
response = self.test_app.post(
|
do_follow=True,
|
||||||
'/submit/', {
|
**self.upload_data(EVIL_JPG))
|
||||||
'title': 'Malicious Upload 2'
|
|
||||||
}, upload_files=[(
|
|
||||||
'file', EVIL_JPG)])
|
|
||||||
response.follow()
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
urlparse.urlsplit(response.location)[2],
|
urlparse.urlsplit(response.location)[2],
|
||||||
'/u/chris/')
|
'/u/chris/')
|
||||||
@ -260,13 +204,9 @@ class TestSubmission:
|
|||||||
|
|
||||||
# Test non-supported file with .png extension
|
# Test non-supported file with .png extension
|
||||||
# -------------------------------------------
|
# -------------------------------------------
|
||||||
template.clear_test_template_context()
|
response, context = self.do_post({'title': 'Malicious Upload 3'},
|
||||||
response = self.test_app.post(
|
do_follow=True,
|
||||||
'/submit/', {
|
**self.upload_data(EVIL_PNG))
|
||||||
'title': 'Malicious Upload 3'
|
|
||||||
}, upload_files=[(
|
|
||||||
'file', EVIL_PNG)])
|
|
||||||
response.follow()
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
urlparse.urlsplit(response.location)[2],
|
urlparse.urlsplit(response.location)[2],
|
||||||
'/u/chris/')
|
'/u/chris/')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user