From 1975b5dd1fdae5ddb2819b8c67a29f9c374fce40 Mon Sep 17 00:00:00 2001 From: Chris Moylan Date: Tue, 21 Jun 2011 00:11:54 -0500 Subject: began work on submission test. nothing to see here --- mediagoblin/tests/test_submission.py | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 mediagoblin/tests/test_submission.py (limited to 'mediagoblin') diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py new file mode 100644 index 00000000..3ed87c28 --- /dev/null +++ b/mediagoblin/tests/test_submission.py @@ -0,0 +1,45 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 Free Software Foundation, Inc +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import urlparse + +from nose.tools import assert_equal + +from mediagoblin.auth import lib as auth_lib +from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin import mg_globals +from mediagoblin import util + +#TEST_JPG = '' + +# TODO: +# - Define test files as globals +# - supported mime types +# - unsupported mime type with supported extension +# - Remove any imports that aren't neccessary +# - Get setup fixture working + +class TestSubmission: + def setUp(self): + test_user = mg_globals.database.User() + test_user['username'] = u'chris' + test_user['email'] = u'chris@example.com' + test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') + test_user.save() + + @setup_fresh_app + def test_something(test_app, self): + pass -- cgit v1.2.3 From 0a78be3e08b93094f979202f56093ac7d1424cfc Mon Sep 17 00:00:00 2001 From: Chris Moylan Date: Thu, 23 Jun 2011 23:02:36 -0500 Subject: removed the @setup_fresh_app decorator because it was messing \ with my fixtures. --- mediagoblin/tests/test_submission.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'mediagoblin') diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 3ed87c28..4df39ae4 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -19,7 +19,7 @@ import urlparse from nose.tools import assert_equal from mediagoblin.auth import lib as auth_lib -from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin.tests.tools import setup_fresh_app, get_test_app from mediagoblin import mg_globals from mediagoblin import util @@ -34,12 +34,14 @@ from mediagoblin import util class TestSubmission: def setUp(self): + self.test_app = get_test_app() + test_user = mg_globals.database.User() test_user['username'] = u'chris' test_user['email'] = u'chris@example.com' test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') test_user.save() - @setup_fresh_app - def test_something(test_app, self): + def test_something(self): pass + -- cgit v1.2.3 From 75ce65cf5928a5174819f0b7992b7d95c1bc0fb0 Mon Sep 17 00:00:00 2001 From: Chris Moylan Date: Thu, 30 Jun 2011 00:13:47 -0500 Subject: mocked out submission spec --- mediagoblin/tests/test_submission.py | 40 +++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'mediagoblin') diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 4df39ae4..4489692e 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -23,25 +23,59 @@ from mediagoblin.tests.tools import setup_fresh_app, get_test_app from mediagoblin import mg_globals from mediagoblin import util -#TEST_JPG = '' +IMAGE_ROOT = 'mediagoblin/tests/test_submission' +GOOD_JPG = 'good.jpg' +GOOD_PNG = 'good.png' +EVIL_JPG = '' +EVIL_PNG = '' + # TODO: # - Define test files as globals # - supported mime types # - unsupported mime type with supported extension # - Remove any imports that aren't neccessary -# - Get setup fixture working class TestSubmission: def setUp(self): self.test_app = get_test_app() + # TODO: Possibly abstract into a decorator like: + # @as_authenticated_user('chris') test_user = mg_globals.database.User() test_user['username'] = u'chris' test_user['email'] = u'chris@example.com' test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') test_user.save() - def test_something(self): + self.test_app.post( + '/auth/login/', { + 'username': u'chris', + 'password': 'toast'}) + + def test_missing_fields(self): + # Test missing title + # Test missing description (if it's required) + # Test missing file + pass + + def test_normal_uploads(self): + # FYI: + # upload_files is for file uploads. It should be a list of + # [(fieldname, filename, file_content)]. You can also use + # just [(fieldname, filename)] and the file content will be + # read from disk. + + # Test JPG + # Test PNG + # Test additional supported formats + + #resp = self.test_app.get('/') + #print resp + pass + + def test_malicious_uploads(self): + # Test non-supported file with .jpg extension + # Test non-supported file with .png extension pass -- cgit v1.2.3 From ad35dd490cc0e82acea852befd25126c0dcb61ad Mon Sep 17 00:00:00 2001 From: Chris Moylan Date: Tue, 5 Jul 2011 21:42:41 -0500 Subject: finished basic submission testing --- mediagoblin/tests/test_submission.py | 106 +++++++++++++++++++++++++---- mediagoblin/tests/test_submission/evil | Bin 0 -> 96284 bytes mediagoblin/tests/test_submission/evil.jpg | Bin 0 -> 96284 bytes mediagoblin/tests/test_submission/evil.png | Bin 0 -> 96284 bytes mediagoblin/tests/test_submission/good.jpg | Bin 0 -> 10059 bytes mediagoblin/tests/test_submission/good.png | Bin 0 -> 50598 bytes 6 files changed, 94 insertions(+), 12 deletions(-) create mode 100755 mediagoblin/tests/test_submission/evil create mode 100755 mediagoblin/tests/test_submission/evil.jpg create mode 100755 mediagoblin/tests/test_submission/evil.png create mode 100644 mediagoblin/tests/test_submission/good.jpg create mode 100644 mediagoblin/tests/test_submission/good.png (limited to 'mediagoblin') diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 4489692e..20c187e7 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import urlparse +from os import getcwd from nose.tools import assert_equal @@ -23,11 +24,12 @@ from mediagoblin.tests.tools import setup_fresh_app, get_test_app from mediagoblin import mg_globals from mediagoblin import util -IMAGE_ROOT = 'mediagoblin/tests/test_submission' +IMAGE_ROOT = getcwd() + '/mediagoblin/tests/test_submission/' GOOD_JPG = 'good.jpg' GOOD_PNG = 'good.png' -EVIL_JPG = '' -EVIL_PNG = '' +EVIL_FILE = 'evil' +EVIL_JPG = 'evil.jpg' +EVIL_PNG = 'evil.png' # TODO: @@ -45,6 +47,8 @@ class TestSubmission: test_user = mg_globals.database.User() test_user['username'] = u'chris' test_user['email'] = u'chris@example.com' + test_user['email_verified'] = True + test_user['status'] = u'active' test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') test_user.save() @@ -54,10 +58,25 @@ class TestSubmission: 'password': 'toast'}) def test_missing_fields(self): - # Test missing title - # Test missing description (if it's required) - # Test missing file - pass + # Test blank form + # --------------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', {}) + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + form = context['submit_form'] + assert form.file.errors == [u'You must provide a file.'] + + # Test blank file + # --------------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', { + 'title': 'test title'}) + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + form = context['submit_form'] + assert form.file.errors == [u'You must provide a file.'] + def test_normal_uploads(self): # FYI: @@ -67,15 +86,78 @@ class TestSubmission: # read from disk. # Test JPG + # -------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', { + 'title': 'Normal upload 1' + }, upload_files=[( + 'file', IMAGE_ROOT + GOOD_JPG)]) + + # User should be redirected + response.follow() + assert_equal( + urlparse.urlsplit(response.location)[2], + '/submit/success/') + assert util.TEMPLATE_TEST_CONTEXT.has_key( + 'mediagoblin/submit/success.html') + # Test PNG - # Test additional supported formats + # -------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', { + 'title': 'Normal upload 2' + }, upload_files=[( + 'file', IMAGE_ROOT + GOOD_PNG)]) + + response.follow() + assert_equal( + urlparse.urlsplit(response.location)[2], + '/submit/success/') + assert util.TEMPLATE_TEST_CONTEXT.has_key( + 'mediagoblin/submit/success.html') + + # TODO: Test additional supported formats - #resp = self.test_app.get('/') - #print resp - pass def test_malicious_uploads(self): + # Test non-suppoerted file with non-supported extension + # ----------------------------------------------------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', { + 'title': 'Malicious Upload 2' + }, upload_files=[( + 'file', IMAGE_ROOT + EVIL_FILE)]) + + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + form = context['submit_form'] + assert form.file.errors == ['The file doesn\'t seem to be an image!'] + # Test non-supported file with .jpg extension + # ------------------------------------------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', { + 'title': 'Malicious Upload 2' + }, upload_files=[( + 'file', IMAGE_ROOT + EVIL_JPG)]) + + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + form = context['submit_form'] + assert form.file.errors == ['The file doesn\'t seem to be an image!'] + # Test non-supported file with .png extension - pass + # ------------------------------------------- + util.clear_test_template_context() + response = self.test_app.post( + '/submit/', { + 'title': 'Malicious Upload 3' + }, upload_files=[( + 'file', IMAGE_ROOT + EVIL_PNG)]) + + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + form = context['submit_form'] + assert form.file.errors == ['The file doesn\'t seem to be an image!'] diff --git a/mediagoblin/tests/test_submission/evil b/mediagoblin/tests/test_submission/evil new file mode 100755 index 00000000..775da664 Binary files /dev/null and b/mediagoblin/tests/test_submission/evil differ diff --git a/mediagoblin/tests/test_submission/evil.jpg b/mediagoblin/tests/test_submission/evil.jpg new file mode 100755 index 00000000..775da664 Binary files /dev/null and b/mediagoblin/tests/test_submission/evil.jpg differ diff --git a/mediagoblin/tests/test_submission/evil.png b/mediagoblin/tests/test_submission/evil.png new file mode 100755 index 00000000..775da664 Binary files /dev/null and b/mediagoblin/tests/test_submission/evil.png differ diff --git a/mediagoblin/tests/test_submission/good.jpg b/mediagoblin/tests/test_submission/good.jpg new file mode 100644 index 00000000..936458e9 Binary files /dev/null and b/mediagoblin/tests/test_submission/good.jpg differ diff --git a/mediagoblin/tests/test_submission/good.png b/mediagoblin/tests/test_submission/good.png new file mode 100644 index 00000000..c1eadf9c Binary files /dev/null and b/mediagoblin/tests/test_submission/good.png differ -- cgit v1.2.3 From 9df37e8a4a9387e9ebe6690d0f18b94a31d7ae4d Mon Sep 17 00:00:00 2001 From: Chris Moylan Date: Sun, 10 Jul 2011 22:45:27 -0500 Subject: added suggestions as per http://bugs.foocorp.net/issues/365#note-9 --- mediagoblin/tests/test_submission.py | 88 +++++++++++++++++------------------- 1 file changed, 41 insertions(+), 47 deletions(-) (limited to 'mediagoblin') diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 20c187e7..22b6117c 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -15,7 +15,7 @@ # along with this program. If not, see . import urlparse -from os import getcwd +import pkg_resources from nose.tools import assert_equal @@ -24,20 +24,18 @@ from mediagoblin.tests.tools import setup_fresh_app, get_test_app from mediagoblin import mg_globals from mediagoblin import util -IMAGE_ROOT = getcwd() + '/mediagoblin/tests/test_submission/' -GOOD_JPG = 'good.jpg' -GOOD_PNG = 'good.png' -EVIL_FILE = 'evil' -EVIL_JPG = 'evil.jpg' -EVIL_PNG = 'evil.png' +GOOD_JPG = pkg_resources.resource_filename( + 'mediagoblin.tests', 'test_submission/good.jpg') +GOOD_PNG = pkg_resources.resource_filename( + 'mediagoblin.tests', 'test_submission/good.png') +EVIL_FILE = pkg_resources.resource_filename( + 'mediagoblin.tests', 'test_submission/evil') +EVIL_JPG = pkg_resources.resource_filename( + 'mediagoblin.tests', 'test_submission/evil.jpg') +EVIL_PNG = pkg_resources.resource_filename( + 'mediagoblin.tests', 'test_submission/evil.png') -# TODO: -# - Define test files as globals -# - supported mime types -# - unsupported mime type with supported extension -# - Remove any imports that aren't neccessary - class TestSubmission: def setUp(self): self.test_app = get_test_app() @@ -79,12 +77,6 @@ class TestSubmission: def test_normal_uploads(self): - # FYI: - # upload_files is for file uploads. It should be a list of - # [(fieldname, filename, file_content)]. You can also use - # just [(fieldname, filename)] and the file content will be - # read from disk. - # Test JPG # -------- util.clear_test_template_context() @@ -92,15 +84,15 @@ class TestSubmission: '/submit/', { 'title': 'Normal upload 1' }, upload_files=[( - 'file', IMAGE_ROOT + GOOD_JPG)]) + 'file', GOOD_JPG)]) # User should be redirected response.follow() assert_equal( urlparse.urlsplit(response.location)[2], - '/submit/success/') + '/u/chris/') assert util.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/submit/success.html') + 'mediagoblin/user_pages/user.html') # Test PNG # -------- @@ -109,16 +101,14 @@ class TestSubmission: '/submit/', { 'title': 'Normal upload 2' }, upload_files=[( - 'file', IMAGE_ROOT + GOOD_PNG)]) + 'file', GOOD_PNG)]) response.follow() assert_equal( urlparse.urlsplit(response.location)[2], - '/submit/success/') + '/u/chris/') assert util.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/submit/success.html') - - # TODO: Test additional supported formats + 'mediagoblin/user_pages/user.html') def test_malicious_uploads(self): @@ -129,35 +119,39 @@ class TestSubmission: '/submit/', { 'title': 'Malicious Upload 2' }, upload_files=[( - 'file', IMAGE_ROOT + EVIL_FILE)]) + 'file', EVIL_FILE)]) context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] form = context['submit_form'] assert form.file.errors == ['The file doesn\'t seem to be an image!'] + # NOTE: The following 2 tests will fail. These can be uncommented + # after http://bugs.foocorp.net/issues/324 is resolved and + # bad files are handled properly. + # Test non-supported file with .jpg extension # ------------------------------------------- - util.clear_test_template_context() - response = self.test_app.post( - '/submit/', { - 'title': 'Malicious Upload 2' - }, upload_files=[( - 'file', IMAGE_ROOT + EVIL_JPG)]) + #util.clear_test_template_context() + #response = self.test_app.post( + # '/submit/', { + # 'title': 'Malicious Upload 2' + # }, upload_files=[( + # 'file', EVIL_JPG)]) - context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] - form = context['submit_form'] - assert form.file.errors == ['The file doesn\'t seem to be an image!'] + #context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + #form = context['submit_form'] + #assert form.file.errors == ['The file doesn\'t seem to be an image!'] # Test non-supported file with .png extension # ------------------------------------------- - util.clear_test_template_context() - response = self.test_app.post( - '/submit/', { - 'title': 'Malicious Upload 3' - }, upload_files=[( - 'file', IMAGE_ROOT + EVIL_PNG)]) - - context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] - form = context['submit_form'] - assert form.file.errors == ['The file doesn\'t seem to be an image!'] + #util.clear_test_template_context() + #response = self.test_app.post( + # '/submit/', { + # 'title': 'Malicious Upload 3' + # }, upload_files=[( + # 'file', EVIL_PNG)]) + + #context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html'] + #form = context['submit_form'] + #assert form.file.errors == ['The file doesn\'t seem to be an image!'] -- cgit v1.2.3