aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/tools/console/test_keymap.py
blob: 0d6f9e8880c1154950183502c42bbaa87a26238d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from mitmproxy.tools.console import keymap
from mitmproxy.test import taddons
from unittest import mock
import pytest


def test_binding():
    b = keymap.Binding("space", "cmd", ["options"], "")
    assert b.keyspec() == " "


def test_bind():
    with taddons.context() as tctx:
        km = keymap.Keymap(tctx.master)
        km.executor = mock.Mock()

        with pytest.raises(ValueError):
            km.add("foo", "bar", ["unsupported"])

        km.add("key", "str", ["options", "commands"])
        assert km.get("options", "key")
        assert km.get("commands", "key")
        assert not km.get("flowlist", "key")
        assert len((km.list("commands"))) == 1

        km.handle("unknown", "unknown")
        assert not km.executor.called

        km.handle("options", "key")
        assert km.executor.called

        km.add("glob", "str", ["global"])
        km.executor = mock.Mock()
        km.handle("options", "glob")
        assert km.executor.called

        assert len((km.list("global"))) == 1


def test_join():
    with taddons.context() as tctx:
        km = keymap.Keymap(tctx.master)
        km.add("key", "str", ["options"], "help1")
        km.add("key", "str", ["commands"])

        assert len(km.bindings) == 1
        assert len(km.bindings[0].contexts) == 2
        assert km.bindings[0].help == "help1"
        km.add("key", "str", ["commands"], "help2")
        assert len(km.bindings) == 1
        assert len(km.bindings[0].contexts) == 2
        assert km.bindings[0].help == "help2"

        assert km.get("commands", "key")
        km.unbind(km.bindings[0])
        assert len(km.bindings) == 0
        assert not km.get("commands", "key")


def test_remove():
    with taddons.context() as tctx:
        km = keymap.Keymap(tctx.master)
        km.add("key", "str", ["options", "commands"], "help1")
        assert len(km.bindings) == 1
        assert "options" in km.bindings[0].contexts

        km.remove("key", ["options"])
        assert len(km.bindings) == 1
        assert "options" not in km.bindings[0].contexts

        km.remove("key", ["commands"])
        assert len(km.bindings) == 0


def test_load_path(tmpdir):
    dst = str(tmpdir.join("conf"))

    kmc = keymap.KeymapConfig()
    with taddons.context(kmc) as tctx:
        km = keymap.Keymap(tctx.master)
        tctx.master.keymap = km

        with open(dst, 'wb') as f:
            f.write(b"\xff\xff\xff")
        with pytest.raises(keymap.KeyBindingError, match="expected UTF8"):
            kmc.load_path(km, dst)

        with open(dst, 'w') as f:
            f.write("'''")
        with pytest.raises(keymap.KeyBindingError):
            kmc.load_path(km, dst)

        with open(dst, 'w') as f:
            f.write(
                """
                    -   key: key1
                        ctx: [unknown]
                        cmd: >
                            foo bar
                            foo bar
                """
            )
        with pytest.raises(keymap.KeyBindingError):
            kmc.load_path(km, dst)

        with open(dst, 'w') as f:
            f.write(
                """
                    -   key: key1
                        ctx: [chooser]
                        help: one
                        cmd: >
                            foo bar
                            foo bar
                """
            )
        kmc.load_path(km, dst)
        assert(km.get("chooser", "key1"))

        km.add("key123", "str", ["flowlist", "flowview"])
        with open(dst, 'w') as f:
            f.write(
                """
                    -   key: key123
                        ctx: [options]
                        cmd: foo
                """
            )
        kmc.load_path(km, dst)
        for b in km.bindings:
            if b.key == "key123":
                assert b.contexts == ["options"]
                break


def test_parse():
    kmc = keymap.KeymapConfig()
    with taddons.context(kmc):
        assert kmc.parse("") == []
        assert kmc.parse("\n\n\n   \n") == []
        with pytest.raises(keymap.KeyBindingError, match="expected a list of keys"):
            kmc.parse("key: val")
        with pytest.raises(keymap.KeyBindingError, match="expected a list of keys"):
            kmc.parse("val")
        with pytest.raises(keymap.KeyBindingError, match="Unknown key attributes"):
            kmc.parse(
                """
                    -   key: key1
                        nonexistent: bar
                """
            )
        with pytest.raises(keymap.KeyBindingError, match="Missing required key attributes"):
            kmc.parse(
                """
                    -   help: key1
                """
            )
        with pytest.raises(keymap.KeyBindingError, match="Invalid type for cmd"):
            kmc.parse(
                """
                    -   key: key1
                        cmd: [ cmd ]
                """
            )
        with pytest.raises(keymap.KeyBindingError, match="Invalid type for ctx"):
            kmc.parse(
                """
                    -   key: key1
                        ctx: foo
                        cmd: cmd
                """
            )
        assert kmc.parse(
            """
                -   key: key1
                    ctx: [one, two]
                    help: one
                    cmd: >
                        foo bar
                        foo bar
            """
        ) == [{"key": "key1", "ctx": ["one", "two"], "help": "one", "cmd": "foo bar foo bar\n"}]