1 # This file is part of CherryPy <http://www.cherrypy.org/>
2 # -*- coding: utf-8 -*-
3 # vim:ts=4:sw=4:expandtab:fileencoding=utf-8
6 from cherrypy
._cpcompat
import md5
, ntob
7 from cherrypy
.lib
import auth_basic
8 from cherrypy
.test
import helper
11 class BasicAuthTest(helper
.CPWebCase
):
16 return "This is public."
21 return "Hello %s, you've been authorized." % cherrypy
.request
.login
24 class BasicProtected2
:
26 return "Hello %s, you've been authorized." % cherrypy
.request
.login
29 userpassdict
= {'xuser' : 'xpassword'}
30 userhashdict
= {'xuser' : md5(ntob('xpassword')).hexdigest()}
32 def checkpasshash(realm
, user
, password
):
33 p
= userhashdict
.get(user
)
34 return p
and p
== md5(ntob(password
)).hexdigest() or False
36 conf
= {'/basic': {'tools.auth_basic.on': True,
37 'tools.auth_basic.realm': 'wonderland',
38 'tools.auth_basic.checkpassword': auth_basic
.checkpassword_dict(userpassdict
)},
39 '/basic2': {'tools.auth_basic.on': True,
40 'tools.auth_basic.realm': 'wonderland',
41 'tools.auth_basic.checkpassword': checkpasshash
},
45 root
.basic
= BasicProtected()
46 root
.basic2
= BasicProtected2()
47 cherrypy
.tree
.mount(root
, config
=conf
)
48 setup_server
= staticmethod(setup_server
)
52 self
.assertStatus('200 OK')
53 self
.assertHeader('Content-Type', 'text/html;charset=utf-8')
54 self
.assertBody('This is public.')
57 self
.getPage("/basic/")
58 self
.assertStatus(401)
59 self
.assertHeader('WWW-Authenticate', 'Basic realm="wonderland"')
61 self
.getPage('/basic/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
62 self
.assertStatus(401)
64 self
.getPage('/basic/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
65 self
.assertStatus('200 OK')
66 self
.assertBody("Hello xuser, you've been authorized.")
69 self
.getPage("/basic2/")
70 self
.assertStatus(401)
71 self
.assertHeader('WWW-Authenticate', 'Basic realm="wonderland"')
73 self
.getPage('/basic2/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
74 self
.assertStatus(401)
76 self
.getPage('/basic2/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
77 self
.assertStatus('200 OK')
78 self
.assertBody("Hello xuser, you've been authorized.")