|
| 1 | +"""Tests for Shaarli configuration utilities""" |
| 2 | +# pylint: disable=invalid-name,redefined-outer-name |
| 3 | +from argparse import Namespace |
| 4 | +from configparser import ConfigParser |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from shaarli_client.config import InvalidConfiguration, get_credentials |
| 9 | + |
| 10 | +SHAARLI_URL = 'http://shaar.li' |
| 11 | +SHAARLI_SECRET = 's3kr37' |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope='session') |
| 15 | +def shaarli_config(tmpdir_factory): |
| 16 | + """Generate a client configuration file""" |
| 17 | + config = ConfigParser() |
| 18 | + config['shaarli'] = { |
| 19 | + 'url': SHAARLI_URL, |
| 20 | + 'secret': SHAARLI_SECRET |
| 21 | + } |
| 22 | + config['shaarli:shaaplin'] = { |
| 23 | + 'url': SHAARLI_URL, |
| 24 | + 'secret': SHAARLI_SECRET |
| 25 | + } |
| 26 | + config['shaarli:nourl'] = { |
| 27 | + 'secret': SHAARLI_SECRET |
| 28 | + } |
| 29 | + config['shaarli:nosecret'] = { |
| 30 | + 'url': SHAARLI_URL, |
| 31 | + } |
| 32 | + |
| 33 | + config_path = tmpdir_factory.mktemp('config').join('shaarli_client.ini') |
| 34 | + |
| 35 | + with config_path.open('w') as f_config: |
| 36 | + config.write(f_config) |
| 37 | + |
| 38 | + return config_path |
| 39 | + |
| 40 | + |
| 41 | +def test_get_credentials_from_cli(): |
| 42 | + """Get authentication information as CLI parameters""" |
| 43 | + url, secret = get_credentials( |
| 44 | + Namespace(url=SHAARLI_URL, secret=SHAARLI_SECRET) |
| 45 | + ) |
| 46 | + assert url == SHAARLI_URL |
| 47 | + assert secret == SHAARLI_SECRET |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize('instance', [None, 'shaaplin']) |
| 51 | +def test_get_credentials_from_config(tmpdir, shaarli_config, instance): |
| 52 | + """Read credentials from a standard location""" |
| 53 | + with tmpdir.as_cwd(): |
| 54 | + shaarli_config.copy(tmpdir.join('shaarli_client.ini')) |
| 55 | + |
| 56 | + url, secret = get_credentials( |
| 57 | + Namespace( |
| 58 | + config=None, |
| 59 | + instance=instance, |
| 60 | + url=None, |
| 61 | + secret=None |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + assert url == SHAARLI_URL |
| 66 | + assert secret == SHAARLI_SECRET |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize('instance', [None, 'shaaplin']) |
| 70 | +def test_get_credentials_from_userconfig(shaarli_config, instance): |
| 71 | + """Read credentials from a user-provided configuration file""" |
| 72 | + url, secret = get_credentials( |
| 73 | + Namespace( |
| 74 | + config=str(shaarli_config), |
| 75 | + instance=instance, |
| 76 | + url=None, |
| 77 | + secret=None |
| 78 | + ) |
| 79 | + ) |
| 80 | + assert url == SHAARLI_URL |
| 81 | + assert secret == SHAARLI_SECRET |
| 82 | + |
| 83 | + |
| 84 | +def test_get_credentials_no_config(monkeypatch): |
| 85 | + """No configuration file found""" |
| 86 | + monkeypatch.setattr(ConfigParser, 'read', lambda x, y: []) |
| 87 | + |
| 88 | + with pytest.raises(InvalidConfiguration) as exc: |
| 89 | + get_credentials( |
| 90 | + Namespace( |
| 91 | + config=None, |
| 92 | + instance=None, |
| 93 | + url=None, |
| 94 | + secret=None |
| 95 | + ) |
| 96 | + ) |
| 97 | + assert "No configuration file found" in str(exc.value) |
| 98 | + |
| 99 | + |
| 100 | +def test_get_credentials_missing_section(shaarli_config): |
| 101 | + """The specified instance has no configuration section""" |
| 102 | + with pytest.raises(InvalidConfiguration) as exc: |
| 103 | + get_credentials( |
| 104 | + Namespace( |
| 105 | + config=str(shaarli_config), |
| 106 | + instance='nonexistent', |
| 107 | + url=None, |
| 108 | + secret=None |
| 109 | + ) |
| 110 | + ) |
| 111 | + assert "Missing entry: 'shaarli:nonexistent'" in str(exc.value) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize('attribute', ['url', 'secret']) |
| 115 | +def test_get_credentials_missing_attribute(shaarli_config, attribute): |
| 116 | + """The specified instance has no configuration section""" |
| 117 | + with pytest.raises(InvalidConfiguration) as exc: |
| 118 | + get_credentials( |
| 119 | + Namespace( |
| 120 | + config=str(shaarli_config), |
| 121 | + instance='no{}'.format(attribute), |
| 122 | + url=None, |
| 123 | + secret=None |
| 124 | + ) |
| 125 | + ) |
| 126 | + assert "Missing entry: '{}'".format(attribute) in str(exc.value) |
0 commit comments