"""Test Config class"""
# Copyright © 2020 Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.
import contextlib
import decimal
import itertools
import operator
import os
import re
import git
from pathlib import Path
import pytest
from . import testutil
@@ -131,24 +132,35 @@ def test_rt_credentials_from_file_and_environment_mixed(rt_environ, index, drop_
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 test_rt_credentials_idstr():
actual = {
config_mod.RTCredentials(server, user).idstr()
for server, user in itertools.product(
[None, 'https://example.org/rt'],
[None, 'example'],
)}
assert len(actual) == 4
for idstr in actual:
assert '/' not in idstr
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)
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 == (