summaryrefslogtreecommitdiff
path: root/tests/test_cli.py
blob: ba6528089cb4b83d7a2f454504892ae3b04de60d (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
import argparse
import json
import sys

import pytest

import jwt
from jwt.__main__ import build_argparser, decode_payload, encode_payload, main


class TestCli:
    def test_build_argparse(self):
        args = ["--key", "1234", "encode", "name=Vader"]
        parser = build_argparser()
        parsed_args = parser.parse_args(args)

        assert parsed_args.key == "1234"

    def test_encode_payload_raises_value_error_key_is_required(self):
        encode_args = ["encode", "name=Vader", "job=Sith"]
        parser = build_argparser()

        args = parser.parse_args(encode_args)

        with pytest.raises(ValueError) as excinfo:
            encode_payload(args)

        assert "Key is required when encoding" in str(excinfo.value)

    def test_encode_header_raises_value_error_bad_dict(self):
        encode_args = [
            "--key=secret",
            "--header=dfsfd",
            "encode",
            "name=Vader",
            "job=Sith",
        ]
        parser = build_argparser()

        args = parser.parse_args(encode_args)

        with pytest.raises(ValueError) as excinfo:
            encode_payload(args)

        assert "Error loading header:" in str(excinfo.value)

    def test_decode_payload_raises_decoded_error(self):
        decode_args = ["--key", "1234", "decode", "wrong-token"]
        parser = build_argparser()

        args = parser.parse_args(decode_args)

        with pytest.raises(jwt.DecodeError) as excinfo:
            decode_payload(args)

        assert "There was an error decoding the token" in str(excinfo.value)

    def test_decode_payload_raises_decoded_error_isatty(self, monkeypatch):
        def patched_sys_stdin_read():
            raise jwt.DecodeError()

        decode_args = ["--key", "1234", "decode", "wrong-token"]
        parser = build_argparser()

        args = parser.parse_args(decode_args)

        monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
        monkeypatch.setattr(sys.stdin, "read", patched_sys_stdin_read)

        with pytest.raises(jwt.DecodeError) as excinfo:
            decode_payload(args)

        assert "There was an error decoding the token" in str(excinfo.value)

    def test_decode_payload_terminal_tty(self, monkeypatch):
        encode_args = [
            "--key=secret-key",
            '--header={"alg":"HS256"}',
            "encode",
            "name=hello-world",
        ]
        parser = build_argparser()
        parsed_encode_args = parser.parse_args(encode_args)
        token = encode_payload(parsed_encode_args)

        decode_args = ["--key=secret-key", "decode"]
        parsed_decode_args = parser.parse_args(decode_args)

        monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
        monkeypatch.setattr(sys.stdin, "readline", lambda: token)

        actual = json.loads(decode_payload(parsed_decode_args))
        assert actual["name"] == "hello-world"

    def test_decode_payload_raises_terminal_not_a_tty(self, monkeypatch):
        decode_args = ["--key", "1234", "decode"]
        parser = build_argparser()
        args = parser.parse_args(decode_args)

        monkeypatch.setattr(sys.stdin, "isatty", lambda: False)

        with pytest.raises(IOError) as excinfo:
            decode_payload(args)
            assert "Cannot read from stdin: terminal not a TTY" in str(
                excinfo.value
            )

    @pytest.mark.parametrize(
        "key,header,name,job,exp,verify",
        [
            ("1234", "{}", "Vader", "Sith", None, None),
            ("4567", '{"typ":"test"}', "Anakin", "Jedi", "+1", None),
            ("4321", "", "Padme", "Queen", "4070926800", "true"),
        ],
    )
    def test_encode_decode(self, key, header, name, job, exp, verify):
        encode_args = [
            "--key={0}".format(key),
            "--header={0}".format(header),
            "encode",
            "name={0}".format(name),
            "job={0}".format(job),
        ]
        if exp:
            encode_args.append("exp={0}".format(exp))
        if verify:
            encode_args.append("verify={0}".format(verify))

        parser = build_argparser()
        parsed_encode_args = parser.parse_args(encode_args)
        token = encode_payload(parsed_encode_args)
        assert token is not None
        assert token != ""

        decode_args = ["--key={0}".format(key), "decode", token]
        parser = build_argparser()
        parsed_decode_args = parser.parse_args(decode_args)

        actual = json.loads(decode_payload(parsed_decode_args))
        expected = {"job": job, "name": name}
        assert actual["name"] == expected["name"]
        assert actual["job"] == expected["job"]

    @pytest.mark.parametrize(
        "key,name,job,exp,verify",
        [
            ("1234", "Vader", "Sith", None, None),
            ("4567", "Anakin", "Jedi", "+1", None),
            ("4321", "Padme", "Queen", "4070926800", "true"),
        ],
    )
    def test_main(self, monkeypatch, key, name, job, exp, verify):
        args = [
            "test_cli.py",
            "--key={0}".format(key),
            "encode",
            "name={0}".format(name),
            "job={0}".format(job),
        ]
        if exp:
            args.append("exp={0}".format(exp))
        if verify:
            args.append("verify={0}".format(verify))
        monkeypatch.setattr(sys, "argv", args)
        main()

    def test_main_throw_exception(self, monkeypatch, capsys):
        def patched_argparser_parse_args(self, args):
            raise Exception("NOOOOOOOOOOO!")

        monkeypatch.setattr(
            argparse.ArgumentParser, "parse_args", patched_argparser_parse_args
        )
        main()
        out, _ = capsys.readouterr()

        assert "NOOOOOOOOOOO!" in out