aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_authentication.py
blob: f7a5ecd34fd97d491338ca07d0e134f739a5c557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import binascii
from libmproxy import authentication
from netlib import odict
import tutils


class TestNullProxyAuth:
    def test_simple(self):
        na = authentication.NullProxyAuth(authentication.PermissivePasswordManager())
        assert not na.auth_challenge_headers()
        assert na.authenticate("foo")
        na.clean({})


class TestBasicProxyAuth:
    def test_simple(self):
        ba = authentication.BasicProxyAuth(authentication.PermissivePasswordManager(), "test")
        h = odict.ODictCaseless()
        assert ba.auth_challenge_headers()
        assert not ba.authenticate(h)

    def test_parse_auth_value(self):
        ba = authentication.BasicProxyAuth(authentication.PermissivePasswordManager(), "test")
        vals = ("basic", "foo", "bar")
        assert ba.parse_auth_value(ba.unparse_auth_value(*vals)) == vals
        tutils.raises(ValueError, ba.parse_auth_value, "")
        tutils.raises(ValueError, ba.parse_auth_value, "foo bar")

        v = "basic " + binascii.b2a_base64("foo")
        tutils.raises(ValueError, ba.parse_auth_value, v)

    def test_authenticate_clean(self):
        ba = authentication.BasicProxyAuth(authentication.PermissivePasswordManager(), "test")

        hdrs = odict.ODictCaseless()
        vals = ("basic", "foo", "bar")
        hdrs[ba.AUTH_HEADER] = [ba.unparse_auth_value(*vals)]
        assert ba.authenticate(hdrs)

        ba.clean(hdrs)
        assert not ba.AUTH_HEADER in hdrs


        hdrs[ba.AUTH_HEADER] = [""]
        assert not ba.authenticate(hdrs)

        hdrs[ba.AUTH_HEADER] = ["foo"]
        assert not ba.authenticate(hdrs)

        vals = ("foo", "foo", "bar")
        hdrs[ba.AUTH_HEADER] = [ba.unparse_auth_value(*vals)]
        assert not ba.authenticate(hdrs)

        ba = authentication.BasicProxyAuth(authentication.PasswordManager(), "test")
        vals = ("basic", "foo", "bar")
        hdrs[ba.AUTH_HEADER] = [ba.unparse_auth_value(*vals)]
        assert not ba.authenticate(hdrs)