Files @ 8d3816a8fd87
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_config.py

Brett Smith
config: Add Config.rt_credentials method.

This loads settings from the same environment variables and ~/.rtrc
file as the rt CLI.

Note that it does *not* support RTCONFIG and the config file
searching, because right now that seems like more work for more
trouble to me.
"""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 pytest

from . import testutil

from conservancy_beancount import config as config_mod

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',
)

@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)
    yield
    _update_environ(revert)

def test_repository_from_environment():
    with update_environ(CONSERVANCY_REPOSITORY='/usr/local'):
        config = config_mod.Config()
        assert config.repository_path().parts == ('/', 'usr', 'local')

def test_no_repository():
    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')