Files
@ 96ef7c9cbbef
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_config.py - annotation
96ef7c9cbbef
6.9 KiB
text/x-python
test_config: Make update_environ more robust against failures.
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 | 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 f09f029fc4cb 71531913d5d8 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 71531913d5d8 71531913d5d8 f09f029fc4cb f09f029fc4cb 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 71531913d5d8 96ef7c9cbbef 96ef7c9cbbef 96ef7c9cbbef 96ef7c9cbbef 71531913d5d8 71531913d5d8 d8b0a56fb4c6 d8b0a56fb4c6 71531913d5d8 71531913d5d8 d8b0a56fb4c6 d8b0a56fb4c6 d8b0a56fb4c6 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a c4ce59da753a | """Test Config class"""
# Copyright © 2020 Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import contextlib
import os
import re
import pytest
from . import testutil
from conservancy_beancount import config as config_mod
RT_AUTH_METHODS = frozenset(['basic', 'gssapi', 'rt'])
RT_ENV_KEYS = (
'RTSERVER',
'RTUSER',
'RTPASSWD',
'RTAUTH',
)
RT_ENV_CREDS = (
'https://example.org/envrt',
'envuser',
'env password',
'gssapi',
)
RT_FILE_CREDS = (
'https://example.org/filert',
'fileuser',
'file password',
'basic',
)
RT_GENERIC_CREDS = config_mod.RTCredentials(
'https://example.org/genericrt',
'genericuser',
'generic password',
None,
)
@pytest.fixture
def rt_environ():
return dict(zip(RT_ENV_KEYS, RT_ENV_CREDS))
def _update_environ(updates):
for key, value in updates.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = str(value)
@contextlib.contextmanager
def update_environ(**kwargs):
revert = {key: os.environ.get(key) for key in kwargs}
_update_environ(kwargs)
try:
yield
finally:
_update_environ(revert)
def test_repository_from_environment():
config = config_mod.Config()
assert config.repository_path() == testutil.test_path('repository')
def test_no_repository():
with update_environ(CONSERVANCY_REPOSITORY=None):
config = config_mod.Config()
assert config.repository_path() is None
def test_no_rt_credentials():
with update_environ(HOME=testutil.TESTS_DIR):
config = config_mod.Config()
rt_credentials = config.rt_credentials()
assert rt_credentials.server is None
assert rt_credentials.user is None
assert rt_credentials.passwd is None
assert rt_credentials.auth == 'rt'
def test_rt_credentials_from_file():
config = config_mod.Config()
rt_credentials = config.rt_credentials()
assert rt_credentials == RT_FILE_CREDS
def test_rt_credentials_from_environment(rt_environ):
with update_environ(**rt_environ):
config = config_mod.Config()
rt_credentials = config.rt_credentials()
assert rt_credentials == RT_ENV_CREDS
@pytest.mark.parametrize('index,drop_key', enumerate(RT_ENV_KEYS))
def test_rt_credentials_from_file_and_environment_mixed(rt_environ, index, drop_key):
del rt_environ[drop_key]
with update_environ(**rt_environ):
config = config_mod.Config()
rt_credentials = config.rt_credentials()
expected = list(RT_ENV_CREDS)
expected[index] = RT_FILE_CREDS[index]
assert rt_credentials == tuple(expected)
def test_rt_credentials_from_all_sources_mixed(tmp_path):
server = 'https://example.org/mixedrt'
with (tmp_path / '.rtrc').open('w') as rtrc_file:
print('user basemix', 'passwd mixed up', file=rtrc_file, sep='\n')
with update_environ(HOME=tmp_path, RTSERVER=server, RTUSER='mixedup'):
config = config_mod.Config()
rt_credentials = config.rt_credentials()
assert rt_credentials == (server, 'mixedup', 'mixed up', 'rt')
def check_rt_client_url(credentials, client):
pattern = '^{}/?$'.format(re.escape(credentials[0].rstrip('/') + '/REST/1.0'))
assert re.match(pattern, client.url)
@pytest.mark.parametrize('authmethod', RT_AUTH_METHODS)
def test_rt_client(authmethod):
rt_credentials = RT_GENERIC_CREDS._replace(auth=authmethod)
config = config_mod.Config()
rt_client = config.rt_client(rt_credentials, testutil.RTClient)
check_rt_client_url(RT_GENERIC_CREDS, rt_client)
assert rt_client.auth_method == ('HTTPBasicAuth' if authmethod == 'basic' else 'login')
assert rt_client.last_login == (
RT_GENERIC_CREDS.user,
RT_GENERIC_CREDS.passwd,
True,
)
def test_default_rt_client(rt_environ):
with update_environ(**rt_environ):
config = config_mod.Config()
rt_client = config.rt_client(client=testutil.RTClient)
check_rt_client_url(RT_ENV_CREDS, rt_client)
assert rt_client.last_login[:-1] == RT_ENV_CREDS[1:3]
assert rt_client.last_login[-1]
@pytest.mark.parametrize('authmethod', RT_AUTH_METHODS)
def test_rt_client_login_failure(authmethod):
rt_credentials = RT_GENERIC_CREDS._replace(
auth=authmethod,
passwd='bad{}'.format(authmethod),
)
config = config_mod.Config()
assert config.rt_client(rt_credentials, testutil.RTClient) is None
def test_no_rt_client_without_server():
rt_credentials = RT_GENERIC_CREDS._replace(server=None, auth='rt')
config = config_mod.Config()
assert config.rt_client(rt_credentials, testutil.RTClient) is None
def test_rt_wrapper():
config = config_mod.Config()
rt = config.rt_wrapper(RT_GENERIC_CREDS._replace(auth='rt'), testutil.RTClient)
assert rt.exists(1)
def test_rt_wrapper_default_creds():
config = config_mod.Config()
rt = config.rt_wrapper(None, testutil.RTClient)
assert rt.rt.url.startswith(RT_FILE_CREDS[0])
def test_rt_wrapper_default_creds_from_environ(rt_environ):
with update_environ(**rt_environ):
config = config_mod.Config()
rt = config.rt_wrapper(None, testutil.RTClient)
assert rt.rt.url.startswith(RT_ENV_CREDS[0])
def test_rt_wrapper_no_creds():
with update_environ(HOME=testutil.TESTS_DIR):
config = config_mod.Config()
assert config.rt_wrapper(None, testutil.RTClient) is None
def test_rt_wrapper_bad_creds():
rt_credentials = RT_GENERIC_CREDS._replace(passwd='badpass', auth='rt')
config = config_mod.Config()
assert config.rt_wrapper(rt_credentials, testutil.RTClient) is None
def test_rt_wrapper_caches():
rt_credentials = RT_GENERIC_CREDS._replace(auth='rt')
config = config_mod.Config()
rt1 = config.rt_wrapper(rt_credentials, testutil.RTClient)
rt2 = config.rt_wrapper(rt_credentials, testutil.RTClient)
assert rt1 is rt2
def test_rt_wrapper_caches_by_creds():
config = config_mod.Config()
rt1 = config.rt_wrapper(RT_GENERIC_CREDS._replace(auth='rt'), testutil.RTClient)
rt2 = config.rt_wrapper(None, testutil.RTClient)
assert rt1 is not rt2
def test_rt_wrapper_cache_responds_to_external_credential_changes(rt_environ):
config = config_mod.Config()
rt1 = config.rt_wrapper(None, testutil.RTClient)
with update_environ(**rt_environ):
rt2 = config.rt_wrapper(None, testutil.RTClient)
assert rt1 is not rt2
|