From 9754802d4bca036b8fb0b50db948dd2eb8f64bd6 Mon Sep 17 00:00:00 2001 From: Elrond Date: Thu, 1 Dec 2011 23:33:47 +0100 Subject: [PATCH] fixture_add_user: Factoring a unit test tool Some unit tests need a user in the database, especially to act as that user. Some routines did that on their own. So factored this whole thing into a new function and use it around. --- mediagoblin/tests/test_auth.py | 8 ++------ mediagoblin/tests/test_edit.py | 21 ++++----------------- mediagoblin/tests/test_submission.py | 12 +++--------- mediagoblin/tests/tools.py | 17 +++++++++++++++++ 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 153c6e53..acef3d26 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -20,7 +20,7 @@ import datetime 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, fixture_add_user from mediagoblin import mg_globals from mediagoblin.tools import template, mail @@ -332,11 +332,7 @@ def test_authentication_views(test_app): Test logging in and logging out """ # Make a new user - 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() + test_user = fixture_add_user(active_user=False) # Get login # --------- diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py index 3637b046..c29ddfe9 100644 --- a/mediagoblin/tests/test_edit.py +++ b/mediagoblin/tests/test_edit.py @@ -15,23 +15,16 @@ # along with this program. If not, see . from mediagoblin import mg_globals -from mediagoblin.tests.tools import setup_fresh_app +from mediagoblin.tests.tools import setup_fresh_app, fixture_add_user from mediagoblin.tools import template -from mediagoblin.auth.lib import bcrypt_check_password, \ - bcrypt_gen_password_hash +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 = 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'] = bcrypt_gen_password_hash('toast') - test_user.save() + test_user = fixture_add_user() test_app.post( '/auth/login/', { @@ -73,13 +66,7 @@ def test_change_password(test_app): def change_bio_url(test_app): """Test changing bio and URL""" # set up new user - 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'] = bcrypt_gen_password_hash('toast') - test_user.save() + test_user = fixture_add_user() # test changing the bio and the URL properly test_app.post( diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index eea5747f..7ea6c4bc 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -19,8 +19,8 @@ import pkg_resources from nose.tools import assert_equal, assert_true, assert_false -from mediagoblin.auth import lib as auth_lib -from mediagoblin.tests.tools import setup_fresh_app, get_test_app +from mediagoblin.tests.tools import setup_fresh_app, get_test_app, \ + fixture_add_user from mediagoblin import mg_globals from mediagoblin.tools import template, common @@ -45,13 +45,7 @@ class TestSubmission: # 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['email_verified'] = True - test_user['status'] = u'active' - test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') - test_user.save() + test_user = fixture_add_user() self.test_user = test_user diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py index 01813e96..49a3d33e 100644 --- a/mediagoblin/tests/tools.py +++ b/mediagoblin/tests/tools.py @@ -27,6 +27,7 @@ from mediagoblin.init.config import read_mediagoblin_config from mediagoblin.decorators import _make_safe from mediagoblin.db.open import setup_connection_and_db_from_config from mediagoblin.meddleware import BaseMeddleware +from mediagoblin.auth.lib import bcrypt_gen_password_hash MEDIAGOBLIN_TEST_DB_NAME = u'__mediagoblin_tests__' @@ -200,3 +201,19 @@ def assert_db_meets_expected(db, expected): document = collection.find_one({'_id': expected_document['_id']}) assert document is not None # make sure it exists assert document == expected_document # make sure it matches + + +def fixture_add_user(username = u'chris', password = 'toast', + active_user = True): + test_user = mg_globals.database.User() + test_user.username = username + test_user.email = username + u'@example.com' + if password is not None: + test_user.pw_hash = bcrypt_gen_password_hash(password) + if active_user: + test_user.email_verified = True + test_user.status = u'active' + + test_user.save() + + return test_user -- 2.25.1