From 460ce56493442b1d89270313e7789fd455ef71e6 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 10:34:29 -0500 Subject: The first bit of the registration tests working. Not fully there, but it's clear that the webtest part is working, without having tested the database yet. :) --- mediagoblin/tests/test_auth.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 94ce6bba..b0355732 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -17,6 +17,10 @@ from mediagoblin.auth import lib as auth_lib +from mediagoblin.tests.tools import get_test_app + +from mediagoblin import util + ######################## # Test bcrypt auth funcs @@ -57,3 +61,25 @@ def test_bcrypt_gen_password_hash(): pw, hashed_pw, '3><7R45417') assert not auth_lib.bcrypt_check_password( 'notthepassword', hashed_pw, '3><7R45417') + + +def test_register_views(): + util.clear_test_template_context() + test_app = get_test_app() + + # Test doing a simple GET on the page + test_app.get('/auth/register/') + # Make sure it rendered with the appropriate template + assert util.TEMPLATE_TEST_CONTEXT.has_key( + 'mediagoblin/auth/register.html') + + # Try to register without providing anything, should error + util.clear_test_template_context() + test_app.post( + '/auth/register/', {}) + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] + form = context['register_form'] + assert form.username.errors == [u'This field is required.'] + assert form.password.errors == [u'This field is required.'] + assert form.confirm_password.errors == [u'This field is required.'] + assert form.email.errors == [u'This field is required.'] -- cgit v1.2.3 From 651403f02553da25a8e611804ce36ac92bbaa8cd Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 15:30:07 -0500 Subject: Test registration form integrity --- mediagoblin/tests/test_auth.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index b0355732..6631c675 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -19,6 +19,7 @@ from mediagoblin.auth import lib as auth_lib from mediagoblin.tests.tools import get_test_app +from mediagoblin import globals as mgoblin_globals from mediagoblin import util @@ -68,12 +69,16 @@ def test_register_views(): test_app = get_test_app() # Test doing a simple GET on the page + # ----------------------------------- + test_app.get('/auth/register/') # Make sure it rendered with the appropriate template assert util.TEMPLATE_TEST_CONTEXT.has_key( 'mediagoblin/auth/register.html') # Try to register without providing anything, should error + # -------------------------------------------------------- + util.clear_test_template_context() test_app.post( '/auth/register/', {}) @@ -83,3 +88,62 @@ def test_register_views(): assert form.password.errors == [u'This field is required.'] assert form.confirm_password.errors == [u'This field is required.'] assert form.email.errors == [u'This field is required.'] + + # Try to register with fields that are known to be invalid + # -------------------------------------------------------- + + ## too short + util.clear_test_template_context() + test_app.post( + '/auth/register/', { + 'username': 'l', + 'password': 'o', + 'confirm_password': 'o', + 'email': 'l'}) + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] + form = context['register_form'] + + assert form.username.errors == [ + u'Field must be between 3 and 30 characters long.'] + assert form.password.errors == [ + u'Field must be between 6 and 30 characters long.'] + + ## bad form + util.clear_test_template_context() + test_app.post( + '/auth/register/', { + 'username': '@_@', + 'email': 'lollerskates'}) + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] + form = context['register_form'] + + assert form.username.errors == [ + u'Invalid input.'] + assert form.email.errors == [ + u'Invalid email address.'] + + ## mismatching passwords + util.clear_test_template_context() + test_app.post( + '/auth/register/', { + 'password': 'herpderp', + 'confirm_password': 'derpherp'}) + context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] + form = context['register_form'] + + assert form.password.errors == [ + u'Passwords must match.'] + + ## At this point there should be no users in the database ;) + assert not mgoblin_globals.database.User.find().count() + + # Successful register + # ------------------- + ## Did we redirect to the proper page? Use the right template? + ## Make sure user is in place + ## Make sure we get email confirmation + ## Try logging in + + # We shouldn't be able to register with that user twice though... + + # Also check for double instances of an email address -- cgit v1.2.3 From cb9bac0c83b7dd6d9153fc153b7282c26fe466f6 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 15:33:48 -0500 Subject: Just a bit of formatting for these unfinished tests ;) --- mediagoblin/tests/test_auth.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 6631c675..7a46a1ff 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -144,6 +144,8 @@ def test_register_views(): ## Make sure we get email confirmation ## Try logging in - # We shouldn't be able to register with that user twice though... + # Uniqueness checks + # ----------------- + ## We shouldn't be able to register with that user twice - # Also check for double instances of an email address + ## Also check for double instances of an email address -- cgit v1.2.3 From 1972a888b3619d23968a4efcd2cfab550ca588f1 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 17:39:47 -0500 Subject: Make sure we can register, and then that we get the verification email --- mediagoblin/tests/test_auth.py | 50 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 7a46a1ff..43778a64 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -14,11 +14,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import urlparse -from mediagoblin.auth import lib as auth_lib +from nose.tools import assert_equal +from mediagoblin.auth import lib as auth_lib from mediagoblin.tests.tools import get_test_app - from mediagoblin import globals as mgoblin_globals from mediagoblin import util @@ -139,10 +140,51 @@ def test_register_views(): # Successful register # ------------------- + util.clear_test_template_context() + response = test_app.post( + '/auth/register/', { + 'username': 'happygirl', + 'password': 'iamsohappy', + 'confirm_password': 'iamsohappy', + 'email': 'happygrrl@example.org'}) + response.follow() + ## Did we redirect to the proper page? Use the right template? + assert_equal( + urlparse.urlsplit(response.location)[2], + '/auth/register/success/') + assert util.TEMPLATE_TEST_CONTEXT.has_key( + 'mediagoblin/auth/register_success.html') + ## Make sure user is in place - ## Make sure we get email confirmation - ## Try logging in + new_user = mgoblin_globals.database.User.find_one( + {'username': 'happygirl'}) + assert new_user + assert new_user['status'] == u'needs_email_verification' + assert new_user['email_verified'] == False + + ## Make sure we get email confirmation, and try verifying + assert len(util.EMAIL_TEST_INBOX) == 1 + message = util.EMAIL_TEST_INBOX.pop() + assert message['To'] == 'happygrrl@example.org' + email_context = util.TEMPLATE_TEST_CONTEXT[ + 'mediagoblin/auth/verification_email.txt'] + assert email_context['verification_url'] in message.get_payload(decode=True) + + path = urlparse.urlsplit(email_context['verification_url'])[2] + get_params = urlparse.urlsplit(email_context['verification_url'])[3] + assert path == u'/auth/verify_email/' + parsed_get_params = urlparse.parse_qs(get_params) + + ### user should have these same parameters + assert parsed_get_params['userid'] == [ + unicode(new_user['_id'])] + assert parsed_get_params['token'] == [ + new_user['verification_key']] + + ## Verify the email + + ## TODO: Try logging in # Uniqueness checks # ----------------- -- cgit v1.2.3 From 7b1e17ed0d3224e997507e1ef14a111577b8b7b1 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 17:49:43 -0500 Subject: Email verification view test works --- mediagoblin/tests/test_auth.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 43778a64..4009c466 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -182,7 +182,31 @@ def test_register_views(): assert parsed_get_params['token'] == [ new_user['verification_key']] - ## Verify the email + ## Try verifying with bs verification key, shouldn't work + util.clear_test_template_context() + test_app.get( + "/auth/verify_email/?userid=%s&token=total_bs" % unicode( + new_user['_id'])) + context = util.TEMPLATE_TEST_CONTEXT[ + 'mediagoblin/auth/verify_email.html'] + assert context['verification_successful'] == False + new_user = mgoblin_globals.database.User.find_one( + {'username': 'happygirl'}) + assert new_user + assert new_user['status'] == u'needs_email_verification' + assert new_user['email_verified'] == False + + ## Verify the email activation works + util.clear_test_template_context() + test_app.get("%s?%s" % (path, get_params)) + context = util.TEMPLATE_TEST_CONTEXT[ + 'mediagoblin/auth/verify_email.html'] + assert context['verification_successful'] == True + new_user = mgoblin_globals.database.User.find_one( + {'username': 'happygirl'}) + assert new_user + assert new_user['status'] == u'active' + assert new_user['email_verified'] == True ## TODO: Try logging in -- cgit v1.2.3 From 8a869db8e46d120ff12854462e828220cd5ebf6a Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 18:14:48 -0500 Subject: Make sure that two users with the same username can't register. --- mediagoblin/tests/test_auth.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 4009c466..0f954ee0 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -213,5 +213,18 @@ def test_register_views(): # Uniqueness checks # ----------------- ## We shouldn't be able to register with that user twice + util.clear_test_template_context() + response = test_app.post( + '/auth/register/', { + 'username': 'happygirl', + 'password': 'iamsohappy2', + 'confirm_password': 'iamsohappy2', + 'email': 'happygrrl2@example.org'}) + + context = util.TEMPLATE_TEST_CONTEXT[ + 'mediagoblin/auth/register.html'] + form = context['register_form'] + assert form.username.errors == [ + u'Sorry, a user with that name already exists.'] - ## Also check for double instances of an email address + ## TODO: Also check for double instances of an email address? -- cgit v1.2.3 From 2fecc29d06f882541a2b476a618e22a0a90d2736 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 5 Jun 2011 18:16:31 -0500 Subject: Docstring for test_register_views() --- mediagoblin/tests/test_auth.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index 0f954ee0..cf6d48f5 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -66,6 +66,9 @@ def test_bcrypt_gen_password_hash(): def test_register_views(): + """ + Massive test function that all our registration-related views all work. + """ util.clear_test_template_context() test_app = get_test_app() -- cgit v1.2.3 From 3aa4c668b9bfe53ed58d4ae21ed91210df7ad9ff Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Mon, 6 Jun 2011 07:45:18 -0500 Subject: A setup_fresh_app decorator which should make writing tests a bit easier. Setting test_register_views() to use it also. --- mediagoblin/tests/test_auth.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'mediagoblin/tests/test_auth.py') diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index cf6d48f5..cdfeccab 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.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 get_test_app +from mediagoblin.tests.tools import setup_fresh_app from mediagoblin import globals as mgoblin_globals from mediagoblin import util @@ -65,13 +65,11 @@ def test_bcrypt_gen_password_hash(): 'notthepassword', hashed_pw, '3><7R45417') -def test_register_views(): +@setup_fresh_app +def test_register_views(test_app): """ Massive test function that all our registration-related views all work. """ - util.clear_test_template_context() - test_app = get_test_app() - # Test doing a simple GET on the page # ----------------------------------- -- cgit v1.2.3