File: test_config.py

package info (click to toggle)
python-internetarchive 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,000 kB
  • sloc: python: 7,445; xml: 180; makefile: 180
file content (438 lines) | stat: -rw-r--r-- 14,898 bytes parent folder | download | duplicates (2)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import contextlib
import os
import tempfile
from unittest import mock

import pytest
import requests.adapters
import responses

import internetarchive.config
import internetarchive.session
from internetarchive.exceptions import AuthenticationError


@responses.activate
def test_get_auth_config():
    test_body = """{
        "success": true,
        "values": {
            "cookies": {
                "logged-in-sig": "foo-sig",
                "logged-in-user": "foo%40example.com"
            },
            "email": "foo@example.com",
            "itemname": "@jakej",
            "s3": {
                "access": "Ac3ssK3y",
                "secret": "S3cretK3y"
            },
            "screenname":"jakej"
        },
        "version": 1}"""
    responses.add(responses.POST, 'https://archive.org/services/xauthn/',
                  body=test_body)
    r = internetarchive.config.get_auth_config('test@example.com', 'password1')
    assert r['s3']['access'] == 'Ac3ssK3y'
    assert r['s3']['secret'] == 'S3cretK3y'
    assert r['cookies']['logged-in-user'] == 'foo%40example.com'
    assert r['cookies']['logged-in-sig'] == 'foo-sig'


@responses.activate
def test_get_auth_config_auth_fail():
    # No logged-in-sig cookie set raises AuthenticationError.
    responses.add(responses.POST, 'https://archive.org/services/xauthn/',
                  body='{"error": "failed"}')
    try:
        r = internetarchive.config.get_auth_config('test@example.com', 'password1')
    except AuthenticationError as exc:
        return
        assert str(exc) == ('Authentication failed. Please check your credentials '
                            'and try again.')


def test_get_config():
    config = internetarchive.config.get_config()
    assert isinstance(config, dict)


def test_get_config_with_config_file(tmpdir):
    test_conf = ('[s3]\n'
                 'access = test-access\n'
                 'secret = test-secret\n'
                 '[cookies]\n'
                 'logged-in-sig = test-sig\n'
                 'logged-in-user = test@archive.org\n')

    tmpdir.chdir()
    with open('ia_test.ini', 'w') as fp:
        fp.write(test_conf)

    config = internetarchive.config.get_config(config_file='ia_test.ini',
                                               config={'custom': 'test'})
    assert config['cookies']['logged-in-sig'] == 'test-sig'
    assert config['cookies']['logged-in-user'] == 'test@archive.org'
    assert config['s3']['access'] == 'test-access'
    assert config['s3']['secret'] == 'test-secret'
    assert config['custom'] == 'test'


def test_get_config_no_config_file():
    os.environ['HOME'] = ''
    config = internetarchive.config.get_config()
    assert config == {}


def test_get_config_with_config():
    test_conf = {
        's3': {
            'access': 'custom-access',
            'secret': 'custom-secret',
        },
        'cookies': {
            'logged-in-user': 'test@archive.org',
            'logged-in-sig': 'test-sig',
        },
    }

    os.environ['HOME'] = ''
    config = internetarchive.config.get_config(config=test_conf)
    assert config['cookies']['logged-in-sig'] == 'test-sig'
    assert config['cookies']['logged-in-user'] == 'test@archive.org'
    assert config['s3']['access'] == 'custom-access'
    assert config['s3']['secret'] == 'custom-secret'


def test_get_config_home_not_set():
    os.environ['HOME'] = '/none'
    config = internetarchive.config.get_config()
    assert isinstance(config, dict)


def test_get_config_home_not_set_with_config():
    test_conf = {
        's3': {
            'access': 'no-home-access',
            'secret': 'no-home-secret',
        },
    }
    os.environ['HOME'] = '/none'
    config = internetarchive.config.get_config(config=test_conf)
    assert isinstance(config, dict)
    assert config['s3']['access'] == 'no-home-access'
    assert config['s3']['secret'] == 'no-home-secret'


def test_get_config_config_and_config_file(tmpdir):
    test_conf = ('[s3]\n'
                 'access = test-access\n'
                 'secret = test-secret\n'
                 '[cookies]\n'
                 'logged-in-sig = test-sig\n'
                 'logged-in-user = test@archive.org\n')

    tmpdir.chdir()

    with open('ia_test.ini', 'w') as fp:
        fp.write(test_conf)

    test_conf = {
        's3': {
            'access': 'custom-access',
            'secret': 'custom-secret',
        },
        'cookies': {
            'logged-in-user': 'test@archive.org',
            'logged-in-sig': 'test-sig',
        },
    }
    del test_conf['s3']['access']
    config = internetarchive.config.get_config(config_file='ia_test.ini',
                                               config=test_conf)
    assert config['cookies']['logged-in-sig'] == 'test-sig'
    assert config['cookies']['logged-in-user'] == 'test@archive.org'
    assert config['s3']['access'] == 'test-access'
    assert config['s3']['secret'] == 'custom-secret'


