def test_login_unvalidated(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) user.validated = False cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD) cfg['allow-unvalidated'] = False request = self.make_request(POST={ 'login': '******', 'password': '******' }) request.environ['HTTP_HOST'] = 'example.com' form = login.LoginForm(None, request) form.update_form() form.login_handler() self.assertIn('Account is not validated.', request.render_messages())
def test_suspended(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) user.suspended = True ptah.auth_service.set_userid(user.__uri__) request = self.make_request() res = self.render_route_view(None, request, 'ptahcrowd-login-suspended') self.assertIn('Your account is suspended', res.text)
def test_validation_initiate(self): from ptahcrowd import validation from ptahcrowd.provider import CrowdUser origValidationTemplate = validation.ValidationTemplate class Stub(origValidationTemplate): status = '' token = None def send(self): Stub.status = 'Email has been sended' Stub.token = self.token validation.ValidationTemplate = Stub user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) validation.initiate_email_validation(user.email, user, self.request) self.assertEqual(Stub.status, 'Email has been sended') self.assertIsNotNone(Stub.token) t = ptah.token.service.get_bydata(validation.TOKEN_TYPE, user.__uri__) self.assertEqual(Stub.token, t) validation.ValidationTemplate = origValidationTemplate
def test_password_changer(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='user-email', password='******') self.assertTrue(ptah.pwd_tool.can_change_password(user))
def test_suspended_not(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(name="name", login="******", email="email", password="******") CrowdUser.__type__.add(user) uri = user.__uri__ user.suspended = False request = DummyRequest() ptah.auth_service.set_userid(uri) request = DummyRequest() res = self.render_route_view(None, request, "ptah-login-suspended") self.assertIsInstance(res, HTTPFound)
def test_suspended(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(name="name", login="******", email="email", password="******") CrowdUser.__type__.add(user) uri = user.__uri__ user.suspended = True ptah.auth_service.set_userid(user.__uri__) request = DummyRequest() res = self.render_route_view(None, request, "ptah-login-suspended") self.assertIn("Your account is suspended", res.text)
def test_login(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) request = self.make_request() form = login.LoginForm(None, request) form.update_form() data, errors = form.extract() self.assertEqual(len(errors), 2) form.login_handler() self.assertIn('Please fix indicated errors.', request.render_messages()) request = self.make_request(POST={ 'login': '******', 'password': '******' }) request.environ['HTTP_HOST'] = 'example.com' form = login.LoginForm(None, request) form.update_form() res = form.login_handler() self.assertIsInstance(res, HTTPFound) self.assertEqual(res.headers['location'], 'http://example.com/login-success.html')
def test_suspended_not(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) uri = user.__uri__ user.suspended = False request = self.make_request() ptah.auth_service.set_userid(uri) request = self.make_request() res = self.render_route_view(None, request, 'ptahcrowd-login-suspended') self.assertIsInstance(res, HTTPFound)
def test_join_error(self): from ptahcrowd.provider import CrowdUser from ptahcrowd.registration import Registration user = CrowdUser(username='******', email='email') ptah.get_session().add(user) ptah.get_session().flush() request = self.make_request( POST = {'username': '******', 'email': 'custom login', 'password': '******', 'confirm_password': '******'}) form = Registration(None, request) form.update_form() data, errors = form.extract() self.assertEqual(len(errors), 1) self.assertEqual(errors[0].msg[0], 'Invalid email address') form.register_handler() self.assertIn('Please fix indicated errors.', request.render_messages()) request = self.make_request( POST = {'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******'}) form = Registration(None, request) form.update_form() data, errors = form.extract() self.assertEqual(len(errors), 0)
def test_join(self): from ptahcrowd.provider import CrowdUser from ptahcrowd.registration import Registration user = CrowdUser(username='******', email='email') ptah.get_session().add(user) ptah.get_session().flush() class Stub(object): status = '' def send(self, frm, to, msg): Stub.status = 'Email has been sended' MAIL = ptah.get_settings(ptah.CFG_ID_PTAH) MAIL['mailer'] = Stub() request = self.make_request( POST = {'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******'}) request.environ['HTTP_HOST'] = 'example.com' form = Registration(None, request) form.update_form() res = form.register_handler() self.assertIsInstance(res, HTTPFound) self.assertEqual(res.headers['location'], 'http://example.com/login-success.html') user = ptah.auth_service.get_principal_bylogin('*****@*****.**') self.assertIsInstance(user, CrowdUser) self.assertEqual(user.name, 'test')
def test_crowd_user_change_password(self): from ptahcrowd.provider import CrowdUser, change_password user = CrowdUser(username='******', email='user-email', password='******') change_password(user, '123456') self.assertEqual(user.password, '123456')
def test_login_suspended(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(name="name", login="******", email="email", password="******") CrowdUser.__type__.add(user) uri = user.__uri__ user.suspended = True request = DummyRequest(POST={"login": "******", "password": "******"}) request.environ["HTTP_HOST"] = "example.com" form = login.LoginForm(None, request) form.update() res = form.login_handler() self.assertEqual(res.headers["location"], "http://example.com/login-suspended.html")
def test_validation_registered_no_validation(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD) cfg['validation'] = False self.registry.notify(ptah.events.PrincipalRegisteredEvent(user)) self.assertTrue(user.validated)
def test_login_suspended(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) user.suspended = True request = self.make_request( POST={'login': '******', 'password': '******'}) request.environ['HTTP_HOST'] = 'example.com' form = login.LoginForm(None, request) form.update_form() res = form.login_handler() self.assertEqual(res.headers['location'], 'http://example.com/login-suspended.html')
def test_login_unvalidated(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(name="name", login="******", email="email", password="******") CrowdUser.__type__.add(user) uri = user.__uri__ user.validated = False cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD) cfg["allow-unvalidated"] = False request = DummyRequest(POST={"login": "******", "password": "******"}) request.environ["HTTP_HOST"] = "example.com" form = login.LoginForm(None, request) form.update() form.login_handler() self.assertIn("Account is not validated.", ptah.render_messages(request))
def test_crowd_user_ctor(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='user-email', password='******') self.assertEqual(user.username, 'user-name') self.assertEqual(user.email, 'user-email') self.assertEqual(user.password, 'passwd') self.assertTrue(user.__uri__.startswith('ptah-crowd-user:'******'user-name') self.assertEqual(repr(user), 'CrowdUser<%s:%s>'%( user.name, user.__uri__))
def test_login_unvalidated(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) user.validated = False cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD) cfg['allow-unvalidated'] = False request = self.make_request( POST={'login': '******', 'password': '******'}) request.environ['HTTP_HOST'] = 'example.com' form = login.LoginForm(None, request) form.update_form() form.login_handler() self.assertIn('Account is not validated.',request.render_messages())
def test_validate(self): from ptahcrowd import validation from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) t = ptah.token.service.generate(validation.TOKEN_TYPE, user.__uri__) res = validation.validate(self.request) self.assertIsInstance(res, HTTPFound) self.assertIn( "Can't validate email address.", self.request.render_messages()) user.validated = False self.request.GET['token'] = t self.request.session.clear() res = validation.validate(self.request) self.assertIn("Account has been successfully validated.", self.request.render_messages()) self.assertTrue(user.validated)
def test_login_suspended(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) user.suspended = True request = self.make_request(POST={ 'login': '******', 'password': '******' }) request.environ['HTTP_HOST'] = 'example.com' form = login.LoginForm(None, request) form.update_form() res = form.login_handler() self.assertEqual(res.headers['location'], 'http://example.com/login-suspended.html')
def test_login_success(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) uri = user.__uri__ ptah.auth_service.set_userid(uri) res = self.render_route_view(None, self.request, 'ptahcrowd-login-success') self.assertIn('You are now logged in', res.text)
def test_get_bylogin(self): from ptahcrowd.provider import \ CrowdAuthProvider, CrowdUser provider = CrowdAuthProvider() self.assertIsNone(provider.get_principal_bylogin('test')) user = CrowdUser(username='******', email='*****@*****.**', password=ptah.pwd_tool.encode('12345')) ptah.get_session().add(user) ptah.get_session().flush() user = provider.get_principal_bylogin('test') self.assertIsInstance(user, CrowdUser) self.assertEqual(user.username, 'test')
def test_resetpassword_form_update(self): from ptahcrowd.provider import CrowdUser from ptahcrowd.resetpassword import ResetPasswordForm user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) passcode = ptah.pwd_tool.generate_passcode(user) request = self.make_request(subpath=(passcode, )) form = ResetPasswordForm(None, request) form.update_form() # self.assertEqual(form.title, user.name) self.assertEqual(form.passcode, passcode)
def test_crowd_user_change_search(self): from ptahcrowd.provider import CrowdUser, CrowdAuthProvider user = CrowdUser(username='******', email='user-email', password='******') ptah.get_session().add(user) ptah.get_session().flush() uri = user.__uri__ users = list(CrowdAuthProvider.search('user')) self.assertEqual(len(users), 1) self.assertEqual(users[0].__uri__, uri) users = list(CrowdAuthProvider.search('email')) self.assertEqual(len(users), 1) self.assertEqual(users[0].__uri__, uri)
def test_manage_module_get(self): from ptahcrowd.module import CrowdModule from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email') user.__type__.add(user) uri = user.__uri__ mod = CrowdModule(None, self.make_request()) self.assertRaises(KeyError, mod.__getitem__, 'unkown') wu = mod[str(user.id)] self.assertIsInstance(wu, CrowdUser) self.assertEqual(wu.__uri__, uri)
def test_logout(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) uri = user.__uri__ request = self.make_request() request.environ['HTTP_HOST'] = 'example.com' ptah.auth_service.set_userid(uri) res = login.logout(request) self.assertIsInstance(res, HTTPFound) self.assertIsNone(ptah.auth_service.get_userid())
def test_validation_template(self): from ptahcrowd import validation from ptahcrowd.provider import CrowdUser origValidationTemplate = validation.ValidationTemplate user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) template = validation.ValidationTemplate( user, self.request, email = user.email, token = 'test-token') template.update() res = text_(template.render()) self.assertIn( "You're close to completing the registration process.", res) self.assertIn( "http://example.com/validateaccount.html?token=test-token", res) validation.ValidationTemplate = origValidationTemplate
def test_login_wrong_login(self): from ptahcrowd import login from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email', password='******') CrowdUser.__type__.add(user) request = self.make_request(POST={ 'login': '******', 'password': '******' }) request.environ['HTTP_HOST'] = 'example.com' form = login.LoginForm(None, request) form.update_form() form.login_handler() self.assertIn("You have entered the wrong login or password.", request.render_messages())
def test_resetpassword_template(self): from ptahcrowd.provider import CrowdUser from ptahcrowd.resetpassword import ResetPasswordTemplate user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) request = self.make_request() passcode = ptah.pwd_tool.generate_passcode(user) template = ResetPasswordTemplate(user, request) template.passcode = passcode template.update() text = template.render() self.assertIn( "The password resetting process has been initiated. You must visit the link below to complete it:", text) self.assertIn("http://example.com/resetpassword.html/%s/" % passcode, text)
def test_resetpassword_form_change_errors(self): from ptahcrowd.provider import CrowdUser from ptahcrowd.resetpassword import ResetPasswordForm user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) passcode = ptah.pwd_tool.generate_passcode(user) request = self.make_request(subpath=(passcode, ), POST={ 'password': '******', 'confirm_password': '******', 'form.buttons.change': 'Change' }) request.environ['HTTP_HOST'] = 'example.com' form = ResetPasswordForm(None, request) form.update_form() msg = request.render_messages() self.assertIn("Please fix indicated errors.", msg)
def test_authenticate(self): from ptahcrowd.provider import \ CrowdAuthProvider, CrowdUser provider = CrowdAuthProvider() self.assertFalse( provider.authenticate( {'login': '******', 'password': '******'})) user = CrowdUser(username='******', email='*****@*****.**', password=ptah.pwd_tool.encode('12345')) ptah.get_session().add(user) ptah.get_session().flush() self.assertTrue( provider.authenticate( {'login': '******', 'password': '******'})) self.assertFalse( provider.authenticate( {'login': '******', 'password': '******'}))
def test_resetpassword(self): from ptahcrowd.resetpassword import ResetPassword from ptahcrowd.resetpassword import ResetPasswordTemplate from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) data = [1, None] def send(self): data[0] = 2 data[1] = self.passcode ResetPasswordTemplate.send = send request = self.make_request(POST={ 'login': '******', 'form.buttons.reset': 'Reset' }) form = ResetPassword(None, request) res = form() msg = request.render_messages() self.assertIn("We have started resetting your password.", msg) self.assertIsInstance(res, HTTPFound) self.assertEqual(res.headers['location'], 'http://example.com') self.assertEqual(data[0], 2) principal = ptah.pwd_tool.get_principal(data[1]) self.assertEqual(principal.username, 'username') self.assertEqual(principal.email, 'email') del ResetPasswordTemplate.send
def test_resetpassword_form_change(self): from ptahcrowd.provider import CrowdUser from ptahcrowd.resetpassword import ResetPasswordForm user = CrowdUser(username='******', email='email') CrowdUser.__type__.add(user) passcode = ptah.pwd_tool.generate_passcode(user) request = self.make_request(subpath=(passcode, ), POST={ 'password': '******', 'confirm_password': '******', 'form.buttons.change': 'Change' }) request.environ['HTTP_HOST'] = 'example.com' form = ResetPasswordForm(None, request) res = form() msg = request.render_messages() self.assertIn("You have successfully changed your password.", msg) self.assertEqual(res.headers['location'], 'http://example.com') self.assertTrue(ptah.pwd_tool.check(user.password, '123456'))
def _make_user(self): from ptahcrowd.provider import CrowdUser user = CrowdUser(username='******', email='email') return user.__type__.add(user)