summaryrefslogtreecommitdiff
path: root/tests/unit/test_httpapi_vyos.py
blob: 54875d3419af7bf4aa7e3eb1003e7fff213be4dc (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-
"""Unit tests for plugins/httpapi/vyos.py

Tests cover all five auth methods (key, header, bearer, mtls, oidc) and token
caching behaviour. The Ansible connection layer is mocked so no
real device is needed.
"""
from __future__ import absolute_import, division, print_function


__metaclass__ = type

import json
import time
import unittest

from io import BytesIO
from unittest.mock import MagicMock, patch

from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils.connection import ConnectionError
from ansible_collections.vyos.rest.plugins.httpapi.vyos import HttpApi


def _make_response(payload, status=200):
    """Return a (response, BytesIO) pair like connection.send() does."""
    resp = MagicMock()
    resp.status = status
    return resp, BytesIO(json.dumps(payload).encode())


def _make_plugin(auth_method="key", api_key="testkey", **extra):
    """Create a plugin instance with mocked connection and options."""
    conn = MagicMock()
    plugin = HttpApi(conn)
    options = {"api_key": api_key, "auth_method": auth_method}
    options.update(extra)

    def _get_option(opt):
        return options.get(opt)

    plugin.get_option = _get_option
    return plugin


class TestHttpApiInit(unittest.TestCase):
    def test_bearer_token_initially_none(self):
        plugin = _make_plugin()
        self.assertIsNone(plugin._bearer_token)
        self.assertEqual(plugin._bearer_token_expiry, 0)

    def test_oidc_token_initially_none(self):
        plugin = _make_plugin()
        self.assertIsNone(plugin._oidc_token)
        self.assertEqual(plugin._oidc_token_expiry, 0)

    def test_logout_clears_all_tokens(self):
        plugin = _make_plugin()
        plugin._bearer_token = "sometoken"
        plugin._bearer_token_expiry = 9999999999
        plugin._oidc_token = "oidctoken"
        plugin._oidc_token_expiry = 9999999999
        plugin.logout()
        self.assertIsNone(plugin._bearer_token)
        self.assertEqual(plugin._bearer_token_expiry, 0)
        self.assertIsNone(plugin._oidc_token)
        self.assertEqual(plugin._oidc_token_expiry, 0)


class TestGetApiKey(unittest.TestCase):
    def tearDown(self):
        import os

        os.environ.pop("VYOS_API_KEY", None)

    def test_returns_option_key(self):
        plugin = _make_plugin(api_key="mykey")
        self.assertEqual(plugin._get_api_key(), "mykey")

    def test_falls_back_to_env_var(self):
        import os

        os.environ["VYOS_API_KEY"] = "envkey"
        plugin = _make_plugin(api_key=None)
        self.assertEqual(plugin._get_api_key(), "envkey")

    def test_raises_when_no_key(self):
        plugin = _make_plugin(api_key=None)
        with self.assertRaises(ConnectionError):
            plugin._get_api_key()


class TestSendRequestKeyMethod(unittest.TestCase):
    def test_key_method_sends_form_field(self):
        plugin = _make_plugin(auth_method="key", api_key="testkey")
        plugin.connection.send.return_value = _make_response(
            {"success": True, "data": {"host-name": "vyos"}, "error": None},
        )
        result = plugin.send_request("/retrieve", op="showConfig", path=["system"])
        self.assertTrue(result["success"])
        call_kwargs = plugin.connection.send.call_args
        self.assertIn("key=testkey", call_kwargs[1]["data"])
        self.assertNotIn("X-API-Key", call_kwargs[1].get("headers", {}))

    def test_key_method_raises_on_success_false(self):
        plugin = _make_plugin(auth_method="key")
        plugin.connection.send.return_value = _make_response(
            {"success": False, "error": "Invalid key", "data": None},
        )
        with self.assertRaises(ConnectionError) as ctx:
            plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertIn("Invalid key", str(ctx.exception))


class TestSendRequestHeaderMethod(unittest.TestCase):
    def test_header_method_sends_x_api_key_header(self):
        plugin = _make_plugin(auth_method="header", api_key="testkey")
        plugin.connection.send.return_value = _make_response(
            {"success": True, "data": {}, "error": None},
        )
        plugin.send_request("/retrieve", op="showConfig", path=[])
        call_kwargs = plugin.connection.send.call_args[1]
        self.assertEqual(call_kwargs["headers"]["X-API-Key"], "testkey")

    def test_header_method_no_key_in_body(self):
        plugin = _make_plugin(auth_method="header", api_key="testkey")
        plugin.connection.send.return_value = _make_response(
            {"success": True, "data": {}, "error": None},
        )
        plugin.send_request("/retrieve", op="showConfig", path=[])
        call_kwargs = plugin.connection.send.call_args[1]
        self.assertNotIn("key=testkey", call_kwargs["data"])


class TestSendRequestBearerMethod(unittest.TestCase):
    def _token_response(self, token="jwt123", expires_in=3600):
        return _make_response(
            {
                "success": True,
                "data": {"token": token, "expires_in": expires_in},
                "error": None,
            },
        )

    def _retrieve_response(self):
        return _make_response(
            {"success": True, "data": {"host-name": "vyos"}, "error": None},
        )

    def test_bearer_fetches_token_then_sends_auth_header(self):
        plugin = _make_plugin(auth_method="bearer", api_key="testkey")
        plugin.connection.send.side_effect = [
            self._token_response(),
            self._retrieve_response(),
        ]
        result = plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertTrue(result["success"])
        first_call = plugin.connection.send.call_args_list[0]
        self.assertEqual(first_call[0][0], "/token")
        second_call = plugin.connection.send.call_args_list[1]
        self.assertEqual(
            second_call[1]["headers"]["Authorization"],
            "Bearer jwt123",
        )

    def test_bearer_caches_token(self):
        plugin = _make_plugin(auth_method="bearer", api_key="testkey")
        plugin.connection.send.side_effect = [
            self._token_response(),
            self._retrieve_response(),
            self._retrieve_response(),
        ]
        plugin.send_request("/retrieve", op="showConfig", path=[])
        plugin.send_request("/retrieve", op="showConfig", path=[])
        token_calls = [c for c in plugin.connection.send.call_args_list if c[0][0] == "/token"]
        self.assertEqual(len(token_calls), 1)

    def test_bearer_refreshes_expired_token(self):
        plugin = _make_plugin(auth_method="bearer", api_key="testkey")
        plugin._bearer_token = "oldtoken"
        plugin._bearer_token_expiry = time.time() - 100
        plugin.connection.send.side_effect = [
            self._token_response(token="newtoken"),
            self._retrieve_response(),
        ]
        plugin.send_request("/retrieve", op="showConfig", path=[])
        token_calls = [c for c in plugin.connection.send.call_args_list if c[0][0] == "/token"]
        self.assertEqual(len(token_calls), 1)
        self.assertEqual(plugin._bearer_token, "newtoken")

    def test_bearer_raises_on_token_failure(self):
        plugin = _make_plugin(auth_method="bearer", api_key="testkey")
        plugin.connection.send.return_value = _make_response(
            {"success": False, "error": "Invalid key", "data": None},
        )
        with self.assertRaises(ConnectionError) as ctx:
            plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertIn("Invalid key", str(ctx.exception))


class TestSendRequestMtlsMethod(unittest.TestCase):
    def test_mtls_sends_no_api_key(self):
        plugin = _make_plugin(auth_method="mtls", api_key=None)
        plugin.connection.send.return_value = _make_response(
            {"success": True, "data": {}, "error": None},
        )
        plugin.send_request("/retrieve", op="showConfig", path=[])
        call_kwargs = plugin.connection.send.call_args[1]
        self.assertNotIn("key=", call_kwargs["data"])
        self.assertNotIn("X-API-Key", call_kwargs.get("headers", {}))
        self.assertNotIn("Authorization", call_kwargs.get("headers", {}))

    def test_mtls_sends_no_authorization_header(self):
        plugin = _make_plugin(auth_method="mtls", api_key=None)
        plugin.connection.send.return_value = _make_response(
            {"success": True, "data": {}, "error": None},
        )
        plugin.send_request("/retrieve", op="showConfig", path=[])
        headers = plugin.connection.send.call_args[1].get("headers", {})
        self.assertNotIn("Authorization", headers)


class TestSendRequestOidcMethod(unittest.TestCase):
    def _plugin(
        self,
        token_url="http://idp/token",
        client_id="vyos-api",
        client_secret="secret",
    ):
        return _make_plugin(
            auth_method="oidc",
            api_key=None,
            oidc_token_url=token_url,
            oidc_client_id=client_id,
            oidc_client_secret=client_secret,
        )

    def _idp_response(self, token="oidctoken123", expires_in=3600):
        return json.dumps(
            {
                "access_token": token,
                "expires_in": expires_in,
                "token_type": "Bearer",
            },
        ).encode()

    def _retrieve_response(self):
        return _make_response(
            {"success": True, "data": {"host-name": "vyos"}, "error": None},
        )

    def test_oidc_fetches_token_from_idp(self):
        plugin = self._plugin()
        mock_resp = MagicMock()
        mock_resp.read.return_value = self._idp_response()
        with patch(
            "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url",
            return_value=mock_resp,
        ):
            plugin.connection.send.return_value = self._retrieve_response()
            plugin.send_request("/retrieve", op="showConfig", path=[])
        call_kwargs = plugin.connection.send.call_args[1]
        self.assertEqual(
            call_kwargs["headers"]["Authorization"],
            "Bearer oidctoken123",
        )

    def test_oidc_caches_token(self):
        plugin = self._plugin()
        mock_resp = MagicMock()
        mock_resp.read.return_value = self._idp_response()
        with patch(
            "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url",
            return_value=mock_resp,
        ) as mock_open_url:
            plugin.connection.send.return_value = self._retrieve_response()
            plugin.send_request("/retrieve", op="showConfig", path=[])
            plugin.connection.send.return_value = self._retrieve_response()
            plugin.send_request("/retrieve", op="showConfig", path=[])
            self.assertEqual(mock_open_url.call_count, 1)

    def test_oidc_refreshes_expired_token(self):
        plugin = self._plugin()
        plugin._oidc_token = "oldtoken"
        plugin._oidc_token_expiry = time.time() - 100
        mock_resp = MagicMock()
        mock_resp.read.return_value = self._idp_response(token="newtoken")
        with patch(
            "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url",
            return_value=mock_resp,
        ):
            plugin.connection.send.return_value = self._retrieve_response()
            plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertEqual(plugin._oidc_token, "newtoken")

    def test_oidc_raises_when_token_url_missing(self):
        plugin = self._plugin(token_url=None)
        with self.assertRaises(ConnectionError) as ctx:
            plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertIn("oidc_token_url", str(ctx.exception))

    def test_oidc_raises_when_idp_unreachable(self):
        plugin = self._plugin()
        with patch(
            "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url",
            side_effect=Exception("Connection refused"),
        ):
            with self.assertRaises(ConnectionError) as ctx:
                plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertIn("OIDC token fetch failed", str(ctx.exception))

    def test_oidc_raises_when_access_token_missing(self):
        plugin = self._plugin()
        mock_resp = MagicMock()
        mock_resp.read.return_value = json.dumps({"error": "invalid_client"}).encode()
        with patch(
            "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url",
            return_value=mock_resp,
        ):
            with self.assertRaises(ConnectionError) as ctx:
                plugin.send_request("/retrieve", op="showConfig", path=[])
        self.assertIn("access_token", str(ctx.exception))


class TestHandleHttpError(unittest.TestCase):
    def test_401_raises_connection_failure(self):
        plugin = _make_plugin()
        exc = MagicMock()
        exc.code = 401
        with self.assertRaises(AnsibleConnectionFailure):
            plugin.handle_httperror(exc)

    def test_other_errors_returned(self):
        plugin = _make_plugin()
        exc = MagicMock()
        exc.code = 500
        result = plugin.handle_httperror(exc)
        self.assertEqual(result, exc)


if __name__ == "__main__":
    unittest.main()