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

Source Code for Module cherrypy.test.test_sessionauthenticate

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