@contextlib.contextmanager
def _environ(**kwargs):
    old_values = {k: os.environ.get(k) for k in kwargs}
    try:
        for k, v in kwargs.items():
            if v is not None:
                os.environ[k] = v
            else:
                del os.environ[k]
        yield
    finally:
        for k, v in old_values.items():
            if v is not None:
                os.environ[k] = v
            else:
                del os.environ[k]


def _test_parse_config_file(
        expected_result,
        config_file_contents='',
        config_file_paths=None,
        home=None,
        xdg_config_home=None,
        config_file_param=None):
    # expected_result: (config_file_path, is_xdg); config isn't compared.
    # config_file_contents: str
    # config_file_paths: list of filenames to write config_file_contents to
    # home: str, override HOME env var; default: path of the temporary dir
    # xdg_config_home: str, set XDG_CONFIG_HOME
    # config_file_param: str, filename to pass to parse_config_file
    # All paths starting with '$TMPTESTDIR/' get evaluated relative to the temp dir.

    if not config_file_paths:
        config_file_paths = []

    with tempfile.TemporaryDirectory() as tmp_test_dir:
        def _replace_path(s):
            if s and s.startswith('$TMPTESTDIR/'):
                return os.path.join(tmp_test_dir, s.split('/', 1)[1])
            return s

        expected_result = (_replace_path(expected_result[0]), expected_result[1])
        config_file_paths = [_replace_path(x) for x in config_file_paths]
        home = _replace_path(home)
        xdg_config_home = _replace_path(xdg_config_home)
        config_file_param = _replace_path(config_file_param)

        for p in config_file_paths:
            os.makedirs(os.path.dirname(p), exist_ok=True)
            with open(p, 'w') as fp:
                fp.write(config_file_contents)

        if home is None:
            home = tmp_test_dir
        env = {'HOME': home}
        if xdg_config_home is not None:
            env['XDG_CONFIG_HOME'] = xdg_config_home
        with _environ(**env):
            config_file_path, is_xdg, config = internetarchive.config.parse_config_file(
                config_file=config_file_param)

    assert (config_file_path, is_xdg) == expected_result[0:2]


def test_parse_config_file_blank():
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.config/internetarchive/ia.ini', True)
    )


def test_parse_config_file_existing_config_ia():
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.config/ia.ini', False),
        config_file_paths=['$TMPTESTDIR/.config/ia.ini'],
    )


def test_parse_config_file_existing_dotia():
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.ia', False),
        config_file_paths=['$TMPTESTDIR/.ia'],
    )


def test_parse_config_file_existing_config_ia_and_dotia():
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.config/ia.ini', False),
        config_file_paths=['$TMPTESTDIR/.config/ia.ini', '$TMPTESTDIR/.ia'],
    )


def test_parse_config_file_existing_all():
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.config/internetarchive/ia.ini', True),
        config_file_paths=[
            '$TMPTESTDIR/.config/internetarchive/ia.ini',
            '$TMPTESTDIR/.config/ia.ini',
            '$TMPTESTDIR/.ia'
        ],
    )


def test_parse_config_file_custom_xdg():
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.xdg/internetarchive/ia.ini', True),
        xdg_config_home='$TMPTESTDIR/.xdg',
    )


def test_parse_config_file_empty_xdg():
    # Empty XDG_CONFIG_HOME should be treated as if not set, i.e. default
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.config/internetarchive/ia.ini', True),
        xdg_config_home='',
    )


def test_parse_config_file_relative_xdg():
    # Relative XDG_CONFIG_HOME is invalid and should be ignored, i.e. default ~/.config used instead
    _test_parse_config_file(
        expected_result=('$TMPTESTDIR/.config/internetarchive/ia.ini', True),
        xdg_config_home='relative/.config',
    )


def test_parse_config_file_direct_path_overrides_existing_files():
    _test_parse_config_file(
        expected_result=('/path/to/ia.ini', False),
        config_file_paths=[
            '$TMPTESTDIR/.config/internetarchive/ia.ini',
            '$TMPTESTDIR/.config/ia.ini',
            '$TMPTESTDIR/.ia'
        ],
        config_file_param='/path/to/ia.ini',
    )


def test_parse_config_file_with_environment_variable():
    with _environ(IA_CONFIG_FILE='/inexistent.ia.ini'):
        _test_parse_config_file(
            expected_result=('/inexistent.ia.ini', False),
        )


