spoc/tests/test_config.py

35 lines
1.4 KiB
Python
Raw Permalink Normal View History

from unittest.mock import mock_open, patch
2021-07-06 18:06:54 +02:00
from spoc import config
@patch('builtins.open', new_callable=mock_open,
read_data='{"auths": {"example.com": {"auth": "c29tZXVzZXI6c29tZXBhc3N3b3Jk"}}}')
def test_read_auth(auth_open):
config.read_auth()
2021-07-06 18:06:54 +02:00
auth_open.assert_called_once_with(config.REGISTRY_AUTH_FILE, encoding='utf-8')
assert config.REGISTRY_HOST == 'example.com'
assert config.REGISTRY_AUTH == ('someuser', 'somepassword')
assert config.REPO_FILE_URL == 'https://example.com/repository.json'
2021-07-06 18:06:54 +02:00
@patch('builtins.open', side_effect=FileNotFoundError('auth.json'))
def test_read_auth_fallback(auth_open):
config.read_auth()
2021-07-06 18:06:54 +02:00
auth_open.assert_called_once_with(config.REGISTRY_AUTH_FILE, encoding='utf-8')
assert config.REGISTRY_HOST == 'localhost'
assert config.REGISTRY_AUTH is None
2021-07-06 18:06:54 +02:00
assert config.REPO_FILE_URL == 'https://localhost/repository.json'
@patch('builtins.open', new_callable=mock_open)
def test_write_auth(auth_open):
config.write_auth('example.org', 'user', 'anotherpwd')
auth_open.assert_called_once_with(config.REGISTRY_AUTH_FILE, 'w', encoding='utf-8')
expected_data = '{"auths": {"example.org": {"auth": "dXNlcjphbm90aGVycHdk"}}}'
auth_open().write.assert_called_once_with(expected_data)
assert config.REGISTRY_HOST == 'example.org'
assert config.REGISTRY_AUTH == ('user', 'anotherpwd')
assert config.REPO_FILE_URL == 'https://example.org/repository.json'