| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2022 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 json |
| 11 |
|
| 12 |
from .....plugins.modules import vault_login |
| 13 |
from .....plugins.module_utils._hashi_vault_common import HashiVaultValueError |
| 14 |
|
| 15 |
|
| 16 |
hvac = pytest.importorskip('hvac') |
| 17 |
|
| 18 |
|
| 19 |
pytestmark = pytest.mark.usefixtures( |
| 20 |
'patch_ansible_module', |
| 21 |
'patch_authenticator', |
| 22 |
'patch_get_vault_client', |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def _connection_options(): |
| 27 |
return { |
| 28 |
'auth_method': 'token', |
| 29 |
'url': 'http://myvault', |
| 30 |
'token': 'beep-boop', |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
def _sample_options(): |
| 35 |
return {} |
| 36 |
|
| 37 |
|
| 38 |
def _combined_options(**kwargs): |
| 39 |
opt = _connection_options() |
| 40 |
opt.update(_sample_options()) |
| 41 |
opt.update(kwargs) |
| 42 |
return opt |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture |
| 46 |
def token_lookup_full_response(fixture_loader): |
| 47 |
return fixture_loader('lookup-self_with_meta.json') |
| 48 |
|
| 49 |
|
| 50 |
class TestModuleVaultLogin(): |
| 51 |
|
| 52 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 53 |
@pytest.mark.parametrize('exc', [HashiVaultValueError('throwaway msg'), NotImplementedError('throwaway msg')]) |
| 54 |
def test_vault_login_authentication_error(self, authenticator, exc, capfd): |
| 55 |
authenticator.authenticate.side_effect = exc |
| 56 |
|
| 57 |
with pytest.raises(SystemExit) as e: |
| 58 |
vault_login.main() |
| 59 |
|
| 60 |
out, err = capfd.readouterr() |
| 61 |
result = json.loads(out) |
| 62 |
|
| 63 |
assert e.value.code != 0, "result: %r" % (result,) |
| 64 |
assert result['msg'] == 'throwaway msg', "result: %r" % result |
| 65 |
|
| 66 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 67 |
@pytest.mark.parametrize('exc', [HashiVaultValueError('throwaway msg'), NotImplementedError('throwaway msg')]) |
| 68 |
def test_vault_login_auth_validation_error(self, authenticator, exc, capfd): |
| 69 |
authenticator.validate.side_effect = exc |
| 70 |
|
| 71 |
with pytest.raises(SystemExit) as e: |
| 72 |
vault_login.main() |
| 73 |
|
| 74 |
out, err = capfd.readouterr() |
| 75 |
result = json.loads(out) |
| 76 |
|
| 77 |
assert e.value.code != 0, "result: %r" % (result,) |
| 78 |
assert result['msg'] == 'throwaway msg' |
| 79 |
|
| 80 |
@pytest.mark.parametrize('opt__ansible_check_mode', [False, True]) |
| 81 |
@pytest.mark.parametrize( |
| 82 |
['opt_auth_method', 'opt_token', 'opt_role_id'], |
| 83 |
[ |
| 84 |
('token', 'beep-boop-bloop', None), |
| 85 |
('approle', None, 'not-used'), |
| 86 |
] |
| 87 |
) |
| 88 |
@pytest.mark.parametrize('patch_ansible_module', [[ |
| 89 |
_combined_options(), |
| 90 |
'_ansible_check_mode', |
| 91 |
'auth_method', |
| 92 |
'token', |
| 93 |
'role_id', |
| 94 |
]], indirect=True) |
| 95 |
def test_vault_login_return_data( |
| 96 |
self, patch_ansible_module, token_lookup_full_response, authenticator, vault_client, |
| 97 |
opt__ansible_check_mode, opt_auth_method, opt_token, opt_role_id, capfd |
| 98 |
): |
| 99 |
authenticator.authenticate.return_value = token_lookup_full_response |
| 100 |
|
| 101 |
with pytest.raises(SystemExit) as e: |
| 102 |
vault_login.main() |
| 103 |
|
| 104 |
out, err = capfd.readouterr() |
| 105 |
result = json.loads(out) |
| 106 |
|
| 107 |
assert e.value.code == 0, "result: %r" % (result,) |
| 108 |
|
| 109 |
authenticator.validate.assert_called_once() |
| 110 |
|
| 111 |
assert result['changed'] == (opt_auth_method != 'token') |
| 112 |
|
| 113 |
if opt__ansible_check_mode: |
| 114 |
authenticator.authenticate.assert_not_called() |
| 115 |
assert result['login'] == {'auth': {'client_token': None}} |
| 116 |
else: |
| 117 |
authenticator.authenticate.assert_called_once_with(vault_client) |
| 118 |
assert result['login'] == token_lookup_full_response, "expected: %r\ngot: %r" % (token_lookup_full_response, result['login']) |