Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-3.0.0/cherrypy/test/test_sessionauthenticate.py

Revision 1324 (checked in by fumanchu, 2 years ago)

More SessionAuth? cleanups. Replaced 'login' with 'username' whenever appropriate ('login' should be used as the verb only ('log in'), not as a synonym for 'username'). Also made symmetric do_* and on_* methods. The new on_check method replaces the load_user_by_username method--this facilitates a user-supplied fix for #224 (store the user details in the session) by passing the same loaduserbyusername callback to tools.session_auth.on_login instead of tools.session_auth.on_check.

  • Property svn:eol-style set to native
Line 
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 != 'login' or password != 'password':
11             return u'Wrong login/password'
12    
13     class Test:
14        
15         _cp_config = {'tools.sessions.on': True,
16                       'tools.session_auth.on': True,
17                       'tools.session_auth.check_username_and_password': check,
18                       }
19        
20         def index(self):
21             return "Hi, you are logged in"
22         index.exposed = True
23    
24     cherrypy.tree.mount(Test())
25     cherrypy.config.update({'environment': 'test_suite'})
26
27
28 from cherrypy.test import helper
29
30
31 class SessionAuthenticateTest(helper.CPWebCase):
32    
33     def testSessionAuthenticate(self):
34         # request a page and check for login form
35         self.getPage('/')
36         self.assertInBody('<form method="post" action="do_login">')
37        
38         # setup credentials
39         login_body = 'username=login&password=password&from_page=/'
40        
41         # attempt a login
42         self.getPage('/do_login', method='POST', body=login_body)
43         self.assertStatus((302, 303))
44        
45         # get the page now that we are logged in
46         self.getPage('/', self.cookies)
47         self.assertBody('Hi, you are logged in')
48        
49         # do a logout
50         self.getPage('/do_logout', self.cookies)
51         self.assertStatus((302, 303))
52        
53         # verify we are logged out
54         self.getPage('/', self.cookies)
55         self.assertInBody('<form method="post" action="do_login">')
56
57
58 if __name__ == "__main__":
59     setup_server()
60     helper.testmain()
61
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets