diff --git a/pylons_app/tests/__init__.py b/pylons_app/tests/__init__.py --- a/pylons_app/tests/__init__.py +++ b/pylons_app/tests/__init__.py @@ -15,7 +15,7 @@ from pylons import config, url from routes.util import URLGenerator from webtest import TestApp import os - +from pylons_app.model import meta import pylons.test __all__ = ['environ', 'url', 'TestController'] @@ -32,5 +32,6 @@ class TestController(TestCase): config = wsgiapp.config self.app = TestApp(wsgiapp) url._push_object(URLGenerator(config['routes.map'], environ)) + self.sa = meta.Session TestCase.__init__(self, *args, **kwargs) diff --git a/pylons_app/tests/functional/test_login.py b/pylons_app/tests/functional/test_login.py --- a/pylons_app/tests/functional/test_login.py +++ b/pylons_app/tests/functional/test_login.py @@ -1,18 +1,21 @@ from pylons_app.tests import * +from pylons_app.model.db import User +from pylons_app.lib.auth import check_password + class TestLoginController(TestController): def test_index(self): response = self.app.get(url(controller='login', action='index')) - assert response.status == '200 OK','Wrong response from login page' + assert response.status == '200 OK', 'Wrong response from login page got %s' % response.status # Test response... def test_login_admin_ok(self): response = self.app.post(url(controller='login', action='index'), {'username':'test_admin', 'password':'test'}) - assert response.status == '302 Found','Wrong response code from login' - assert response.session['hg_app_user'].username =='test_admin','wrong logged in user' + assert response.status == '302 Found', 'Wrong response code from login got %s' % response.status + assert response.session['hg_app_user'].username == 'test_admin', 'wrong logged in user' response = response.follow() assert 'auto description for vcs_test' in response.body @@ -20,32 +23,89 @@ class TestLoginController(TestController response = self.app.post(url(controller='login', action='index'), {'username':'test_regular', 'password':'test'}) - assert response.status == '302 Found','Wrong response code from login' - assert response.session['hg_app_user'].username =='test_regular','wrong logged in user' + assert response.status == '302 Found', 'Wrong response code from login got %s' % response.status + assert response.session['hg_app_user'].username == 'test_regular', 'wrong logged in user' response = response.follow() assert 'auto description for vcs_test' in response.body assert '' not in response.body def test_login_ok_came_from(self): test_came_from = '/_admin/users' - response = self.app.post(url(controller='login', action='index',came_from=test_came_from), + response = self.app.post(url(controller='login', action='index', came_from=test_came_from), {'username':'test_admin', 'password':'test'}) - assert response.status == '302 Found','Wrong response code from came from redirection' + assert response.status == '302 Found', 'Wrong response code from came from redirection' response = response.follow() - assert response.status == '200 OK','Wrong response from login page' - assert 'Users administration' in response.body,'No proper title in response' + assert response.status == '200 OK', 'Wrong response from login page got %s' % response.status + assert 'Users administration' in response.body, 'No proper title in response' def test_login_wrong(self): response = self.app.post(url(controller='login', action='index'), {'username':'error', 'password':'test'}) - assert response.status == '200 OK','Wrong response from login page' + assert response.status == '200 OK', 'Wrong response from login page' - assert 'invalid user name' in response.body,'No error username message in response' - assert 'invalid password' in response.body,'No error password message in response' + assert 'invalid user name' in response.body, 'No error username message in response' + assert 'invalid password' in response.body, 'No error password message in response' - \ No newline at end of file + def test_register(self): + response = self.app.get(url(controller='login', action='register')) + assert 'Sign Up to hg-app' in response.body, 'wrong page for user registration' + + def test_register_err_same_username(self): + response = self.app.post(url(controller='login', action='register'), + {'username':'test_admin', + 'password':'test', + 'email':'goodmail@domain.com', + 'name':'test', + 'lastname':'test'}) + + assert response.status == '200 OK', 'Wrong response from register page got %s' % response.status + assert 'This username already exists' in response.body + + def test_register_err_wrong_data(self): + response = self.app.post(url(controller='login', action='register'), + {'username':'xs', + 'password':'', + 'email':'goodmailm', + 'name':'test', + 'lastname':'test'}) + + assert response.status == '200 OK', 'Wrong response from register page got %s' % response.status + assert 'An email address must contain a single @' in response.body + assert 'Enter a value 3 characters long or more' in response.body + assert 'Please enter a value<' in response.body + + + + def test_register_ok(self): + username = 'test_regular2' + password = 'qweqwe' + email = 'goodmail@mail.com' + name = 'testname' + lastname = 'testlastname' + + response = self.app.post(url(controller='login', action='register'), + {'username':username, + 'password':password, + 'email':email, + 'name':name, + 'lastname':lastname}) + + assert response.status == '302 Found', 'Wrong response from register page got %s' % response.status + + ret = self.sa.query(User).filter(User.username == 'test_regular2').one() + assert ret.username == username , 'field mismatch %s %s' % (ret.username, username) + assert check_password(password,ret.password) == True , 'password mismatch' + assert ret.email == email , 'field mismatch %s %s' % (ret.email, email) + assert ret.name == name , 'field mismatch %s %s' % (ret.name, name) + assert ret.lastname == lastname , 'field mismatch %s %s' % (ret.lastname, lastname) + + + + + +