def test_parse_config_file_with_environment_variable_and_parameter():
    with _environ(IA_CONFIG_FILE='/inexistent.ia.ini'):
        _test_parse_config_file(
            expected_result=('/inexistent.other.ia.ini', False),
            config_file_param='/inexistent.other.ia.ini',
        )


def _test_write_config_file(
        expected_config_file,
        expected_modes,
        dirs=None,
        create_expected_file=False,
        config_file_param=None):
    # expected_config_file: str
    # expected_modes: list of (path, mode) tuples
    # dirs: list of str, directories to create before running write_config_file
    # create_expected_file: bool, create the expected_config_file if True
    # config_file_param: str, filename to pass to write_config_file
    # Both dirs and the config file are created with mode 777 (minus umask).
    # All paths are evaluated relative to a temporary HOME.
    # Mode comparison accounts for the umask; expected_modes does not need to care about it.

    with tempfile.TemporaryDirectory() as temp_home_dir:
        expected_config_file = os.path.join(temp_home_dir, expected_config_file)
        if dirs:
            dirs = [os.path.join(temp_home_dir, d) for d in dirs]
        expected_modes = [(os.path.join(temp_home_dir, p), m) for p, m in expected_modes]
        if config_file_param:
            config_file_param = os.path.join(temp_home_dir, config_file_param)
        with _environ(HOME=temp_home_dir):
            # Need to account for the umask in the expected_modes comparisons.
            # The umask can't just be retrieved, so set and then restore previous value.
            umask = os.umask(0)
            os.umask(umask)
            if dirs:
                for d in dirs:
                    os.mkdir(d)
            if create_expected_file:
                with open(expected_config_file, 'w') as fp:
                    os.chmod(expected_config_file, 0o777)
            config_file = internetarchive.config.write_config_file({}, config_file_param)
            assert config_file == expected_config_file
            assert os.path.isfile(config_file)
            for path, mode in expected_modes:
                actual_mode = os.stat(path).st_mode & 0o777
                assert actual_mode == mode & ~umask


def test_write_config_file_blank():
    """Test that a blank HOME is populated with expected dirs and modes."""
    _test_write_config_file(
        expected_config_file='.config/internetarchive/ia.ini',
        expected_modes=[
            ('.config/internetarchive/ia.ini', 0o600),
            ('.config/internetarchive', 0o700),
            ('.config', 0o700),
        ],
    )


def test_write_config_file_config_existing():
    """Test that .config's permissions remain but ia gets created correctly."""
    _test_write_config_file(
        dirs=['.config'],
        expected_config_file='.config/internetarchive/ia.ini',
        expected_modes=[
            ('.config/internetarchive/ia.ini', 0o600),
            ('.config/internetarchive', 0o700),
            ('.config', 0o777),
        ],
    )


def test_write_config_file_config_internetarchive_existing():
    """Test that directory permissions are left as is"""
    _test_write_config_file(
        dirs=['.config', '.config/internetarchive'],
        expected_config_file='.config/internetarchive/ia.ini',
        expected_modes=[
            ('.config/internetarchive/ia.ini', 0o600),
            ('.config/internetarchive', 0o777),
            ('.config', 0o777),
        ],
    )


def test_write_config_file_existing_file():
    """Test that the permissions of the file are forced to 600"""
    _test_write_config_file(
        dirs=['.config', '.config/internetarchive'],
        expected_config_file='.config/internetarchive/ia.ini',
        create_expected_file=True,
        expected_modes=[
            ('.config/internetarchive/ia.ini', 0o600),
            ('.config/internetarchive', 0o777),
            ('.config', 0o777),
        ],
    )


def test_write_config_file_existing_other_file():
    """Test that the permissions of the file are forced to 600 even outside XDG"""
    _test_write_config_file(
        dirs=['foo'],
        expected_config_file='foo/ia.ini',
        create_expected_file=True,
        config_file_param='foo/ia.ini',
        expected_modes=[
            ('foo/ia.ini', 0o600),
            ('foo', 0o777),
        ],
    )


def test_write_config_file_custom_path_existing():
    """Test the creation of a config file at a custom location"""
    _test_write_config_file(
        dirs=['foo'],
        expected_config_file='foo/ia.ini',
        config_file_param='foo/ia.ini',
        expected_modes=[
            ('foo/ia.ini', 0o600),
            ('foo', 0o777),
        ],
    )


def test_write_config_file_custom_path_not_existing():
    """Ensure that an exception is thrown if the custom path dir doesn't exist"""
    with tempfile.TemporaryDirectory() as temp_home_dir:
        with _environ(HOME=temp_home_dir):
            config_file = os.path.join(temp_home_dir, 'foo/ia.ini')
            with pytest.raises(IOError):
                internetarchive.config.write_config_file({}, config_file)