def test_connect(self): msg = sshx.handle_init() self.assertEqual('success', msg['status']) msg = sshx.handle_add(NAME1, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) msg = sshx.handle_add(NAME2, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY2) self.assertEqual('success', msg['status']) with mock.patch('sshx.sshwrap.ssh') as m: sshx.handle_connect(NAME1) m.assert_called() # assert_called_with() failed, # maybe because the bug of mock. # m.assert_called_with(cfg.Account( # NAME1, user=USER1, host=HOST1, port=PORT1, password=PASSWORD1, identity=IDENTITY1)) sshx.handle_connect(NAME2) m.assert_called()
def test_del(self): msg = sshx.handle_init() self.assertEqual('success', msg['status']) msg = sshx.handle_add(NAME1, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) msg = sshx.handle_add(NAME2, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) config = cfg.read_config() self.assertEqual(2, len(config.accounts)) sshx.handle_del(NAME1) config = cfg.read_config() self.assertEqual(1, len(config.accounts)) self.assertIsNone(cfg.find_by_name(config.accounts, NAME1)) sshx.handle_del(NAME2) config = cfg.read_config() self.assertEqual(0, len(config.accounts)) self.assertIsNone(cfg.find_by_name(config.accounts, NAME2))
def test_add(self): msg = sshx.handle_init() msg = sshx.handle_add(NAME1, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) self.assertEqual(1, cfg.accounts_num()) acc = cfg.read_account(NAME1) self.assertEqual(HOST1, acc.host) self.assertEqual(PORT1, acc.port) self.assertEqual(USER1, acc.user) self.assertEqual(PASSWORD1, acc.password) self.assertEqual(IDENTITY1, acc.identity) msg = sshx.handle_add(NAME2, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) self.assertEqual(2, cfg.accounts_num())
def test_init(self): self.assertEqual(cfg.STATUS_UNINIT, cfg.check_init()) msg = sshx.handle_init(force=False) self.assertEqual('success', msg['status']) config = cfg.read_config() self.assertTrue(utils.is_str(config.phrase)) self.assertEqual(0, len(config.accounts)) self.assertEqual(cfg.STATUS_INITED, cfg.check_init()) msg = sshx.handle_init(force=False) self.assertEqual('fail', msg['status']) config = cfg.read_config() self.assertTrue(utils.is_str(config.phrase)) self.assertEqual(0, len(config.accounts)) self.assertEqual(cfg.STATUS_INITED, cfg.check_init()) phrase1 = config.phrase msg = sshx.handle_init(force=True) self.assertEqual('success', msg['status']) config = cfg.read_config() self.assertNotEqual(phrase1, config.phrase) self.assertEqual(0, len(config.accounts)) self.assertEqual(cfg.STATUS_INITED, cfg.check_init())
def test_update(self): msg = sshx.handle_init() self.assertEqual('success', msg['status']) msg = sshx.handle_add(NAME1, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) msg = sshx.handle_update(NAME1, update_fields={ 'identity': IDENTITY2, 'password': PASSWORD2, }) self.assertEqual('success', msg['status']) self.assertEqual(1, cfg.accounts_num()) account = cfg.read_account(NAME1) self.assertEqual(IDENTITY2, account.identity) self.assertEqual(PASSWORD2, account.password) msg = sshx.handle_update(NAME2, update_fields={ 'identity': IDENTITY2, }) self.assertEqual('fail', msg['status']) self.assertEqual(1, cfg.accounts_num()) msg = sshx.handle_update(NAME1, update_fields={ 'name': NAME2, }) self.assertEqual('success', msg['status']) self.assertEqual(1, cfg.accounts_num()) account = cfg.read_account(NAME1) self.assertIsNone(account) account = cfg.read_account(NAME2) self.assertEqual(IDENTITY2, account.identity)
def test_add_via(self): msg = sshx.handle_init() msg = sshx.handle_add(NAME1, HOST1, port=PORT1, via=NAME1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('fail', msg['status']) msg = sshx.handle_add(NAME1, HOST1, port=PORT1, via=NAME2, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('fail', msg['status']) msg = sshx.handle_add(NAME1, HOST1, port=PORT1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status']) msg = sshx.handle_add(NAME2, HOST1, port=PORT1, via=NAME1, user=USER1, password=PASSWORD1, identity=IDENTITY1) self.assertEqual('success', msg['status'])