Debian Codegraph @

ansible_collections/community/hashi_vault/tests/unit/plugins/module_utils/authentication/conftest.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 Brian Scholer (@briantist)
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8
9 import pytest
10 import contextlib
11
12 try:
13 import hvac
14 except ImportError:
15 # python 2.6, which isn't supported anyway
16 pass
17
18 from ansible_collections.community.hashi_vault.tests.unit.compat import mock
19
20 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import (
21 HashiVaultAuthMethodBase,
22 HashiVaultOptionAdapter,
23 )
24
25
26 class HashiVaultAuthMethodFake(HashiVaultAuthMethodBase):
27 NAME = 'fake'
28 OPTIONS = []
29
30 def __init__(self, option_adapter, warning_callback, deprecate_callback):
31 super(HashiVaultAuthMethodFake, self).__init__(option_adapter, warning_callback, deprecate_callback)
32
33 validate = mock.MagicMock()
34 authenticate = mock.MagicMock()
35
36
37 @pytest.fixture
38 def option_dict():
39 return {'auth_method': 'fake'}
40
41
42 @pytest.fixture
43 def adapter(option_dict):
44 return HashiVaultOptionAdapter.from_dict(option_dict)
45
46
47 @pytest.fixture
48 def fake_auth_class(adapter, warner, deprecator):
49 return HashiVaultAuthMethodFake(adapter, warner, deprecator)
50
51
52 @pytest.fixture
53 def client():
54 return hvac.Client()
55
56
57 @pytest.fixture
58 def warner():
59 return mock.MagicMock()
60
61
62 @pytest.fixture
63 def deprecator():
64 return mock.MagicMock()
65
66
67 @pytest.fixture
68 def mock_import_error():
69 @contextlib.contextmanager
70 def _mock_import_error(*names):
71 import builtins
72
73 real_import = builtins.__import__
74
75 def _fake_importer(name, *args, **kwargs):
76 if name in names:
77 raise ImportError
78
79 return real_import(name, *args, **kwargs)
80
81 with mock.patch.object(builtins, '__import__', side_effect=_fake_importer):
82 yield
83
84 return _mock_import_error