2 from cherrypy
.test
import helper
5 class SessionAuthenticateTest(helper
.CPWebCase
):
9 def check(username
, password
):
10 # Dummy check_username_and_password function
11 if username
!= 'test' or password
!= 'password':
12 return 'Wrong login/password'
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)
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
35 cherrypy
.tree
.mount(Test())
36 setup_server
= staticmethod(setup_server
)
39 def testSessionAuthenticate(self
):
40 # request a page and check for login form
42 self
.assertInBody('<form method="post" action="do_login">')
45 login_body
= 'username=test&password=password&from_page=/'
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')
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">')