Package cherrypy :: Package test :: Module test_sessionauthenticate
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_sessionauthenticate

 1  from cherrypy.test import test 
 2  test.prefer_parent_path() 
 3   
 4  import cherrypy 
 5   
6 -def setup_server():
7 8 def check(username, password): 9 # Dummy check_username_and_password function 10 if username != 'test' or password != 'password': 11 return u'Wrong login/password'
12 13 def augment_params(): 14 # A simple tool to add some things to request.params 15 # This is to check to make sure that session_auth can handle request 16 # params (ticket #780) 17 cherrypy.request.params["test"] = "test" 18 19 cherrypy.tools.augment_params = cherrypy.Tool('before_handler', 20 augment_params, None, priority=30) 21 22 class Test: 23 24 _cp_config = {'tools.sessions.on': True, 25 'tools.session_auth.on': True, 26 'tools.session_auth.check_username_and_password': check, 27 'tools.augment_params.on': True, 28 } 29 30 def index(self, **kwargs): 31 return "Hi %s, you are logged in" % cherrypy.request.login 32 index.exposed = True 33 34 cherrypy.tree.mount(Test()) 35 cherrypy.config.update({'environment': 'test_suite'}) 36 37 38 from cherrypy.test import helper 39 40
41 -class SessionAuthenticateTest(helper.CPWebCase):
42
43 - def testSessionAuthenticate(self):
44 # request a page and check for login form 45 self.getPage('/') 46 self.assertInBody('<form method="post" action="do_login">') 47 48 # setup credentials 49 login_body = 'username=test&password=password&from_page=/' 50 51 # attempt a login 52 self.getPage('/do_login', method='POST', body=login_body) 53 self.assertStatus((302, 303)) 54 55 # get the page now that we are logged in 56 self.getPage('/', self.cookies) 57 self.assertBody('Hi test, you are logged in') 58 59 # do a logout 60 self.getPage('/do_logout', self.cookies) 61 self.assertStatus((302, 303)) 62 63 # verify we are logged out 64 self.getPage('/', self.cookies) 65 self.assertInBody('<form method="post" action="do_login">')
66 67 68 if __name__ == "__main__": 69 setup_server() 70 helper.testmain() 71