PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/web/test_basicauth.py

https://bitbucket.org/prologic/circuits/
Python | 38 lines | 28 code | 10 blank | 0 comment | 1 complexity | fdc545451e685a4cd385c21bcf9f6994 MD5 | raw file
 1from circuits.web import Controller
 2from circuits.web.tools import check_auth, basic_auth
 3
 4from .helpers import HTTPError, HTTPBasicAuthHandler
 5from .helpers import urlopen, build_opener, install_opener
 6
 7
 8class Root(Controller):
 9
10    def index(self):
11        realm = "Test"
12        users = {"admin": "admin"}
13        encrypt = str
14
15        if check_auth(self.request, self.response, realm, users, encrypt):
16            return "Hello World!"
17
18        return basic_auth(self.request, self.response, realm, users, encrypt)
19
20
21def test(webapp):
22    try:
23        f = urlopen(webapp.server.http.base)
24    except HTTPError as e:
25        assert e.code == 401
26        assert e.msg == "Unauthorized"
27    else:
28        assert False
29
30    handler = HTTPBasicAuthHandler()
31    handler.add_password("Test", webapp.server.http.base, "admin", "admin")
32    opener = build_opener(handler)
33    install_opener(opener)
34
35    f = urlopen(webapp.server.http.base)
36    s = f.read()
37    assert s == b"Hello World!"
38    install_opener(None)