File: test_init.py

package info (click to toggle)
python-hvac 2.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: python: 29,360; makefile: 42; sh: 14
file content (175 lines) | stat: -rw-r--r-- 5,124 bytes parent folder | download | duplicates (2)
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
import pytest

from hvac.exceptions import ParamValidationError
from hvac.api.system_backend.init import Init


@pytest.fixture
def sys_init(mock_adapter):
    return Init(mock_adapter)


INIT_SECRET_PGP_ERROR_MSG = (
    r"length of pgp_keys list argument must equal secret_shares value"
)
INIT_RECOVERY_PGP_ERROR_MSG = (
    r"length of recovery_pgp_keys list argument must equal recovery_shares value"
)
INIT_RECOVERY_SHARES_ERROR_MSG = r"value for recovery_threshold argument must be less than or equal to recovery_shares argument"
INIT_STORED_SHARES_ERROR_MSG = (
    r"value for stored_shares argument must equal secret_shares argument"
)


class TestInit:
    @pytest.mark.parametrize(
        ["secret_shares", "recovery_shares", "expected_value", "expected_warn"],
        [
            (None, None, 5, True),
            (3, None, 3, False),
            (5, 7, 5, False),
            (None, 9, None, False),
        ],
    )
    def test_initialize_default_secret_shares(
        self,
        sys_init,
        mock_warn,
        secret_shares,
        recovery_shares,
        expected_value,
        expected_warn,
    ):
        (r_args, r_kwargs) = sys_init.initialize(
            secret_shares=secret_shares,
            recovery_shares=recovery_shares,
            recovery_threshold=0,
        )
        params = r_kwargs["json"]
        assert params["secret_shares"] == expected_value

        if expected_warn:
            mock_warn.assert_called_once()
        else:
            mock_warn.assert_not_called()

    @pytest.mark.parametrize(
        ["secret_threshold", "recovery_threshold", "expected_value", "expected_warn"],
        [
            (None, None, 3, True),
            (3, None, 3, False),
            (5, 7, 5, False),
            (None, 9, None, False),
        ],
    )
    def test_initialize_default_secret_threshold(
        self,
        sys_init,
        mock_warn,
        secret_threshold,
        recovery_threshold,
        expected_value,
        expected_warn,
    ):
        (r_args, r_kwargs) = sys_init.initialize(
            secret_threshold=secret_threshold,
            recovery_threshold=recovery_threshold,
            secret_shares=0,
        )
        params = r_kwargs["json"]
        assert params["secret_threshold"] == expected_value

        if expected_warn:
            mock_warn.assert_called_once()
        else:
            mock_warn.assert_not_called()

    @pytest.mark.parametrize(
        [
            "secret_shares",
            "pgp_keys",
            "stored_shares",
            "recovery_shares",
            "recovery_pgp_keys",
            "recovery_threshold",
            "exc_msg",
        ],
        [
            (
                2,
                [1, 2, 3],
                2,
                None,
                None,
                None,
                INIT_SECRET_PGP_ERROR_MSG,
            ),
            (
                2,
                [1, 2, 3],
                3,
                None,
                None,
                None,
                INIT_SECRET_PGP_ERROR_MSG,
            ),
            (
                2,
                [1, 2],
                3,
                None,
                None,
                None,
                INIT_STORED_SHARES_ERROR_MSG,
            ),
            (2, [1, 2], 2, 3, [1, 2], None, INIT_RECOVERY_PGP_ERROR_MSG),
            (2, [1, 2], 2, 3, [1, 2], 1, INIT_RECOVERY_PGP_ERROR_MSG),
            (2, [1, 2], 2, 3, [1, 2], 9, INIT_RECOVERY_SHARES_ERROR_MSG),
        ],
    )
    def test_initialize_errors(
        self,
        sys_init,
        mock_adapter,
        secret_shares,
        pgp_keys,
        stored_shares,
        recovery_shares,
        recovery_pgp_keys,
        recovery_threshold,
        exc_msg,
    ):
        with pytest.raises(ParamValidationError, match=exc_msg):
            sys_init.initialize(
                secret_threshold=0,  # TODO(v3.0.0): remove this, only set to suppress warning
                secret_shares=secret_shares,
                pgp_keys=pgp_keys,
                stored_shares=stored_shares,
                recovery_shares=recovery_shares,
                recovery_pgp_keys=recovery_pgp_keys,
                recovery_threshold=recovery_threshold,
            )

        mock_adapter.request.assert_not_called()

    def test_initialize_value_pass(self, sys_init):
        (r_args, r_kwargs) = sys_init.initialize(
            secret_threshold=0,
            secret_shares=2,
            root_token_pgp_key="abc",
            pgp_keys=[1, 2],
            stored_shares=2,
            recovery_shares=3,
            recovery_pgp_keys=[1, 2, 3],
            recovery_threshold=3,
        )
        params = r_kwargs["json"]

        assert params["secret_threshold"] == 0
        assert params["secret_shares"] == 2
        assert params["root_token_pgp_key"] == "abc"
        assert params["pgp_keys"] == [1, 2]
        assert params["stored_shares"] == 2
        assert params["recovery_shares"] == 3
        assert params["recovery_pgp_keys"] == [1, 2, 3]
        assert params["recovery_threshold"] == 3