Bundled cherrypy.
[smonitor.git] / monitor / cherrypy / test / test_sessionauthenticate.py
blobab1fe51ebbcd4a1dc4363d8ff7260094c438deca
1 import cherrypy
2 from cherrypy.test import helper
5 class SessionAuthenticateTest(helper.CPWebCase):
7 def setup_server():
9 def check(username, password):
10 # Dummy check_username_and_password function
11 if username != 'test' or password != 'password':
12 return 'Wrong login/password'
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"
20 cherrypy.tools.augment_params = cherrypy.Tool('before_handler',
21 augment_params, None, priority=30)
23 class Test:
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,
31 def index(self, **kwargs):
32 return "Hi %s, you are logged in" % cherrypy.request.login
33 index.exposed = True
35 cherrypy.tree.mount(Test())
36 setup_server = staticmethod(setup_server)
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">')
44 # setup credentials
45 login_body = 'username=test&password=password&from_page=/'
47 # attempt a login
48 self.getPage('/do_login', method='POST', body=login_body)
49 self.assertStatus((302, 303))
51 # get the page now that we are logged in
52 self.getPage('/', self.cookies)
53 self.assertBody('Hi test, you are logged in')
55 # do a logout
56 self.getPage('/do_logout', self.cookies, method='POST')
57 self.assertStatus((302, 303))
59 # verify we are logged out
60 self.getPage('/', self.cookies)
61 self.assertInBody('<form method="post" action="do_login">')