Debian Codegraph @

ansible_collections/community/hashi_vault/tests/unit/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 sys
10 import os
11 import json
12 import pytest
13
14 from .compat import mock
15
16 from ...plugins.module_utils._authenticator import HashiVaultAuthenticator
17
18
19 @pytest.fixture(autouse=True)
20 def skip_python():
21 if sys.version_info < (3, 8):
22 pytest.skip('Skipping on Python %s. community.hashi_vault supports Python 3.8 and higher.' % sys.version)
23
24
25 @pytest.fixture
26 def fixture_loader():
27 def _loader(name, parse='json'):
28 here = os.path.dirname(os.path.realpath(__file__))
29 fixture = os.path.join(here, 'fixtures', name)
30
31 if parse == 'path':
32 return fixture
33
34 with open(fixture, 'r') as f:
35 if parse == 'json':
36 d = json.load(f)
37 elif parse == 'lines':
38 d = f.readlines()
39 elif parse == 'raw':
40 d = f.read()
41 else:
42 raise ValueError("Unknown value '%s' for parse" % parse)
43
44 return d
45
46 return _loader
47
48
49 @pytest.fixture
50 def vault_client():
51 return mock.MagicMock()
52
53
54 @pytest.fixture
55 def authenticator():
56 authenticator = HashiVaultAuthenticator
57 authenticator.validate = mock.Mock(wraps=lambda: True)
58 authenticator.authenticate = mock.Mock(wraps=lambda client: 'throwaway')
59
60 return authenticator
61
62
63 @pytest.fixture
64 def patch_authenticator(authenticator):
65 with mock.patch('ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module.HashiVaultAuthenticator', new=authenticator):
66 yield
67
68
69 @pytest.fixture
70 def patch_get_vault_client(vault_client):
71 with mock.patch(
72 'ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common.HashiVaultHelper.get_vault_client', return_value=vault_client
73 ):
74 yield
75
76
77 @pytest.fixture
78 def requests_unparseable_response():
79 r = mock.MagicMock()
80 r.json.side_effect = json.JSONDecodeError
81 return r
82
83
84 # https://github.com/hvac/hvac/issues/797
85 @pytest.fixture
86 def empty_response(requests_unparseable_response):
87 r = requests_unparseable_response
88 r.status_code = 204
89 r._content = b""
90 r.content = b""
91 r.text = ""
92 return r