2018-04-23 08:46:49 +00:00
|
|
|
import os.path
|
|
|
|
import unittest
|
2018-04-23 09:03:58 +00:00
|
|
|
import paramiko
|
2018-04-23 08:46:49 +00:00
|
|
|
import tornado.options as options
|
|
|
|
|
2018-08-24 07:11:21 +00:00
|
|
|
from tests.utils import make_tests_data_path
|
2018-08-18 09:39:50 +00:00
|
|
|
from webssh.policy import load_host_keys
|
2018-04-26 14:38:03 +00:00
|
|
|
from webssh.settings import (
|
2018-05-06 10:32:31 +00:00
|
|
|
get_host_keys_settings, get_policy_setting, base_dir, print_version
|
2018-04-26 14:38:03 +00:00
|
|
|
)
|
2018-05-06 10:32:31 +00:00
|
|
|
from webssh._version import __version__
|
2018-04-23 08:46:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSettings(unittest.TestCase):
|
|
|
|
|
2018-05-06 10:32:31 +00:00
|
|
|
def test_print_version(self):
|
|
|
|
self.assertNotEqual(print_version(False), 2, msg=__version__)
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
self.assertEqual(print_version(True), 2, msg=__version__)
|
|
|
|
|
2018-04-23 08:46:49 +00:00
|
|
|
def test_get_host_keys_settings(self):
|
|
|
|
options.hostFile = ''
|
|
|
|
options.sysHostFile = ''
|
|
|
|
dic = get_host_keys_settings(options)
|
|
|
|
|
|
|
|
filename = os.path.join(base_dir, 'known_hosts')
|
|
|
|
self.assertEqual(dic['host_keys'], load_host_keys(filename))
|
|
|
|
self.assertEqual(dic['host_keys_filename'], filename)
|
|
|
|
self.assertEqual(
|
|
|
|
dic['system_host_keys'],
|
|
|
|
load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
|
|
|
|
)
|
|
|
|
|
2018-08-24 07:11:21 +00:00
|
|
|
options.hostFile = make_tests_data_path('known_hosts_example')
|
|
|
|
options.sysHostFile = make_tests_data_path('known_hosts_example2')
|
2018-04-23 08:46:49 +00:00
|
|
|
dic2 = get_host_keys_settings(options)
|
|
|
|
self.assertEqual(dic2['host_keys'], load_host_keys(options.hostFile))
|
|
|
|
self.assertEqual(dic2['host_keys_filename'], options.hostFile)
|
|
|
|
self.assertEqual(dic2['system_host_keys'],
|
|
|
|
load_host_keys(options.sysHostFile))
|
2018-04-23 09:03:58 +00:00
|
|
|
|
|
|
|
def test_get_policy_setting(self):
|
|
|
|
options.policy = 'warning'
|
|
|
|
options.hostFile = ''
|
|
|
|
options.sysHostFile = ''
|
|
|
|
settings = get_host_keys_settings(options)
|
|
|
|
instance = get_policy_setting(options, settings)
|
|
|
|
self.assertIsInstance(instance, paramiko.client.WarningPolicy)
|
|
|
|
|
|
|
|
options.policy = 'autoadd'
|
|
|
|
options.hostFile = ''
|
|
|
|
options.sysHostFile = ''
|
|
|
|
settings = get_host_keys_settings(options)
|
|
|
|
instance = get_policy_setting(options, settings)
|
|
|
|
self.assertIsInstance(instance, paramiko.client.AutoAddPolicy)
|
2018-04-24 01:06:27 +00:00
|
|
|
os.unlink(settings['host_keys_filename'])
|
2018-04-23 09:03:58 +00:00
|
|
|
|
|
|
|
options.policy = 'reject'
|
|
|
|
options.hostFile = ''
|
|
|
|
options.sysHostFile = ''
|
|
|
|
settings = get_host_keys_settings(options)
|
|
|
|
try:
|
|
|
|
instance = get_policy_setting(options, settings)
|
|
|
|
except ValueError:
|
2018-04-23 09:11:07 +00:00
|
|
|
self.assertFalse(
|
|
|
|
settings['host_keys'] and settings['system_host_keys']
|
|
|
|
)
|
2018-04-23 09:03:58 +00:00
|
|
|
else:
|
|
|
|
self.assertIsInstance(instance, paramiko.client.RejectPolicy)
|