diff options
Diffstat (limited to 'mediagoblin/tests')
-rw-r--r-- | mediagoblin/tests/test_api.py | 2 | ||||
-rw-r--r-- | mediagoblin/tests/test_auth.py | 10 | ||||
-rw-r--r-- | mediagoblin/tests/test_csrf_middleware.py | 19 | ||||
-rw-r--r-- | mediagoblin/tests/test_edit.py | 173 | ||||
-rw-r--r-- | mediagoblin/tests/test_http_callback.py | 2 | ||||
-rw-r--r-- | mediagoblin/tests/test_messages.py | 13 | ||||
-rw-r--r-- | mediagoblin/tests/test_misc.py | 12 | ||||
-rw-r--r-- | mediagoblin/tests/test_sql_migrations.py | 2 | ||||
-rw-r--r-- | mediagoblin/tests/test_submission.py | 18 | ||||
-rw-r--r-- | mediagoblin/tests/test_tags.py | 6 | ||||
-rw-r--r-- | mediagoblin/tests/test_tests.py | 14 | ||||
-rw-r--r-- | mediagoblin/tests/tools.py | 10 |
12 files changed, 155 insertions, 126 deletions
diff --git a/mediagoblin/tests/test_api.py b/mediagoblin/tests/test_api.py index 188cdadb..4b784da3 100644 --- a/mediagoblin/tests/test_api.py +++ b/mediagoblin/tests/test_api.py @@ -44,7 +44,7 @@ BIG_BLUE = resource('bigblue.png') class TestAPI(object): def setUp(self): - self.app = get_test_app() + self.app = get_test_app(dump_old_app=False) self.db = mg_globals.database self.user_password = u'4cc355_70k3N' diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index f4a31a81..a40c9cbc 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -22,7 +22,7 @@ from nose.tools import assert_equal from mediagoblin import mg_globals from mediagoblin.auth import lib as auth_lib from mediagoblin.db.models import User -from mediagoblin.tests.tools import setup_fresh_app, fixture_add_user +from mediagoblin.tests.tools import get_test_app, fixture_add_user from mediagoblin.tools import template, mail @@ -67,11 +67,11 @@ def test_bcrypt_gen_password_hash(): 'notthepassword', hashed_pw, '3><7R45417') -@setup_fresh_app -def test_register_views(test_app): +def test_register_views(): """ Massive test function that all our registration-related views all work. """ + test_app = get_test_app(dump_old_app=False) # Test doing a simple GET on the page # ----------------------------------- @@ -311,11 +311,11 @@ def test_register_views(test_app): 'mediagoblin/root.html') -@setup_fresh_app -def test_authentication_views(test_app): +def test_authentication_views(): """ Test logging in and logging out """ + test_app = get_test_app(dump_old_app=False) # Make a new user test_user = fixture_add_user(active_user=False) diff --git a/mediagoblin/tests/test_csrf_middleware.py b/mediagoblin/tests/test_csrf_middleware.py index d730909f..22a0eb04 100644 --- a/mediagoblin/tests/test_csrf_middleware.py +++ b/mediagoblin/tests/test_csrf_middleware.py @@ -14,13 +14,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin.tests.tools import get_test_app from mediagoblin import mg_globals -@setup_fresh_app -def test_csrf_cookie_set(test_app): - +def test_csrf_cookie_set(): + test_app = get_test_app(dump_old_app=False) cookie_name = mg_globals.app_config['csrf_cookie_name'] # get login page @@ -34,8 +33,11 @@ def test_csrf_cookie_set(test_app): assert response.headers.get('Vary', False) == 'Cookie' -@setup_fresh_app -def test_csrf_token_must_match(test_app): +def test_csrf_token_must_match(): + # We need a fresh app for this test on webtest < 1.3.6. + # We do not understand why, but it fixes the tests. + # If we require webtest >= 1.3.6, we can switch to a non fresh app here. + test_app = get_test_app(dump_old_app=True) # construct a request with no cookie or form token assert test_app.post('/auth/login/', @@ -65,9 +67,8 @@ def test_csrf_token_must_match(test_app): extra_environ={'gmg.verify_csrf': True}).\ status_int == 200 -@setup_fresh_app -def test_csrf_exempt(test_app): - +def test_csrf_exempt(): + test_app = get_test_app(dump_old_app=False) # monkey with the views to decorate a known endpoint import mediagoblin.auth.views from mediagoblin.meddleware.csrf import csrf_exempt diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py index 353a7eb9..cbdad649 100644 --- a/mediagoblin/tests/test_edit.py +++ b/mediagoblin/tests/test_edit.py @@ -14,83 +14,104 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +from nose.tools import assert_equal + from mediagoblin import mg_globals -from mediagoblin.tests.tools import setup_fresh_app, fixture_add_user +from mediagoblin.db.models import User +from mediagoblin.tests.tools import get_test_app, fixture_add_user from mediagoblin.tools import template from mediagoblin.auth.lib import bcrypt_check_password - -@setup_fresh_app -def test_change_password(test_app): - """Test changing password correctly and incorrectly""" - # set up new user - test_user = fixture_add_user() - - test_app.post( - '/auth/login/', { - 'username': u'chris', - 'password': 'toast'}) - - # test that the password can be changed - # template.clear_test_template_context() - test_app.post( - '/edit/account/', { - 'old_password': 'toast', - 'new_password': '123456', - 'wants_comment_notification': 'y' - }) - - # test_user has to be fetched again in order to have the current values - test_user = mg_globals.database.User.one({'username': u'chris'}) - - assert bcrypt_check_password('123456', test_user.pw_hash) - - # test that the password cannot be changed if the given old_password - # is wrong - # template.clear_test_template_context() - test_app.post( - '/edit/account/', { - 'old_password': 'toast', - 'new_password': '098765', - }) - - test_user = mg_globals.database.User.one({'username': u'chris'}) - - assert not bcrypt_check_password('098765', test_user.pw_hash) - - -@setup_fresh_app -def change_bio_url(test_app): - """Test changing bio and URL""" - # set up new user - test_user = fixture_add_user() - - # test changing the bio and the URL properly - test_app.post( - '/edit/profile/', { - 'bio': u'I love toast!', - 'url': u'http://dustycloud.org/'}) - - test_user = mg_globals.database.User.one({'username': u'chris'}) - - assert test_user.bio == u'I love toast!' - assert test_user.url == u'http://dustycloud.org/' - - # test changing the bio and the URL inproperly - too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't' - - test_app.post( - '/edit/profile/', { - # more than 500 characters - 'bio': too_long_bio, - 'url': 'this-is-no-url'}) - - test_user = mg_globals.database.User.one({'username': u'chris'}) - - context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/edit/edit_profile.html'] - form = context['edit_profile_form'] - - assert form.bio.errors == [u'Field must be between 0 and 500 characters long.'] - assert form.url.errors == [u'Improperly formed URL'] - - # test changing the url inproperly +class TestUserEdit(object): + def setUp(self): + self.app = get_test_app(dump_old_app=False) + # set up new user + self.user_password = u'toast' + self.user = fixture_add_user(password = self.user_password) + self.login() + + def login(self): + self.app.post( + '/auth/login/', { + 'username': self.user.username, + 'password': self.user_password}) + + + def test_change_password(self): + """Test changing password correctly and incorrectly""" + # test that the password can be changed + # template.clear_test_template_context() + res = self.app.post( + '/edit/account/', { + 'old_password': 'toast', + 'new_password': '123456', + 'wants_comment_notification': 'y' + }) + + # Check for redirect on success + assert_equal(res.status_int, 302) + # test_user has to be fetched again in order to have the current values + test_user = User.query.filter_by(username=u'chris').first() + assert bcrypt_check_password('123456', test_user.pw_hash) + # Update current user passwd + self.user_password = '123456' + + # test that the password cannot be changed if the given + # old_password is wrong template.clear_test_template_context() + self.app.post( + '/edit/account/', { + 'old_password': 'toast', + 'new_password': '098765', + }) + + test_user = User.query.filter_by(username=u'chris').first() + assert not bcrypt_check_password('098765', test_user.pw_hash) + + + + def test_change_bio_url(self): + """Test changing bio and URL""" + # Test if legacy profile editing URL redirects correctly + res = self.app.post( + '/edit/profile/', { + 'bio': u'I love toast!', + 'url': u'http://dustycloud.org/'}, expect_errors=True) + + # Should redirect to /u/chris/edit/ + assert_equal (res.status_int, 302) + assert res.headers['Location'].endswith("/u/chris/edit/") + + res = self.app.post( + '/u/chris/edit/', { + 'bio': u'I love toast!', + 'url': u'http://dustycloud.org/'}) + + test_user = User.query.filter_by(username=u'chris').first() + assert_equal(test_user.bio, u'I love toast!') + assert_equal(test_user.url, u'http://dustycloud.org/') + + # change a different user than the logged in (should fail with 403) + fixture_add_user(username=u"foo") + res = self.app.post( + '/u/foo/edit/', { + 'bio': u'I love toast!', + 'url': u'http://dustycloud.org/'}, expect_errors=True) + assert_equal(res.status_int, 403) + + # test changing the bio and the URL inproperly + too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't' + + self.app.post( + '/u/chris/edit/', { + # more than 500 characters + 'bio': too_long_bio, + 'url': 'this-is-no-url'}) + + # Check form errors + context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/edit/edit_profile.html'] + form = context['form'] + + assert_equal(form.bio.errors, [u'Field must be between 0 and 500 characters long.']) + assert_equal(form.url.errors, [u'This address contains errors']) + +# test changing the url inproperly diff --git a/mediagoblin/tests/test_http_callback.py b/mediagoblin/tests/test_http_callback.py index 8b0a03b9..0f6e489f 100644 --- a/mediagoblin/tests/test_http_callback.py +++ b/mediagoblin/tests/test_http_callback.py @@ -27,7 +27,7 @@ from mediagoblin.tests import test_oauth as oauth class TestHTTPCallback(object): def setUp(self): - self.app = get_test_app() + self.app = get_test_app(dump_old_app=False) self.db = mg_globals.database self.user_password = u'secret' diff --git a/mediagoblin/tests/test_messages.py b/mediagoblin/tests/test_messages.py index d3b84828..c587e599 100644 --- a/mediagoblin/tests/test_messages.py +++ b/mediagoblin/tests/test_messages.py @@ -15,30 +15,31 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from mediagoblin.messages import fetch_messages, add_message -from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin.tests.tools import get_test_app from mediagoblin.tools import template -@setup_fresh_app -def test_messages(test_app): + +def test_messages(): """ Added messages should show up in the request.session, fetched messages should be the same as the added ones, and fetching should clear the message list. """ + test_app = get_test_app(dump_old_app=False) # Aquire a request object test_app.get('/') context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] request = context['request'] - + # The message queue should be empty assert request.session.get('messages', []) == [] - + # Adding a message should modify the session accordingly add_message(request, 'herp_derp', 'First!') test_msg_queue = [{'text': 'First!', 'level': 'herp_derp'}] assert request.session['messages'] == test_msg_queue - + # fetch_messages should return and empty the queue assert fetch_messages(request) == test_msg_queue assert request.session.get('messages') == [] diff --git a/mediagoblin/tests/test_misc.py b/mediagoblin/tests/test_misc.py index 94ae5a51..8a96e7d0 100644 --- a/mediagoblin/tests/test_misc.py +++ b/mediagoblin/tests/test_misc.py @@ -16,11 +16,9 @@ from nose.tools import assert_equal -from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin.tests.tools import get_test_app - -@setup_fresh_app -def test_404_for_non_existent(test_app): - assert_equal(test_app.get('/does-not-exist/', - expect_errors=True).status_int, - 404) +def test_404_for_non_existent(): + test_app = get_test_app(dump_old_app=False) + res = test_app.get('/does-not-exist/', expect_errors=True) + assert_equal(res.status_int, 404) diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 26979bdf..2fc4c043 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -26,7 +26,7 @@ from sqlalchemy.sql import select, insert from migrate import changeset from mediagoblin.db.base import GMGTableBase -from mediagoblin.db.util import MigrationManager, RegisterMigration +from mediagoblin.db.migration_tools import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 589ba7ed..53330c48 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -50,7 +50,7 @@ REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request'] class TestSubmission: def setUp(self): - self.test_app = get_test_app() + self.test_app = get_test_app(dump_old_app=False) # TODO: Possibly abstract into a decorator like: # @as_authenticated_user('chris') @@ -132,11 +132,11 @@ class TestSubmission: def test_tags(self): # Good tag string # -------- - response, request = self.do_post({'title': u'Balanced Goblin', + response, request = self.do_post({'title': u'Balanced Goblin 2', 'tags': GOOD_TAG_STRING}, *REQUEST_CONTEXT, do_follow=True, **self.upload_data(GOOD_JPG)) - media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) + media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1) assert media.tags[0]['name'] == u'yin' assert media.tags[0]['slug'] == u'yin' @@ -145,7 +145,7 @@ class TestSubmission: # Test tags that are too long # --------------- - response, form = self.do_post({'title': u'Balanced Goblin', + response, form = self.do_post({'title': u'Balanced Goblin 2', 'tags': BAD_TAG_STRING}, *FORM_CONTEXT, **self.upload_data(GOOD_JPG)) @@ -161,11 +161,17 @@ class TestSubmission: media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) media_id = media.id + # At least render the edit page + edit_url = request.urlgen( + 'mediagoblin.edit.edit_media', + user=self.test_user.username, media_id=media_id) + self.test_app.get(edit_url) + # Add a comment, so we can test for its deletion later. self.check_comments(request, media_id, 0) comment_url = request.urlgen( 'mediagoblin.user_pages.media_post_comment', - user=self.test_user.username, media=media_id) + user=self.test_user.username, media_id=media_id) response = self.do_post({'comment_content': 'i love this test'}, url=comment_url, do_follow=True)[0] self.check_comments(request, media_id, 1) @@ -174,7 +180,7 @@ class TestSubmission: # --------------------------------------------------- delete_url = request.urlgen( 'mediagoblin.user_pages.media_confirm_delete', - user=self.test_user.username, media=media_id) + user=self.test_user.username, media_id=media_id) # Empty data means don't confirm response = self.do_post({}, do_follow=True, url=delete_url)[0] media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) diff --git a/mediagoblin/tests/test_tags.py b/mediagoblin/tests/test_tags.py index bc657660..73af2eea 100644 --- a/mediagoblin/tests/test_tags.py +++ b/mediagoblin/tests/test_tags.py @@ -14,17 +14,17 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin.tests.tools import get_test_app from mediagoblin.tools import text -@setup_fresh_app -def test_list_of_dicts_conversion(test_app): +def test_list_of_dicts_conversion(): """ When the user adds tags to a media entry, the string from the form is converted into a list of tags, where each tag is stored in the database as a dict. Each tag dict should contain the tag's name and slug. Another function performs the reverse operation when populating a form to edit tags. """ + test_app = get_test_app(dump_old_app=False) # Leading, trailing, and internal whitespace should be removed and slugified assert text.convert_to_tag_list_of_dicts('sleep , 6 AM, chainsaw! ') == [ {'name': u'sleep', 'slug': u'sleep'}, diff --git a/mediagoblin/tests/test_tests.py b/mediagoblin/tests/test_tests.py index b11dc730..d09e8f28 100644 --- a/mediagoblin/tests/test_tests.py +++ b/mediagoblin/tests/test_tests.py @@ -15,7 +15,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from mediagoblin import mg_globals -from mediagoblin.tests.tools import get_test_app +from mediagoblin.tests.tools import get_test_app, fixture_add_user from mediagoblin.db.models import User @@ -23,16 +23,14 @@ def test_get_test_app_wipes_db(): """ Make sure we get a fresh database on every wipe :) """ - get_test_app() + get_test_app(dump_old_app=True) assert User.query.count() == 0 - new_user = mg_globals.database.User() - new_user.username = u'lolcat' - new_user.email = u'lol@cats.example.org' - new_user.pw_hash = u'pretend_this_is_a_hash' - new_user.save() + fixture_add_user() assert User.query.count() == 1 - get_test_app() + get_test_app(dump_old_app=False) + assert User.query.count() == 1 + get_test_app(dump_old_app=True) assert User.query.count() == 0 diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py index 31afb08b..3e78b2e3 100644 --- a/mediagoblin/tests/tools.py +++ b/mediagoblin/tests/tools.py @@ -25,6 +25,7 @@ from paste.deploy import loadapp from webtest import TestApp from mediagoblin import mg_globals +from mediagoblin.db.models import User from mediagoblin.tools import testing from mediagoblin.init.config import read_mediagoblin_config from mediagoblin.db.open import setup_connection_and_db_from_config @@ -202,9 +203,12 @@ def assert_db_meets_expected(db, expected): assert document == expected_document # make sure it matches -def fixture_add_user(username=u'chris', password='toast', +def fixture_add_user(username=u'chris', password=u'toast', active_user=True): - test_user = mg_globals.database.User() + # Reuse existing user or create a new one + test_user = User.query.filter_by(username=username).first() + if test_user is None: + test_user = User() test_user.username = username test_user.email = username + u'@example.com' if password is not None: @@ -216,7 +220,7 @@ def fixture_add_user(username=u'chris', password='toast', test_user.save() # Reload - test_user = mg_globals.database.User.find_one({'username': username}) + test_user = User.query.filter_by(username=username).first() # ... and detach from session: Session.expunge(test_user) |