2021-07-11 00:58:28 +02:00
|
|
|
from unittest.mock import mock_open, patch
|
2021-07-06 18:06:54 +02:00
|
|
|
|
|
|
|
from spoc import config
|
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
@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
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
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'
|
2021-07-06 18:06:54 +02:00
|
|
|
|
2021-07-11 00:58:28 +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
|
|
|
|
2021-07-11 00:58:28 +02:00
|
|
|
auth_open.assert_called_once_with(config.REGISTRY_AUTH_FILE)
|
|
|
|
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'
|
2021-07-11 00:58:28 +02:00
|
|
|
|
|
|
|
@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'
|