spoc/tests/test_config.py

35 lines
1.4 KiB
Python

from unittest.mock import mock_open, patch
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()
auth_open.assert_called_once_with(config.REGISTRY_AUTH_FILE)
assert config.REGISTRY_HOST == 'example.com'
assert config.REGISTRY_AUTH == ('someuser', 'somepassword')
assert config.REPO_FILE_URL == 'https://example.com/repository.json'
@patch('builtins.open', side_effect=FileNotFoundError('auth.json'))
def test_read_auth_fallback(auth_open):
config.read_auth()
auth_open.assert_called_once_with(config.REGISTRY_AUTH_FILE)
assert config.REGISTRY_HOST == 'localhost'
assert config.REGISTRY_AUTH is None
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')
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'