def test_wallet_open(self): with patch('neo.Prompt.PromptData.PromptData.Prompt'): with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[self.wallet_1_pass()]): if self._wallet1 is None: shutil.copyfile(self.wallet_1_path(), self.wallet_1_dest()) # test wallet open successful args = ['open', self.wallet_1_dest()] res = CommandWallet().execute(args) self.assertEqual(type(res), UserWallet) # test wallet open with no path; this will also close the open wallet args = ['open'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet open with bad path args = ['open', 'badpath'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet open unsuccessful with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=["testpassword"]): with patch('neo.Implementations.Wallets.peewee.UserWallet.UserWallet.Open', side_effect=[Exception('test exception')]): args = ['open', 'fixtures/testwallet.db3'] res = CommandWallet().execute(args) self.assertFalse(res)
def test_token_history(self): with self.OpenWallet1(): # test with no parameters with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'history'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with unknown token with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'history', 'bad_token'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("does not represent a known NEP5 token", mock_print.getvalue()) # test with known token (has sent event) with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'history', 'NXT4'] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn( "Sent 1000.00000000 NXT4 to AGYaEi3W6ndHPUmW7T12FFfsbQ6DWymkEm", mock_print.getvalue()) with self.OpenWallet2(): with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'history', 'NXT4'] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn( "Received 1000.00000000 NXT4 from AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3", mock_print.getvalue())
def test_wallet_alias(self): # test wallet alias with no wallet open args = [ 'address', 'alias', 'AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3', 'mine' ] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() # test wallet alias with no argument args = ['address', 'alias'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet alias with 1 argument args = ['address', 'alias', 'AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet alias successful self.assertNotIn('mine', [n.Title for n in PromptData.Wallet.NamedAddr]) args = [ 'address', 'alias', 'AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3', 'mine' ] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn('mine', [n.Title for n in PromptData.Wallet.NamedAddr])
def test_wallet_close(self): with patch('neo.Prompt.PromptData.PromptData.Prompt'): # test wallet close with no wallet args = ['close'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet close with open wallet with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[self.wallet_1_pass()]): if self._wallet1 is None: shutil.copyfile(self.wallet_1_path(), self.wallet_1_dest()) args = ['open', self.wallet_1_dest()] res = CommandWallet().execute(args) self.assertEqual(type(res), UserWallet) # now close the open wallet manually args = ['close'] res = CommandWallet().execute(args) self.assertTrue(res)
def test_wallet_create_address(self): # test wallet create address with no wallet open args = ['address', 'create', 1] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() # test wallet create address with no argument args = ['address', 'create'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet create address with negative number args = ['address', 'create', -1] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet create address successful args = ['address', 'create', 1] res = CommandWallet().execute(args) self.assertTrue(res) self.assertEqual(type(res), UserWallet) self.assertEqual(len(res.Addresses), 2) # Has one address when created. args = ['address', 'create', 7] res = CommandWallet().execute(args) self.assertTrue(res) self.assertEqual(type(res), UserWallet) self.assertEqual(len(res.Addresses), 9)
def test_wallet_delete_address(self): # test wallet delete address with no wallet open args = ['address', 'delete'] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() # test wallet delete address with no argument args = ['address', 'delete'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet delete address with invalid address args = ['address', 'delete', '1234'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet delete address with unknown address args = ['address', 'delete', self.watch_addr_str] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet delete successful self.assertTrue(len(PromptData.Wallet.Addresses), 1) args = ['address', 'delete', PromptData.Wallet.Addresses[0]] res = CommandWallet().execute(args) self.assertTrue(res) self.assertEqual(type(res), bool) self.assertEqual(len(PromptData.Wallet.Addresses), 0)
def test_wallet_export_baseclass(self): self.OpenWallet1() # test with no argument with patch('sys.stdout', new=StringIO()) as mock_print: args = ['export'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("run `export help` to see supported queries", mock_print.getvalue()) # test with an invalid action with patch('sys.stdout', new=StringIO()) as mock_print: args = ['export', 'bad_action'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("is an invalid parameter", mock_print.getvalue()) # test with a good action with patch( 'neo.Prompt.Commands.Wallet.CommandWalletExport.execute_sub_command', side_effect=[True]): args = ['export', 'mocked_action'] res = CommandWallet().execute(args) self.assertTrue(res)
def test_wallet_import_watchaddr(self): self.OpenWallet1() # test missing wif key argument with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'watch_addr'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with bad address with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'watch_addr', 'too_short_addr'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Invalid address specified", mock_print.getvalue()) # test with good address with patch('sys.stdout', new=StringIO()) as mock_print: address = 'AZfFBeBqtJvaTK9JqG8uk6N7FppQY6byEg' args = ['import', 'watch_addr', address] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn("Added address", mock_print.getvalue()) self.assertIn("watch-only", mock_print.getvalue()) self.assertIn(PromptData.Wallet.ToScriptHash(address), PromptData.Wallet.LoadWatchOnly()) # test address already exists with patch('sys.stdout', new=StringIO()) as mock_print: address = 'AZfFBeBqtJvaTK9JqG8uk6N7FppQY6byEg' args = ['import', 'watch_addr', address] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Address already exists in wallet", mock_print.getvalue())
def test_wallet_open(self): with patch('neo.Prompt.PromptData.PromptData.Prompt'): with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[self.wallet_1_pass()]): if self._wallet1 is None: shutil.copyfile(self.wallet_1_path(), self.wallet_1_dest()) # test wallet open successful args = ['open', self.wallet_1_dest()] res = CommandWallet().execute(args) self.assertEqual(type(res), UserWallet) # test wallet open with no path; this will also close the open wallet with patch('sys.stdout', new=StringIO()) as mock_print: args = ['open'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Please specify the required parameter", mock_print.getvalue()) # test wallet open with bad path with patch('sys.stdout', new=StringIO()) as mock_print: args = ['open', 'badpath'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Wallet file not found", mock_print.getvalue()) # test wallet open unsuccessful with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=["testpassword"]): with patch( 'neo.Implementations.Wallets.peewee.UserWallet.UserWallet.Open', side_effect=[Exception('test exception')]): args = ['open', 'fixtures/testwallet.db3'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Could not open wallet", mock_print.getvalue()) # test wallet open with keyboard interrupt with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[KeyboardInterrupt]): args = ['open', self.wallet_1_dest()] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Wallet opening cancelled", mock_print.getvalue())
def test_wallet(self): # without wallet opened res = CommandWallet().execute(None) self.assertFalse(res) # with wallet opened self.OpenWallet1() res = CommandWallet().execute(None) self.assertEqual(type(res), UserWallet)
def test_wallet_verbose(self): # test wallet verbose with no wallet opened args = ['verbose'] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() # test wallet close with open wallet args = ['verbose'] res = CommandWallet().execute(args) self.assertTrue(res)
def test_wallet_claim_4(self): self.OpenWallet2() # test with --from-addr and --to-addr with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]): args = ['claim', '--from-addr=' + self.wallet_1_addr, '--to-addr=' + self.wallet_2_addr] claim_tx, relayed = CommandWallet().execute(args) self.assertIsInstance(claim_tx, ClaimTransaction) self.assertTrue(relayed) json_tx = claim_tx.ToJson() self.assertEqual(json_tx['vout'][0]['address'], self.wallet_2_addr) # note how the --to-addr also supercedes the from address if both are specified
async def run_test(): if self._wallet1 is None: shutil.copyfile(self.wallet_1_path(), self.wallet_1_dest()) args = ['open', self.wallet_1_dest()] res = CommandWallet().execute(args) self.assertEqual(type(res), UserWallet) # now close the open wallet manually args = ['close'] res = CommandWallet().execute(args) self.assertTrue(res)
def test_wallet_import_wif(self): self.OpenWallet1() # test missing wif key argument with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'wif'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with bad wif length with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'wif', 'too_short'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Please provide a wif with a length of 52 bytes", mock_print.getvalue()) # test with invalid wif with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'wif', 'a' * 52] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Invalid format", mock_print.getvalue()) # test with exception in key creation with patch('sys.stdout', new=StringIO()) as mock_print: with patch( 'neo.Prompt.Commands.Wallet.PromptData.Wallet.CreateKey', side_effect=[Exception("unittest_error")]): with self.assertRaises(Exception) as context: args = [ 'import', 'wif', 'Ky94Rq8rb1z8UzTthYmy1ApbZa9xsKTvQCiuGUZJZbaDJZdkvLRV' ] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("unittest_error", str(context.exception)) self.assertIn("unittest_error", mock_print.getvalue()) # test with valid wif with patch('sys.stdout', new=StringIO()) as mock_print: args = [ 'import', 'wif', 'Ky94Rq8rb1z8UzTthYmy1ApbZa9xsKTvQCiuGUZJZbaDJZdkvLRV' ] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn(self.wallet_1_addr, mock_print.getvalue())
def test_show_account(self): # setup wallet_1_addr = 'AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3' # test no account address entered args = ['account'] res = CommandShow().execute(args) self.assertFalse(res) # test good account args = ['account', wallet_1_addr] res = CommandShow().execute(args) self.assertTrue(res) self.assertEqual(res['address'], wallet_1_addr) self.assertIn('balances', res) # test empty account with patch('neo.Prompt.PromptData.PromptData.Prompt'): with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=["testpassword", "testpassword"]): with patch('neo.Prompt.Commands.Wallet.asyncio'): with patch('neo.Wallets.Wallet.Wallet.sync_wallet'): args = ['create', 'testwallet.wallet'] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIsInstance(res, UserWallet) addr = res.Addresses[0] args = ['account', addr] res = CommandShow().execute(args) self.assertFalse(res) # remove test wallet os.remove("testwallet.wallet")
def test_wallet_token_allowance(self): with self.OpenWallet1(): # test with no parameters with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'allowance'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameters", mock_print.getvalue()) # test with insufficient parameters (1 instead of 4) with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'allowance', 'arg1'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with an invalid token argument with patch('sys.stdout', new=StringIO()) as mock_print: args = [ 'token', 'allowance', 'invalid_token_name', 'arg2', 'arg3' ] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("does not represent a known NEP5 token", mock_print.getvalue()) # test with valid token arg, but invalid from_addr (as that tests exceptions from the internal _validate_nep5_args() function) # that function is tested enough in other command tests with patch('sys.stdout', new=StringIO()) as mock_print: args = [ 'token', 'allowance', 'NXT4', 'invalid_from_addr', 'arg3' ] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("not a valid address", mock_print.getvalue()) with patch('neo.Prompt.Commands.Tokens.token_get_allowance', return_value=12300000000): with patch('sys.stdout', new=StringIO()) as mock_print: addr_from = PromptData.Wallet.GetDefaultContract().Address addr_to = self.watch_addr_str args = ['token', 'allowance', 'NXT4', addr_from, addr_to] res = CommandWallet().execute(args) self.assertTrue(res)
def test_wallet_import_token(self): token_hash = '31730cc9a1844891a3bafd1aa929a4142860d8d3' self.OpenWallet1() # test missing contract hash with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'token'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with invalid contract hash with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'token', 'does_not_exist'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("Invalid contract hash", mock_print.getvalue()) # test with valid but unknown contract hash with patch('sys.stdout', new=StringIO()) as mock_print: args = [ 'import', 'token', '31730cc9a1844891a3bafd1aa929a41000000000' ] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("Could not find the contract hash", mock_print.getvalue()) # Test with impossibility to import the token with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Wallets.NEP5Token.NEP5Token.Query', side_effect=[None]): args = ['import', 'token', token_hash] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("Could not import token", mock_print.getvalue()) # test with good hash args = ['import', 'token', token_hash] token = CommandWallet().execute(args) self.assertIsInstance(token, NEP5Token) self.assertEqual(token.name, 'NEX Template V4') self.assertEqual(token.symbol, 'NXT4') self.assertEqual(token.decimals, 8) self.assertEqual(token.Address, 'Ab61S1rk2VtCVd3NtGNphmBckWk4cfBdmB')
def test_wallet_claim_3(self): self.OpenWallet1() # test with bad --to-addr with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]): args = ['claim', '--to-addr=AGYaEi3W6ndHPUmW7T12FFfsbQ6DWymkEn'] # bad address checksum claim_tx, relayed = CommandWallet().execute(args) self.assertEqual(claim_tx, None) self.assertFalse(relayed) self.assertIn("Address format error", mock_print.getvalue()) # test with an invalid --to-addr with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]): args = ['claim', '--to-addr=blah'] # completely wrong address format claim_tx, relayed = CommandWallet().execute(args) self.assertEqual(claim_tx, None) self.assertFalse(relayed) self.assertIn("Not correct Address, wrong length", mock_print.getvalue()) # test with --to-addr with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]): args = ['claim', '--to-addr=' + self.watch_addr_str] claim_tx, relayed = CommandWallet().execute(args) self.assertIsInstance(claim_tx, ClaimTransaction) self.assertTrue(relayed) json_tx = claim_tx.ToJson() self.assertEqual(json_tx['vout'][0]['address'], self.watch_addr_str) # note how the --to-addr supercedes the default change address
def test_wallet_claim_2(self): self.OpenWallet2() # test with bad --from-addr with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]): args = ['claim', '--from-addr=AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc'] # address is too short claim_tx, relayed = CommandWallet().execute(args) self.assertEqual(claim_tx, None) self.assertFalse(relayed) self.assertIn("Not correct Address, wrong length.", mock_print.getvalue()) # test with invalid --from-addr with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]): args = ['claim', '--from-addr=VJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3'] # address does not start with 'A' claim_tx, relayed = CommandWallet().execute(args) self.assertEqual(claim_tx, None) self.assertFalse(relayed) self.assertIn("Address format error", mock_print.getvalue()) # successful test with --from-addr with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]): args = ['claim', '--from-addr=' + self.wallet_1_addr] claim_tx, relayed = CommandWallet().execute(args) self.assertIsInstance(claim_tx, ClaimTransaction) self.assertTrue(relayed) json_tx = claim_tx.ToJson() self.assertEqual(json_tx['vout'][0]['address'], self.wallet_1_addr)
def test_wallet_export_wif(self): self.OpenWallet1() # test missing address argument with patch('sys.stdout', new=StringIO()) as mock_print: args = ['export', 'wif'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with an address that's not part of the wallet with patch('sys.stdout', new=StringIO()) as mock_print: args = ['export', 'wif', 'bad_address'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Could not find address", mock_print.getvalue()) # test with good address but bad passw with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletExport.prompt', side_effect=['random_passw']): args = ['export', 'wif', self.wallet_1_addr] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Incorrect password", mock_print.getvalue()) # test with good address but keyboard interrupt with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletExport.prompt', side_effect=[KeyboardInterrupt]): args = ['export', 'wif', self.wallet_1_addr] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Export cancelled", mock_print.getvalue()) # test with good address with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletExport.prompt', side_effect=[self.wallet_1_pass()]): args = ['export', 'wif', self.wallet_1_addr] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn( "Ky94Rq8rb1z8UzTthYmy1ApbZa9xsKTvQCiuGUZJZbaDJZdkvLRV", mock_print.getvalue())
async def run_test(): if self._wallet1 is None: shutil.copyfile(self.wallet_1_path(), self.wallet_1_dest()) # test wallet open successful args = ['open', self.wallet_1_dest()] res = CommandWallet().execute(args) self.assertEqual(type(res), UserWallet) # test wallet open with no path; this will also close the open wallet args = ['open'] res = CommandWallet().execute(args) self.assertFalse(res) # test wallet open with bad path args = ['open', 'badpath'] res = CommandWallet().execute(args) self.assertFalse(res)
def test_wallet_claim_4(self): self.OpenWallet2() # test with --from-addr and --to-addr nodemgr = NodeManager() nodemgr.nodes = [NeoNode(object, object)] with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]): with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)): args = ['claim', '--from-addr=' + self.wallet_1_addr, '--to-addr=' + self.wallet_2_addr] claim_tx, relayed = CommandWallet().execute(args) self.assertIsInstance(claim_tx, ClaimTransaction) self.assertTrue(relayed) json_tx = claim_tx.ToJson() self.assertEqual(json_tx['vout'][0]['address'], self.wallet_2_addr) # note how the --to-addr also supercedes the from address if both are specified nodemgr.reset_for_test()
def test_wallet_import_nep2(self): self.OpenWallet1() # test missing nep2 key argument with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'nep2'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with bad nep2 length with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletImport.prompt', side_effect=['random_passw']): args = ['import', 'nep2', 'too_short_nep2_key'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Please provide a nep2_key with a length of 58 bytes", mock_print.getvalue()) # test with ok NEP2, bad password with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletImport.prompt', side_effect=['wrong_password']): args = ['import', 'nep2', '6PYK1E3skTFLgtsnVNKDCEdUQxeKbRmKBnbkPFxvGGggfeB2JacnMpqkcH'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Wrong passphrase", mock_print.getvalue()) # test with exception in key creation with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletImport.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): with patch('neo.Prompt.Commands.Wallet.PromptData.Wallet.CreateKey', side_effect=[Exception("unittest_error")]): args = ['import', 'nep2', '6PYK1E3skTFLgtsnVNKDCEdUQxeKbRmKBnbkPFxvGGggfeB2JacnMpqkcH'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Key creation error", mock_print.getvalue()) self.assertIn("unittest_error", mock_print.getvalue()) # test with ok with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.WalletImport.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): args = ['import', 'nep2', '6PYK1E3skTFLgtsnVNKDCEdUQxeKbRmKBnbkPFxvGGggfeB2JacnMpqkcH'] res = CommandWallet().execute(args) self.assertFalse(res) # if we imported successfully we get the wallet1 address self.assertIn(self.wallet_1_addr, mock_print.getvalue())
def test_wallet_rebuild(self): with patch('neo.Prompt.PromptData.PromptData.Prompt'): # test wallet rebuild with no wallet open args = ['rebuild'] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() PromptData.Wallet._current_height = 12345 # test wallet rebuild with no argument args = ['rebuild'] CommandWallet().execute(args) self.assertEqual(PromptData.Wallet._current_height, 0) # test wallet rebuild with start block args = ['rebuild', '42'] CommandWallet().execute(args) self.assertEqual(PromptData.Wallet._current_height, 42)
def test_wallet_import_contract_addr(self): # test with no wallet open with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'contract_addr', 'contract_hash', 'pubkey'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("open a wallet", mock_print.getvalue()) self.OpenWallet1() # test with not enough arguments (must have 2 arguments) with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'contract_addr'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("specify the required parameters", mock_print.getvalue()) # test with too many arguments (must have 2 arguments) with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'contract_addr', 'arg1', 'arg2', 'arg3'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("specify the required parameters", mock_print.getvalue()) # test with invalid contract hash with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'contract_addr', 'invalid_contract_hash', '03cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("Invalid contract hash", mock_print.getvalue()) # test with valid contract hash but that doesn't exist with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'contract_addr', '31730cc9a1844891a3bafd1aa929000000000000', '03cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("Could not find contract", mock_print.getvalue()) # test with invalid pubkey with patch('sys.stdout', new=StringIO()) as mock_print: args = ['import', 'contract_addr', '31730cc9a1844891a3bafd1aa929a4142860d8d3', 'invalid_pubkey'] res = CommandWallet().execute(args) self.assertIsNone(res) self.assertIn("Invalid pubkey", mock_print.getvalue()) # test with valid arguments contract_hash = UInt160.ParseString('31730cc9a1844891a3bafd1aa929a4142860d8d3') with patch('sys.stdout', new=StringIO()) as mock_print: self.assertIsNone(PromptData.Wallet.GetContract(contract_hash)) args = ['import', 'contract_addr', '31730cc9a1844891a3bafd1aa929a4142860d8d3', '03cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6'] res = CommandWallet().execute(args) self.assertIsInstance(res, Contract) self.assertTrue(PromptData.Wallet.GetContract(contract_hash)) self.assertIn("Added contract address", mock_print.getvalue())
def test_wallet_verbose(self): # test wallet verbose with no wallet opened args = ['verbose'] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() # first test normal wallet printing with patch('sys.stdout', new=StringIO()) as mock_print: args = [''] res = CommandWallet().execute(args) self.assertTrue(res) self.assertNotIn("Script hash", mock_print.getvalue()) self.assertNotIn("Public key", mock_print.getvalue()) # now test wallet verbose with open wallet with patch('sys.stdout', new=StringIO()) as mock_print: args = ['verbose'] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn("Script hash", mock_print.getvalue()) self.assertIn("Public key", mock_print.getvalue()) # also test "v" with patch('sys.stdout', new=StringIO()) as mock_print: args = ['v'] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn("Script hash", mock_print.getvalue()) self.assertIn("Public key", mock_print.getvalue()) # and "--v" with patch('sys.stdout', new=StringIO()) as mock_print: args = ['--v'] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn("Script hash", mock_print.getvalue()) self.assertIn("Public key", mock_print.getvalue())
async def run_test(): # test wallet rebuild with no wallet open args = ['rebuild'] res = CommandWallet().execute(args) self.assertFalse(res) self.OpenWallet1() # test wallet rebuild with no argument args = ['rebuild'] task = CommandWallet().execute(args) # "rebuild" creates a task to start syncing # we have to wait for it to have started before we can assert the call status await asyncio.gather(task) mocked_sync_wallet.assert_called_with(0, rebuild=True) # test wallet rebuild with start block args = ['rebuild', '42'] task = CommandWallet().execute(args) await asyncio.gather(task) mocked_sync_wallet.assert_called_with(42, rebuild=True)
def test_wallet_token(self): """ Generic tests for the Token subcommand of the Wallet group """ # test token no wallet with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("open a wallet", mock_print.getvalue()) with self.OpenWallet1(): # test token with insufficient args with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("run `token help` to see supported queries", mock_print.getvalue()) with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', None] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("run `token help` to see supported queries", mock_print.getvalue()) # test token with invalid subcommands with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', -1] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("invalid parameter", mock_print.getvalue()) with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'nope'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("invalid parameter", mock_print.getvalue())
def test_token_register(self): with self.OpenWallet1(): # test token delete with no parameter with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'register'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test token delete with insufficient parameters with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'register', 'arg1'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("specify the required parameter", mock_print.getvalue()) # test with invalid token argument with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'register', 'invalid_token_name', 'arg2'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("does not represent a known NEP5 token", mock_print.getvalue()) # test with invalid address with patch('sys.stdout', new=StringIO()) as mock_print: args = ['token', 'register', 'NXT4', 'bad_addr'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("is not a valid address", mock_print.getvalue()) # test with some exception in NEP5Token.CrowdsaleRegister with patch('neo.Wallets.NEP5Token.NEP5Token.CrowdsaleRegister' ) as mocked_register: with patch('sys.stdout', new=StringIO()) as mock_print: mocked_register.return_value = (object(), 0, [] ) # tx, fee, results args = [ 'token', 'register', 'NXT4', 'AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y' ] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Could not register address(es)", mock_print.getvalue()) # test with valid address with patch('sys.stdout', new=StringIO()) as mock_print: args = [ 'token', 'register', 'NXT4', 'AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y' ] res = CommandWallet().execute(args) self.assertTrue(res) self.assertIn("[NXT4] Will register addresses", mock_print.getvalue())
def test_wallet_claim_1(self): # test with no wallet with patch('sys.stdout', new=StringIO()) as mock_print: args = ['claim'] res = CommandWallet().execute(args) self.assertFalse(res) self.assertIn("Please open a wallet", mock_print.getvalue()) self.OpenWallet1() # test wrong password with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=["wrong"]): args = ['claim'] claim_tx, relayed = CommandWallet().execute(args) self.assertEqual(claim_tx, None) self.assertFalse(relayed) self.assertIn("Incorrect password", mock_print.getvalue()) # test successful nodemgr = NodeManager() nodemgr.nodes = [NeoNode(object, object)] with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]): with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)): args = ['claim'] claim_tx, relayed = CommandWallet().execute(args) self.assertIsInstance(claim_tx, ClaimTransaction) self.assertTrue(relayed) json_tx = claim_tx.ToJson() self.assertEqual(json_tx['vout'][0]['address'], self.wallet_1_addr) nodemgr.reset_for_test() # test nothing to claim anymore with patch('sys.stdout', new=StringIO()) as mock_print: with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]): args = ['claim'] claim_tx, relayed = CommandWallet().execute(args) self.assertEqual(claim_tx, None) self.assertFalse(relayed) self.assertIn("No claims to process", mock_print.getvalue())