class RegistrationModuleTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.env = EnvironmentStub( enable=['trac.*', 'acct_mgr.api.*', 'acct_mgr.register.*']) self.env.path = tempfile.mkdtemp() self.reg_template = 'register.html' self.req.method = 'POST' self.acctmgr = AccountManager(self.env) self.check = BasicCheck(self.env) self.rmod = RegistrationModule(self.env) def test_check(self): # Default configuration: All default checks enabled. response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) # Custom configuration: Do basic username checks only. self.req.args['username'] = '******' self.req.args['email'] = '*****@*****.**' self.env.config.set('account-manager', 'register_check', 'BasicCheck') response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) # Custom configuration: No check at all, if you insist. self.env.config.set('account-manager', 'register_check', '') self.assertFalse(self.acctmgr._register_check) response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template)
def setUp(self): _BaseTestCase.setUp(self) self.env = EnvironmentStub( enable=['trac.*', 'acct_mgr.api.*', 'acct_mgr.register.*']) self.env.path = tempfile.mkdtemp() self.reg_template = 'register.html' self.req.method = 'POST' self.acctmgr = AccountManager(self.env) self.check = BasicCheck(self.env) self.rmod = RegistrationModule(self.env)
def setUp(self): _BaseTestCase.setUp(self) self.env = EnvironmentStub(enable=[ 'trac.*', 'acct_mgr.api.*', 'acct_mgr.db.*', 'acct_mgr.register.*', 'acct_mgr.pwhash.HtDigestHashMethod' ]) self.env.path = tempfile.mkdtemp() self.reg_template = 'register.html' self.req.method = 'POST' self.env.config.set('account-manager', 'password_store', 'SessionStore') self.acctmgr = AccountManager(self.env) self.check = BasicCheck(self.env) self.rmod = RegistrationModule(self.env) self.store = SessionStore(self.env)
def process_request(self, req): if req.path_info.startswith('/login') and req.authname == 'anonymous': try: referer = self._referer(req) except AttributeError: # Fallback for Trac 0.11 compatibility. referer = req.get_header('Referer') # Steer clear of requests going nowhere or loop to self. if referer is None or \ referer.startswith(str(req.abs_href()) + '/login'): referer = req.abs_href() data = { '_dgettext': dgettext, 'login_opt_list': self.login_opt_list, 'persistent_sessions': AccountManager(self.env).persistent_sessions, 'referer': referer, 'registration_enabled': RegistrationModule(self.env).enabled, 'reset_password_enabled': AccountModule(self.env).reset_password_enabled } if req.method == 'POST': self.log.debug( "LoginModule.process_request: 'user_locked' = %s" % req.args.get('user_locked')) if not req.args.get('user_locked'): # TRANSLATOR: Intentionally obfuscated login error data['login_error'] = _("Invalid username or password") else: f_user = req.args.get('username') release_time = AccountGuard(self.env).pretty_release_time( req, f_user) if not release_time is None: data['login_error'] = _( """Account locked, please try again after %(release_time)s """, release_time=release_time) else: data['login_error'] = _("Account locked") return 'login.html', data, None else: n_plural = req.args.get('failed_logins') if n_plural > 0: add_warning( req, Markup( tag.span( tag( ngettext( "Login after %(attempts)s failed attempt", "Login after %(attempts)s failed attempts", n_plural, attempts=n_plural))))) return auth.LoginModule.process_request(self, req)
class RegistrationModuleTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.env = EnvironmentStub(enable=[ 'trac.*', 'acct_mgr.api.*', 'acct_mgr.db.*', 'acct_mgr.register.*', 'acct_mgr.pwhash.HtDigestHashMethod' ]) self.env.path = tempfile.mkdtemp() self.reg_template = 'register.html' self.req.method = 'POST' self.env.config.set('account-manager', 'password_store', 'SessionStore') self.acctmgr = AccountManager(self.env) self.check = BasicCheck(self.env) self.rmod = RegistrationModule(self.env) self.store = SessionStore(self.env) def test_checks(self): # Default configuration: All default checks enabled. response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) # Custom configuration: Do basic username checks only. self.req.args['username'] = '******' self.req.args['email'] = '*****@*****.**' self.env.config.set('account-manager', 'register_check', 'BasicCheck') response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) # Custom configuration: No check at all, if you insist. self.env.config.set('account-manager', 'register_check', '') self.assertFalse(self.acctmgr._register_check) response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) def test_mandatory_email_registration(self): user = '******' passwd = 'test' # A more complete mock of a request object is required for this test. req = Mock(authname='anonymous', method='POST', args={ 'action': 'create', 'username': user, 'name': 'Tester', 'password': passwd, 'password_confirm': passwd }, chrome=dict(notices=[], warnings=[]), href=self.env.abs_href, perm=MockPerm(), redirect=lambda x: None ) # Fail to register the user. self.rmod.process_request(req) self.assertTrue('email address' in str(req.chrome['warnings'])) self.assertEqual(list(self.store.get_users()), []) def test_optional_email_registration(self): user = '******' passwd = 'test' def redirect_noop(href): """Log relevant information for checking registration result.""" #print req.chrome['notices'] return # A more complete mock of a request object is required for this test. req = Mock(authname='anonymous', method='POST', args={ 'action': 'create', 'username': user, 'name': 'Tester', 'password': passwd, 'password_confirm': passwd }, chrome=dict(notices=[], warnings=[]), href=self.env.abs_href, perm=MockPerm(), redirect=redirect_noop ) self.env.config.set('account-manager', 'verify_email', False) # Successfully register the user. # Note: This would have raised an AttributeError without graceful # request checking for 'email'. self.rmod.process_request(req) # DEVEL: Check registration success more directly. self.assertEqual(req.chrome['warnings'], []) self.assertEqual([user], list(self.store.get_users())) self.assertTrue(self.store.check_password(user, passwd))
class RegistrationModuleTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.env = EnvironmentStub(enable=[ 'trac.*', 'acct_mgr.api.*', 'acct_mgr.db.*', 'acct_mgr.register.*', 'acct_mgr.pwhash.HtDigestHashMethod' ]) self.env.path = tempfile.mkdtemp() self.reg_template = 'register.html' self.req.method = 'POST' self.env.config.set('account-manager', 'password_store', 'SessionStore') self.acctmgr = AccountManager(self.env) self.check = BasicCheck(self.env) self.rmod = RegistrationModule(self.env) self.store = SessionStore(self.env) def test_checks(self): # Default configuration: All default checks enabled. response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) # Custom configuration: Do basic username checks only. self.req.args['username'] = '******' self.req.args['email'] = '*****@*****.**' self.env.config.set('account-manager', 'register_check', 'BasicCheck') response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) # Custom configuration: No check at all, if you insist. self.env.config.set('account-manager', 'register_check', '') self.assertFalse(self.acctmgr.register_checks) response = self.rmod.process_request(self.req) self.assertEqual(response[0], self.reg_template) def test_mandatory_email_registration(self): user = '******' passwd = 'test' # A more complete mock of a request object is required for this test. req = Mock(authname='anonymous', method='POST', args={ 'action': 'create', 'username': user, 'name': 'Tester', 'password': passwd, 'password_confirm': passwd }, chrome=dict(notices=[], warnings=[]), href=self.env.abs_href, path_info='/register', perm=MockPerm(), redirect=lambda x: None) # Fail to register the user. self.rmod.process_request(req) self.assertTrue('email address' in str(req.chrome['warnings'])) self.assertEqual(list(self.store.get_users()), []) def test_optional_email_registration(self): user = '******' passwd = 'test' def redirect_noop(href): """Log relevant information for checking registration result.""" # print req.chrome['notices'] return # A more complete mock of a request object is required for this test. req = Mock(authname='anonymous', method='POST', args={ 'action': 'create', 'username': user, 'name': 'Tester', 'password': passwd, 'password_confirm': passwd }, chrome=dict(notices=[], warnings=[]), href=self.env.abs_href, path_info='/register', perm=MockPerm(), redirect=redirect_noop) self.env.config.set('account-manager', 'verify_email', False) # Successfully register the user. # Note: This would have raised an AttributeError without graceful # request checking for 'email'. self.rmod.process_request(req) # DEVEL: Check registration success more directly. self.assertEqual(req.chrome['warnings'], []) self.assertEqual([user], list(self.store.get_users())) self.assertTrue(self.store.check_password(user, passwd))