def test_deposit_twice(self, prepared_accounts, amount, first_valid, later_income, second_valid): # create cheque and try to deposit it cheque = new_cheque(prepared_accounts.source, prepared_accounts.target, amount) with error.expected(None if first_valid else ReturnValueError): transfer(cheque, prepared_accounts.source, prepared_accounts.target) expected_source = 100 expected_target = 0 if (first_valid): expected_source -= amount expected_target += amount prepared_accounts.assert_balances(-100, expected_source, expected_target) # now transfer more funds to source if later_income != 0: income = new_cheque(prepared_accounts.issuer, prepared_accounts.source, later_income) transfer(income, prepared_accounts.issuer, prepared_accounts.source) expected_source += later_income # and repeat cheque deposit with error.expected(None if second_valid else ReturnValueError): deposit = cheque.deposit(prepared_accounts.target.nym, prepared_accounts.target) if second_valid: expected_source -= amount expected_target += amount assert is_message_success(deposit) prepared_accounts.assert_balances(-100 - later_income, expected_source, expected_target)
def test_create_account_nonexistent_asset(): '''Test that we can't create an account for an asset that doesn't exist''' fake_id = Nym().create()._id # just to get a plausible asset id fake_asset = Asset(_id=fake_id, server_id=server.first_active_id()) acct = account.Account(fake_asset, Nym().register()) with error.expected(ReturnValueError): acct.create()
def test_create_account_nonexistent_asset(): '''Test that we can't create an account for an asset that doesn't exist''' fake_id = Nym().create()._id # just to get a plausible asset id fake_asset = Asset(_id=fake_id, server_id=server.first_active_id()) acct = account.Account(fake_asset, Nym().register()) with error.expected(ReturnValueError): acct.create()
def test_not_account_owner(prepared_accounts, instrument_constructor): '''Test that we get a graceful failure when we try to deposit an instrument for an account we don't own''' instrument = instrument_constructor( prepared_accounts.source, prepared_accounts.target, 50, source_nym=prepared_accounts.target.nym) with error.expected(ReturnValueError): transfer(instrument, prepared_accounts.source, prepared_accounts.target)
def test_cancel_instrument(instrument_constructor): '''Cancel an instrument and make sure it can't be deposited.''' accounts = data.TransferAccounts().initial_balance() instrument = instrument_constructor(accounts.source, accounts.target, 50) write(instrument) instrument.cancel() with error.expected(ReturnValueError): instrument.deposit(accounts.target.nym, accounts.target) accounts.assert_balances(-100, 100, 0)
def test_issue_asset_contract(issue_for_other_nym, expect_success): server_id = server.first_id() nym = Nym().register(server_id) issue_for_nym = Nym().register(server_id) if issue_for_other_nym else nym with error.expected(None if expect_success else ReturnValueError): Asset().issue(nym, open(data.btc_contract_file), server_id, issue_for_nym=issue_for_nym)
def test_cancel_instrument(instrument_constructor): '''Cancel an instrument and make sure it can't be deposited.''' accounts = data.TransferAccounts().initial_balance() instrument = instrument_constructor(accounts.source, accounts.target, 50) write(instrument) instrument.cancel() with error.expected(ReturnValueError): instrument.deposit(accounts.target.nym, accounts.target) accounts.assert_balances(-100, 100, 0)
def test_simple_transfer(self, prepared_accounts, amount, should_pass, instrument_constructor): instrument = instrument_constructor( prepared_accounts.issuer, prepared_accounts.target, amount) with error.expected(None if should_pass else ReturnValueError): transfer(instrument, prepared_accounts.issuer, prepared_accounts.target) if should_pass: prepared_accounts.assert_balances(-100 - amount, 100, amount) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_expired_cheque(self, prepared_accounts, valid_from, valid_to, valid): with error.expected(None if valid else ReturnValueError): transfer(new_cheque(prepared_accounts.source, prepared_accounts.target, 10, valid_from, valid_to), prepared_accounts.source, prepared_accounts.target) if valid: prepared_accounts.assert_balances(-100, 90, 10) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_wrong_asset_type(instrument_constructor): '''Try to transfer eg a cheque from one asset account to another of a different type. Should fail''' ta_asset1 = data.TransferAccounts().initial_balance() ta_asset2 = data.TransferAccounts().initial_balance() source = ta_asset1.source target = ta_asset2.target instrument = instrument_constructor(source, target, 50) with error.expected(ReturnValueError): transfer(instrument, source, target) ta_asset2.assert_balances(-100, 100, 0)
def test_simple_transfer(self, prepared_accounts, amount, should_pass, instrument_constructor): instrument = instrument_constructor(prepared_accounts.source, prepared_accounts.target, amount) with error.expected(None if should_pass else ReturnValueError): transfer(instrument, prepared_accounts.source, prepared_accounts.target) if should_pass: prepared_accounts.assert_balances(-100, 100 - amount, amount) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_expired_cheque(self, prepared_accounts, valid_from, valid_to, valid): with error.expected(None if valid else ReturnValueError): transfer( new_cheque(prepared_accounts.source, prepared_accounts.target, 10, valid_from, valid_to), prepared_accounts.source, prepared_accounts.target) if valid: prepared_accounts.assert_balances(-100, 90, 10) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_wrong_asset_type(instrument_constructor): '''Try to transfer eg a cheque from one asset account to another of a different type. Should fail''' ta_asset1 = data.TransferAccounts().initial_balance() ta_asset2 = data.TransferAccounts().initial_balance() source = ta_asset1.source target = ta_asset2.target instrument = instrument_constructor(source, target, 50) with error.expected(ReturnValueError): transfer(instrument, source, target) ta_asset2.assert_balances(-100, 100, 0)
def test_not_account_owner(prepared_accounts, instrument_constructor): '''Test that we get a graceful failure when we try to deposit an instrument for an account we don't own''' instrument = instrument_constructor( prepared_accounts.source, prepared_accounts.target, 50, source_nym=prepared_accounts.target.nym) with error.expected(ReturnValueError): transfer(instrument, prepared_accounts.source, prepared_accounts.target)
def test_deposit_wrong_nym(prepared_accounts): '''Export cash to one nym, try to deposit with another - should fail''' source = prepared_accounts.source target = prepared_accounts.target issuer = prepared_accounts.issuer cash.create_mint(prepared_accounts.asset) c = Cash(10) c.export(source, nym=issuer.nym) # export to the issuer but with error.expected(ReturnValueError): c.deposit(target) # try to deposit as the target # the cash is still taken out of the account prepared_accounts.assert_balances(-100, 90, 0)
def test_invoice(prepared_accounts, amount, should_pass): '''an invoice is just a cheque for a negative amount, so the target will invoice the source by writing him a cheque for a negative amount. After a successful deposit, funds move from source to target. ''' invoice = new_cheque(prepared_accounts.target, prepared_accounts.source, amount) invoice.write() with error.expected(None if should_pass else ReturnValueError): invoice.deposit(prepared_accounts.source.nym, prepared_accounts.source) prepared_accounts.assert_balances(-100, 100 + amount if should_pass else 100, -amount if should_pass else 0)
def test_out_of_band(prepared_accounts, amount): '''Export cash, pass it to other nym, he deposits it''' source = prepared_accounts.source target = prepared_accounts.target cash.create_mint(prepared_accounts.asset) c = Cash(amount) shoulderror = lambda amount: amount <= 0 or amount > 100 with error.expected(ReturnValueError if shoulderror(amount) else None): c.export(source, nym=target.nym) if not shoulderror(amount): c.deposit(target) prepared_accounts.assert_balances(-100, 100 - amount, amount) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_deposit_twice(self, prepared_accounts, amount, first_valid, later_income, second_valid): # create cheque and try to deposit it cheque = new_cheque(prepared_accounts.source, prepared_accounts.target, amount) with error.expected(None if first_valid else ReturnValueError): transfer(cheque, prepared_accounts.source, prepared_accounts.target) expected_source = 100 expected_target = 0 if (first_valid): expected_source -= amount expected_target += amount prepared_accounts.assert_balances(-100, expected_source, expected_target) # now transfer more funds to source if later_income != 0: income = new_cheque(prepared_accounts.issuer, prepared_accounts.source, later_income) transfer(income, prepared_accounts.issuer, prepared_accounts.source) expected_source += later_income # and repeat cheque deposit with error.expected(None if second_valid else ReturnValueError): deposit = cheque.deposit(prepared_accounts.target.nym, prepared_accounts.target) if second_valid: expected_source -= amount expected_target += amount assert is_message_success(deposit) prepared_accounts.assert_balances(-100 - later_income, expected_source, expected_target)
def test_invoice(prepared_accounts, amount, should_pass): '''an invoice is just a cheque for a negative amount, so the target will invoice the source by writing him a cheque for a negative amount. After a successful deposit, funds move from source to target. ''' invoice = new_cheque(prepared_accounts.target, prepared_accounts.source, amount) invoice.write() with error.expected(None if should_pass else ReturnValueError): invoice.deposit(prepared_accounts.source.nym, prepared_accounts.source) prepared_accounts.assert_balances(-100, 100 + amount if should_pass else 100, -amount if should_pass else 0)
def test_simple_transfer(self, prepared_accounts, amount, should_pass, instrument_constructor): instrument = instrument_constructor( prepared_accounts.source, prepared_accounts.target, amount) if instrument_constructor == new_cash: # skip if testing with large amount if amount > 1000: pytest.skip("https://github.com/Open-Transactions/opentxs/issues/536") # make sure there's a mint created for this asset cash.create_mint(prepared_accounts.asset) with error.expected(None if should_pass else ReturnValueError): transfer(instrument, prepared_accounts.source, prepared_accounts.target) if should_pass: prepared_accounts.assert_balances(-100, 100 - amount, amount) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_simple_transfer(self, prepared_accounts, amount, should_pass, instrument_constructor): instrument = instrument_constructor(prepared_accounts.source, prepared_accounts.target, amount) if instrument_constructor == new_cash: # skip if testing with large amount if amount > 1000: pytest.skip( "https://github.com/Open-Transactions/opentxs/issues/536") # make sure there's a mint created for this asset cash.create_mint(prepared_accounts.asset) with error.expected(None if should_pass else ReturnValueError): transfer(instrument, prepared_accounts.source, prepared_accounts.target) if should_pass: prepared_accounts.assert_balances(-100, 100 - amount, amount) else: prepared_accounts.assert_balances(-100, 100, 0)
def test_check_nym_unregistered(): me = Nym().register() with error.expected(ReturnValueError): nym.check(server.first_active_id(), me._id, Nym().create()._id)
def test_account_nym_not_registered(): nym = Nym().register() asset = Asset().issue(nym, open(data.btc_contract_file)) with error.expected(ReturnValueError): account.Account(asset, Nym().create()).create()
def test_delete_nonempty_nym(prepared_accounts): with error.expected(ReturnValueError): prepared_accounts.source.nym.delete()
def test_delete_issuer_nym(prepared_accounts): with error.expected(ReturnValueError): prepared_accounts.issuer.nym.delete()
def test_check_nym_unregistered(): me = Nym().register() with error.expected(ReturnValueError): nym.check(server.first_active_id(), me._id, Nym().create()._id)
def test_double_delete(): n = Nym().register() n.delete() with error.expected(ReturnValueError): n.delete()
def test_asset_nym_not_registered(): with error.expected(ReturnValueError): Asset().issue(Nym().create(), open(data.btc_contract_file))
def test_delete_nonempty_nym(prepared_accounts): with error.expected(ReturnValueError): prepared_accounts.source.nym.delete()
def test_asset_nym_not_registered(): with error.expected(ReturnValueError): Asset().issue(Nym().create(), open(data.btc_contract_file))
def test_issue_asset_contract(issue_for_other_nym, expect_success): server_id = server.first_active_id() nym = Nym().register(server_id) issue_for_nym = Nym().register(server_id) if issue_for_other_nym else nym with error.expected(None if expect_success else ReturnValueError): Asset().issue(nym, open(data.btc_contract_file), server_id, issue_for_nym=issue_for_nym)
def test_account_nym_not_registered(): nym = Nym().register() asset = Asset().issue(nym, open(data.btc_contract_file)) with error.expected(ReturnValueError): account.Account(asset, Nym().create()).create()
def test_delete_issuer_nym(prepared_accounts): with error.expected(ReturnValueError): prepared_accounts.issuer.nym.delete()
def test_double_delete(): n = Nym().register() n.delete() with error.expected(ReturnValueError): n.delete()