PageRenderTime 20ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/web/test_digestauth.py

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