fix setup and teardown for wallet tests

-also fixes a few wallet tests that weren't checking exceptions correctly
This commit is contained in:
Jack Robison 2018-02-13 14:20:54 -05:00
parent 9d87de1433
commit 73f09edcf0
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -16,42 +16,53 @@ from lbryum.simple_config import SimpleConfig
from lbryschema.claim import ClaimDict from lbryschema.claim import ClaimDict
test_metadata = { test_metadata = {
'license': 'NASA', 'license': 'NASA',
'version': '_0_1_0', 'version': '_0_1_0',
'description': 'test', 'description': 'test',
'language': 'en', 'language': 'en',
'author': 'test', 'author': 'test',
'title': 'test', 'title': 'test',
'nsfw': False, 'nsfw': False,
'thumbnail': 'test' 'thumbnail': 'test'
} }
test_claim_dict = { test_claim_dict = {
'version':'_0_0_1', 'version': '_0_0_1',
'claimType':'streamType', 'claimType': 'streamType',
'stream':{'metadata':test_metadata, 'version':'_0_0_1', 'source': 'stream': {'metadata': test_metadata, 'version': '_0_0_1', 'source':
{'source': '8655f713819344980a9a0d67b198344e2c462c90f813e86f' {'source': '8655f713819344980a9a0d67b198344e2c462c90f813e86f'
'0c63789ab0868031f25c54d0bb31af6658e997e2041806eb', '0c63789ab0868031f25c54d0bb31af6658e997e2041806eb',
'sourceType': 'lbry_sd_hash', 'contentType': 'video/mp4', 'version': '_0_0_1'}, 'sourceType': 'lbry_sd_hash', 'contentType': 'video/mp4', 'version': '_0_0_1'},
}} }}
class MocLbryumWallet(LBRYumWallet): class MocLbryumWallet(LBRYumWallet):
def __init__(self): def __init__(self, db_dir):
# LBRYumWallet.__init__(self) LBRYumWallet.__init__(self, SQLiteStorage(db_dir), SimpleConfig(
self.config = SimpleConfig() {"lbryum_path": db_dir, "wallet_path": os.path.join(db_dir, "testwallet")}
))
self.db_dir = db_dir
self.wallet_balance = Decimal(10.0) self.wallet_balance = Decimal(10.0)
self.total_reserved_points = Decimal(0.0) self.total_reserved_points = Decimal(0.0)
self.queued_payments = defaultdict(Decimal) self.queued_payments = defaultdict(Decimal)
self.network = FakeNetwork() self.network = FakeNetwork()
self.db_dir = tempfile.mkdtemp() assert self.config.get_wallet_path() == os.path.join(self.db_dir, "testwallet")
self.storage = SQLiteStorage(self.db_dir)
def __del__(self): @defer.inlineCallbacks
shutil.rmtree(self.db_dir) def setup(self, password=None, seed=None):
yield self.storage.setup()
seed = seed or "travel nowhere air position hill peace suffer parent beautiful rise " \
"blood power home crumble teach"
storage = lbryum.wallet.WalletStorage(self.config.get_wallet_path())
self.wallet = lbryum.wallet.NewWallet(storage)
self.wallet.add_seed(seed, password)
self.wallet.create_master_keys(password)
self.wallet.create_main_account()
def setup(self): @defer.inlineCallbacks
return self.storage.setup() def stop(self):
yield self.storage.stop()
yield threads.deferToThread(shutil.rmtree, self.db_dir)
def get_least_used_address(self, account=None, for_change=False, max_count=100): def get_least_used_address(self, account=None, for_change=False, max_count=100):
return defer.succeed(None) return defer.succeed(None)
@ -64,41 +75,27 @@ class MocLbryumWallet(LBRYumWallet):
class WalletTest(unittest.TestCase): class WalletTest(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
wallet = MocLbryumWallet()
yield wallet.setup()
seed_text = "travel nowhere air position hill peace suffer parent beautiful rise " \
"blood power home crumble teach"
password = "secret"
user_dir = tempfile.mkdtemp() user_dir = tempfile.mkdtemp()
path = os.path.join(user_dir, "somewallet") self.wallet = MocLbryumWallet(user_dir)
storage = lbryum.wallet.WalletStorage(path) yield self.wallet.setup()
wallet.wallet = lbryum.wallet.NewWallet(storage) self.assertEqual(self.wallet.get_balance(), Decimal(10))
wallet.wallet.add_seed(seed_text, password)
wallet.wallet.create_master_keys(password)
wallet.wallet.create_main_account()
self.wallet_path = path
self.enc_wallet = wallet
self.enc_wallet_password = password
def tearDown(self): def tearDown(self):
shutil.rmtree(os.path.dirname(self.wallet_path)) return self.wallet.stop()
def test_failed_send_name_claim(self): def test_failed_send_name_claim(self):
def not_enough_funds_send_name_claim(self, name, val, amount): def not_enough_funds_send_name_claim(self, name, val, amount):
claim_out = {'success':False, 'reason':'Not enough funds'} claim_out = {'success': False, 'reason': 'Not enough funds'}
return claim_out return claim_out
MocLbryumWallet._send_name_claim = not_enough_funds_send_name_claim self.wallet._send_name_claim = not_enough_funds_send_name_claim
wallet = MocLbryumWallet() d = self.wallet.claim_name('test', 1, test_claim_dict)
d = wallet.setup()
d.addCallback(lambda _: wallet.claim_name('test', 1, test_claim_dict))
self.assertFailure(d, Exception) self.assertFailure(d, Exception)
return d return d
@defer.inlineCallbacks
def test_successful_send_name_claim(self): def test_successful_send_name_claim(self):
expected_claim_out = { expected_claim_out = {
"claim_id": "f43dc06256a69988bdbea09a58c80493ba15dcfa", "claim_id": "f43dc06256a69988bdbea09a58c80493ba15dcfa",
@ -112,35 +109,26 @@ class WalletTest(unittest.TestCase):
"channel_name": "" "channel_name": ""
} }
def check_out(claim_out):
self.assertTrue('success' not in claim_out)
self.assertEqual(expected_claim_out['claim_id'], claim_out['claim_id'])
self.assertEqual(expected_claim_out['fee'], claim_out['fee'])
self.assertEqual(expected_claim_out['nout'], claim_out['nout'])
self.assertEqual(expected_claim_out['txid'], claim_out['txid'])
self.assertEqual(expected_claim_out['value'], claim_out['value'])
def success_send_name_claim(self, name, val, amount, certificate_id=None, def success_send_name_claim(self, name, val, amount, certificate_id=None,
claim_address=None, change_address=None): claim_address=None, change_address=None):
return expected_claim_out return defer.succeed(expected_claim_out)
MocLbryumWallet._send_name_claim = success_send_name_claim self.wallet._send_name_claim = success_send_name_claim
wallet = MocLbryumWallet() claim_out = yield self.wallet.claim_name('test', 1, test_claim_dict)
d = wallet.setup() self.assertTrue('success' not in claim_out)
d.addCallback(lambda _: wallet.claim_name('test', 1, test_claim_dict)) self.assertEqual(expected_claim_out['claim_id'], claim_out['claim_id'])
d.addCallback(lambda claim_out: check_out(claim_out)) self.assertEqual(expected_claim_out['fee'], claim_out['fee'])
return d self.assertEqual(expected_claim_out['nout'], claim_out['nout'])
self.assertEqual(expected_claim_out['txid'], claim_out['txid'])
self.assertEqual(expected_claim_out['value'], claim_out['value'])
@defer.inlineCallbacks
def test_failed_support(self): def test_failed_support(self):
def failed_support_claim(self, name, claim_id, amount): # wallet.support_claim will check the balance before calling _support_claim
claim_out = {'success':False, 'reason':'Not enough funds'} try:
return threads.deferToThread(lambda: claim_out) yield self.wallet.support_claim('test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 1000)
MocLbryumWallet._support_claim = failed_support_claim except InsufficientFundsError:
wallet = MocLbryumWallet() pass
d = wallet.setup()
d.addCallback(lambda _: wallet.support_claim('test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 1))
self.assertFailure(d, Exception)
return d
def test_succesful_support(self): def test_succesful_support(self):
expected_support_out = { expected_support_out = {
@ -150,32 +138,32 @@ class WalletTest(unittest.TestCase):
"txid": "11030a76521e5f552ca87ad70765d0cc52e6ea4c0dc0063335e6cf2a9a85085f" "txid": "11030a76521e5f552ca87ad70765d0cc52e6ea4c0dc0063335e6cf2a9a85085f"
} }
def check_out(claim_out): expected_result = {
self.assertTrue('success' not in claim_out) "fee": 0.000129,
self.assertEqual(expected_support_out['fee'], claim_out['fee']) "nout": 0,
self.assertEqual(expected_support_out['nout'], claim_out['nout']) "txid": "11030a76521e5f552ca87ad70765d0cc52e6ea4c0dc0063335e6cf2a9a85085f"
self.assertEqual(expected_support_out['txid'], claim_out['txid']) }
def success_support_claim(self, name, val, amount): def check_out(claim_out):
return threads.deferToThread(lambda: expected_support_out) self.assertDictEqual(expected_result, claim_out)
MocLbryumWallet._support_claim = success_support_claim
wallet = MocLbryumWallet() def success_support_claim(name, val, amount):
d = wallet.setup() return defer.succeed(expected_support_out)
d.addCallback(lambda _: wallet.support_claim('test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 1))
self.wallet._support_claim = success_support_claim
d = self.wallet.support_claim('test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 1)
d.addCallback(lambda claim_out: check_out(claim_out)) d.addCallback(lambda claim_out: check_out(claim_out))
return d return d
@defer.inlineCallbacks
def test_failed_abandon(self): def test_failed_abandon(self):
def failed_abandon_claim(self, claim_outpoint): try:
claim_out = {'success':False, 'reason':'Not enough funds'} yield self.wallet.abandon_claim("f43dc06256a69988bdbea09a58c80493ba15dcfa", None, None)
return threads.deferToThread(lambda: claim_out) raise Exception("test failed")
MocLbryumWallet._abandon_claim = failed_abandon_claim except Exception as err:
wallet = MocLbryumWallet() self.assertSubstring("claim not found", err.message)
d = wallet.setup()
d.addCallback(lambda _: wallet.abandon_claim("f43dc06256a69988bdbea09a58c80493ba15dcfa", None, None))
self.assertFailure(d, Exception)
return d
@defer.inlineCallbacks
def test_successful_abandon(self): def test_successful_abandon(self):
expected_abandon_out = { expected_abandon_out = {
"fee": "0.000096", "fee": "0.000096",
@ -183,59 +171,57 @@ class WalletTest(unittest.TestCase):
"txid": "0578c161ad8d36a7580c557d7444f967ea7f988e194c20d0e3c42c3cabf110dd" "txid": "0578c161ad8d36a7580c557d7444f967ea7f988e194c20d0e3c42c3cabf110dd"
} }
def check_out(claim_out): expected_abandon_result = {
self.assertTrue('success' not in claim_out) "fee": 0.000096,
self.assertEqual(expected_abandon_out['fee'], claim_out['fee']) "txid": "0578c161ad8d36a7580c557d7444f967ea7f988e194c20d0e3c42c3cabf110dd"
self.assertEqual(expected_abandon_out['txid'], claim_out['txid']) }
def success_abandon_claim(self, claim_outpoint, txid, nout): def success_abandon_claim(claim_outpoint, txid, nout):
return threads.deferToThread(lambda: expected_abandon_out) return defer.succeed(expected_abandon_out)
MocLbryumWallet._abandon_claim = success_abandon_claim self.wallet._abandon_claim = success_abandon_claim
wallet = MocLbryumWallet() claim_out = yield self.wallet.abandon_claim("f43dc06256a69988bdbea09a58c80493ba15dcfa", None, None)
d = wallet.storage.setup() self.assertDictEqual(expected_abandon_result, claim_out)
d.addCallback(lambda _: wallet.abandon_claim("f43dc06256a69988bdbea09a58c80493ba15dcfa", None, None))
d.addCallback(lambda claim_out: check_out(claim_out))
return d
@defer.inlineCallbacks
def test_point_reservation_and_balance(self): def test_point_reservation_and_balance(self):
# check that point reservations and cancellation changes the balance # check that point reservations and cancellation changes the balance
# properly # properly
def update_balance(): def update_balance():
return defer.succeed(5) return defer.succeed(5)
wallet = MocLbryumWallet()
wallet._update_balance = update_balance self.wallet._update_balance = update_balance
d = wallet.setup() yield self.wallet.update_balance()
d.addCallback(lambda _: wallet.update_balance()) self.assertEqual(5, self.wallet.get_balance())
# test point reservation # test point reservation
d.addCallback(lambda _: self.assertEqual(5, wallet.get_balance())) yield self.wallet.reserve_points('testid', 2)
d.addCallback(lambda _: wallet.reserve_points('testid', 2)) self.assertEqual(3, self.wallet.get_balance())
d.addCallback(lambda _: self.assertEqual(3, wallet.get_balance())) self.assertEqual(2, self.wallet.total_reserved_points)
d.addCallback(lambda _: self.assertEqual(2, wallet.total_reserved_points))
# test reserved points cancellation # test reserved points cancellation
d.addCallback(lambda _: wallet.cancel_point_reservation(ReservedPoints('testid', 2))) yield self.wallet.cancel_point_reservation(ReservedPoints('testid', 2))
d.addCallback(lambda _: self.assertEqual(5, wallet.get_balance())) self.assertEqual(5, self.wallet.get_balance())
d.addCallback(lambda _: self.assertEqual(0, wallet.total_reserved_points)) self.assertEqual(0, self.wallet.total_reserved_points)
# test point sending # test point sending
d.addCallback(lambda _: wallet.reserve_points('testid', 2)) reserve_points = yield self.wallet.reserve_points('testid', 2)
d.addCallback(lambda reserve_points: wallet.send_points_to_address(reserve_points, 1)) yield self.wallet.send_points_to_address(reserve_points, 1)
d.addCallback(lambda _: self.assertEqual(3, wallet.get_balance())) self.assertEqual(3, self.wallet.get_balance())
# test failed point reservation # test failed point reservation
d.addCallback(lambda _: wallet.reserve_points('testid', 4)) out = yield self.wallet.reserve_points('testid', 4)
d.addCallback(lambda out: self.assertEqual(None, out)) self.assertEqual(None, out)
return d
def test_point_reservation_and_claim(self): def test_point_reservation_and_claim(self):
# check that claims take into consideration point reservations # check that claims take into consideration point reservations
def update_balance(): def update_balance():
return defer.succeed(5) return defer.succeed(5)
wallet = MocLbryumWallet()
wallet._update_balance = update_balance self.wallet._update_balance = update_balance
d = wallet.setup() d = self.wallet.update_balance()
d.addCallback(lambda _: wallet.update_balance()) d.addCallback(lambda _: self.assertEqual(5, self.wallet.get_balance()))
d.addCallback(lambda _: self.assertEqual(5, wallet.get_balance())) d.addCallback(lambda _: self.wallet.reserve_points('testid', 2))
d.addCallback(lambda _: wallet.reserve_points('testid', 2)) d.addCallback(lambda _: self.wallet.claim_name('test', 4, test_claim_dict))
d.addCallback(lambda _: wallet.claim_name('test', 4, test_claim_dict))
self.assertFailure(d, InsufficientFundsError) self.assertFailure(d, InsufficientFundsError)
return d return d
@ -243,39 +229,45 @@ class WalletTest(unittest.TestCase):
# check that supports take into consideration point reservations # check that supports take into consideration point reservations
def update_balance(): def update_balance():
return defer.succeed(5) return defer.succeed(5)
wallet = MocLbryumWallet()
wallet._update_balance = update_balance self.wallet._update_balance = update_balance
d = wallet.setup() d = self.wallet.update_balance()
d.addCallback(lambda _: wallet.update_balance()) d.addCallback(lambda _: self.assertEqual(5, self.wallet.get_balance()))
d.addCallback(lambda _: self.assertEqual(5, wallet.get_balance())) d.addCallback(lambda _: self.wallet.reserve_points('testid', 2))
d.addCallback(lambda _: wallet.reserve_points('testid', 2)) d.addCallback(lambda _: self.wallet.support_claim(
d.addCallback(lambda _: wallet.support_claim(
'test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 4)) 'test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 4))
self.assertFailure(d, InsufficientFundsError) self.assertFailure(d, InsufficientFundsError)
return d return d
class WalletEncryptionTests(unittest.TestCase):
def setUp(self):
user_dir = tempfile.mkdtemp()
self.wallet = MocLbryumWallet(user_dir)
return self.wallet.setup(password="password")
def tearDown(self):
return self.wallet.stop()
def test_unlock_wallet(self): def test_unlock_wallet(self):
wallet = self.enc_wallet self.wallet._cmd_runner = Commands(
wallet._cmd_runner = Commands( self.wallet.config, self.wallet.wallet, self.wallet.network, None, "password")
wallet.config, wallet.wallet, wallet.network, None, self.enc_wallet_password) cmd_runner = self.wallet.get_cmd_runner()
cmd_runner = wallet.get_cmd_runner() cmd_runner.unlock_wallet("password")
cmd_runner.unlock_wallet(self.enc_wallet_password)
self.assertIsNone(cmd_runner.new_password) self.assertIsNone(cmd_runner.new_password)
self.assertEqual(cmd_runner._password, self.enc_wallet_password) self.assertEqual(cmd_runner._password, "password")
def test_encrypt_decrypt_wallet(self): def test_encrypt_decrypt_wallet(self):
wallet = self.enc_wallet self.wallet._cmd_runner = Commands(
wallet._cmd_runner = Commands( self.wallet.config, self.wallet.wallet, self.wallet.network, None, "password")
wallet.config, wallet.wallet, wallet.network, None, self.enc_wallet_password) self.wallet.encrypt_wallet("secret2", False)
wallet.encrypt_wallet("secret2", False) self.wallet.decrypt_wallet()
wallet.decrypt_wallet()
def test_update_password_keyring_off(self): def test_update_password_keyring_off(self):
wallet = self.enc_wallet self.wallet.config.use_keyring = False
wallet.config.use_keyring = False self.wallet._cmd_runner = Commands(
wallet._cmd_runner = Commands( self.wallet.config, self.wallet.wallet, self.wallet.network, None, "password")
wallet.config, wallet.wallet, wallet.network, None, self.enc_wallet_password)
# no keyring available, so ValueError is expected # no keyring available, so ValueError is expected
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
wallet.encrypt_wallet("secret2", True) self.wallet.encrypt_wallet("secret2", True)