diff options
Diffstat (limited to 'packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts')
-rw-r--r-- | packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts | 160 |
1 files changed, 125 insertions, 35 deletions
diff --git a/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts b/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts index 4062e186d..3b1f4bf27 100644 --- a/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts +++ b/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts @@ -20,39 +20,82 @@ import { AmountString, Duration, + j2s, PaymentInsufficientBalanceDetails, TalerErrorCode, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; import { + ExchangeService, + getTestHarnessPaytoForLabel, GlobalTestState, - generateRandomPayto, + HarnessExchangeBankAccount, setupDb, } from "../harness/harness.js"; -import { createSimpleTestkudosEnvironmentV3, withdrawViaBankV3 } from "../harness/helpers.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; export async function runWalletInsufficientBalanceTest(t: GlobalTestState) { // Set up test environment - const db = await setupDb(t); - const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); - let { - bankClient, - exchange, - merchant, - walletService, - walletClient, - } = await createSimpleTestkudosEnvironmentV3(t, coinConfig, { - skipWireFeeCreation: true, + let { bankClient, bank, exchange, merchant, walletClient } = + await createSimpleTestkudosEnvironmentV3(t, coinConfig, { + skipWireFeeCreation: true, + }); + + const dbTwo = await setupDb(t, { + nameSuffix: "two", }); + const exchangeTwo = ExchangeService.create(t, { + name: "testexchange-2", + currency: "TESTKUDOS", + httpPort: 9081, + database: dbTwo.connStr, + }); + + { + const receiverName = "Exchange2"; + const exchangeBankUsername = "exchange2"; + const exchangeBankPassword = "mypw"; + const exchangePaytoUri = getTestHarnessPaytoForLabel(exchangeBankUsername); + const wireGatewayApiBaseUrl = new URL( + `accounts/${exchangeBankUsername}/taler-wire-gateway/`, + bank.corebankApiBaseUrl, + ).href; + + const exchangeBankAccount: HarnessExchangeBankAccount = { + wireGatewayApiBaseUrl, + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + accountPaytoUri: exchangePaytoUri, + skipWireFeeCreation: true, + }; + + await exchangeTwo.addBankAccount("1", exchangeBankAccount); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + exchangeTwo.addCoinConfigList(coinConfig); + + await exchangeTwo.start(); + } + await merchant.addInstanceWithWireAccount({ id: "default", name: "Default Instance", - paytoUris: [generateRandomPayto("merchant-default")], + paytoUris: [getTestHarnessPaytoForLabel("merchant-default")], defaultWireTransferDelay: Duration.toTalerProtocolDuration( Duration.fromSpec({ minutes: 1 }), ), @@ -61,7 +104,7 @@ export async function runWalletInsufficientBalanceTest(t: GlobalTestState) { await merchant.addInstanceWithWireAccount({ id: "minst1", name: "minst1", - paytoUris: [generateRandomPayto("minst1")], + paytoUris: [getTestHarnessPaytoForLabel("minst1")], defaultWireTransferDelay: Duration.toTalerProtocolDuration( Duration.fromSpec({ minutes: 1 }), ), @@ -75,6 +118,8 @@ export async function runWalletInsufficientBalanceTest(t: GlobalTestState) { }, }); + t.logStep("setup-done"); + const wres = await withdrawViaBankV3(t, { amount: "TESTKUDOS:10", bankClient, @@ -83,29 +128,74 @@ export async function runWalletInsufficientBalanceTest(t: GlobalTestState) { }); await wres.withdrawalFinishedCond; - const exc = await t.assertThrowsTalerErrorAsync(async () => { - await walletClient.call(WalletApiOperation.PrepareDeposit, { - amount: "TESTKUDOS:5" as AmountString, - depositPaytoUri: "payto://x-taler-bank/localhost/foobar", + { + const exc = await t.assertThrowsTalerErrorAsync(async () => { + await walletClient.call(WalletApiOperation.CheckDeposit, { + amount: "TESTKUDOS:5" as AmountString, + depositPaytoUri: "payto://x-taler-bank/localhost/foobar", + }); + }); + + t.assertDeepEqual( + exc.errorDetail.code, + TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE, + ); + + const insufficientBalanceDetails: PaymentInsufficientBalanceDetails = + exc.errorDetail.insufficientBalanceDetails; + + t.assertAmountEquals( + insufficientBalanceDetails.balanceAvailable, + "TESTKUDOS:9.72", + ); + t.assertAmountEquals( + insufficientBalanceDetails.balanceExchangeDepositable, + "TESTKUDOS:0", + ); + } + + t.logStep("start-p2p-push-test"); + + // Now check for p2p-push + + { + const wres2 = await withdrawViaBankV3(t, { + amount: "TESTKUDOS:5", + bankClient, + exchange: exchangeTwo, + walletClient, + }); + await wres2.withdrawalFinishedCond; + + const exc = await t.assertThrowsTalerErrorAsync(async () => { + await walletClient.call(WalletApiOperation.CheckPeerPushDebit, { + amount: "TESTKUDOS:20" as AmountString, + }); }); - }); - t.assertDeepEqual( - exc.errorDetail.code, - TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE, - ); - - const insufficientBalanceDetails: PaymentInsufficientBalanceDetails = - exc.errorDetail.insufficientBalanceDetails; - - t.assertAmountEquals( - insufficientBalanceDetails.balanceAvailable, - "TESTKUDOS:9.72", - ); - t.assertAmountEquals( - insufficientBalanceDetails.balanceExchangeDepositable, - "TESTKUDOS:0", - ); + const insufficientBalanceDetails: PaymentInsufficientBalanceDetails = + exc.errorDetail.insufficientBalanceDetails; + + const perMyExchange = + insufficientBalanceDetails.perExchange[exchange.baseUrl]; + + t.assertTrue(!!perMyExchange); + + console.log(j2s(exc.errorDetail)); + + t.assertAmountEquals( + insufficientBalanceDetails.amountRequested, + "TESTKUDOS:20", + ); + t.assertAmountEquals( + insufficientBalanceDetails.maxEffectiveSpendAmount, + "TESTKUDOS:14.22", + ); + t.assertAmountEquals( + perMyExchange.maxEffectiveSpendAmount, + "TESTKUDOS:9.47", + ); + } } runWalletInsufficientBalanceTest.suites = ["wallet"]; |