| 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 |
|
|---|
| 10 |
if username != 'test' 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 %s, you are logged in" % cherrypy.request.login |
|---|
| 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 |
|
|---|
| 35 |
self.getPage('/') |
|---|
| 36 |
self.assertInBody('<form method="post" action="do_login">') |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
login_body = 'username=test&password=password&from_page=/' |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
self.getPage('/do_login', method='POST', body=login_body) |
|---|
| 43 |
self.assertStatus((302, 303)) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
self.getPage('/', self.cookies) |
|---|
| 47 |
self.assertBody('Hi test, you are logged in') |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
self.getPage('/do_logout', self.cookies) |
|---|
| 51 |
self.assertStatus((302, 303)) |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 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 |
|
|---|