diff options
Diffstat (limited to 'packages/taler-harness/src/integrationtests')
94 files changed, 16352 insertions, 0 deletions
diff --git a/packages/taler-harness/src/integrationtests/test-age-restrictions-deposit.ts b/packages/taler-harness/src/integrationtests/test-age-restrictions-deposit.ts new file mode 100644 index 000000000..a0e97c218 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-age-restrictions-deposit.ts @@ -0,0 +1,114 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + NotificationType, + TransactionMajorState, + TransactionMinorState, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, generateRandomPayto } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runAgeRestrictionsDepositTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3( + t, + defaultCoinConfig.map((x) => x("TESTKUDOS")), + { + ageMaskSpec: "8:10:12:14:16:18:21", + }, + ); + + // Withdraw digital cash into the wallet. + + const withdrawalResult = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await withdrawalResult.withdrawalFinishedCond; + + const dgIdResp = await walletClient.client.call( + WalletApiOperation.GenerateDepositGroupTxId, + {}, + ); + + const depositTxId = dgIdResp.transactionId; + + const depositTrack = walletClient.waitForNotificationCond( + (n) => + n.type == NotificationType.TransactionStateTransition && + n.transactionId == depositTxId && + n.newTxState.major == TransactionMajorState.Pending && + n.newTxState.minor == TransactionMinorState.Track, + ); + + const depositDone = walletClient.waitForNotificationCond( + (n) => + n.type == NotificationType.TransactionStateTransition && + n.transactionId == depositTxId && + n.newTxState.major == TransactionMajorState.Done, + ); + + const depositGroupResult = await walletClient.client.call( + WalletApiOperation.CreateDepositGroup, + { + amount: "TESTKUDOS:10" as AmountString, + depositPaytoUri: generateRandomPayto("foo"), + transactionId: depositTxId, + }, + ); + + t.assertDeepEqual(depositGroupResult.transactionId, depositTxId); + + await depositTrack; + + await exchange.runAggregatorOnceWithTimetravel({ + timetravelMicroseconds: 1000 * 1000 * 60 * 60 * 3, + }); + + await depositDone; + + const transactions = await walletClient.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log("transactions", JSON.stringify(transactions, undefined, 2)); + t.assertDeepEqual(transactions.transactions[0].type, "withdrawal"); + t.assertDeepEqual(transactions.transactions[1].type, "deposit"); + // The raw amount is what ends up on the bank account, which includes + // deposit and wire fees. + t.assertDeepEqual(transactions.transactions[1].amountRaw, "TESTKUDOS:9.79"); +} + +runAgeRestrictionsDepositTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-age-restrictions-merchant.ts b/packages/taler-harness/src/integrationtests/test-age-restrictions-merchant.ts new file mode 100644 index 000000000..85bd96034 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-age-restrictions-merchant.ts @@ -0,0 +1,174 @@ +/* + This file is part of GNU Taler + (C) 2022-2023 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { AmountString, TalerMerchantApi } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runAgeRestrictionsMerchantTest(t: GlobalTestState) { + // Set up test environment + + const { + walletClient: walletClientOne, + bankClient, + exchange, + merchant, + exchangeBankAccount, + } = await createSimpleTestkudosEnvironmentV3( + t, + defaultCoinConfig.map((x) => x("TESTKUDOS")), + { + ageMaskSpec: "8:10:12:14:16:18:21", + }, + ); + + const { walletClient: walletClientTwo } = await createWalletDaemonWithClient( + t, + { + name: "w2", + }, + ); + + const { walletClient: walletClientThree } = + await createWalletDaemonWithClient(t, { + name: "w3", + }); + + { + const { walletClient: walletClientZero } = + await createWalletDaemonWithClient(t, { + name: "w0", + }); + + const wres = await withdrawViaBankV3(t, { + walletClient: walletClientZero, + bankClient, + exchange, + amount: "TESTKUDOS:20" as AmountString, + restrictAge: 13, + }); + await wres.withdrawalFinishedCond; + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + minimum_age: 9, + }; + + await makeTestPaymentV2(t, { + walletClient: walletClientZero, + merchant, + order, + }); + await walletClientZero.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + } + + { + const walletClient = walletClientOne; + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + restrictAge: 13, + }); + await wres.withdrawalFinishedCond; + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + minimum_age: 9, + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + } + + { + const walletClient = walletClientTwo; + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20" as AmountString, + restrictAge: 13, + }); + await wres.withdrawalFinishedCond; + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + } + + { + const walletClient = walletClientThree; + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20" as AmountString, + }); + await wres.withdrawalFinishedCond; + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + minimum_age: 9, + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + } +} + +runAgeRestrictionsMerchantTest.suites = ["wallet"]; +runAgeRestrictionsMerchantTest.timeoutMs = 120 * 1000; diff --git a/packages/taler-harness/src/integrationtests/test-age-restrictions-mixed-merchant.ts b/packages/taler-harness/src/integrationtests/test-age-restrictions-mixed-merchant.ts new file mode 100644 index 000000000..e822b15d8 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-age-restrictions-mixed-merchant.ts @@ -0,0 +1,129 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { AmountString, TalerMerchantApi } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runAgeRestrictionsMixedMerchantTest(t: GlobalTestState) { + // Set up test environment + + const { + walletClient: walletOne, + bankClient, + exchange, + merchant, + } = await createSimpleTestkudosEnvironmentV3( + t, + defaultCoinConfig.map((x) => x("TESTKUDOS")), + { + ageMaskSpec: "8:10:12:14:16:18:21", + mixedAgeRestriction: true, + }, + ); + + const { walletClient: walletTwo } = await createWalletDaemonWithClient(t, { + name: "w2", + }); + + const { walletClient: walletThree } = await createWalletDaemonWithClient(t, { + name: "w3", + }); + + { + const walletClient = walletOne; + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20" as AmountString, + restrictAge: 13, + }); + + await wres.withdrawalFinishedCond; + + const order = { + summary: "Buy me!", + amount: "TESTKUDOS:5" as AmountString, + fulfillment_url: "taler://fulfillment-success/thx", + minimum_age: 9, + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + } + + { + const wres = await withdrawViaBankV3(t, { + walletClient: walletTwo, + bankClient, + exchange, + amount: "TESTKUDOS:20" as AmountString, + restrictAge: 13, + }); + + await wres.withdrawalFinishedCond; + + const order = { + summary: "Buy me!", + amount: "TESTKUDOS:5" as AmountString, + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient: walletTwo, merchant, order }); + await walletTwo.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + } + + { + const wres = await withdrawViaBankV3(t, { + walletClient: walletThree, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + minimum_age: 9, + }; + + await makeTestPaymentV2(t, { walletClient: walletThree, merchant, order }); + await walletThree.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + } +} + +runAgeRestrictionsMixedMerchantTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-age-restrictions-peer.ts b/packages/taler-harness/src/integrationtests/test-age-restrictions-peer.ts new file mode 100644 index 000000000..c9faa586a --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-age-restrictions-peer.ts @@ -0,0 +1,134 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runAgeRestrictionsPeerTest(t: GlobalTestState) { + // Set up test environment + + const { bankClient, exchange } = await createSimpleTestkudosEnvironmentV3( + t, + defaultCoinConfig.map((x) => x("TESTKUDOS")), + { + ageMaskSpec: "8:10:12:14:16:18:21", + }, + ); + + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + }); + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + persistent: true, + }); + + const wallet1 = w1.walletClient; + const wallet2 = w2.walletClient; + + { + const withdrawalRes = await withdrawViaBankV3(t, { + walletClient: wallet1, + bankClient, + exchange, + amount: "TESTKUDOS:20", + restrictAge: 13, + }); + + await withdrawalRes.withdrawalFinishedCond; + + const purse_expiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const initResp = await wallet1.client.call( + WalletApiOperation.InitiatePeerPushDebit, + { + partialContractTerms: { + summary: "Hello, World", + amount: "TESTKUDOS:1" as AmountString, + purse_expiration, + }, + }, + ); + + const peerPushReadyCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready && + x.transactionId === initResp.transactionId, + ); + + await peerPushReadyCond; + + const txDetails = await wallet1.call( + WalletApiOperation.GetTransactionById, + { + transactionId: initResp.transactionId, + }, + ); + t.assertDeepEqual(txDetails.type, TransactionType.PeerPushDebit); + t.assertTrue(!!txDetails.talerUri); + + const checkResp = await wallet2.call( + WalletApiOperation.PreparePeerPushCredit, + { + talerUri: txDetails.talerUri, + }, + ); + + await wallet2.call(WalletApiOperation.ConfirmPeerPushCredit, { + transactionId: checkResp.transactionId, + }); + + const peerPullCreditDoneCond = wallet2.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Done && + x.transactionId === checkResp.transactionId, + ); + + await peerPullCreditDoneCond; + } +} + +runAgeRestrictionsPeerTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-bank-api.ts b/packages/taler-harness/src/integrationtests/test-bank-api.ts new file mode 100644 index 000000000..58f8bb106 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-bank-api.ts @@ -0,0 +1,169 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + TalerCorebankApiClient, + CreditDebitIndicator, + WireGatewayApiClient, + createEddsaKeyPair, + encodeCrock, +} from "@gnu-taler/taler-util"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runBankApiTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + currency: "TESTKUDOS", + httpPort: 8082, + database: db.connStr, + allowRegistrations: true, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + let wireGatewayApiBaseUrl = new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href; + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + exchange.addOfferedCoins(defaultCoinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const bankUser = await bankClient.registerAccount("user1", "pw1"); + + // Make sure that registering twice results in a 409 Conflict + { + const e = await t.assertThrowsTalerErrorAsync(async () => { + await bankClient.registerAccount("user1", "pw2"); + }); + t.assertTrue(e.errorDetail.httpStatusCode === 409); + } + + let balResp = await bankClient.getAccountBalance(bankUser.username); + + console.log(balResp); + + // Check that we got the sign-up bonus. + t.assertAmountEquals(balResp.balance.amount, "TESTKUDOS:100"); + t.assertTrue( + balResp.balance.credit_debit_indicator === CreditDebitIndicator.Credit, + ); + + const res = createEddsaKeyPair(); + + const wireGatewayApiClient = new WireGatewayApiClient( + wireGatewayApiBaseUrl, + { + auth: { + username: "admin", + password: "adminpw", + }, + }, + ); + + await wireGatewayApiClient.adminAddIncoming({ + amount: "TESTKUDOS:115", + debitAccountPayto: bankUser.accountPaytoUri, + reservePub: encodeCrock(res.eddsaPub), + }); + + balResp = await bankClient.getAccountBalance(bankUser.username); + t.assertAmountEquals(balResp.balance.amount, "TESTKUDOS:15"); + t.assertTrue( + balResp.balance.credit_debit_indicator === CreditDebitIndicator.Debit, + ); +} + +runBankApiTest.suites = ["fakebank"] diff --git a/packages/taler-harness/src/integrationtests/test-claim-loop.ts b/packages/taler-harness/src/integrationtests/test-claim-loop.ts new file mode 100644 index 000000000..01be6ea80 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-claim-loop.ts @@ -0,0 +1,82 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { URL } from "url"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; +import { MerchantApiClient } from "@gnu-taler/taler-util"; + +/** + * Run test for the merchant's order lifecycle. + * + * FIXME: Is this test still necessary? We initially wrote if to confirm/document + * assumptions about how the merchant should work. + */ +export async function runClaimLoopTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Set up order. + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }, + }); + + // Query private order status before claiming it. + let orderStatusBefore = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + t.assertTrue(orderStatusBefore.order_status === "unpaid"); + let statusUrlBefore = new URL(orderStatusBefore.order_status_url); + + // Make wallet claim the unpaid order. + t.assertTrue(orderStatusBefore.order_status === "unpaid"); + const talerPayUri = orderStatusBefore.taler_pay_uri; + await walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri, + }); + + // Query private order status after claiming it. + let orderStatusAfter = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + t.assertTrue(orderStatusAfter.order_status === "claimed"); + + await t.shutdown(); +} + +runClaimLoopTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-clause-schnorr.ts b/packages/taler-harness/src/integrationtests/test-clause-schnorr.ts new file mode 100644 index 000000000..c104edc85 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-clause-schnorr.ts @@ -0,0 +1,104 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { TalerMerchantApi } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runClauseSchnorrTest(t: GlobalTestState) { + // Set up test environment + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => { + return { + ...x("TESTKUDOS"), + cipher: "CS", + }; + }); + + // We need to have at least one RSA denom configured + coinConfig.push({ + cipher: "RSA", + rsaKeySize: 1024, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:42", + value: "TESTKUDOS:0.0001", + feeWithdraw: "TESTKUDOS:42", + feeRefresh: "TESTKUDOS:42", + feeRefund: "TESTKUDOS:42", + name: "rsa_dummy", + }); + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t, coinConfig); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres.withdrawalFinishedCond; + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Test JSON normalization of contract terms: Does the wallet + // agree with the merchant? + const order2: TalerMerchantApi.Order = { + summary: "Testing “unicode” characters", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order: order2 }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Test JSON normalization of contract terms: Does the wallet + // agree with the merchant? + const order3: TalerMerchantApi.Order = { + summary: "Testing\nNewlines\rAnd\tStuff\nHere\b", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order: order3 }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runClauseSchnorrTest.suites = ["experimental-wallet"]; +runClauseSchnorrTest.experimental = true; diff --git a/packages/taler-harness/src/integrationtests/test-currency-scope.ts b/packages/taler-harness/src/integrationtests/test-currency-scope.ts new file mode 100644 index 000000000..69e45f678 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-currency-scope.ts @@ -0,0 +1,191 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { Duration, j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + FakebankService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runCurrencyScopeTest(t: GlobalTestState) { + // Set up test environment + const dbDefault = await setupDb(t); + + const dbExchangeTwo = await setupDb(t, { + nameSuffix: "exchange2", + }); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: dbDefault.connStr, + httpPort: 8082, + }); + + const exchangeOne = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: dbDefault.connStr, + }); + + const exchangeTwo = ExchangeService.create(t, { + name: "testexchange-2", + currency: "TESTKUDOS", + httpPort: 8281, + database: dbExchangeTwo.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: dbDefault.connStr, + }); + + const exchangeOneBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + await exchangeOne.addBankAccount("1", exchangeOneBankAccount); + + const exchangeTwoBankAccount = await bank.createExchangeAccount( + "myexchange2", + "x", + ); + await exchangeTwo.addBankAccount("1", exchangeTwoBankAccount); + + bank.setSuggestedExchange( + exchangeOne, + exchangeOneBankAccount.accountPaytoUri, + ); + + await bank.start(); + + await bank.pingUntilAvailable(); + + // Set up the first exchange + + exchangeOne.addOfferedCoins(defaultCoinConfig); + await exchangeOne.start(); + await exchangeOne.pingUntilAvailable(); + + // Set up the second exchange + + exchangeTwo.addOfferedCoins(defaultCoinConfig); + await exchangeTwo.start(); + await exchangeTwo.pingUntilAvailable(); + + // Start and configure merchant + + merchant.addExchange(exchangeOne); + merchant.addExchange(exchangeTwo); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "wallet", + }); + + console.log("setup done!"); + + // Withdraw digital cash into the wallet. + + const w1 = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: exchangeOne, + amount: "TESTKUDOS:6", + }); + + const w2 = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: exchangeTwo, + amount: "TESTKUDOS:6", + }); + + await w1.withdrawalFinishedCond; + await w2.withdrawalFinishedCond; + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(j2s(bal)); + + // Separate balances, exchange-scope. + t.assertDeepEqual(bal.balances.length, 2); + + await walletClient.call(WalletApiOperation.AddGlobalCurrencyExchange, { + currency: "TESTKUDOS", + exchangeBaseUrl: exchangeOne.baseUrl, + exchangeMasterPub: exchangeOne.masterPub, + }); + + await walletClient.call(WalletApiOperation.AddGlobalCurrencyExchange, { + currency: "TESTKUDOS", + exchangeBaseUrl: exchangeTwo.baseUrl, + exchangeMasterPub: exchangeTwo.masterPub, + }); + + const ex = walletClient.call( + WalletApiOperation.ListGlobalCurrencyExchanges, + {}, + ); + console.log("global currency exchanges:"); + console.log(j2s(ex)); + + const bal2 = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(j2s(bal2)); + + // Global currencies are merged + t.assertDeepEqual(bal2.balances.length, 1); +} + +runCurrencyScopeTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-denom-lost.ts b/packages/taler-harness/src/integrationtests/test-denom-lost.ts new file mode 100644 index 000000000..b57518437 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-denom-lost.ts @@ -0,0 +1,81 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for refreshe after a payment. + */ +export async function runDenomLostTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + const dsBefore = await walletClient.call( + WalletApiOperation.TestingGetDenomStats, + { + exchangeBaseUrl: exchange.baseUrl, + }, + ); + + t.assertDeepEqual(dsBefore.numLost, 0); + t.assertDeepEqual(dsBefore.numOffered, dsBefore.numKnown); + + await exchange.stop(); + + await exchange.purgeSecmodKeys(); + + await exchange.start(); + + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchange.baseUrl, + force: true, + }); + + const dsAfter = await walletClient.call( + WalletApiOperation.TestingGetDenomStats, + { + exchangeBaseUrl: exchange.baseUrl, + }, + ); + + // All previous denominations were lost + t.assertDeepEqual(dsBefore.numOffered, dsAfter.numLost); + // But we have new ones! + t.assertTrue(dsAfter.numKnown > dsBefore.numKnown); +} + +runDenomLostTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-denom-unoffered.ts b/packages/taler-harness/src/integrationtests/test-denom-unoffered.ts new file mode 100644 index 000000000..8042c0817 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-denom-unoffered.ts @@ -0,0 +1,164 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + MerchantApiClient, + PreparePayResultType, + TalerErrorCode, + TalerMerchantApi, + TransactionType, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +export async function runDenomUnofferedTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Make the exchange forget the denomination. + // Effectively we completely reset the exchange, + // but keep the exchange master public key. + + await merchant.stop(); + + await exchange.stop(); + await exchange.purgeDatabase(); + await exchange.purgeSecmodKeys(); + await exchange.start(); + await exchange.pingUntilAvailable(); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const orderResp = await merchantClient.createOrder({ + order: order, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + const confirmResp = await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + const tx = await walletClient.call(WalletApiOperation.GetTransactionById, { + transactionId: confirmResp.transactionId, + }); + + t.assertTrue(tx.error != null); + t.assertTrue( + tx.error.code === TalerErrorCode.WALLET_PAY_MERCHANT_SERVER_ERROR, + ); + + const merchantErrorCode = (tx.error as any).requestError.errorResponse.code; + + t.assertDeepEqual( + merchantErrorCode, + TalerErrorCode.MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS, + ); + + const exchangeErrorCode = (tx.error as any).requestError.errorResponse + .exchange_ec; + + t.assertDeepEqual( + exchangeErrorCode, + TalerErrorCode.EXCHANGE_GENERIC_DENOMINATION_KEY_UNKNOWN, + ); + + // Depending on whether the merchant has seen the new denominations or not, + // the error code might be different here. + // t.assertDeepEqual( + // merchantErrorCode, + // TalerErrorCode.MERCHANT_POST_ORDERS_ID_PAY_DENOMINATION_KEY_NOT_FOUND, + // ); + + // Force updating the exchange entry so that the wallet knows about the new denominations. + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchange.baseUrl, + force: true, + }); + + await walletClient.call(WalletApiOperation.DeleteTransaction, { + transactionId: confirmResp.transactionId, + }); + + // Now withdrawal should work again. + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const txs = await walletClient.call(WalletApiOperation.GetTransactions, { + sort: "stable-ascending", + includeRefreshes: true, + }); + console.log(JSON.stringify(txs, undefined, 2)); + + t.assertDeepEqual(txs.transactions[0].type, TransactionType.Withdrawal); + t.assertDeepEqual(txs.transactions[1].type, TransactionType.Refresh); + t.assertDeepEqual(txs.transactions[2].type, TransactionType.DenomLoss); + t.assertDeepEqual(txs.transactions[3].type, TransactionType.Withdrawal); +} + +runDenomUnofferedTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-deposit.ts b/packages/taler-harness/src/integrationtests/test-deposit.ts new file mode 100644 index 000000000..0879c9e9f --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-deposit.ts @@ -0,0 +1,122 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + NotificationType, + TransactionMajorState, + TransactionMinorState, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, generateRandomPayto } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runDepositTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const withdrawalResult = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await withdrawalResult.withdrawalFinishedCond; + + const dgIdResp = await walletClient.client.call( + WalletApiOperation.GenerateDepositGroupTxId, + {}, + ); + + const depositTxId = dgIdResp.transactionId; + + const depositTrack = walletClient.waitForNotificationCond( + (n) => + n.type == NotificationType.TransactionStateTransition && + n.transactionId == depositTxId && + n.newTxState.major == TransactionMajorState.Pending && + n.newTxState.minor == TransactionMinorState.Track, + ); + + const depositDone = walletClient.waitForNotificationCond( + (n) => + n.type == NotificationType.TransactionStateTransition && + n.transactionId == depositTxId && + n.newTxState.major == TransactionMajorState.Done, + ); + + const depositGroupResult = await walletClient.client.call( + WalletApiOperation.CreateDepositGroup, + { + amount: "TESTKUDOS:10" as AmountString, + depositPaytoUri: generateRandomPayto("foo"), + transactionId: depositTxId, + }, + ); + + t.assertDeepEqual(depositGroupResult.transactionId, depositTxId); + + const balDuring = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(`balances during deposit: ${j2s(balDuring)}`); + t.assertAmountEquals(balDuring.balances[0].pendingOutgoing, "TESTKUDOS:10"); + + await depositTrack; + + t.logStep("before-aggregator"); + + await exchange.runAggregatorOnceWithTimetravel({ + timetravelMicroseconds: 1000 * 1000 * 60 * 60 * 3, + }); + + await exchange.runTransferOnceWithTimetravel({ + timetravelMicroseconds: 1000 * 1000 * 60 * 60 * 3, + }); + + await depositDone; + + const transactions = await walletClient.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log("transactions", JSON.stringify(transactions, undefined, 2)); + t.assertDeepEqual(transactions.transactions[0].type, "withdrawal"); + t.assertDeepEqual(transactions.transactions[1].type, "deposit"); + // The raw amount is what ends up on the bank account, which includes + // deposit and wire fees. + t.assertDeepEqual(transactions.transactions[1].amountRaw, "TESTKUDOS:9.79"); + + const balAfter = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(`balances after deposit: ${j2s(balAfter)}`); + t.assertAmountEquals(balAfter.balances[0].pendingOutgoing, "TESTKUDOS:0"); +} + +runDepositTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-exchange-deposit.ts b/packages/taler-harness/src/integrationtests/test-exchange-deposit.ts new file mode 100644 index 000000000..47a17a1f2 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-exchange-deposit.ts @@ -0,0 +1,159 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + encodeCrock, + getRandomBytes, + j2s, + TalerError, +} from "@gnu-taler/taler-util"; +import { createPlatformHttpLib } from "@gnu-taler/taler-util/http"; +import { + CryptoDispatcher, + SynchronousCryptoWorkerFactoryPlain, +} from "@gnu-taler/taler-wallet-core"; +import { + checkReserve, + depositCoin, + downloadExchangeInfo, + findDenomOrThrow, + topupReserveWithBank, + withdrawCoin, +} from "@gnu-taler/taler-wallet-core/dbless"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV2 } from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runExchangeDepositTest(t: GlobalTestState) { + // Set up test environment + + const { bank, exchange } = await createSimpleTestkudosEnvironmentV2(t); + + const http = createPlatformHttpLib({ + enableThrottling: false, + }); + const cryptiDisp = new CryptoDispatcher( + new SynchronousCryptoWorkerFactoryPlain(), + ); + const cryptoApi = cryptiDisp.cryptoApi; + + try { + // Withdraw digital cash into the wallet. + + const exchangeInfo = await downloadExchangeInfo(exchange.baseUrl, http); + + const reserveKeyPair = await cryptoApi.createEddsaKeypair({}); + + await topupReserveWithBank({ + http, + amount: "TESTKUDOS:10" as AmountString, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeInfo, + reservePub: reserveKeyPair.pub, + }); + + await exchange.runWirewatchOnce(); + + await checkReserve(http, exchange.baseUrl, reserveKeyPair.pub); + + const d1 = findDenomOrThrow( + exchangeInfo, + "TESTKUDOS:8" as AmountString, + {}, + ); + + const coin = await withdrawCoin({ + http, + cryptoApi, + reserveKeyPair: { + reservePriv: reserveKeyPair.priv, + reservePub: reserveKeyPair.pub, + }, + denom: d1, + exchangeBaseUrl: exchange.baseUrl, + }); + + const wireSalt = encodeCrock(getRandomBytes(16)); + const merchantPub = encodeCrock(getRandomBytes(32)); + const contractTermsHash = encodeCrock(getRandomBytes(64)); + + await depositCoin({ + contractTermsHash, + merchantPub, + wireSalt, + amount: "TESTKUDOS:4" as AmountString, + coin: coin, + cryptoApi, + exchangeBaseUrl: exchange.baseUrl, + http, + }); + + // Idempotency + await depositCoin({ + contractTermsHash, + merchantPub, + wireSalt, + amount: "TESTKUDOS:4" as AmountString, + coin: coin, + cryptoApi, + exchangeBaseUrl: exchange.baseUrl, + http, + }); + + try { + // Non-idempotent request with different amount + await depositCoin({ + contractTermsHash, + merchantPub, + wireSalt, + amount: "TESTKUDOS:3.5" as AmountString, + coin: coin, + cryptoApi, + exchangeBaseUrl: exchange.baseUrl, + http, + }); + } catch (e) { + if (e instanceof TalerError && e.errorDetail.code === 7005) { + if (e.errorDetail.httpStatusCode === 409) { + console.log("got expected error response from exchange"); + console.log(e); + console.log(j2s(e.errorDetail)); + } else { + console.log("did not expect deposit error from exchange"); + throw e; + } + } else { + throw e; + } + } + } catch (e) { + if (e instanceof TalerError) { + console.log(e); + console.log(j2s(e.errorDetail)); + } else { + console.log(e); + } + throw e; + } +} + +runExchangeDepositTest.suites = ["exchange"]; diff --git a/packages/taler-harness/src/integrationtests/test-exchange-management-fault.ts b/packages/taler-harness/src/integrationtests/test-exchange-management-fault.ts new file mode 100644 index 000000000..801162ac8 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-exchange-management-fault.ts @@ -0,0 +1,302 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ExchangesListResponse, + TalerCorebankApiClient, + TalerErrorCode, + URL, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + FaultInjectedExchangeService, + FaultInjectionResponseContext, +} from "../harness/faultInjection.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + WalletCli, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; + +/** + * Test if the wallet handles outdated exchange versions correctly. + */ +export async function runExchangeManagementFaultTest( + t: GlobalTestState, +): Promise<void> { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + const faultyExchange = new FaultInjectedExchangeService(t, exchange, 8091); + // Base URL must contain port that the proxy is listening on. + await exchange.modifyConfig(async (config) => { + config.setString("exchange", "base_url", "http://localhost:8091/"); + }); + + bank.setSuggestedExchange( + faultyExchange, + exchangePaytoUri, + ); + + await bank.start(); + + await bank.pingUntilAvailable(); + + exchange.addOfferedCoins(defaultCoinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + /* + * ========================================================================= + * Check that the exchange can be added to the wallet + * (without any faults active). + * ========================================================================= + */ + + const wallet = new WalletCli(t); + + let exchangesList: ExchangesListResponse; + + exchangesList = await wallet.client.call( + WalletApiOperation.ListExchanges, + {}, + ); + console.log("exchanges list:", j2s(exchangesList)); + t.assertTrue(exchangesList.exchanges.length === 0); + + // Try before fault is injected + await wallet.client.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: faultyExchange.baseUrl, + }); + + exchangesList = await wallet.client.call( + WalletApiOperation.ListExchanges, + {}, + ); + t.assertTrue(exchangesList.exchanges.length === 1); + + await wallet.client.call(WalletApiOperation.ListExchanges, {}); + + console.log("listing exchanges"); + + exchangesList = await wallet.client.call( + WalletApiOperation.ListExchanges, + {}, + ); + t.assertTrue(exchangesList.exchanges.length === 1); + + console.log("got list", exchangesList); + + /* + * ========================================================================= + * Check what happens if the exchange returns something totally + * bogus for /keys. + * ========================================================================= + */ + + wallet.deleteDatabase(); + + exchangesList = await wallet.client.call( + WalletApiOperation.ListExchanges, + {}, + ); + t.assertTrue(exchangesList.exchanges.length === 0); + + faultyExchange.faultProxy.addFault({ + async modifyResponse(ctx: FaultInjectionResponseContext) { + const url = new URL(ctx.request.requestUrl); + if (url.pathname === "/keys") { + const body = { + version: "whaaat", + }; + ctx.responseBody = Buffer.from(JSON.stringify(body), "utf-8"); + } + }, + }); + + const err1 = await t.assertThrowsTalerErrorAsync(async () => { + await wallet.client.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: faultyExchange.baseUrl, + }); + }); + + console.log("got error", err1); + + // Response is malformed, since it didn't even contain a version code + // in a format the wallet can understand. + t.assertTrue( + err1.errorDetail.code === TalerErrorCode.WALLET_EXCHANGE_UNAVAILABLE, + ); + + exchangesList = await wallet.client.call( + WalletApiOperation.ListExchanges, + {}, + ); + console.log("exchanges list", j2s(exchangesList)); + t.assertTrue(exchangesList.exchanges.length === 1); + t.assertTrue( + exchangesList.exchanges[0].lastUpdateErrorInfo?.error.code === + TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, + ); + + /* + * ========================================================================= + * Check what happens if the exchange returns an old, unsupported + * version for /keys + * ========================================================================= + */ + + wallet.deleteDatabase(); + faultyExchange.faultProxy.clearAllFaults(); + + faultyExchange.faultProxy.addFault({ + async modifyResponse(ctx: FaultInjectionResponseContext) { + const url = new URL(ctx.request.requestUrl); + if (url.pathname === "/keys") { + const keys = ctx.responseBody?.toString("utf-8"); + t.assertTrue(keys != null); + const keysJson = JSON.parse(keys); + keysJson["version"] = "2:0:0"; + ctx.responseBody = Buffer.from(JSON.stringify(keysJson), "utf-8"); + } + }, + }); + + const err2 = await t.assertThrowsTalerErrorAsync(async () => { + await wallet.client.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: faultyExchange.baseUrl, + }); + }); + + t.assertTrue(err2.hasErrorCode(TalerErrorCode.WALLET_EXCHANGE_UNAVAILABLE)); + + exchangesList = await wallet.client.call( + WalletApiOperation.ListExchanges, + {}, + ); + t.assertTrue(exchangesList.exchanges.length === 1); + t.assertTrue( + exchangesList.exchanges[0].lastUpdateErrorInfo?.error.code === + TalerErrorCode.WALLET_EXCHANGE_PROTOCOL_VERSION_INCOMPATIBLE, + ); + + /* + * ========================================================================= + * Check that the exchange version is also checked when + * the exchange is implicitly added via the suggested + * exchange of a bank-integrated withdrawal. + * ========================================================================= + */ + + // Fault from above is still active! + + // Create withdrawal operation + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth({ + username: user.username, + password: user.password, + }); + + const wop = await bankClient.createWithdrawalOperation( + user.username, + "TESTKUDOS:10", + ); + + // Hand it to the wallet + + const wd = await wallet.client.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + // Make sure the faulty exchange isn't used for the suggestion. + t.assertTrue(wd.possibleExchanges.length === 0); +} + +runExchangeManagementFaultTest.suites = ["wallet", "exchange"]; diff --git a/packages/taler-harness/src/integrationtests/test-exchange-management.ts b/packages/taler-harness/src/integrationtests/test-exchange-management.ts new file mode 100644 index 000000000..072e9736d --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-exchange-management.ts @@ -0,0 +1,82 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +/** + * Test if the wallet handles outdated exchange versions correctly. + */ +export async function runExchangeManagementTest( + t: GlobalTestState, +): Promise<void> { + // Set up test environment + + const { walletClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + // Since the default exchanges can change, we start the wallet in tests + // with no built-in defaults. Thus the list of exchanges is empty here. + const exchangesListResult = await walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesListResult.exchanges.length, 0); + + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchange.baseUrl, + force: true, + }); + + const exchangesListResult2 = await walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesListResult2.exchanges.length, 1); + + await walletClient.call(WalletApiOperation.DeleteExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + const exchangesListResult3 = await walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesListResult3.exchanges.length, 0); + + // Check for regression: Can we re-add a deleted exchange? + + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchange.baseUrl, + force: true, + }); + + const exchangesListResult4 = await walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesListResult4.exchanges.length, 1); +} + +runExchangeManagementTest.suites = ["wallet", "exchange"]; diff --git a/packages/taler-harness/src/integrationtests/test-exchange-purse.ts b/packages/taler-harness/src/integrationtests/test-exchange-purse.ts new file mode 100644 index 000000000..6666e2d0b --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-exchange-purse.ts @@ -0,0 +1,224 @@ +/* + This file is part of GNU Taler + (C) 2023 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + ContractTermsUtil, + decodeCrock, + Duration, + encodeCrock, + getRandomBytes, + hash, + j2s, + PeerContractTerms, + TalerError, +} from "@gnu-taler/taler-util"; +import { + CryptoDispatcher, + EncryptContractRequest, + SpendCoinDetails, + SynchronousCryptoWorkerFactoryPlain, +} from "@gnu-taler/taler-wallet-core"; +import { + checkReserve, + downloadExchangeInfo, + findDenomOrThrow, + topupReserveWithBank, + withdrawCoin, +} from "@gnu-taler/taler-wallet-core/dbless"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV2 } from "../harness/helpers.js"; + +/** + * Test the exchange's purse API. + */ +export async function runExchangePurseTest(t: GlobalTestState) { + // Set up test environment + + const { bank, exchange } = await createSimpleTestkudosEnvironmentV2(t); + + const http = harnessHttpLib; + const cryptoDisp = new CryptoDispatcher( + new SynchronousCryptoWorkerFactoryPlain(), + ); + const cryptoApi = cryptoDisp.cryptoApi; + + try { + // Withdraw digital cash into the wallet. + + const exchangeInfo = await downloadExchangeInfo(exchange.baseUrl, http); + + const reserveKeyPair = await cryptoApi.createEddsaKeypair({}); + + let reserveUrl = new URL( + `reserves/${reserveKeyPair.pub}`, + exchange.baseUrl, + ); + reserveUrl.searchParams.set("timeout_ms", "30000"); + const longpollReq = http.fetch(reserveUrl.href, { + method: "GET", + }); + + await topupReserveWithBank({ + amount: "TESTKUDOS:10" as AmountString, + http, + reservePub: reserveKeyPair.pub, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeInfo, + }); + + console.log("waiting for longpoll request"); + const resp = await longpollReq; + console.log(`got response, status ${resp.status}`); + + console.log(exchangeInfo); + + await checkReserve(http, exchange.baseUrl, reserveKeyPair.pub); + + const d1 = findDenomOrThrow( + exchangeInfo, + "TESTKUDOS:8" as AmountString, + {}, + ); + + const coin = await withdrawCoin({ + http, + cryptoApi, + reserveKeyPair: { + reservePriv: reserveKeyPair.priv, + reservePub: reserveKeyPair.pub, + }, + denom: d1, + exchangeBaseUrl: exchange.baseUrl, + }); + + const amount = "TESTKUDOS:5" as AmountString; + + const contractTerms: PeerContractTerms = { + amount, + summary: "Hello", + purse_expiration: AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ minutes: 1 }), + ), + ), + }; + + const mergeReservePair = await cryptoApi.createEddsaKeypair({}); + const pursePair = await cryptoApi.createEddsaKeypair({}); + const mergePair = await cryptoApi.createEddsaKeypair({}); + const contractPair = await cryptoApi.createEddsaKeypair({}); + const contractEncNonce = encodeCrock(getRandomBytes(24)); + + const pursePub = pursePair.pub; + + const hContractTerms = ContractTermsUtil.hashContractTerms(contractTerms); + + const purseSigResp = await cryptoApi.signPurseCreation({ + hContractTerms, + mergePub: mergePair.pub, + minAge: 0, + purseAmount: amount, + purseExpiration: contractTerms.purse_expiration, + pursePriv: pursePair.priv, + }); + + const coinSpend: SpendCoinDetails = { + ageCommitmentProof: undefined, + coinPriv: coin.coinPriv, + coinPub: coin.coinPub, + contribution: amount, + denomPubHash: coin.denomPubHash, + denomSig: coin.denomSig, + }; + + const depositSigsResp = await cryptoApi.signPurseDeposits({ + exchangeBaseUrl: exchange.baseUrl, + pursePub: pursePair.pub, + coins: [coinSpend], + }); + + const encryptContractRequest: EncryptContractRequest = { + contractTerms: contractTerms, + mergePriv: mergePair.priv, + pursePriv: pursePair.priv, + pursePub: pursePair.pub, + contractPriv: contractPair.priv, + contractPub: contractPair.pub, + nonce: contractEncNonce, + }; + + const econtractResp = await cryptoApi.encryptContractForMerge( + encryptContractRequest, + ); + + const econtractHash = encodeCrock( + hash(decodeCrock(econtractResp.econtract.econtract)), + ); + + const createPurseUrl = new URL( + `purses/${pursePair.pub}/create`, + exchange.baseUrl, + ); + + const reqBody = { + amount: amount, + merge_pub: mergePair.pub, + purse_sig: purseSigResp.sig, + h_contract_terms: hContractTerms, + purse_expiration: contractTerms.purse_expiration, + deposits: depositSigsResp.deposits, + min_age: 0, + econtract: econtractResp.econtract, + }; + + const httpResp = await http.fetch(createPurseUrl.href, { + method: "POST", + body: reqBody, + }); + + const respBody = await httpResp.json(); + + console.log("status", httpResp.status); + + console.log(j2s(respBody)); + + const mergeUrl = new URL(`purses/${pursePub}/merge`, exchange.baseUrl); + mergeUrl.searchParams.set("timeout_ms", "300"); + const statusResp = await http.fetch(mergeUrl.href, {}); + + const statusRespBody = await statusResp.json(); + + console.log(j2s(statusRespBody)); + + t.assertTrue(statusRespBody.merge_timestamp === undefined); + } catch (e) { + if (e instanceof TalerError) { + console.log(e); + console.log(j2s(e.errorDetail)); + } else { + console.log(e); + } + throw e; + } +} + +runExchangePurseTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-exchange-timetravel.ts b/packages/taler-harness/src/integrationtests/test-exchange-timetravel.ts new file mode 100644 index 000000000..4f2fb1ee4 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-exchange-timetravel.ts @@ -0,0 +1,287 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + codecForExchangeKeysJson, + DenominationPubKey, + DenomKeyType, + Duration, + ExchangeKeysJson, + Logger, + TalerCorebankApiClient, +} from "@gnu-taler/taler-util"; +import { + createPlatformHttpLib, + readSuccessResponseJsonOrThrow, +} from "@gnu-taler/taler-util/http"; +import { makeNoFeeCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + generateRandomPayto, + GlobalTestState, + MerchantService, + setupDb, +} from "../harness/harness.js"; +import { + applyTimeTravelV2, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +const logger = new Logger("test-exchange-timetravel.ts"); + +interface DenomInfo { + denomPub: DenominationPubKey; + expireDeposit: string; +} + +function getDenomInfoFromKeys(ek: ExchangeKeysJson): DenomInfo[] { + const denomInfos: DenomInfo[] = []; + for (const denomGroup of ek.denominations) { + switch (denomGroup.cipher) { + case "RSA": + case "RSA+age_restricted": { + let ageMask = 0; + if (denomGroup.cipher === "RSA+age_restricted") { + ageMask = denomGroup.age_mask; + } + for (const denomIn of denomGroup.denoms) { + const denomPub: DenominationPubKey = { + age_mask: ageMask, + cipher: DenomKeyType.Rsa, + rsa_public_key: denomIn.rsa_pub, + }; + denomInfos.push({ + denomPub, + expireDeposit: AbsoluteTime.stringify( + AbsoluteTime.fromProtocolTimestamp(denomIn.stamp_expire_deposit), + ), + }); + } + break; + } + case "CS+age_restricted": + case "CS": + logger.warn("Clause-Schnorr denominations not supported"); + continue; + default: + logger.warn( + `denomination type ${(denomGroup as any).cipher} not supported`, + ); + continue; + } + } + return denomInfos; +} + +const http = createPlatformHttpLib({ + enableThrottling: false, +}); + +/** + * Basic time travel test. + */ +export async function runExchangeTimetravelTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + exchange.addCoinConfigList(makeNoFeeCoinConfig("TESTKUDOS")); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "default", + }); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:15", + }); + await wres.withdrawalFinishedCond; + + const keysResp1 = await http.fetch(exchange.baseUrl + "keys"); + const keys1 = await readSuccessResponseJsonOrThrow( + keysResp1, + codecForExchangeKeysJson(), + ); + console.log( + "keys 1 (before time travel):", + JSON.stringify(keys1, undefined, 2), + ); + + // Travel into the future, the deposit expiration is two years + // into the future. + console.log("applying first time travel"); + await applyTimeTravelV2( + Duration.toMilliseconds(Duration.fromSpec({ days: 400 })), + { + walletClient, + exchange, + merchant, + }, + ); + + const keysResp2 = await http.fetch(exchange.baseUrl + "keys"); + const keys2 = await readSuccessResponseJsonOrThrow( + keysResp2, + codecForExchangeKeysJson(), + ); + console.log( + "keys 2 (after time travel):", + JSON.stringify(keys2, undefined, 2), + ); + + const denomPubs1 = getDenomInfoFromKeys(keys1); + const denomPubs2 = getDenomInfoFromKeys(keys2); + + const dps2 = new Set(denomPubs2.map((x) => x.denomPub)); + + console.log("=== KEYS RESPONSE 1 ==="); + + console.log( + "list issue date", + AbsoluteTime.stringify( + AbsoluteTime.fromProtocolTimestamp(keys1.list_issue_date), + ), + ); + console.log("num denoms", denomPubs1.length); + console.log("denoms", JSON.stringify(denomPubs1, undefined, 2)); + + console.log("=== KEYS RESPONSE 2 ==="); + + console.log( + "list issue date", + AbsoluteTime.stringify( + AbsoluteTime.fromProtocolTimestamp(keys2.list_issue_date), + ), + ); + console.log("num denoms", denomPubs2.length); + console.log("denoms", JSON.stringify(denomPubs2, undefined, 2)); + + for (const da of denomPubs1) { + let found = false; + for (const db of denomPubs2) { + const d1 = da.denomPub; + const d2 = db.denomPub; + if (DenominationPubKey.cmp(d1, d2) === 0) { + found = true; + break; + } + } + if (!found) { + console.log("=== ERROR ==="); + console.log( + `denomination with public key ${da.denomPub} is not present in new /keys response`, + ); + console.log( + `the new /keys response was issued ${AbsoluteTime.stringify( + AbsoluteTime.fromProtocolTimestamp(keys2.list_issue_date), + )}`, + ); + console.log( + `however, the missing denomination has stamp_expire_deposit ${da.expireDeposit}`, + ); + console.log("see above for the verbatim /keys responses"); + t.assertTrue(false); + } + } +} + +runExchangeTimetravelTest.suites = ["exchange"]; diff --git a/packages/taler-harness/src/integrationtests/test-fee-regression.ts b/packages/taler-harness/src/integrationtests/test-fee-regression.ts new file mode 100644 index 000000000..6ae7b5de8 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-fee-regression.ts @@ -0,0 +1,241 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + TalerCorebankApiClient, + TalerMerchantApi, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + SimpleTestEnvironmentNg3, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run a test case with a simple TESTKUDOS Taler environment, consisting + * of one exchange, one bank and one merchant. + */ +export async function createMyTestkudosEnvironment( + t: GlobalTestState, +): Promise<SimpleTestEnvironmentNg3> { + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL( + "accounts/exchange/taler-wire-gateway/", + bank.baseUrl, + ).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + rsaKeySize: 1024, + feeDeposit: "TESTKUDOS:0.0025", + feeWithdraw: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + }; + + exchange.addCoinConfigList([ + { + ...coinCommon, + name: "c1", + value: "TESTKUDOS:1.28", + }, + { + ...coinCommon, + name: "c2", + value: "TESTKUDOS:0.64", + }, + { + ...coinCommon, + name: "c3", + value: "TESTKUDOS:0.32", + }, + { + ...coinCommon, + name: "c4", + value: "TESTKUDOS:0.16", + }, + { + ...coinCommon, + name: "c5", + value: "TESTKUDOS:0.08", + }, + { + ...coinCommon, + name: "c5", + value: "TESTKUDOS:0.04", + }, + { + ...coinCommon, + name: "c6", + value: "TESTKUDOS:0.02", + }, + { + ...coinCommon, + name: "c7", + value: "TESTKUDOS:0.01", + }, + ]); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addDefaultInstance(); + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const { walletClient, walletService } = await createWalletDaemonWithClient( + t, + { + name: "w1", + }, + ); + + return { + commonDb: db, + exchange, + merchant, + walletClient, + walletService, + bankClient, + exchangeBankAccount: { + accountName: "", + accountPassword: "", + accountPaytoUri: "", + wireGatewayApiBaseUrl: "", + }, + }; +} + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runFeeRegressionTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createMyTestkudosEnvironment(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:1.92", + }); + + await wres.withdrawalFinishedCond; + + const coins = await walletClient.call(WalletApiOperation.DumpCoins, {}); + + // Make sure we really withdraw one 0.64 and one 1.28 coin. + t.assertTrue(coins.coins.length === 2); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:1.30", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const txs = await walletClient.call(WalletApiOperation.GetTransactions, {}); + t.assertAmountEquals(txs.transactions[1].amountEffective, "TESTKUDOS:1.30"); + console.log(txs); +} + +runFeeRegressionTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-forced-selection.ts b/packages/taler-harness/src/integrationtests/test-forced-selection.ts new file mode 100644 index 000000000..839ddd927 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-forced-selection.ts @@ -0,0 +1,86 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { AmountString, j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV2 } from "../harness/helpers.js"; + +/** + * Run test for forced denom/coin selection. + */ +export async function runForcedSelectionTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bank, exchange, merchant } = + await createSimpleTestkudosEnvironmentV2(t); + + await walletClient.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + await walletClient.call(WalletApiOperation.WithdrawTestBalance, { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10" as AmountString, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + forcedDenomSel: { + denoms: [ + { + value: "TESTKUDOS:2" as AmountString, + count: 3, + }, + ], + }, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const coinDump = await walletClient.call(WalletApiOperation.DumpCoins, {}); + console.log(coinDump); + t.assertDeepEqual(coinDump.coins.length, 3); + + const payResp = await walletClient.call(WalletApiOperation.TestPay, { + amount: "TESTKUDOS:3" as AmountString, + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + summary: "bla", + forcedCoinSel: { + coins: [ + { + value: "TESTKUDOS:2" as AmountString, + contribution: "TESTKUDOS:1" as AmountString, + }, + { + value: "TESTKUDOS:2" as AmountString, + contribution: "TESTKUDOS:1" as AmountString, + }, + { + value: "TESTKUDOS:2" as AmountString, + contribution: "TESTKUDOS:1" as AmountString, + }, + ], + }, + }); + + console.log(j2s(payResp)); + + // Without forced selection, we would only use 2 coins. + t.assertDeepEqual(payResp.numCoins, 3); +} + +runForcedSelectionTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-kyc.ts b/packages/taler-harness/src/integrationtests/test-kyc.ts new file mode 100644 index 000000000..213dd9df4 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-kyc.ts @@ -0,0 +1,451 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Duration, + Logger, + NotificationType, + TalerCorebankApiClient, + TransactionMajorState, + TransactionMinorState, + TransactionType, + j2s, +} from "@gnu-taler/taler-util"; +import { createPlatformHttpLib } from "@gnu-taler/taler-util/http"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import * as http from "node:http"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + WalletClient, + WalletService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { EnvOptions, SimpleTestEnvironmentNg3 } from "../harness/helpers.js"; + +const logger = new Logger("test-kyc.ts"); + +export async function createKycTestkudosEnvironment( + t: GlobalTestState, + coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")), + opts: EnvOptions = {}, +): Promise<SimpleTestEnvironmentNg3> { + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const ageMaskSpec = opts.ageMaskSpec; + + if (ageMaskSpec) { + exchange.enableAgeRestrictions(ageMaskSpec); + // Enable age restriction for all coins. + exchange.addCoinConfigList( + coinConfig.map((x) => ({ + ...x, + name: `${x.name}-age`, + ageRestricted: true, + })), + ); + // For mixed age restrictions, we also offer coins without age restrictions + if (opts.mixedAgeRestriction) { + exchange.addCoinConfigList( + coinConfig.map((x) => ({ ...x, ageRestricted: false })), + ); + } + } else { + exchange.addCoinConfigList(coinConfig); + } + + await exchange.modifyConfig(async (config) => { + const myprov = "kyc-provider-myprov"; + config.setString(myprov, "cost", "0"); + config.setString(myprov, "logic", "oauth2"); + config.setString(myprov, "provided_checks", "dummy1"); + config.setString(myprov, "user_type", "individual"); + config.setString(myprov, "kyc_oauth2_validity", "forever"); + config.setString( + myprov, + "kyc_oauth2_token_url", + "http://localhost:6666/oauth/v2/token", + ); + config.setString( + myprov, + "kyc_oauth2_authorize_url", + "http://localhost:6666/oauth/v2/login", + ); + config.setString( + myprov, + "kyc_oauth2_info_url", + "http://localhost:6666/oauth/v2/info", + ); + config.setString( + myprov, + "kyc_oauth2_converter_helper", + "taler-exchange-kyc-oauth2-test-converter.sh", + ); + config.setString(myprov, "kyc_oauth2_client_id", "taler-exchange"); + config.setString(myprov, "kyc_oauth2_client_secret", "exchange-secret"); + config.setString(myprov, "kyc_oauth2_post_url", "https://taler.net"); + + config.setString( + "kyc-legitimization-withdraw1", + "operation_type", + "withdraw", + ); + config.setString( + "kyc-legitimization-withdraw1", + "required_checks", + "dummy1", + ); + config.setString("kyc-legitimization-withdraw1", "timeframe", "1d"); + config.setString( + "kyc-legitimization-withdraw1", + "threshold", + "TESTKUDOS:5", + ); + }); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const walletService = new WalletService(t, { + name: "wallet", + useInMemoryDb: true, + }); + await walletService.start(); + await walletService.pingUntilAvailable(); + + const walletClient = new WalletClient({ + name: "wallet", + unixPath: walletService.socketPath, + onNotification(n) { + console.log("got notification", n); + }, + }); + await walletClient.connect(); + await walletClient.client.call(WalletApiOperation.InitWallet, { + config: { + testing: { + skipDefaults: true, + }, + }, + }); + + console.log("setup done!"); + + return { + commonDb: db, + exchange, + merchant, + walletClient, + walletService, + bankClient, + exchangeBankAccount: { + accountName: '', + accountPassword: '', + accountPaytoUri: '', + wireGatewayApiBaseUrl: '', + }, + }; +} + +interface TestfakeKycService { + stop: () => void; +} + +function splitInTwoAt(s: string, separator: string): [string, string] { + const idx = s.indexOf(separator); + if (idx === -1) { + return [s, ""]; + } + return [s.slice(0, idx), s.slice(idx + 1)]; +} + +/** + * Testfake for the kyc service that the exchange talks to. + */ +async function runTestfakeKycService(): Promise<TestfakeKycService> { + const server = http.createServer((req, res) => { + const requestUrl = req.url!; + logger.info(`kyc: got ${req.method} request, ${requestUrl}`); + + const [path, query] = splitInTwoAt(requestUrl, "?"); + + const qp = new URLSearchParams(query); + + if (path === "/oauth/v2/login") { + // Usually this would render some HTML page for the user to log in, + // but we return JSON here. + const redirUriUnparsed = qp.get("redirect_uri"); + if (!redirUriUnparsed) { + throw Error("missing redirect_url"); + } + const state = qp.get("state"); + if (!state) { + throw Error("missing state"); + } + const redirUri = new URL(redirUriUnparsed); + redirUri.searchParams.set("code", "code_is_ok"); + redirUri.searchParams.set("state", state); + res.writeHead(200, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + redirect_uri: redirUri.href, + }), + ); + } else if (path === "/oauth/v2/token") { + let reqBody = ""; + req.on("data", (x) => { + reqBody += x; + }); + + req.on("end", () => { + logger.info("login request body:", reqBody); + + res.writeHead(200, { "Content-Type": "application/json" }); + // Normally, the access_token would also include which user we're trying + // to get info about, but we (for now) skip it in this test. + res.end( + JSON.stringify({ + access_token: "exchange_access_token", + token_type: "Bearer", + }), + ); + }); + } else if (path === "/oauth/v2/info") { + logger.info("authorization header:", req.headers.authorization); + res.writeHead(200, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + status: "success", + data: { + id: "foobar", + }, + }), + ); + } else { + res.writeHead(400, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ code: 1, message: "bad request" })); + } + }); + await new Promise<void>((resolve, reject) => { + server.listen(6666, () => resolve()); + }); + return { + stop() { + server.close(); + }, + }; +} + +export async function runKycTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createKycTestkudosEnvironment(t); + + const kycServer = await runTestfakeKycService(); + + // Withdraw digital cash into the wallet. + + const amount = "TESTKUDOS:20"; + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth({ + username: user.username, + password: user.password, + }); + + const wop = await bankClient.createWithdrawalOperation(user.username, amount); + + // Hand it to the wallet + + await walletClient.client.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + // Withdraw + + const acceptResp = await walletClient.client.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + const withdrawalTxId = acceptResp.transactionId; + + // Confirm it + + await bankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + const kycNotificationCond = walletClient.waitForNotificationCond((x) => { + if ( + x.type === NotificationType.TransactionStateTransition && + x.transactionId === withdrawalTxId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.KycRequired + ) { + return x; + } + return false; + }); + + const withdrawalDoneCond = walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === withdrawalTxId && + x.newTxState.major === TransactionMajorState.Done, + ); + + const kycNotif = await kycNotificationCond; + + logger.info("got kyc notification:", j2s(kycNotif)); + + const txState = await walletClient.client.call( + WalletApiOperation.GetTransactionById, + { + transactionId: withdrawalTxId, + }, + ); + + t.assertDeepEqual(txState.type, TransactionType.Withdrawal); + + const kycUrl = txState.kycUrl; + + t.assertTrue(!!kycUrl); + + logger.info(`kyc URL is ${kycUrl}`); + + // We now simulate the user interacting with the KYC service, + // which would usually done in the browser. + + const httpLib = createPlatformHttpLib({ + enableThrottling: false, + }); + const kycServerResp = await httpLib.fetch(kycUrl); + const kycLoginResp = await kycServerResp.json(); + logger.info(`kyc server resp: ${j2s(kycLoginResp)}`); + const kycProofUrl = kycLoginResp.redirect_uri; + // We need to "visit" the KYC proof URL at least once to trigger the exchange + // asking for the KYC status. + const proofHttpResp = await httpLib.fetch(kycProofUrl); + logger.info(`proof resp status ${proofHttpResp.status}`); + logger.info(`resp headers ${j2s(proofHttpResp.headers.toJSON())}`); + if ( + !(proofHttpResp.status >= 200 && proofHttpResp.status <= 299) && + proofHttpResp.status !== 303 + ) { + logger.error("kyc proof failed"); + logger.info(await proofHttpResp.text()); + t.assertTrue(false); + } + + // Now that KYC is done, withdrawal should finally succeed. + + await withdrawalDoneCond; + + kycServer.stop(); +} + +runKycTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-libeufin-bank.ts b/packages/taler-harness/src/integrationtests/test-libeufin-bank.ts new file mode 100644 index 000000000..01b20ddbf --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-libeufin-bank.ts @@ -0,0 +1,229 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + CreditDebitIndicator, + Logger, + TalerCorebankApiClient, + TransactionMajorState, + TransactionMinorState, + WireGatewayApiClient, + createEddsaKeyPair, + encodeCrock, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + GlobalTestState, + LibeufinBankService, + MerchantService, + generateRandomPayto, + generateRandomTestIban, + setupDb, +} from "../harness/harness.js"; +import { createWalletDaemonWithClient } from "../harness/helpers.js"; + +const logger = new Logger("test-libeufin-bank.ts"); + +/** + * Run test for the basic functionality of libeufin-bank. + */ +export async function runLibeufinBankTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await LibeufinBankService.create(t, { + currency: "TESTKUDOS", + httpPort: 8082, + database: db.connStr, + allowRegistrations: true, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + const exchangeBankUsername = "exchange"; + const exchangeBankPw = "mypw"; + const exchangePayto = generateRandomPayto(exchangeBankUsername); + const wireGatewayApiBaseUrl = new URL( + "accounts/exchange/taler-wire-gateway/", + bank.baseUrl, + ).href; + + logger.info("creating bank account for the exchange"); + + exchange.addBankAccount("1", { + wireGatewayApiBaseUrl, + accountName: exchangeBankUsername, + accountPassword: exchangeBankPw, + accountPaytoUri: exchangePayto, + }); + + bank.setSuggestedExchange(exchange); + + await bank.start(); + + await bank.pingUntilAvailable(); + + exchange.addOfferedCoins(defaultCoinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "wallet", + }); + + console.log("setup done!"); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + // register exchange bank account + await bankClient.registerAccountExtended({ + name: "Exchange", + password: exchangeBankPw, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePayto, + }); + + const bankUser = await bankClient.registerAccount("user1", "pw1"); + bankClient.setAuth({ + username: "user1", + password: "pw1", + }); + + // Make sure that registering twice results in a 409 Conflict + // { + // const e = await t.assertThrowsTalerErrorAsync(async () => { + // await bankClient.registerAccount("user1", "pw2"); + // }); + // t.assertTrue(e.errorDetail.httpStatusCode === 409); + // } + + let balResp = await bankClient.getAccountBalance(bankUser.username); + + console.log(balResp); + + // Check that we got the sign-up bonus. + t.assertAmountEquals(balResp.balance.amount, "TESTKUDOS:100"); + t.assertTrue( + balResp.balance.credit_debit_indicator === CreditDebitIndicator.Credit, + ); + + const res = createEddsaKeyPair(); + + // Not a normal client, but one with admin credentials, + // as /add-incoming is testing functionality only allowed by the admin. + const wireGatewayApiAdminClient = new WireGatewayApiClient( + wireGatewayApiBaseUrl, + { + auth: { + username: "admin", + password: "adminpw", + }, + }, + ); + + await wireGatewayApiAdminClient.adminAddIncoming({ + amount: "TESTKUDOS:115", + debitAccountPayto: bankUser.accountPaytoUri, + reservePub: encodeCrock(res.eddsaPub), + }); + + balResp = await bankClient.getAccountBalance(bankUser.username); + t.assertAmountEquals(balResp.balance.amount, "TESTKUDOS:15"); + t.assertTrue( + balResp.balance.credit_debit_indicator === CreditDebitIndicator.Debit, + ); + + const wop = await bankClient.createWithdrawalOperation( + bankUser.username, + "TESTKUDOS:10", + ); + + const r1 = await walletClient.client.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + console.log(j2s(r1)); + + const r2 = await walletClient.client.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: r2.transactionId, + txState: { + major: TransactionMajorState.Pending, + minor: TransactionMinorState.BankConfirmTransfer, + }, + }); + + await bankClient.confirmWithdrawalOperation(bankUser.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runLibeufinBankTest.suites = ["fakebank"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-exchange-confusion.ts b/packages/taler-harness/src/integrationtests/test-merchant-exchange-confusion.ts new file mode 100644 index 000000000..19f89ae2c --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-exchange-confusion.ts @@ -0,0 +1,264 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + codecForMerchantOrderStatusUnpaid, + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + TalerCorebankApiClient, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { URL } from "url"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + FaultInjectedExchangeService, + FaultInjectedMerchantService, +} from "../harness/faultInjection.js"; +import { + BankService, + ExchangeService, + generateRandomPayto, + GlobalTestState, + harnessHttpLib, + MerchantService, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + FaultyMerchantTestEnvironmentNg, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run a test case with a simple TESTKUDOS Taler environment, consisting + * of one exchange, one bank and one merchant. + */ +export async function createConfusedMerchantTestkudosEnvironment( + t: GlobalTestState, +): Promise<FaultyMerchantTestEnvironmentNg> { + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + const faultyMerchant = new FaultInjectedMerchantService(t, merchant, 9083); + const faultyExchange = new FaultInjectedExchangeService(t, exchange, 9081); + + // Base URL must contain port that the proxy is listening on. + await exchange.modifyConfig(async (config) => { + config.setString("exchange", "base_url", "http://localhost:9081/"); + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + exchange.addOfferedCoins(defaultCoinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + // Confuse the merchant by adding the non-proxied exchange. + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "default", + }); + + return { + commonDb: db, + exchange, + merchant, + walletClient, + bankClient, + faultyMerchant, + faultyExchange, + }; +} + +/** + * Confuse the merchant by having one URL for the same exchange in the config, + * but sending coins from the same exchange with a different URL. + */ +export async function runMerchantExchangeConfusionTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, faultyExchange, faultyMerchant } = + await createConfusedMerchantTestkudosEnvironment(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange: faultyExchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + /** + * ========================================================================= + * Create an order and let the wallet pay under a session ID + * + * We check along the way that the JSON response to /orders/{order_id} + * returns the right thing. + * ========================================================================= + */ + + const merchant = faultyMerchant; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + let orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-one", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + let publicOrderStatusUrl = orderStatus.order_status_url; + + let publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + let pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + await publicOrderStatusResp.json(), + ); + + console.log(pubUnpaidStatus); + + let preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: pubUnpaidStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + const proposalId = preparePayResp.proposalId; + + const orderUrlWithHash = new URL(publicOrderStatusUrl); + orderUrlWithHash.searchParams.set( + "h_contract", + preparePayResp.contractTermsHash, + ); + + console.log("requesting", orderUrlWithHash.href); + + publicOrderStatusResp = await harnessHttpLib.fetch(orderUrlWithHash.href); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (after claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + await publicOrderStatusResp.json(), + ); + + const confirmPayRes = await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: proposalId, + }); + + t.assertTrue(confirmPayRes.type === ConfirmPayResultType.Done); +} + +runMerchantExchangeConfusionTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-instances-delete.ts b/packages/taler-harness/src/integrationtests/test-merchant-instances-delete.ts new file mode 100644 index 000000000..c0c9353e4 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-instances-delete.ts @@ -0,0 +1,137 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { MerchantApiClient, TalerError, URL } from "@gnu-taler/taler-util"; +import { + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + harnessHttpLib, + setupDb, +} from "../harness/harness.js"; + +/** + * Test instance deletion and authentication for it + */ +export async function runMerchantInstancesDeleteTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + // We add the exchange to the config, but note that the exchange won't be started. + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + // Base URL for the default instance. + const baseUrl = merchant.makeInstanceBaseUrl(); + + { + const r = await harnessHttpLib.fetch(new URL("config", baseUrl).href); + const data = await r.json(); + console.log(data); + t.assertDeepEqual(data.currency, "TESTKUDOS"); + } + + // Instances should initially be empty + { + const r = await harnessHttpLib.fetch( + new URL("management/instances", baseUrl).href, + ); + const data = await r.json(); + t.assertDeepEqual(data.instances, []); + } + + // Add an instance, no auth! + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + auth: { + method: "external", + }, + }); + + // Add an instance, no auth! + await merchant.addInstanceWithWireAccount({ + id: "myinst", + name: "Second Instance", + paytoUris: [generateRandomPayto("merchant-default")], + auth: { + method: "external", + }, + }); + + let merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl(), { + auth: { + method: "external", + }, + }); + + await merchantClient.changeAuth({ + method: "token", + token: "secret-token:foobar", + }); + + merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl(), { + auth: { + method: "token", + token: "secret-token:foobar", + }, + }); + + // Check that deleting an instance checks the auth + // of the default instance. + { + const unauthMerchantClient = new MerchantApiClient( + merchant.makeInstanceBaseUrl(), + { + auth: { + method: "token", + token: "secret-token:invalid", + }, + }, + ); + + const exc = await t.assertThrowsAsync(async () => { + await unauthMerchantClient.deleteInstance("myinst"); + }); + console.log("Got expected exception", exc); + t.assertTrue(exc instanceof TalerError); + t.assertDeepEqual(exc.errorDetail.httpStatusCode, 401); + } +} + +runMerchantInstancesDeleteTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-instances-urls.ts b/packages/taler-harness/src/integrationtests/test-merchant-instances-urls.ts new file mode 100644 index 000000000..b631ea1a4 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-instances-urls.ts @@ -0,0 +1,179 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { Duration, MerchantApiClient } from "@gnu-taler/taler-util"; +import { + ExchangeService, + GlobalTestState, + MerchantService, + harnessHttpLib, + setupDb, +} from "../harness/harness.js"; + +/** + * Do basic checks on instance management and authentication. + */ +export async function runMerchantInstancesUrlsTest(t: GlobalTestState) { + const db = await setupDb(t); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + const clientForDefault = new MerchantApiClient( + merchant.makeInstanceBaseUrl(), + { + auth: { + method: "token", + token: "secret-token:i-am-default", + }, + }, + ); + + await clientForDefault.createInstance({ + id: "default", + address: {}, + use_stefan: true, + default_pay_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ seconds: 60 }), + ), + default_wire_transfer_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ seconds: 60 }), + ), + jurisdiction: {}, + name: "My Default Instance", + auth: { + method: "token", + token: "secret-token:i-am-default", + }, + }); + + await clientForDefault.createInstance({ + id: "myinst", + address: {}, + default_pay_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ seconds: 60 }), + ), + use_stefan: true, + default_wire_transfer_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ seconds: 60 }), + ), + jurisdiction: {}, + name: "My Second Instance", + auth: { + method: "token", + token: "secret-token:i-am-myinst", + }, + }); + + async function check(url: string, token: string, expectedStatus: number) { + const resp = await harnessHttpLib.fetch(url, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + console.log( + `checking ${url}, expected ${expectedStatus}, got ${resp.status}`, + ); + t.assertDeepEqual(resp.status, expectedStatus); + } + + const tokDefault = "secret-token:i-am-default"; + + const defaultBaseUrl = merchant.makeInstanceBaseUrl(); + + await check( + `${defaultBaseUrl}private/instances/default/instances/default/config`, + tokDefault, + 404, + ); + + // Instance management is only available when accessing the default instance + // directly. + await check( + `${defaultBaseUrl}instances/default/private/instances`, + "foo", + 404, + ); + + // Non-default instances don't allow instance management. + await check(`${defaultBaseUrl}instances/foo/private/instances`, "foo", 404); + await check( + `${defaultBaseUrl}instances/myinst/private/instances`, + "foo", + 404, + ); + + await check(`${defaultBaseUrl}config`, "foo", 200); + await check(`${defaultBaseUrl}instances/default/config`, "foo", 200); + await check(`${defaultBaseUrl}instances/myinst/config`, "foo", 200); + await check(`${defaultBaseUrl}instances/foo/config`, "foo", 404); + await check( + `${defaultBaseUrl}instances/default/instances/config`, + "foo", + 404, + ); + + await check( + `${defaultBaseUrl}private/instances/myinst/config`, + tokDefault, + 404, + ); + + await check( + `${defaultBaseUrl}instances/myinst/private/orders`, + tokDefault, + 401, + ); + + await check( + `${defaultBaseUrl}instances/myinst/private/orders`, + tokDefault, + 401, + ); + + await check( + `${defaultBaseUrl}instances/myinst/private/orders`, + "secret-token:i-am-myinst", + 200, + ); + + await check( + `${defaultBaseUrl}private/instances/myinst/orders`, + tokDefault, + 404, + ); +} + +runMerchantInstancesUrlsTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-instances.ts b/packages/taler-harness/src/integrationtests/test-merchant-instances.ts new file mode 100644 index 000000000..188451e15 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-instances.ts @@ -0,0 +1,205 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { MerchantApiClient, URL } from "@gnu-taler/taler-util"; +import { + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + harnessHttpLib, + setupDb, +} from "../harness/harness.js"; + +/** + * Do basic checks on instance management and authentication. + */ +export async function runMerchantInstancesTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + // We add the exchange to the config, but note that the exchange won't be started. + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + // Base URL for the default instance. + const baseUrl = merchant.makeInstanceBaseUrl(); + + { + const r = await harnessHttpLib.fetch(new URL("config", baseUrl).href); + const data = await r.json(); + console.log(data); + t.assertDeepEqual(data.currency, "TESTKUDOS"); + } + + // Instances should initially be empty + { + const r = await harnessHttpLib.fetch( + new URL("management/instances", baseUrl).href, + ); + const data = await r.json(); + t.assertDeepEqual(data.instances, []); + } + + // Add an instance, no auth! + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + auth: { + method: "external", + }, + }); + + // Add it again, should be idempotent + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + auth: { + method: "external", + }, + }); + + // Add an instance, no auth! + await merchant.addInstanceWithWireAccount({ + id: "myinst", + name: "Second Instance", + paytoUris: [generateRandomPayto("merchant-default")], + auth: { + method: "external", + }, + }); + + let merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl(), { + auth: { + method: "external", + }, + }); + + { + const r = await merchantClient.getInstances(); + t.assertDeepEqual(r.instances.length, 2); + } + + // Check that a "malformed" bearer Authorization header gets ignored + { + const url = merchant.makeInstanceBaseUrl(); + const resp = await harnessHttpLib.fetch( + new URL("management/instances", url).href, + { + headers: { + Authorization: "foo bar-baz", + }, + }, + ); + t.assertDeepEqual(resp.status, 200); + } + + { + const fullDetails = await merchantClient.getInstanceFullDetails("default"); + t.assertDeepEqual(fullDetails.auth.method, "external"); + } + + await merchantClient.changeAuth({ + method: "token", + token: "secret-token:foobar", + }); + + // Now this should fail, as we didn't change the auth of the client yet. + const exc = await t.assertThrowsAsync(async () => { + console.log("requesting instances with auth", merchantClient.auth); + const resp = await merchantClient.getInstances(); + console.log("instances result:", resp); + }); + + console.log(exc); + t.assertTrue(exc.errorDetail.httpStatusCode === 401); + + merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl(), { + auth: { + method: "token", + token: "secret-token:foobar", + }, + }); + + // With the new client auth settings, request should work again. + await merchantClient.getInstances(); + + // Now, try some variations. + { + const url = merchant.makeInstanceBaseUrl(); + const resp = await harnessHttpLib.fetch( + new URL("management/instances", url).href, + { + headers: { + // Note the spaces + Authorization: "Bearer secret-token:foobar", + }, + }, + ); + t.assertDeepEqual(resp.status, 200); + } + + // Check that auth is reported properly + { + const fullDetails = await merchantClient.getInstanceFullDetails("default"); + t.assertDeepEqual(fullDetails.auth.method, "token"); + // Token should *not* be reported back. + t.assertDeepEqual(fullDetails.auth.token, undefined); + } + + // Check that deleting an instance checks the auth + // of the default instance. + { + const unauthMerchantClient = new MerchantApiClient( + merchant.makeInstanceBaseUrl(), + { + auth: { + method: "external", + }, + }, + ); + + const exc = await t.assertThrowsAsync(async () => { + await unauthMerchantClient.deleteInstance("myinst"); + }); + console.log(exc); + t.assertTrue(exc.errorDetail.httpStatusCode === 401); + } +} + +runMerchantInstancesTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-longpolling.ts b/packages/taler-harness/src/integrationtests/test-merchant-longpolling.ts new file mode 100644 index 000000000..656fc4ded --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-longpolling.ts @@ -0,0 +1,165 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + URL, + codecForMerchantOrderStatusUnpaid, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runMerchantLongpollingTest(t: GlobalTestState) { + // Set up test environment + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + /** + * ========================================================================= + * Create an order and let the wallet pay under a session ID + * + * We check along the way that the JSON response to /orders/{order_id} + * returns the right thing. + * ========================================================================= + */ + + let orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + }, + create_token: false, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-one", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + let publicOrderStatusUrl = new URL(orderStatus.order_status_url); + + // First, request order status without longpolling + { + console.log("requesting", publicOrderStatusUrl.href); + let publicOrderStatusResp = await harnessHttpLib.fetch( + publicOrderStatusUrl.href, + ); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming, no long polling), but got ${publicOrderStatusResp.status}`, + ); + } + } + + // Now do long-polling for half a second! + publicOrderStatusUrl.searchParams.set("timeout_ms", "500"); + + console.log("requesting", publicOrderStatusUrl.href); + let publicOrderStatusResp = await harnessHttpLib.fetch( + publicOrderStatusUrl.href, + ); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming, with long-polling), but got ${publicOrderStatusResp.status}`, + ); + } + + let pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + await publicOrderStatusResp.json(), + ); + + console.log(pubUnpaidStatus); + + /** + * ========================================================================= + * Now actually pay, but WHILE a long poll is active! + * ========================================================================= + */ + + let preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: pubUnpaidStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + publicOrderStatusUrl.searchParams.set("timeout_ms", "5000"); + publicOrderStatusUrl.searchParams.set( + "h_contract", + preparePayResp.contractTermsHash, + ); + + let publicOrderStatusPromise = harnessHttpLib.fetch( + publicOrderStatusUrl.href, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + const proposalId = preparePayResp.proposalId; + + publicOrderStatusResp = await publicOrderStatusPromise; + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (after claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + await publicOrderStatusResp.json(), + ); + + const confirmPayRes = await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: proposalId, + }); + + t.assertTrue(confirmPayRes.type === ConfirmPayResultType.Done); +} + +runMerchantLongpollingTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-refund-api.ts b/packages/taler-harness/src/integrationtests/test-merchant-refund-api.ts new file mode 100644 index 000000000..1d712f745 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-refund-api.ts @@ -0,0 +1,302 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Duration, + MerchantApiClient, + PreparePayResultType, + URL, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { + ExchangeServiceInterface, + GlobalTestState, + MerchantServiceInterface, + WalletClient, + harnessHttpLib, +} from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +async function testRefundApiWithFulfillmentUrl( + t: GlobalTestState, + env: { + merchant: MerchantServiceInterface; + walletClient: WalletClient; + exchange: ExchangeServiceInterface; + }, +): Promise<void> { + const { walletClient, merchant } = env; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Set up order. + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/fulfillment", + }, + refund_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 5 }), + ), + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const talerPayUri = orderStatus.taler_pay_uri; + const orderId = orderResp.order_id; + + // Make wallet pay for the order + + let preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: preparePayResult.proposalId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.AlreadyConfirmed, + ); + + await merchantClient.giveRefund({ + amount: "TESTKUDOS:5", + instance: "default", + justification: "foo", + orderId: orderResp.order_id, + }); + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + t.assertAmountEquals(orderStatus.refund_amount, "TESTKUDOS:5"); + + // Now test what the merchant gives as a response for various requests to the + // public order status URL! + + let publicOrderStatusUrl = new URL( + `orders/${orderId}`, + merchant.makeInstanceBaseUrl(), + ); + publicOrderStatusUrl.searchParams.set( + "h_contract", + preparePayResult.contractTermsHash, + ); + + let publicOrderStatusResp = await harnessHttpLib.fetch( + publicOrderStatusUrl.href, + ); + const respData = await publicOrderStatusResp.json(); + t.assertTrue(publicOrderStatusResp.status === 200); + t.assertAmountEquals(respData.refund_amount, "TESTKUDOS:5"); + + publicOrderStatusUrl = new URL( + `orders/${orderId}`, + merchant.makeInstanceBaseUrl(), + ); + console.log(`requesting order status via '${publicOrderStatusUrl.href}'`); + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl.href); + console.log(publicOrderStatusResp.status); + console.log(await publicOrderStatusResp.json()); + // We didn't give any authentication, so we should get a fulfillment URL back + t.assertTrue(publicOrderStatusResp.status === 403); +} + +async function testRefundApiWithFulfillmentMessage( + t: GlobalTestState, + env: { + merchant: MerchantServiceInterface; + walletClient: WalletClient; + exchange: ExchangeServiceInterface; + }, +): Promise<void> { + const { walletClient, merchant } = env; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Set up order. + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_message: "Thank you for buying foobar", + }, + refund_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 5 }), + ), + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const talerPayUri = orderStatus.taler_pay_uri; + const orderId = orderResp.order_id; + + // Make wallet pay for the order + + let preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: preparePayResult.proposalId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.AlreadyConfirmed, + ); + + await merchantClient.giveRefund({ + amount: "TESTKUDOS:5", + instance: "default", + justification: "foo", + orderId: orderResp.order_id, + }); + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + t.assertAmountEquals(orderStatus.refund_amount, "TESTKUDOS:5"); + + // Now test what the merchant gives as a response for various requests to the + // public order status URL! + + let publicOrderStatusUrl = new URL( + `orders/${orderId}`, + merchant.makeInstanceBaseUrl(), + ); + publicOrderStatusUrl.searchParams.set( + "h_contract", + preparePayResult.contractTermsHash, + ); + + let publicOrderStatusResp = await harnessHttpLib.fetch( + publicOrderStatusUrl.href, + ); + let respData = await publicOrderStatusResp.json(); + console.log(respData); + t.assertTrue(publicOrderStatusResp.status === 200); + t.assertAmountEquals(respData.refund_amount, "TESTKUDOS:5"); + + publicOrderStatusUrl = new URL( + `orders/${orderId}`, + merchant.makeInstanceBaseUrl(), + ); + + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl.href); + respData = await publicOrderStatusResp.json(); + console.log(respData); + // We didn't give any authentication, so we should get a fulfillment URL back + t.assertTrue(publicOrderStatusResp.status === 403); +} + +/** + * Test case for the refund API of the merchant backend. + */ +export async function runMerchantRefundApiTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres.withdrawalFinishedCond; + + await testRefundApiWithFulfillmentUrl(t, { + walletClient, + exchange, + merchant, + }); + + await testRefundApiWithFulfillmentMessage(t, { + walletClient, + exchange, + merchant, + }); +} + +runMerchantRefundApiTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-merchant-spec-public-orders.ts b/packages/taler-harness/src/integrationtests/test-merchant-spec-public-orders.ts new file mode 100644 index 000000000..8a22eae57 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-merchant-spec-public-orders.ts @@ -0,0 +1,631 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + TalerCorebankApiClient, + URL, + encodeCrock, + getRandomBytes, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { + ExchangeService, + GlobalTestState, + MerchantService, + harnessHttpLib, +} from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +interface Context { + merchant: MerchantService; + merchantBaseUrl: string; + bankClient: TalerCorebankApiClient; + exchange: ExchangeService; +} + +const httpLib = harnessHttpLib; + +async function testWithClaimToken( + t: GlobalTestState, + c: Context, +): Promise<void> { + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "wct", + }); + const { bankClient, exchange } = c; + const { merchant, merchantBaseUrl } = c; + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres.withdrawalFinishedCond; + const sessionId = "mysession"; + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + const claimToken = orderResp.token; + const orderId = orderResp.order_id; + t.assertTrue(!!claimToken); + let talerPayUri: string; + + { + const httpResp = await httpLib.fetch( + new URL(`orders/${orderId}`, merchantBaseUrl).href, + ); + const r = await httpResp.json(); + t.assertDeepEqual(httpResp.status, 202); + console.log(r); + } + + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("token", claimToken); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + t.assertDeepEqual(httpResp.status, 402); + console.log(r); + talerPayUri = r.taler_pay_uri; + t.assertTrue(!!talerPayUri); + } + + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("token", claimToken); + const httpResp = await httpLib.fetch(url.href, { + headers: { + Accept: "text/html", + }, + }); + const r = await httpResp.text(); + t.assertDeepEqual(httpResp.status, 402); + console.log(r); + } + + const preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + const contractTermsHash = preparePayResp.contractTermsHash; + const proposalId = preparePayResp.proposalId; + + // claimed, unpaid, access with wrong h_contract + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const hcWrong = encodeCrock(getRandomBytes(64)); + url.searchParams.set("h_contract", hcWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // claimed, unpaid, access with wrong claim token + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const ctWrong = encodeCrock(getRandomBytes(16)); + url.searchParams.set("token", ctWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // claimed, unpaid, access with correct claim token + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("token", claimToken); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + } + + // claimed, unpaid, access with correct contract terms hash + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("h_contract", contractTermsHash); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + } + + // claimed, unpaid, access without credentials + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 202); + } + + const confirmPayRes = await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: proposalId, + }); + + t.assertTrue(confirmPayRes.type === ConfirmPayResultType.Done); + + // paid, access without credentials + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 202); + } + + // paid, access with wrong h_contract + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const hcWrong = encodeCrock(getRandomBytes(64)); + url.searchParams.set("h_contract", hcWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // paid, access with wrong claim token + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const ctWrong = encodeCrock(getRandomBytes(16)); + url.searchParams.set("token", ctWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // paid, access with correct h_contract + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("h_contract", contractTermsHash); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 200); + } + + // paid, access with correct claim token, JSON + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("token", claimToken); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 200); + const respFulfillmentUrl = r.fulfillment_url; + t.assertDeepEqual(respFulfillmentUrl, "https://example.com/article42"); + } + + // paid, access with correct claim token, HTML + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("token", claimToken); + const httpResp = await httpLib.fetch(url.href, { + headers: { Accept: "text/html" }, + }); + t.assertDeepEqual(httpResp.status, 200); + } + + const confirmPayRes2 = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + proposalId: proposalId, + sessionId: sessionId, + }, + ); + + t.assertTrue(confirmPayRes2.type === ConfirmPayResultType.Done); + + // Create another order with identical fulfillment URL to test the "already paid" flow + const alreadyPaidOrderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + const apOrderId = alreadyPaidOrderResp.order_id; + const apToken = alreadyPaidOrderResp.token; + t.assertTrue(!!apToken); + + { + const url = new URL(`orders/${apOrderId}`, merchantBaseUrl); + url.searchParams.set("token", apToken); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + } + + // Check for already paid session ID, JSON + { + const url = new URL(`orders/${apOrderId}`, merchantBaseUrl); + url.searchParams.set("token", apToken); + url.searchParams.set("session_id", sessionId); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + const alreadyPaidOrderId = r.already_paid_order_id; + t.assertDeepEqual(alreadyPaidOrderId, orderId); + } + + // Check for already paid session ID, HTML + { + const url = new URL(`orders/${apOrderId}`, merchantBaseUrl); + url.searchParams.set("token", apToken); + url.searchParams.set("session_id", sessionId); + const httpResp = await httpLib.fetch(url.href, { + headers: { Accept: "text/html" }, + redirect: "manual", + }); + console.log( + `requesting GET ${url.href}, expected 302 got ${httpResp.status}`, + ); + t.assertDeepEqual(httpResp.status, 302); + const location = httpResp.headers.get("Location"); + console.log("location header:", location); + t.assertDeepEqual(location, "https://example.com/article42"); + } +} + +async function testWithoutClaimToken( + t: GlobalTestState, + c: Context, +): Promise<void> { + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "wnoct", + }); + const sessionId = "mysession2"; + const { bankClient, exchange } = c; + const { merchant, merchantBaseUrl } = c; + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres.withdrawalFinishedCond; + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + create_token: false, + }); + + const orderId = orderResp.order_id; + let talerPayUri: string; + + { + const httpResp = await httpLib.fetch( + new URL(`orders/${orderId}`, merchantBaseUrl).href, + ); + const r = await httpResp.json(); + t.assertDeepEqual(httpResp.status, 402); + console.log(r); + } + + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + t.assertDeepEqual(httpResp.status, 402); + console.log(r); + talerPayUri = r.taler_pay_uri; + t.assertTrue(!!talerPayUri); + } + + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href, { + headers: { + Accept: "text/html", + }, + }); + const r = await httpResp.text(); + t.assertDeepEqual(httpResp.status, 402); + console.log(r); + } + + const preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + console.log(preparePayResp); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + const contractTermsHash = preparePayResp.contractTermsHash; + const proposalId = preparePayResp.proposalId; + + // claimed, unpaid, access with wrong h_contract + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const hcWrong = encodeCrock(getRandomBytes(64)); + url.searchParams.set("h_contract", hcWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // claimed, unpaid, access with wrong claim token + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const ctWrong = encodeCrock(getRandomBytes(16)); + url.searchParams.set("token", ctWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // claimed, unpaid, no claim token + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + } + + // claimed, unpaid, access with correct contract terms hash + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("h_contract", contractTermsHash); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + } + + // claimed, unpaid, access without credentials + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + // No credentials, but the order doesn't require a claim token. + // This effectively means that the order ID is already considered + // enough authentication, at least to check for the basic order status + t.assertDeepEqual(httpResp.status, 402); + } + + const confirmPayRes = await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: proposalId, + }); + + t.assertTrue(confirmPayRes.type === ConfirmPayResultType.Done); + + // paid, access without credentials + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 200); + } + + // paid, access with wrong h_contract + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const hcWrong = encodeCrock(getRandomBytes(64)); + url.searchParams.set("h_contract", hcWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // paid, access with wrong claim token + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const ctWrong = encodeCrock(getRandomBytes(16)); + url.searchParams.set("token", ctWrong); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 403); + } + + // paid, access with correct h_contract + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + url.searchParams.set("h_contract", contractTermsHash); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 200); + } + + // paid, JSON + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 200); + const respFulfillmentUrl = r.fulfillment_url; + t.assertDeepEqual(respFulfillmentUrl, "https://example.com/article42"); + } + + // paid, HTML + { + const url = new URL(`orders/${orderId}`, merchantBaseUrl); + const httpResp = await httpLib.fetch(url.href, { + headers: { Accept: "text/html" }, + }); + t.assertDeepEqual(httpResp.status, 200); + } + + const confirmPayRes2 = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + proposalId: proposalId, + sessionId: sessionId, + }, + ); + + t.assertTrue(confirmPayRes2.type === ConfirmPayResultType.Done); + + // Create another order with identical fulfillment URL to test the "already paid" flow + const alreadyPaidOrderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + const apOrderId = alreadyPaidOrderResp.order_id; + const apToken = alreadyPaidOrderResp.token; + t.assertTrue(!!apToken); + + { + const url = new URL(`orders/${apOrderId}`, merchantBaseUrl); + url.searchParams.set("token", apToken); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + } + + // Check for already paid session ID, JSON + { + const url = new URL(`orders/${apOrderId}`, merchantBaseUrl); + url.searchParams.set("token", apToken); + url.searchParams.set("session_id", sessionId); + const httpResp = await httpLib.fetch(url.href); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 402); + const alreadyPaidOrderId = r.already_paid_order_id; + t.assertDeepEqual(alreadyPaidOrderId, orderId); + } + + // Check for already paid session ID, HTML + { + const url = new URL(`orders/${apOrderId}`, merchantBaseUrl); + url.searchParams.set("token", apToken); + url.searchParams.set("session_id", sessionId); + const httpResp = await httpLib.fetch(url.href, { + headers: { Accept: "text/html" }, + redirect: "manual", + }); + t.assertDeepEqual(httpResp.status, 302); + const location = httpResp.headers.get("Location"); + console.log("location header:", location); + t.assertDeepEqual(location, "https://example.com/article42"); + } +} + +/** + * Checks for the /orders/{id} endpoint of the merchant. + * + * The tests here should exercise all code paths in the executable + * specification of the endpoint. + */ +export async function runMerchantSpecPublicOrdersTest(t: GlobalTestState) { + const { bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Base URL for the default instance. + const merchantBaseUrl = merchant.makeInstanceBaseUrl(); + + { + const httpResp = await httpLib.fetch( + new URL("config", merchantBaseUrl).href, + ); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(r.currency, "TESTKUDOS"); + } + + { + const httpResp = await httpLib.fetch( + new URL("orders/foo", merchantBaseUrl).href, + ); + const r = await httpResp.json(); + console.log(r); + t.assertDeepEqual(httpResp.status, 404); + // FIXME: also check Taler error code + } + + { + const httpResp = await httpLib.fetch( + new URL("orders/foo", merchantBaseUrl).href, + { + headers: { + Accept: "text/html", + }, + }, + ); + const r = await httpResp.text(); + console.log(r); + t.assertDeepEqual(httpResp.status, 404); + // FIXME: also check Taler error code + } + + await testWithClaimToken(t, { + merchant, + merchantBaseUrl, + exchange, + bankClient, + }); + + await testWithoutClaimToken(t, { + merchant, + merchantBaseUrl, + exchange, + bankClient, + }); +} + +runMerchantSpecPublicOrdersTest.suites = ["merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-multiexchange.ts b/packages/taler-harness/src/integrationtests/test-multiexchange.ts new file mode 100644 index 000000000..b5cf0770f --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-multiexchange.ts @@ -0,0 +1,172 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { Duration, TalerMerchantApi } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + FakebankService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runMultiExchangeTest(t: GlobalTestState) { + // Set up test environment + const dbDefault = await setupDb(t); + + const dbExchangeTwo = await setupDb(t, { + nameSuffix: "exchange2", + }); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: dbDefault.connStr, + httpPort: 8082, + }); + + const exchangeOne = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: dbDefault.connStr, + }); + + const exchangeTwo = ExchangeService.create(t, { + name: "testexchange-2", + currency: "TESTKUDOS", + httpPort: 8281, + database: dbExchangeTwo.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: dbDefault.connStr, + }); + + const exchangeOneBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + await exchangeOne.addBankAccount("1", exchangeOneBankAccount); + + const exchangeTwoBankAccount = await bank.createExchangeAccount( + "myexchange2", + "x", + ); + await exchangeTwo.addBankAccount("1", exchangeTwoBankAccount); + + bank.setSuggestedExchange( + exchangeOne, + exchangeOneBankAccount.accountPaytoUri, + ); + + await bank.start(); + + await bank.pingUntilAvailable(); + + // Set up the first exchange + + exchangeOne.addOfferedCoins(defaultCoinConfig); + await exchangeOne.start(); + await exchangeOne.pingUntilAvailable(); + + // Set up the second exchange + + exchangeTwo.addOfferedCoins(defaultCoinConfig); + await exchangeTwo.start(); + await exchangeTwo.pingUntilAvailable(); + + // Start and configure merchant + + merchant.addExchange(exchangeOne); + merchant.addExchange(exchangeTwo); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const { walletClient, walletService } = await createWalletDaemonWithClient( + t, + { name: "wallet" }, + ); + + console.log("setup done!"); + + // Withdraw digital cash into the wallet. + + await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: exchangeOne, + amount: "TESTKUDOS:6", + }); + + await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: exchangeTwo, + amount: "TESTKUDOS:6", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:10", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + console.log("making test payment"); + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runMultiExchangeTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-otp.ts b/packages/taler-harness/src/integrationtests/test-otp.ts new file mode 100644 index 000000000..4fcc8c6e9 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-otp.ts @@ -0,0 +1,119 @@ +/* + This file is part of GNU Taler + (C) 2024 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + Duration, + MerchantApiClient, + PreparePayResultType, + TransactionType, + j2s, + narrowOpSuccessOrThrow, + randomRfc3548Base32Key, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runOtpTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + const createOtpRes = await merchantClient.createOtpDevice({ + otp_algorithm: 1, + otp_device_description: "Hello", + otp_device_id: "mydevice", + otp_key: randomRfc3548Base32Key(), + }); + narrowOpSuccessOrThrow("createOtpDevice", createOtpRes); + + const createTemplateRes = await merchantClient.createTemplate({ + template_description: "my template", + template_id: "tpl1", + otp_id: "mydevice", + template_contract: { + summary: "test", + amount: "TESTKUDOS:1", + minimum_age: 0, + pay_duration: Duration.toTalerProtocolDuration( + Duration.fromSpec({ hours: 1 }), + ), + }, + }); + narrowOpSuccessOrThrow("createTemplate", createTemplateRes); + + const getTemplateResp = await merchantClient.getTemplate("tpl1"); + narrowOpSuccessOrThrow("getTemplate", getTemplateResp); + + console.log(`template: ${j2s(getTemplateResp.body)}`); + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres.withdrawalFinishedCond; + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForTemplate, + { + talerPayTemplateUri: `taler+http://pay-template/localhost:${merchant.port}/tpl1`, + templateParams: {}, + }, + ); + + console.log(preparePayResult); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + // Pay for it + + const r2 = await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + t.assertTrue(r2.type === ConfirmPayResultType.Done); + + const transaction = await walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: preparePayResult.transactionId, + }, + ); + + console.log(j2s(transaction)); + + t.assertTrue(transaction.type === TransactionType.Payment); + t.assertTrue(transaction.posConfirmation != null); + t.assertTrue(transaction.posConfirmation.length > 10); +} + +runOtpTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-pay-paid.ts b/packages/taler-harness/src/integrationtests/test-pay-paid.ts new file mode 100644 index 000000000..3d93f6e29 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-pay-paid.ts @@ -0,0 +1,213 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + URL, + codecForMerchantOrderStatusUnpaid, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { FaultInjectionRequestContext } from "../harness/faultInjection.js"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { + createFaultInjectedMerchantTestkudosEnvironment, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +/** + * Run test for the wallets repurchase detection mechanism + * based on the fulfillment URL. + * + * FIXME: This test is now almost the same as test-paywall-flow, + * since we can't initiate payment via a "claimed" private order status + * response. + */ +export async function runPayPaidTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bank, faultyExchange, faultyMerchant } = + await createFaultInjectedMerchantTestkudosEnvironment(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: faultyExchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + /** + * ========================================================================= + * Create an order and let the wallet pay under a session ID + * + * We check along the way that the JSON response to /orders/{order_id} + * returns the right thing. + * ========================================================================= + */ + + const merchant = faultyMerchant; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + let orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-one", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + let publicOrderStatusUrl = orderStatus.order_status_url; + + let publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + let pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + console.log(pubUnpaidStatus); + + let preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: pubUnpaidStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + const proposalId = preparePayResp.proposalId; + + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (after claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + const confirmPayRes = await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: proposalId, + }); + + t.assertTrue(confirmPayRes.type === ConfirmPayResultType.Done); + + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + console.log(publicOrderStatusResp.json()); + + if (publicOrderStatusResp.status != 200) { + console.log(publicOrderStatusResp.json()); + throw Error( + `expected status 200 (after paying), but got ${publicOrderStatusResp.status}`, + ); + } + + /** + * ========================================================================= + * Now change up the session ID and do payment re-play! + * ========================================================================= + */ + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-two", + }); + + console.log( + "order status under mysession-two:", + JSON.stringify(orderStatus, undefined, 2), + ); + + // Should be claimed (not paid!) because of a new session ID + t.assertTrue(orderStatus.order_status === "claimed"); + + let numPayRequested = 0; + let numPaidRequested = 0; + + faultyMerchant.faultProxy.addFault({ + async modifyRequest(ctx: FaultInjectionRequestContext) { + const url = new URL(ctx.requestUrl); + if (url.pathname.endsWith("/pay")) { + numPayRequested++; + } else if (url.pathname.endsWith("/paid")) { + numPaidRequested++; + } + }, + }); + + let orderRespTwo = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + let orderStatusTwo = await merchantClient.queryPrivateOrderStatus({ + orderId: orderRespTwo.order_id, + sessionId: "mysession-two", + }); + + t.assertTrue(orderStatusTwo.order_status === "unpaid"); + + // Pay with new taler://pay URI, which should + // have the new session ID! + // Wallet should now automatically re-play payment. + preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatusTwo.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.AlreadyConfirmed); + t.assertTrue(preparePayResp.paid); + + // Make sure the wallet is actually doing the replay properly. + t.assertTrue(numPaidRequested == 1); + t.assertTrue(numPayRequested == 0); +} + +runPayPaidTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-abort.ts b/packages/taler-harness/src/integrationtests/test-payment-abort.ts new file mode 100644 index 000000000..ca8384411 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-abort.ts @@ -0,0 +1,164 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + TalerErrorCode, + TalerErrorDetail, + URL, + codecForMerchantOrderStatusUnpaid, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { FaultInjectionRequestContext } from "../harness/faultInjection.js"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { + createFaultInjectedMerchantTestkudosEnvironment, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +export async function runPaymentAbortTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bank, exchange, faultyMerchant, faultyExchange } = + await createFaultInjectedMerchantTestkudosEnvironment(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: faultyExchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + const merchantClient = new MerchantApiClient( + faultyMerchant.makeInstanceBaseUrl(), + ); + + let orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-one", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + let publicOrderStatusUrl = orderStatus.order_status_url; + + let publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + let pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + console.log(pubUnpaidStatus); + + let preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: pubUnpaidStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (after claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + faultyMerchant.faultProxy.addFault({ + async modifyRequest(ctx: FaultInjectionRequestContext) { + const url = new URL(ctx.requestUrl); + if (!url.pathname.endsWith("/pay")) { + return; + } + ctx.dropRequest = true; + const err: TalerErrorDetail = { + code: TalerErrorCode.GENERIC_CONFIGURATION_INVALID, + hint: "something went wrong", + }; + ctx.substituteResponseStatusCode = 404; + ctx.substituteResponseBody = Buffer.from(JSON.stringify(err)); + console.log("injecting pay fault"); + }, + }); + + const confirmPayResp = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + transactionId: preparePayResp.transactionId, + }, + ); + + // Can't have succeeded yet, but network error results in "pending" state. + t.assertDeepEqual(confirmPayResp.type, ConfirmPayResultType.Pending); + + const txns = await walletClient.call(WalletApiOperation.GetTransactions, { + sort: "stable-ascending", + }); + console.log(j2s(txns)); + + await walletClient.call(WalletApiOperation.AbortTransaction, { + transactionId: txns.transactions[1].transactionId, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const txns2 = await walletClient.call(WalletApiOperation.GetTransactions, { + sort: "stable-ascending", + }); + console.log(j2s(txns2)); + + const txTypes = txns2.transactions.map((x) => x.type); + console.log(txTypes); + t.assertDeepEqual(txTypes, ["withdrawal", "payment", "refund"]); + + // FIXME: also check extended transaction list for refresh. + // FIXME: also check balance +} + +runPaymentAbortTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-claim.ts b/packages/taler-harness/src/integrationtests/test-payment-claim.ts new file mode 100644 index 000000000..dfadd9539 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-claim.ts @@ -0,0 +1,121 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + MerchantApiClient, + PreparePayResultType, + TalerErrorCode, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test where a wallet tries to claim an already claimed order. + */ +export async function runPaymentClaimTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const w2 = await createWalletDaemonWithClient(t, { name: "w2" }); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Set up order. + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const talerPayUri = orderStatus.taler_pay_uri; + + // Make wallet pay for the order + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + const errOne = t.assertThrowsTalerErrorAsync(async () => { + await w2.walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri, + }); + }); + + console.log(errOne); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + await w2.walletClient.call(WalletApiOperation.ClearDb, {}); + + const err = await t.assertThrowsTalerErrorAsync(async () => { + await w2.walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri, + }); + }); + + t.assertTrue(err.hasErrorCode(TalerErrorCode.WALLET_ORDER_ALREADY_CLAIMED)); + + await t.shutdown(); +} + +runPaymentClaimTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-deleted.ts b/packages/taler-harness/src/integrationtests/test-payment-deleted.ts new file mode 100644 index 000000000..bab8a4df1 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-deleted.ts @@ -0,0 +1,104 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test behavior when an order is deleted while the wallet is paying for it. + */ +export async function runPaymentDeletedTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // First, make a "free" payment when we don't even have + // any money in the + + // Withdraw digital cash into the wallet. + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Hello", + amount: "TESTKUDOS:2", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + await merchantClient.deleteOrder({ + orderId: orderResp.order_id, + force: true, + }); + + const r2 = await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + t.assertTrue(r2.type === ConfirmPayResultType.Pending); + + await walletClient.call(WalletApiOperation.AbortTransaction, { + transactionId: preparePayResult.transactionId, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(j2s(bal)); +} + +runPaymentDeletedTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-expired.ts b/packages/taler-harness/src/integrationtests/test-payment-expired.ts new file mode 100644 index 000000000..3f1f7f2dd --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-expired.ts @@ -0,0 +1,132 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + ConfirmPayResultType, + Duration, + MerchantApiClient, + PreparePayResultType, + TalerMerchantApi, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + applyTimeTravelV2, + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run a test for the following scenario: + * + * - Wallet claims an order + * - Merchant goes down + * - Wallet tried to pay, but it fails as the merchant is unavailable + * - The order expires + * - The merchant goes back up again + * - Instead of trying to get an abort-refund, the wallet notices that + * the order is expired, puts the transaction into "failed", + * refreshes allocated coins and thus raises the balance again. + */ +export async function runPaymentExpiredTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Order that can only be paid within five minutes. + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + pay_deadline: AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ minutes: 5 }), + ), + ), + }; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const orderResp = await merchantClient.createOrder({ + order, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertDeepEqual( + preparePayResult.status, + PreparePayResultType.PaymentPossible, + ); + + await applyTimeTravelV2( + Duration.toMilliseconds(Duration.fromSpec({ hours: 1 })), + { walletClient, exchange, merchant }, + ); + + const confirmPayResult = await walletClient.call( + WalletApiOperation.ConfirmPay, + { transactionId: preparePayResult.transactionId }, + ); + console.log("confirm pay result:"); + console.log(j2s(confirmPayResult)); + t.assertDeepEqual(confirmPayResult.type, ConfirmPayResultType.Pending); + await walletClient.call(WalletApiOperation.AbortTransaction, { + transactionId: preparePayResult.transactionId, + }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const txns = await walletClient.call(WalletApiOperation.GetTransactions, { + sort: "stable-ascending", + includeRefreshes: true, + }); + console.log(j2s(txns)); + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(j2s(bal)); + + t.assertAmountEquals(bal.balances[0].available, "TESTKUDOS:18.93"); +} + +runPaymentExpiredTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-fault.ts b/packages/taler-harness/src/integrationtests/test-payment-fault.ts new file mode 100644 index 000000000..dabe42a6b --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-fault.ts @@ -0,0 +1,234 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Sample fault injection test. + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + TalerCorebankApiClient, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + FaultInjectedExchangeService, + FaultInjectionRequestContext, + FaultInjectionResponseContext, +} from "../harness/faultInjection.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runPaymentFaultTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL( + "accounts/exchange/taler-wire-gateway/", + bank.baseUrl, + ).href, + accountPaytoUri: exchangePaytoUri, + }); + + const faultyExchange = new FaultInjectedExchangeService(t, exchange, 8091); + // Base URL must contain port that the proxy is listening on. + await exchange.modifyConfig(async (config) => { + config.setString("exchange", "base_url", "http://localhost:8091/"); + }); + + bank.setSuggestedExchange(faultyExchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + exchange.addOfferedCoins(defaultCoinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + // Print all requests to the exchange + faultyExchange.faultProxy.addFault({ + async modifyRequest(ctx: FaultInjectionRequestContext) { + console.log("got request", ctx); + }, + async modifyResponse(ctx: FaultInjectionResponseContext) { + console.log("got response", ctx); + }, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + merchant.addExchange(faultyExchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + console.log("setup done!"); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "default", + }); + + await walletClient.call(WalletApiOperation.GetBalances, {}); + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange: faultyExchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Set up order. + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const prepResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + // Drop 3 responses from the exchange. + let faultCount = 0; + faultyExchange.faultProxy.addFault({ + async modifyResponse(ctx: FaultInjectionResponseContext) { + console.log(`in modifyResponse for ${ctx.request.requestUrl}`); + if ( + !ctx.request.requestUrl.endsWith("/deposit") && + !ctx.request.requestUrl.endsWith("/batch-deposit") + ) { + return; + } + if (faultCount < 3) { + console.log(`blocking /deposit request #${faultCount}`); + faultCount++; + ctx.dropResponse = true; + } else { + console.log(`letting through /deposit request #${faultCount}`); + } + }, + }); + + // confirmPay won't work, as the exchange is unreachable + + const confirmPayResp = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + transactionId: prepResp.transactionId, + }, + ); + + t.assertDeepEqual(confirmPayResp.type, ConfirmPayResultType.Pending); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); +} + +runPaymentFaultTest.suites = ["wallet"]; +runPaymentFaultTest.timeoutMs = 120000; diff --git a/packages/taler-harness/src/integrationtests/test-payment-forgettable.ts b/packages/taler-harness/src/integrationtests/test-payment-forgettable.ts new file mode 100644 index 000000000..827c299a4 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-forgettable.ts @@ -0,0 +1,86 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; + +/** + * Run test for payment with a contract that has forgettable fields. + */ +export async function runPaymentForgettableTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + { + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + extra: { + foo: { bar: "baz" }, + $forgettable: { + foo: "gnu", + }, + }, + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + } + + console.log("testing with forgettable field without hash"); + + { + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + extra: { + foo: { bar: "baz" }, + $forgettable: { + foo: true, + }, + }, + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + } + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runPaymentForgettableTest.suites = ["wallet", "merchant"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-idempotency.ts b/packages/taler-harness/src/integrationtests/test-payment-idempotency.ts new file mode 100644 index 000000000..4a8e95af3 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-idempotency.ts @@ -0,0 +1,130 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { MerchantApiClient, PreparePayResultType } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test the wallet-core payment API, especially that repeated operations + * return the expected result. + */ +export async function runPaymentIdempotencyTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Set up order. + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const talerPayUri = orderStatus.taler_pay_uri; + + // Make wallet pay for the order + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + const preparePayResultRep = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + t.assertTrue( + preparePayResultRep.status === PreparePayResultType.PaymentPossible, + ); + + const proposalId = preparePayResult.proposalId; + + const confirmPayResult = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + proposalId: proposalId, + }, + ); + + console.log("confirm pay result", confirmPayResult); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + const preparePayResultAfter = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri, + }, + ); + + console.log("result after:", preparePayResultAfter); + + t.assertTrue( + preparePayResultAfter.status === PreparePayResultType.AlreadyConfirmed, + ); + t.assertTrue(preparePayResultAfter.paid === true); + + await t.shutdown(); +} + +runPaymentIdempotencyTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-multiple.ts b/packages/taler-harness/src/integrationtests/test-payment-multiple.ts new file mode 100644 index 000000000..3c902ee17 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-multiple.ts @@ -0,0 +1,193 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { MerchantApiClient, TalerCorebankApiClient } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { coin_ct10, coin_u1 } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +async function setupTest(t: GlobalTestState): Promise<{ + merchant: MerchantService; + exchange: ExchangeService; + bankClient: TalerCorebankApiClient; +}> { + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + exchange.addOfferedCoins([coin_ct10, coin_u1]); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + return { + merchant, + bankClient, + exchange, + }; +} + +/** + * Run test. + * + * This test uses a very sub-optimal denomination structure. + */ +export async function runPaymentMultipleTest(t: GlobalTestState) { + // Set up test environment + + const { merchant, bankClient, exchange } = await setupTest(t); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "default", + }); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:100", + }); + + await wres.withdrawalFinishedCond; + + // Set up order. + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:80", + fulfillment_url: "taler://fulfillment-success/thx", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const r1 = await walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: r1.transactionId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + await t.shutdown(); +} + +runPaymentMultipleTest.suites = ["wallet"]; +runPaymentMultipleTest.timeoutMs = 120000; diff --git a/packages/taler-harness/src/integrationtests/test-payment-share.ts b/packages/taler-harness/src/integrationtests/test-payment-share.ts new file mode 100644 index 000000000..25cfb50c6 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-share.ts @@ -0,0 +1,309 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + ConfirmPayResultType, + MerchantApiClient, + NotificationType, + PreparePayResultType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runPaymentShareTest(t: GlobalTestState) { + // Set up test environment + const { + walletClient: firstWallet, + bankClient, + exchange, + merchant, + } = await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + await withdrawViaBankV3(t, { + walletClient: firstWallet, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await firstWallet.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const { walletClient: secondWallet } = await createWalletDaemonWithClient(t, { + name: "wallet2", + }); + + await withdrawViaBankV3(t, { + walletClient: secondWallet, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await secondWallet.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + { + const first = await firstWallet.call(WalletApiOperation.GetBalances, {}); + const second = await secondWallet.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals(first.balances[0].available, "TESTKUDOS:19.53"); + t.assertAmountEquals(second.balances[0].available, "TESTKUDOS:19.53"); + } + + t.logStep("setup-done"); + + // create two orders to pay + async function createOrder(amount: string) { + const order = { + summary: "Buy me!", + amount: amount as AmountString, + fulfillment_url: "taler://fulfillment-success/thx", + }; + + const args = { order }; + + const orderResp = await merchantClient.createOrder({ + order: args.order, + }); + + const orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + return { id: orderResp.order_id, uri: orderStatus.taler_pay_uri }; + } + + t.logStep("orders-created"); + + /** + * Case 1: + * - Claim with first wallet and pay in the second wallet. + * - First wallet should be notified. + */ + { + const order = await createOrder("TESTKUDOS:5"); + // Claim the order with the first wallet + const claimFirstWallet = await firstWallet.call( + WalletApiOperation.PreparePayForUri, + { talerPayUri: order.uri }, + ); + + t.assertTrue( + claimFirstWallet.status === PreparePayResultType.PaymentPossible, + ); + + t.logStep("w1-payment-possible"); + + // share order from the first wallet + const { privatePayUri } = await firstWallet.call( + WalletApiOperation.SharePayment, + { + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + orderId: order.id, + }, + ); + + t.logStep("w1-payment-shared"); + + // claim from the second wallet + const claimSecondWallet = await secondWallet.call( + WalletApiOperation.PreparePayForUri, + { talerPayUri: privatePayUri }, + ); + + t.assertTrue( + claimSecondWallet.status === PreparePayResultType.PaymentPossible, + ); + + t.logStep("w2-claimed"); + + // pay from the second wallet + const r2 = await secondWallet.call(WalletApiOperation.ConfirmPay, { + transactionId: claimSecondWallet.transactionId, + }); + + t.assertTrue(r2.type === ConfirmPayResultType.Done); + + t.logStep("w2-confirmed"); + + // Wait for refresh to settle before we do checks + await secondWallet.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + t.logStep("w2-refresh-settled"); + + { + const first = await firstWallet.call(WalletApiOperation.GetBalances, {}); + const second = await secondWallet.call( + WalletApiOperation.GetBalances, + {}, + ); + t.assertAmountEquals(first.balances[0].available, "TESTKUDOS:19.53"); + t.assertAmountEquals(second.balances[0].available, "TESTKUDOS:14.23"); + } + + t.logStep("wait-for-payment"); + // firstWallet.waitForNotificationCond(n => + // n.type === NotificationType.TransactionStateTransition && + // n.transactionId === claimFirstWallet.transactionId + // ) + // Claim the order with the first wallet + const claimFirstWalletAgain = await firstWallet.call( + WalletApiOperation.PreparePayForUri, + { talerPayUri: order.uri }, + ); + + t.assertTrue( + claimFirstWalletAgain.status === PreparePayResultType.AlreadyConfirmed, + ); + t.assertTrue( claimFirstWalletAgain.paid ); + + t.logStep("w1-prepared-again"); + + const r1 = await firstWallet.call(WalletApiOperation.ConfirmPay, { + transactionId: claimFirstWallet.transactionId, + }); + + //t.assertTrue(r1.type === ConfirmPayResultType.Pending); + + t.logStep("w1-confirmed-shared"); + + await firstWallet.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + await secondWallet.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + /** + * only the second wallet balance was affected + */ + { + const first = await firstWallet.call(WalletApiOperation.GetBalances, {}); + const second = await secondWallet.call( + WalletApiOperation.GetBalances, + {}, + ); + t.assertAmountEquals(first.balances[0].available, "TESTKUDOS:19.53"); + t.assertAmountEquals(second.balances[0].available, "TESTKUDOS:14.23"); + } + } + + t.logStep("first-case-done"); + + /** + * Case 2: + * - Claim with first wallet and share with the second wallet + * - Pay with the first wallet, second wallet should be notified + */ + { + const order = await createOrder("TESTKUDOS:3"); + // Claim the order with the first wallet + const claimFirstWallet = await firstWallet.call( + WalletApiOperation.PreparePayForUri, + { talerPayUri: order.uri }, + ); + + t.assertTrue( + claimFirstWallet.status === PreparePayResultType.PaymentPossible, + ); + + t.logStep("case2-w1-claimed"); + + // share order from the first wallet + const { privatePayUri } = await firstWallet.call( + WalletApiOperation.SharePayment, + { + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + orderId: order.id, + }, + ); + + t.logStep("case2-w1-shared"); + + // claim from the second wallet + const claimSecondWallet = await secondWallet.call( + WalletApiOperation.PreparePayForUri, + { talerPayUri: privatePayUri }, + ); + + t.logStep("case2-w2-prepared"); + + t.assertTrue( + claimSecondWallet.status === PreparePayResultType.PaymentPossible, + ); + + // pay from the first wallet + const r2 = await firstWallet.call(WalletApiOperation.ConfirmPay, { + transactionId: claimFirstWallet.transactionId, + }); + + t.assertTrue(r2.type === ConfirmPayResultType.Done); + + // Wait for refreshes to settle before doing checks + await firstWallet.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + /** + * only the first wallet balance was affected + */ + const bal1 = await firstWallet.call(WalletApiOperation.GetBalances, {}); + const bal2 = await secondWallet.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals(bal1.balances[0].available, "TESTKUDOS:16.18"); + t.assertAmountEquals(bal2.balances[0].available, "TESTKUDOS:14.23"); + + t.logStep("wait-for-payment"); + // secondWallet.waitForNotificationCond(n => + // n.type === NotificationType.TransactionStateTransition && + // n.transactionId === claimSecondWallet.transactionId + // ) + + // Claim the order with the first wallet + const claimSecondWalletAgain = await secondWallet.call( + WalletApiOperation.PreparePayForUri, + { talerPayUri: order.uri }, + ); + + t.assertTrue( + claimSecondWalletAgain.status === PreparePayResultType.AlreadyConfirmed, + ); + t.assertTrue( + claimSecondWalletAgain.paid, + ); + + } + + t.logStep("second-case-done"); +} + +runPaymentShareTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-template.ts b/packages/taler-harness/src/integrationtests/test-payment-template.ts new file mode 100644 index 000000000..451a7dbe9 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-template.ts @@ -0,0 +1,125 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + ConfirmPayResultType, + Duration, + MerchantApiClient, + PreparePayResultType, + narrowOpSuccessOrThrow, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test for taler://payment-template/ URIs + */ +export async function runPaymentTemplateTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const mySummary = "hello, I'm a summary"; + + const createTemplateRes = await merchantClient.createTemplate({ + template_id: "template1", + template_description: "my test template", + template_contract: { + minimum_age: 0, + pay_duration: Duration.toTalerProtocolDuration( + Duration.fromSpec({ + minutes: 2, + }), + ), + summary: mySummary, + }, + editable_defaults: { + amount: "TESTKUDOS:1" as AmountString, + }, + }); + narrowOpSuccessOrThrow("createTemplate", createTemplateRes); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres.withdrawalFinishedCond; + + const talerPayTemplateUri = `taler+http://pay-template/localhost:${merchant.port}/template1`; + + const checkPayTemplateResult = await walletClient.call( + WalletApiOperation.CheckPayForTemplate, + { + talerPayTemplateUri, + }, + ); + + t.assertDeepEqual( + checkPayTemplateResult.template_contract.summary, + mySummary, + ); + + // Request a template payment + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForTemplate, + { + talerPayTemplateUri, + templateParams: {}, + }, + ); + + console.log(preparePayResult); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + // Pay for it + + const r2 = await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + t.assertTrue(r2.type === ConfirmPayResultType.Done); + + // Check if payment was successful. + + const orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: preparePayResult.contractTerms.order_id, + instance: "default", + }); + + t.assertTrue(orderStatus.order_status === "paid"); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runPaymentTemplateTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-transient.ts b/packages/taler-harness/src/integrationtests/test-payment-transient.ts new file mode 100644 index 000000000..1911b5e92 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-transient.ts @@ -0,0 +1,179 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + TalerErrorCode, + TalerErrorDetail, + URL, + codecForMerchantOrderStatusUnpaid, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { FaultInjectionResponseContext } from "../harness/faultInjection.js"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { + createFaultInjectedMerchantTestkudosEnvironment, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +/** + * Run test for a payment where the merchant has a transient + * failure in /pay + */ +export async function runPaymentTransientTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bank, faultyMerchant, faultyExchange } = + await createFaultInjectedMerchantTestkudosEnvironment(t); + + const merchantClient = new MerchantApiClient( + faultyMerchant.makeInstanceBaseUrl(), + ); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: faultyExchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + let orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-one", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + let publicOrderStatusUrl = orderStatus.order_status_url; + + let publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + let pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + console.log(pubUnpaidStatus); + + let preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: pubUnpaidStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + const proposalId = preparePayResp.proposalId; + + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (after claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + let faultInjected = false; + + faultyMerchant.faultProxy.addFault({ + async modifyResponse(ctx: FaultInjectionResponseContext) { + console.log("in modifyResponse"); + const url = new URL(ctx.request.requestUrl); + console.log("pathname is", url.pathname); + if (!url.pathname.endsWith("/pay")) { + return; + } + if (faultInjected) { + console.log("not injecting pay fault"); + return; + } + faultInjected = true; + console.log("injecting pay fault"); + const err: TalerErrorDetail = { + code: TalerErrorCode.GENERIC_DB_COMMIT_FAILED, + hint: "something went wrong", + }; + ctx.responseBody = Buffer.from(JSON.stringify(err)); + ctx.statusCode = 500; + }, + }); + + const confirmPayResp = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + proposalId, + }, + ); + + console.log(confirmPayResp); + + t.assertTrue(confirmPayResp.type === ConfirmPayResultType.Pending); + t.assertTrue(faultInjected); + + const confirmPayRespTwo = await walletClient.call( + WalletApiOperation.ConfirmPay, + { + proposalId, + }, + ); + + t.assertTrue(confirmPayRespTwo.type === ConfirmPayResultType.Done); + + // Now ask the merchant if paid + + console.log("requesting", publicOrderStatusUrl); + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl); + + console.log(publicOrderStatusResp.json()); + + if (publicOrderStatusResp.status != 200) { + console.log(publicOrderStatusResp.json()); + throw Error( + `expected status 200 (after paying), but got ${publicOrderStatusResp.status}`, + ); + } +} + +runPaymentTransientTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment-zero.ts b/packages/taler-harness/src/integrationtests/test-payment-zero.ts new file mode 100644 index 000000000..3a74a9cf2 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment-zero.ts @@ -0,0 +1,69 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, + makeTestPaymentV2, +} from "../harness/helpers.js"; +import { TransactionMajorState } from "@gnu-taler/taler-util"; + +/** + * Run test for a payment for a "free" order with + * an amount of zero. + */ +export async function runPaymentZeroTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // First, make a "free" payment when we don't even have + // any money in the + + // Withdraw digital cash into the wallet. + await withdrawViaBankV3(t, { walletClient, bankClient, exchange, amount: "TESTKUDOS:20" }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + await makeTestPaymentV2(t, { + walletClient, + merchant, + order: { + summary: "I am free!", + amount: "TESTKUDOS:0", + fulfillment_url: "taler://fulfillment-success/thx", + }, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const transactions = await walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + + for (const tr of transactions.transactions) { + t.assertDeepEqual(tr.txState.major, TransactionMajorState.Done); + } +} + +runPaymentZeroTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-payment.ts b/packages/taler-harness/src/integrationtests/test-payment.ts new file mode 100644 index 000000000..5da6d608d --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-payment.ts @@ -0,0 +1,88 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { TalerMerchantApi, j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runPaymentTest(t: GlobalTestState) { + // Set up test environment + + const { bankClient, walletClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + t.assertTrue(bankClient !== undefined); + await withdrawViaBankV3(t, { + walletClient, + exchange, + amount: "TESTKUDOS:20", + bankClient, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + } satisfies TalerMerchantApi.Order; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Test JSON normalization of contract terms: Does the wallet + // agree with the merchant? + const order2 = { + summary: "Testing “unicode” characters: 😁😱😇🥺🫦", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + } satisfies TalerMerchantApi.Order; + + await makeTestPaymentV2(t, { walletClient, merchant, order: order2 }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Test JSON normalization of contract terms: Does the wallet + // agree with the merchant? + const order3 = { + summary: "Testing\nNewlines\rAnd\tStuff\nHere\b", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + } satisfies TalerMerchantApi.Order; + + await makeTestPaymentV2(t, { walletClient, merchant, order: order3 }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(`balance after 3 payments: ${j2s(bal)}`); + t.assertAmountEquals(bal.balances[0].available, "TESTKUDOS:3.8"); + t.assertAmountEquals(bal.balances[0].pendingIncoming, "TESTKUDOS:0"); + t.assertAmountEquals(bal.balances[0].pendingOutgoing, "TESTKUDOS:0"); +} + +runPaymentTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-paywall-flow.ts b/packages/taler-harness/src/integrationtests/test-paywall-flow.ts new file mode 100644 index 000000000..de3961dec --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-paywall-flow.ts @@ -0,0 +1,250 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + MerchantApiClient, + PreparePayResultType, + URL, + codecForMerchantOrderStatusUnpaid, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runPaywallFlowTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + /** + * ========================================================================= + * Create an order and let the wallet pay under a session ID + * + * We check along the way that the JSON response to /orders/{order_id} + * returns the right thing. + * ========================================================================= + */ + + let orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + const firstOrderId = orderResp.order_id; + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-one", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const talerPayUriOne = orderStatus.taler_pay_uri; + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + let publicOrderStatusUrl = new URL(orderStatus.order_status_url); + + let publicOrderStatusResp = await harnessHttpLib.fetch( + publicOrderStatusUrl.href, + ); + + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (before claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + let pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + console.log(pubUnpaidStatus); + + let preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: pubUnpaidStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.PaymentPossible); + + const proposalId = preparePayResp.proposalId; + + console.log("requesting", publicOrderStatusUrl.href); + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl.href); + console.log("response body", publicOrderStatusResp.json()); + if (publicOrderStatusResp.status != 402) { + throw Error( + `expected status 402 (after claiming), but got ${publicOrderStatusResp.status}`, + ); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + const confirmPayRes = await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: proposalId, + }); + + t.assertTrue(confirmPayRes.type === ConfirmPayResultType.Done); + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl.href); + + console.log(publicOrderStatusResp.json()); + + if (publicOrderStatusResp.status != 200) { + console.log(publicOrderStatusResp.json()); + throw Error( + `expected status 200 (after paying), but got ${publicOrderStatusResp.status}`, + ); + } + + /** + * ========================================================================= + * Now change up the session ID! + * ========================================================================= + */ + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + sessionId: "mysession-two", + }); + + // Should be claimed (not paid!) because of a new session ID + t.assertTrue(orderStatus.order_status === "claimed"); + + // Pay with new taler://pay URI, which should + // have the new session ID! + // Wallet should now automatically re-play payment. + preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: talerPayUriOne, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.AlreadyConfirmed); + t.assertTrue(preparePayResp.paid); + + /** + * ========================================================================= + * Now we test re-purchase detection. + * ========================================================================= + */ + + orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + // Same fulfillment URL as previously! + fulfillment_url: "https://example.com/article42", + public_reorder_url: "https://example.com/article42-share", + }, + }); + + const secondOrderId = orderResp.order_id; + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: secondOrderId, + sessionId: "mysession-three", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + t.assertTrue(orderStatus.already_paid_order_id === undefined); + publicOrderStatusUrl = new URL(orderStatus.order_status_url); + + // Here the re-purchase detection should kick in, + // and the wallet should re-pay for the old order + // under the new session ID (mysession-three). + preparePayResp = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertTrue(preparePayResp.status === PreparePayResultType.AlreadyConfirmed); + t.assertTrue(preparePayResp.paid); + + // The first order should now be paid under "mysession-three", + // as the wallet did re-purchase detection + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: firstOrderId, + sessionId: "mysession-three", + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + // Check that with a completely new session ID, the status would NOT + // be paid. + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: firstOrderId, + sessionId: "mysession-four", + }); + + t.assertTrue(orderStatus.order_status === "claimed"); + + // Now check if the public status of the new order is correct. + + console.log("requesting public status", publicOrderStatusUrl); + + // Ask the order status of the claimed-but-unpaid order + publicOrderStatusResp = await harnessHttpLib.fetch(publicOrderStatusUrl.href); + + if (publicOrderStatusResp.status != 402) { + throw Error(`expected status 402, but got ${publicOrderStatusResp.status}`); + } + + pubUnpaidStatus = codecForMerchantOrderStatusUnpaid().decode( + publicOrderStatusResp.json(), + ); + + console.log(publicOrderStatusResp.json()); + + t.assertTrue(pubUnpaidStatus.already_paid_order_id === firstOrderId); +} + +runPaywallFlowTest.suites = ["merchant", "wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-peer-pull-large.ts b/packages/taler-harness/src/integrationtests/test-peer-pull-large.ts new file mode 100644 index 000000000..6de3c2e33 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-peer-pull-large.ts @@ -0,0 +1,194 @@ +/* + This file is part of GNU Taler + (C) 2024 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + j2s, + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, + WalletNotification, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { + BankServiceHandle, + ExchangeService, + GlobalTestState, + WalletClient, +} from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, + createWalletDaemonWithClient, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, +]; + +export async function runPeerPullLargeTest(t: GlobalTestState) { + // Set up test environment + + const { bank, exchange } = await createSimpleTestkudosEnvironmentV2( + t, + coinConfigList, + ); + + let allW1Notifications: WalletNotification[] = []; + let allW2Notifications: WalletNotification[] = []; + + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + handleNotification(wn) { + allW1Notifications.push(wn); + }, + }); + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + persistent: true, + handleNotification(wn) { + allW2Notifications.push(wn); + }, + }); + + // Withdraw digital cash into the wallet. + const wallet1 = w1.walletClient; + const wallet2 = w2.walletClient; + + await checkNormalPeerPull(t, bank, exchange, wallet1, wallet2); +} + +async function checkNormalPeerPull( + t: GlobalTestState, + bank: BankServiceHandle, + exchange: ExchangeService, + wallet1: WalletClient, + wallet2: WalletClient, +): Promise<void> { + const withdrawRes = await withdrawViaBankV2(t, { + walletClient: wallet2, + bank, + exchange, + amount: "TESTKUDOS:500", + }); + + await withdrawRes.withdrawalFinishedCond; + + const purseExpiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const resp = await wallet1.client.call( + WalletApiOperation.InitiatePeerPullCredit, + { + exchangeBaseUrl: exchange.baseUrl, + partialContractTerms: { + summary: "Hello World", + amount: "TESTKUDOS:200" as AmountString, + purse_expiration: purseExpiration, + }, + }, + ); + + const peerPullCreditReadyCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready, + ); + + await peerPullCreditReadyCond; + + const creditTx = await wallet1.call(WalletApiOperation.GetTransactionById, { + transactionId: resp.transactionId, + }); + + t.assertDeepEqual(creditTx.type, TransactionType.PeerPullCredit); + t.assertTrue(!!creditTx.talerUri); + + const checkResp = await wallet2.client.call( + WalletApiOperation.PreparePeerPullDebit, + { + talerUri: creditTx.talerUri, + }, + ); + + console.log(`checkResp: ${j2s(checkResp)}`); + + const peerPullCreditDoneCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + + const peerPullDebitDoneCond = wallet2.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === checkResp.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + + await wallet2.client.call(WalletApiOperation.ConfirmPeerPullDebit, { + transactionId: checkResp.transactionId, + }); + + await peerPullCreditDoneCond; + await peerPullDebitDoneCond; + + const txn1 = await wallet1.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + const txn2 = await wallet2.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log(`txn1: ${j2s(txn1)}`); + console.log(`txn2: ${j2s(txn2)}`); +} + +runPeerPullLargeTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-peer-push-large.ts b/packages/taler-harness/src/integrationtests/test-peer-push-large.ts new file mode 100644 index 000000000..b7fbe9f6e --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-peer-push-large.ts @@ -0,0 +1,177 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, + WalletNotification, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, + createWalletDaemonWithClient, + withdrawViaBankV2, +} from "../harness/helpers.js"; +import { CoinConfig } from "../harness/denomStructures.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, +]; + +/** + * Run a test for a multi-batch peer push payment. + */ +export async function runPeerPushLargeTest(t: GlobalTestState) { + const { bank, exchange } = await createSimpleTestkudosEnvironmentV2(t, coinConfigList); + + let allW1Notifications: WalletNotification[] = []; + let allW2Notifications: WalletNotification[] = []; + + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + handleNotification(wn) { + allW1Notifications.push(wn); + }, + }); + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + handleNotification(wn) { + allW2Notifications.push(wn); + }, + }); + + // Withdraw digital cash into the wallet. + + const withdrawRes = await withdrawViaBankV2(t, { + walletClient: w1.walletClient, + bank, + exchange, + amount: "TESTKUDOS:300", + }); + + await withdrawRes.withdrawalFinishedCond; + + const purse_expiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const checkResp0 = await w1.walletClient.call( + WalletApiOperation.CheckPeerPushDebit, + { + amount: "TESTKUDOS:200" as AmountString, + }, + ); + + t.assertAmountEquals(checkResp0.amountEffective, "TESTKUDOS:200"); + + const resp = await w1.walletClient.call( + WalletApiOperation.InitiatePeerPushDebit, + { + partialContractTerms: { + summary: "Hello World 🥺", + amount: "TESTKUDOS:200" as AmountString, + purse_expiration, + }, + }, + ); + + console.log(resp); + + const peerPushReadyCond = w1.walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready && + x.transactionId === resp.transactionId, + ); + + await peerPushReadyCond; + + const txDetails = await w1.walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: resp.transactionId, + }, + ); + t.assertDeepEqual(txDetails.type, TransactionType.PeerPushDebit); + t.assertTrue(!!txDetails.talerUri); + + const checkResp = await w2.walletClient.call( + WalletApiOperation.PreparePeerPushCredit, + { + talerUri: txDetails.talerUri, + }, + ); + + console.log(checkResp); + + const acceptResp = await w2.walletClient.call( + WalletApiOperation.ConfirmPeerPushCredit, + { + transactionId: checkResp.transactionId, + }, + ); + + console.log(acceptResp); + + await w2.walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + const txn1 = await w1.walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + const txn2 = await w2.walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log(`txn1: ${j2s(txn1)}`); + console.log(`txn2: ${j2s(txn2)}`); +} + +runPeerPushLargeTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-peer-repair.ts b/packages/taler-harness/src/integrationtests/test-peer-repair.ts new file mode 100644 index 000000000..22d3fe7ad --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-peer-repair.ts @@ -0,0 +1,213 @@ +/* + This file is part of GNU Taler + (C) 2023 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, + WalletNotification, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import * as fs from "node:fs"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +export async function runPeerRepairTest(t: GlobalTestState) { + // Set up test environment + + const { bankClient, exchange } = await createSimpleTestkudosEnvironmentV3(t); + + let allW1Notifications: WalletNotification[] = []; + let allW2Notifications: WalletNotification[] = []; + + let w1 = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + handleNotification(wn) { + allW1Notifications.push(wn); + }, + }); + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + handleNotification(wn) { + allW2Notifications.push(wn); + }, + }); + + // Withdraw digital cash into the wallet. + let wallet1 = w1.walletClient; + const wallet2 = w2.walletClient; + + const withdrawalDoneCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Done && + x.transactionId.startsWith("txn:withdrawal:"), + ); + + await withdrawViaBankV3(t, { + walletClient: wallet1, + bankClient, + exchange, + amount: "TESTKUDOS:5", + }); + + await withdrawalDoneCond; + const w1DbPath = w1.walletService.dbPath; + const w1DbCopyPath = w1.walletService.dbPath + ".copy"; + fs.copyFileSync(w1DbPath, w1DbCopyPath); + + const purse_expiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const resp1 = await wallet1.client.call( + WalletApiOperation.InitiatePeerPushDebit, + { + exchangeBaseUrl: exchange.baseUrl, + partialContractTerms: { + summary: "Hello World", + amount: "TESTKUDOS:3" as AmountString, + purse_expiration, + }, + }, + ); + + const peerPushDebitReady1Cond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp1.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready, + ); + + await peerPushDebitReady1Cond; + + const txDetails = await wallet1.call(WalletApiOperation.GetTransactionById, { + transactionId: resp1.transactionId, + }); + t.assertDeepEqual(txDetails.type, TransactionType.PeerPushDebit); + t.assertTrue(!!txDetails.talerUri); + + const resp2 = await wallet2.client.call( + WalletApiOperation.PreparePeerPushCredit, + { + talerUri: txDetails.talerUri, + }, + ); + + const peerPushCreditDone1Cond = wallet2.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp2.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + + const peerPushDebitDone1Cond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp1.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + + await wallet2.client.call(WalletApiOperation.ConfirmPeerPushCredit, { + transactionId: resp2.transactionId, + }); + + await peerPushCreditDone1Cond; + await peerPushDebitDone1Cond; + + w1.walletClient.remoteWallet?.close(); + await w1.walletService.stop(); + + fs.copyFileSync(w1DbCopyPath, w1DbPath); + + console.log(`copied back to ${w1DbPath}`); + + w1 = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + handleNotification(wn) { + allW1Notifications.push(wn); + }, + }); + wallet1 = w1.walletClient; + + console.log("attempting peer-push-debit, should fail."); + + const initResp2 = await wallet1.client.call( + WalletApiOperation.InitiatePeerPushDebit, + { + exchangeBaseUrl: exchange.baseUrl, + partialContractTerms: { + summary: "Hello World", + amount: "TESTKUDOS:3" as AmountString, + purse_expiration, + }, + }, + ); + + const peerPushDebitFailingCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === initResp2.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.errorInfo != null, + ); + + console.log(`waiting for error on ${initResp2.transactionId}`); + + await peerPushDebitFailingCond; + + console.log("reached error"); + + // Now withdraw so we have enough coins to re-select + + const withdraw2Res = await withdrawViaBankV3(t, { + walletClient: wallet1, + bankClient, + exchange, + amount: "TESTKUDOS:5", + }); + + await withdraw2Res.withdrawalFinishedCond; + + const peerPushDebitReady2Cond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === initResp2.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready, + ); + + await peerPushDebitReady2Cond; +} + +runPeerRepairTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-peer-to-peer-pull.ts b/packages/taler-harness/src/integrationtests/test-peer-to-peer-pull.ts new file mode 100644 index 000000000..d94c5985f --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-peer-to-peer-pull.ts @@ -0,0 +1,285 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + j2s, + NotificationType, + TalerCorebankApiClient, + TransactionMajorState, + TransactionMinorState, + TransactionType, + WalletNotification, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { + ExchangeService, + GlobalTestState, + WalletClient, +} from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runPeerToPeerPullTest(t: GlobalTestState) { + // Set up test environment + + const { bankClient, exchange } = await createSimpleTestkudosEnvironmentV3(t); + + let allW1Notifications: WalletNotification[] = []; + let allW2Notifications: WalletNotification[] = []; + + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + handleNotification(wn) { + allW1Notifications.push(wn); + }, + }); + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + persistent: true, + handleNotification(wn) { + allW2Notifications.push(wn); + }, + }); + + // Withdraw digital cash into the wallet. + const wallet1 = w1.walletClient; + const wallet2 = w2.walletClient; + + await checkNormalPeerPull(t, bankClient, exchange, wallet1, wallet2); + + console.log(`w1 notifications: ${j2s(allW1Notifications)}`); + + // Check that we don't have an excessive number of notifications. + t.assertTrue(allW1Notifications.length <= 60); + + await checkAbortedPeerPull(t, bankClient, exchange, wallet1, wallet2); +} + +async function checkNormalPeerPull( + t: GlobalTestState, + bankClient: TalerCorebankApiClient, + exchange: ExchangeService, + wallet1: WalletClient, + wallet2: WalletClient, +): Promise<void> { + const withdrawRes = await withdrawViaBankV3(t, { + walletClient: wallet2, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await withdrawRes.withdrawalFinishedCond; + + const purseExpiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const resp = await wallet1.client.call( + WalletApiOperation.InitiatePeerPullCredit, + { + exchangeBaseUrl: exchange.baseUrl, + partialContractTerms: { + summary: "Hello World", + amount: "TESTKUDOS:5" as AmountString, + purse_expiration: purseExpiration, + }, + }, + ); + + const peerPullCreditReadyCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready, + ); + + await peerPullCreditReadyCond; + + const creditTx = await wallet1.call(WalletApiOperation.GetTransactionById, { + transactionId: resp.transactionId, + }); + + t.assertDeepEqual(creditTx.type, TransactionType.PeerPullCredit); + t.assertTrue(!!creditTx.talerUri); + + const checkResp = await wallet2.client.call( + WalletApiOperation.PreparePeerPullDebit, + { + talerUri: creditTx.talerUri, + }, + ); + + console.log(`checkResp: ${j2s(checkResp)}`); + + const peerPullCreditDoneCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + + const peerPullDebitDoneCond = wallet2.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === checkResp.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + + await wallet2.client.call(WalletApiOperation.ConfirmPeerPullDebit, { + transactionId: checkResp.transactionId, + }); + + await peerPullCreditDoneCond; + await peerPullDebitDoneCond; + + const txn1 = await wallet1.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + const txn2 = await wallet2.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log(`txn1: ${j2s(txn1)}`); + console.log(`txn2: ${j2s(txn2)}`); +} + +async function checkAbortedPeerPull( + t: GlobalTestState, + bankClient: TalerCorebankApiClient, + exchange: ExchangeService, + wallet1: WalletClient, + wallet2: WalletClient, +): Promise<void> { + const withdrawRes = await withdrawViaBankV3(t, { + walletClient: wallet2, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await withdrawRes.withdrawalFinishedCond; + + const purseExpiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const resp = await wallet1.client.call( + WalletApiOperation.InitiatePeerPullCredit, + { + exchangeBaseUrl: exchange.baseUrl, + partialContractTerms: { + summary: "Hello World", + amount: "TESTKUDOS:5" as AmountString, + purse_expiration: purseExpiration, + }, + }, + ); + + const peerPullCreditReadyCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready, + ); + + await peerPullCreditReadyCond; + + const creditTx = await wallet1.call(WalletApiOperation.GetTransactionById, { + transactionId: resp.transactionId, + }); + + t.assertDeepEqual(creditTx.type, TransactionType.PeerPullCredit); + t.assertTrue(!!creditTx.talerUri); + + const checkResp = await wallet2.client.call( + WalletApiOperation.PreparePeerPullDebit, + { + talerUri: creditTx.talerUri, + }, + ); + + console.log(`checkResp: ${j2s(checkResp)}`); + + const peerPullCreditAbortedCond = wallet1.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === resp.transactionId && + x.newTxState.major === TransactionMajorState.Aborted, + ); + + const peerPullDebitAbortedCond = wallet2.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === checkResp.transactionId && + x.newTxState.major === TransactionMajorState.Aborted, + ); + + await wallet1.call(WalletApiOperation.AbortTransaction, { + transactionId: resp.transactionId, + }); + + await wallet2.client.call(WalletApiOperation.ConfirmPeerPullDebit, { + transactionId: checkResp.transactionId, + }); + + console.log(`waiting for ${resp.transactionId} to go to state aborted`); + console.log("checkpoint: before-aborted-wait"); + await peerPullCreditAbortedCond; + console.log("checkpoint: after-credit-aborted-wait"); + await peerPullDebitAbortedCond; + console.log("checkpoint: after-debit-aborted-wait"); + console.log("checkpoint: after-aborted-wait"); + + const txn1 = await wallet1.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + const txn2 = await wallet2.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log(`txn1: ${j2s(txn1)}`); + console.log(`txn2: ${j2s(txn2)}`); +} + +runPeerToPeerPullTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-peer-to-peer-push.ts b/packages/taler-harness/src/integrationtests/test-peer-to-peer-push.ts new file mode 100644 index 000000000..e38b690ab --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-peer-to-peer-push.ts @@ -0,0 +1,264 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, + WalletNotification, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run a test for basic peer-push payments. + */ +export async function runPeerToPeerPushTest(t: GlobalTestState) { + const { bankClient, exchange } = await createSimpleTestkudosEnvironmentV3(t); + + let allW1Notifications: WalletNotification[] = []; + let allW2Notifications: WalletNotification[] = []; + + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + handleNotification(wn) { + allW1Notifications.push(wn); + }, + }); + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + handleNotification(wn) { + allW2Notifications.push(wn); + }, + }); + + // Withdraw digital cash into the wallet. + + const withdrawRes = await withdrawViaBankV3(t, { + walletClient: w1.walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await withdrawRes.withdrawalFinishedCond; + + const purse_expiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const checkResp0 = await w1.walletClient.call( + WalletApiOperation.CheckPeerPushDebit, + { + amount: "TESTKUDOS:5" as AmountString, + }, + ); + + t.assertAmountEquals(checkResp0.amountEffective, "TESTKUDOS:5.49"); + + { + const resp = await w1.walletClient.call( + WalletApiOperation.InitiatePeerPushDebit, + { + partialContractTerms: { + summary: "Hello World 😁😇", + amount: "TESTKUDOS:5" as AmountString, + purse_expiration, + }, + }, + ); + + console.log(resp); + } + + { + const bal = await w1.walletClient.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals(bal.balances[0].pendingOutgoing, "TESTKUDOS:5.49"); + } + + await w1.walletClient.call(WalletApiOperation.TestingWaitRefreshesFinal, {}); + + const resp = await w1.walletClient.call( + WalletApiOperation.InitiatePeerPushDebit, + { + partialContractTerms: { + summary: "Hello World 🥺", + amount: "TESTKUDOS:5" as AmountString, + purse_expiration, + }, + }, + ); + + console.log(resp); + + const peerPushReadyCond = w1.walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready && + x.transactionId === resp.transactionId, + ); + + await peerPushReadyCond; + + const txDetails = await w1.walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: resp.transactionId, + }, + ); + t.assertDeepEqual(txDetails.type, TransactionType.PeerPushDebit); + t.assertTrue(!!txDetails.talerUri); + + const checkResp = await w2.walletClient.call( + WalletApiOperation.PreparePeerPushCredit, + { + talerUri: txDetails.talerUri, + }, + ); + + console.log(checkResp); + + const acceptResp = await w2.walletClient.call( + WalletApiOperation.ConfirmPeerPushCredit, + { + transactionId: checkResp.transactionId, + }, + ); + + console.log(acceptResp); + + const txn1 = await w1.walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + const txn2 = await w2.walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + + console.log(`txn1: ${j2s(txn1)}`); + console.log(`txn2: ${j2s(txn2)}`); + + // We expect insufficient balance here! + const ex1 = await t.assertThrowsTalerErrorAsync(async () => { + await w1.walletClient.call(WalletApiOperation.InitiatePeerPushDebit, { + partialContractTerms: { + summary: "(this will fail)", + amount: "TESTKUDOS:15" as AmountString, + purse_expiration, + }, + }); + }); + + console.log("got expected exception detail", j2s(ex1.errorDetail)); + + const initiateResp2 = await w1.walletClient.call( + WalletApiOperation.InitiatePeerPushDebit, + { + partialContractTerms: { + summary: "second tx, will expire", + amount: "TESTKUDOS:5" as AmountString, + purse_expiration, + }, + }, + ); + + const peerPushReadyCond2 = w1.walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready && + x.transactionId === initiateResp2.transactionId, + ); + + await peerPushReadyCond2; + + const txDetails3 = await w1.walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: initiateResp2.transactionId, + }, + ); + t.assertDeepEqual(txDetails3.type, TransactionType.PeerPushDebit); + t.assertTrue(!!txDetails3.talerUri); + + await w2.walletClient.call(WalletApiOperation.PreparePeerPushCredit, { + talerUri: txDetails3.talerUri, + }); + + const timetravelOffsetMs = Duration.toMilliseconds( + Duration.fromSpec({ days: 5 }), + ); + + console.log("stopping exchange to apply time-travel"); + + await exchange.stop(); + exchange.setTimetravel(timetravelOffsetMs); + await exchange.start(); + await exchange.pingUntilAvailable(); + + console.log("running expire"); + await exchange.runExpireOnce(); + console.log("done running expire"); + + console.log("purse should now be expired"); + + await w1.walletClient.call(WalletApiOperation.TestingSetTimetravel, { + offsetMs: timetravelOffsetMs, + }); + + await w2.walletClient.call(WalletApiOperation.TestingSetTimetravel, { + offsetMs: timetravelOffsetMs, + }); + + await w1.walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + await w2.walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + const txDetails2 = await w1.walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: initiateResp2.transactionId, + }, + ); + + console.log(`tx details 2: ${j2s(txDetails2)}`); +} + +runPeerToPeerPushTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-refund-auto.ts b/packages/taler-harness/src/integrationtests/test-refund-auto.ts new file mode 100644 index 000000000..5fcfa066a --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-refund-auto.ts @@ -0,0 +1,113 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { Duration, MerchantApiClient } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runRefundAutoTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Set up order. + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + auto_refund: { + d_us: 3000 * 1000, + }, + }, + refund_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 5 }), + ), + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const r1 = await walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: r1.transactionId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + const ref = await merchantClient.giveRefund({ + amount: "TESTKUDOS:5", + instance: "default", + justification: "foo", + orderId: orderResp.order_id, + }); + + console.log(ref); + + // The wallet should now automatically pick up the refund. + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const transactions = await walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log(JSON.stringify(transactions, undefined, 2)); + + const transactionTypes = transactions.transactions.map((x) => x.type); + t.assertDeepEqual(transactionTypes, ["withdrawal", "payment", "refund"]); + + await t.shutdown(); +} + +runRefundAutoTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-refund-gone.ts b/packages/taler-harness/src/integrationtests/test-refund-gone.ts new file mode 100644 index 000000000..ac3a5aebe --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-refund-gone.ts @@ -0,0 +1,139 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + Duration, + MerchantApiClient, + TransactionMajorState, + TransactionType, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + applyTimeTravelV2, + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test wallet behavior when a refund expires before the wallet + * can claim it. + */ +export async function runRefundGoneTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Set up order. + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + pay_deadline: AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ + minutes: 10, + }), + ), + ), + }, + refund_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const r1 = await walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + const r2 = await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: r1.transactionId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + console.log(orderStatus); + + await applyTimeTravelV2( + Duration.toMilliseconds(Duration.fromSpec({ hours: 1 })), + { exchange, merchant, walletClient: walletClient }, + ); + await exchange.stopAggregator(); + await exchange.runAggregatorOnce(); + + const ref = await merchantClient.giveRefund({ + amount: "TESTKUDOS:5", + instance: "default", + justification: "foo", + orderId: orderResp.order_id, + }); + + console.log(ref); + + await walletClient.call(WalletApiOperation.StartRefundQuery, { + transactionId: r1.transactionId, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + let r = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(JSON.stringify(r, undefined, 2)); + + const r3 = await walletClient.call(WalletApiOperation.GetTransactions, {}); + console.log(JSON.stringify(r3, undefined, 2)); + + const refundTx = r3.transactions[2]; + + t.assertDeepEqual(refundTx.type, TransactionType.Refund); + t.assertDeepEqual(refundTx.txState.major, TransactionMajorState.Failed); +} + +runRefundGoneTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-refund-incremental.ts b/packages/taler-harness/src/integrationtests/test-refund-incremental.ts new file mode 100644 index 000000000..2f78d7e91 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-refund-incremental.ts @@ -0,0 +1,203 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Amounts, + Duration, + MerchantApiClient, + TransactionType, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, delayMs } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runRefundIncrementalTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Set up order. + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:10", + fulfillment_url: "taler://fulfillment-success/thx", + }, + refund_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 5 }), + ), + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const r1 = await walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + proposalId: r1.proposalId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + let ref = await merchantClient.giveRefund({ + amount: "TESTKUDOS:2.5", + instance: "default", + justification: "foo", + orderId: orderResp.order_id, + }); + + console.log("first refund increase response", ref); + + { + let wr = await walletClient.call(WalletApiOperation.StartRefundQuery, { + transactionId: r1.transactionId, + }); + await walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + console.log(wr); + const txs = await walletClient.call(WalletApiOperation.GetTransactions, {}); + console.log( + "transactions after applying first refund:", + JSON.stringify(txs, undefined, 2), + ); + } + + // Wait at least a second, because otherwise the increased + // refund will be grouped with the previous one. + await delayMs(1200); + + ref = await merchantClient.giveRefund({ + amount: "TESTKUDOS:5", + instance: "default", + justification: "bar", + orderId: orderResp.order_id, + }); + + console.log("second refund increase response", ref); + + // Wait at least a second, because otherwise the increased + // refund will be grouped with the previous one. + await delayMs(1200); + + ref = await merchantClient.giveRefund({ + amount: "TESTKUDOS:10", + instance: "default", + justification: "bar", + orderId: orderResp.order_id, + }); + + console.log("third refund increase response", ref); + + { + let wr = await walletClient.call(WalletApiOperation.StartRefundQuery, { + transactionId: r1.transactionId, + }); + await walletClient.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + console.log(wr); + } + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + t.assertAmountEquals(orderStatus.refund_amount, "TESTKUDOS:10"); + + console.log(JSON.stringify(orderStatus, undefined, 2)); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(JSON.stringify(bal, undefined, 2)); + + { + const txs = await walletClient.call(WalletApiOperation.GetTransactions, {}); + console.log(JSON.stringify(txs, undefined, 2)); + + const txTypes = txs.transactions.map((x) => x.type); + t.assertDeepEqual(txTypes, ["withdrawal", "payment", "refund", "refund"]); + + for (const tx of txs.transactions) { + if (tx.type !== TransactionType.Refund) { + continue; + } + t.assertAmountLeq(tx.amountEffective, tx.amountRaw); + } + + const raw = Amounts.sum( + txs.transactions + .filter((x) => x.type === TransactionType.Refund) + .map((x) => x.amountRaw), + ).amount; + + t.assertAmountEquals("TESTKUDOS:10", raw); + + const effective = Amounts.sum( + txs.transactions + .filter((x) => x.type === TransactionType.Refund) + .map((x) => x.amountEffective), + ).amount; + + t.assertAmountEquals("TESTKUDOS:8.59", effective); + } + + await t.shutdown(); +} + +runRefundIncrementalTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-refund.ts b/packages/taler-harness/src/integrationtests/test-refund.ts new file mode 100644 index 000000000..4b197a01f --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-refund.ts @@ -0,0 +1,149 @@ +/* + This file is part of GNU Taler + (C) 2020-2024 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Duration, + j2s, + MerchantApiClient, + NotificationType, + TransactionMajorState, + TransactionType, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +export async function runRefundTest(t: GlobalTestState) { + // Set up test environment + + const { + walletClient: wallet, + bankClient, + exchange, + merchant, + } = await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const withdrawalRes = await withdrawViaBankV3(t, { + walletClient: wallet, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await withdrawalRes.withdrawalFinishedCond; + + // Set up order. + const orderResp = await merchantClient.createOrder({ + order: { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }, + refund_delay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 5 }), + ), + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const r1 = await wallet.client.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + await wallet.client.call(WalletApiOperation.ConfirmPay, { + transactionId: r1.transactionId, + }); + + // Check if payment was successful. + + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "paid"); + + { + const tx = await wallet.client.call(WalletApiOperation.GetTransactionById, { + transactionId: r1.transactionId, + }); + + t.assertTrue( + tx.type === TransactionType.Payment && tx.refundPending === undefined, + ); + } + + const ref = await merchantClient.giveRefund({ + amount: "TESTKUDOS:5", + instance: "default", + justification: "foo", + orderId: orderResp.order_id, + }); + + console.log(ref); + + { + const refundFinishedCond = wallet.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === r1.transactionId && + x.newTxState.major === TransactionMajorState.Done, + ); + await wallet.client.call(WalletApiOperation.StartRefundQuery, { + transactionId: r1.transactionId, + }); + await refundFinishedCond; + } + + { + const r2 = await wallet.client.call(WalletApiOperation.GetBalances, {}); + console.log(JSON.stringify(r2, undefined, 2)); + } + + { + const r2 = await wallet.client.call(WalletApiOperation.GetTransactions, {}); + console.log(JSON.stringify(r2, undefined, 2)); + } + + { + const tx = await wallet.client.call(WalletApiOperation.GetTransactionById, { + transactionId: r1.transactionId, + }); + + console.log(j2s(tx)); + + t.assertTrue( + tx.type === TransactionType.Payment && tx.refundPending === undefined, + ); + } +} + +runRefundTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-revocation.ts b/packages/taler-harness/src/integrationtests/test-revocation.ts new file mode 100644 index 000000000..65aa86f98 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-revocation.ts @@ -0,0 +1,267 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + TalerCorebankApiClient, + TalerMerchantApi, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + WalletCli, + WalletClient, + delayMs, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + SimpleTestEnvironmentNg3, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +async function revokeAllWalletCoins(req: { + walletClient: WalletClient; + exchange: ExchangeService; + merchant: MerchantService; +}): Promise<void> { + const { walletClient, exchange, merchant } = req; + const coinDump = await walletClient.call(WalletApiOperation.DumpCoins, {}); + console.log(coinDump); + const usedDenomHashes = new Set<string>(); + for (const coin of coinDump.coins) { + usedDenomHashes.add(coin.denom_pub_hash); + } + for (const x of usedDenomHashes.values()) { + await exchange.revokeDenomination(x); + } + await delayMs(1000); + await exchange.keyup(); + await delayMs(1000); + await merchant.stop(); + await merchant.start(); + await merchant.pingUntilAvailable(); +} + +async function createTestEnvironment( + t: GlobalTestState, +): Promise<SimpleTestEnvironmentNg3> { + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL( + "accounts/exchange/taler-wire-gateway/", + bank.baseUrl, + ).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coin_u1: CoinConfig = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + rsaKeySize: 1024, + name: `TESTKUDOS_u1`, + value: `TESTKUDOS:1`, + feeDeposit: `TESTKUDOS:0`, + feeRefresh: `TESTKUDOS:0`, + feeRefund: `TESTKUDOS:0`, + feeWithdraw: `TESTKUDOS:0`, + }; + + exchange.addCoinConfigList([coin_u1]); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const wallet = new WalletCli(t); + + const { walletService, walletClient } = await createWalletDaemonWithClient( + t, + { + name: "default", + }, + ); + + return { + commonDb: db, + exchange, + merchant, + walletClient, + walletService, + bankClient, + exchangeBankAccount: { + accountName: "", + accountPassword: "", + accountPaytoUri: "", + wireGatewayApiBaseUrl: "", + }, + }; +} + +/** + * Basic time travel test. + */ +export async function runRevocationTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createTestEnvironment(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:15", + }); + await wres.withdrawalFinishedCond; + + console.log("revoking first time"); + await revokeAllWalletCoins({ walletClient, exchange, merchant }); + + // FIXME: this shouldn't be necessary once https://bugs.taler.net/n/6565 + // is implemented. + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchange.baseUrl, + }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log("wallet balance", bal); + + const order = { + summary: "Buy me!", + amount: "TESTKUDOS:10", + fulfillment_url: "taler://fulfillment-success/thx", + } satisfies TalerMerchantApi.Order; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + + await walletClient.call(WalletApiOperation.ClearDb, {}); + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:15", + }); + + const coinDump = await walletClient.call(WalletApiOperation.DumpCoins, {}); + console.log(coinDump); + const coinPubList = coinDump.coins.map((x) => x.coin_pub); + await walletClient.call(WalletApiOperation.ForceRefresh, { + refreshCoinSpecs: coinPubList.map((x) => ({ coinPub: x })), + }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + console.log("revoking second time"); + await revokeAllWalletCoins({ walletClient, exchange, merchant }); + + // FIXME: this shouldn't be necessary once https://bugs.taler.net/n/6565 + // is implemented. + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchange.baseUrl, + }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + { + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log("wallet balance", bal); + } + + await makeTestPaymentV2(t, { walletClient, merchant, order }); +} + +runRevocationTest.timeoutMs = 120000; +runRevocationTest.suites = ["wallet"]; +runRevocationTest.experimental = true; diff --git a/packages/taler-harness/src/integrationtests/test-simple-payment.ts b/packages/taler-harness/src/integrationtests/test-simple-payment.ts new file mode 100644 index 000000000..846b8c8e1 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-simple-payment.ts @@ -0,0 +1,59 @@ +/* + This file is part of GNU Taler + (C) 2023 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + withdrawViaBankV2, + makeTestPaymentV2, + useSharedTestkudosEnvironment, +} from "../harness/helpers.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runSimplePaymentTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bank, exchange, merchant } = + await useSharedTestkudosEnvironment(t); + + // Withdraw digital cash into the wallet. + + await withdrawViaBankV2(t, { + walletClient, + bank, + exchange, + amount: "TESTKUDOS:20", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + } satisfies TalerMerchantApi.Order; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runSimplePaymentTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-stored-backups.ts b/packages/taler-harness/src/integrationtests/test-stored-backups.ts new file mode 100644 index 000000000..732ac0aed --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-stored-backups.ts @@ -0,0 +1,113 @@ +/* + This file is part of GNU Taler + (C) 2023 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + withdrawViaBankV2, + makeTestPaymentV2, + useSharedTestkudosEnvironment, +} from "../harness/helpers.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; + +/** + * Test stored backup wallet-core API. + */ +export async function runStoredBackupsTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bank, exchange, merchant } = + await useSharedTestkudosEnvironment(t); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres; + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const sb1Resp = await walletClient.call( + WalletApiOperation.CreateStoredBackup, + {}, + ); + const sbList = await walletClient.call( + WalletApiOperation.ListStoredBackups, + {}, + ); + t.assertTrue(sbList.storedBackups.length === 1); + t.assertTrue(sbList.storedBackups[0].name === sb1Resp.name); + + const order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + } satisfies TalerMerchantApi.Order; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const txn1 = await walletClient.call(WalletApiOperation.GetTransactions, {}); + t.assertDeepEqual(txn1.transactions.length, 2); + + // Recover from the stored backup now. + + const sb2Resp = await walletClient.call( + WalletApiOperation.CreateStoredBackup, + {}, + ); + + console.log("recovering backup"); + + await walletClient.call(WalletApiOperation.RecoverStoredBackup, { + name: sb1Resp.name, + }); + + console.log("first recovery done"); + + // Recovery went well, now we can delete the backup + // of the old database we stored before importing. + { + const sbl1 = await walletClient.call( + WalletApiOperation.ListStoredBackups, + {}, + ); + t.assertTrue(sbl1.storedBackups.length === 2); + + await walletClient.call(WalletApiOperation.DeleteStoredBackup, { + name: sb1Resp.name, + }); + const sbl2 = await walletClient.call( + WalletApiOperation.ListStoredBackups, + {}, + ); + t.assertTrue(sbl2.storedBackups.length === 1); + } + + const txn2 = await walletClient.call(WalletApiOperation.GetTransactions, {}); + // We only have the withdrawal after restoring + t.assertDeepEqual(txn2.transactions.length, 1); +} + +runStoredBackupsTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-timetravel-autorefresh.ts b/packages/taler-harness/src/integrationtests/test-timetravel-autorefresh.ts new file mode 100644 index 000000000..e6c84b75d --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-timetravel-autorefresh.ts @@ -0,0 +1,234 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ConfirmPayResultType, + Duration, + MerchantApiClient, + NotificationType, + PreparePayResultType, + TalerCorebankApiClient, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { makeNoFeeCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + applyTimeTravelV2, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Basic time travel test. + */ +export async function runTimetravelAutorefreshTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + exchange.addCoinConfigList(makeNoFeeCoinConfig("TESTKUDOS")); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "w1", + }); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:15", + }); + await wres.withdrawalFinishedCond; + + const exchangeUpdated1Cond = walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.ExchangeStateTransition && + x.exchangeBaseUrl === exchange.baseUrl, + ); + + // Travel into the future, the deposit expiration is two years + // into the future. + console.log("applying first time travel"); + await applyTimeTravelV2( + Duration.toMilliseconds(Duration.fromSpec({ days: 400 })), + { + walletClient, + exchange, + merchant, + }, + ); + + // The time travel should cause exchanges to update. + await exchangeUpdated1Cond; + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const wres2 = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + await wres2.withdrawalFinishedCond; + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const exchangeUpdated2Cond = walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.ExchangeStateTransition && + x.exchangeBaseUrl === exchange.baseUrl, + ); + + // Travel into the future, the deposit expiration is two years + // into the future. + console.log("applying second time travel"); + await applyTimeTravelV2( + Duration.toMilliseconds(Duration.fromSpec({ years: 2, months: 6 })), + { + walletClient, + exchange, + merchant, + }, + ); + + // The time travel should cause exchanges to update. + await exchangeUpdated2Cond; + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // At this point, the original coins should've been refreshed. + // It would be too late to refresh them now, as we're past + // the two year deposit expiration. + + const orderResp = await merchantClient.createOrder({ + order: { + fulfillment_url: "http://example.com", + summary: "foo", + amount: "TESTKUDOS:30", + }, + }); + + const orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + instance: "default", + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + const r = await walletClient.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + console.log(r); + + t.assertTrue(r.status === PreparePayResultType.PaymentPossible); + + const cpr = await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: r.transactionId, + }); + + t.assertTrue(cpr.type === ConfirmPayResultType.Done); +} + +runTimetravelAutorefreshTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-timetravel-withdraw.ts b/packages/taler-harness/src/integrationtests/test-timetravel-withdraw.ts new file mode 100644 index 000000000..4ee3a86e9 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-timetravel-withdraw.ts @@ -0,0 +1,109 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Duration, + TransactionMajorState, + TransactionType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Basic time travel test. + */ +export async function runTimetravelWithdrawTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + const wres1 = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:15", + }); + await wres1.withdrawalFinishedCond; + + // Travel 400 days into the future, + // as the deposit expiration is two years + // into the future. + const timetravelDuration: Duration = { + d_ms: 1000 * 60 * 60 * 24 * 400, + }; + + await exchange.stop(); + exchange.setTimetravel(Duration.toMilliseconds(timetravelDuration)); + await exchange.start(); + await exchange.pingUntilAvailable(); + await exchange.keyup(); + + await merchant.stop(); + merchant.setTimetravel(Duration.toMilliseconds(timetravelDuration)); + await merchant.start(); + await merchant.pingUntilAvailable(); + + console.log("starting withdrawal via bank"); + + // This should fail, as the wallet didn't time travel yet. + const wres2 = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + console.log("starting withdrawal done"); + + // Check that transactions are correct for the failed withdrawal + { + const transactions = await walletClient.call( + WalletApiOperation.GetTransactions, + { + sort: "stable-ascending", + }, + ); + console.log(j2s(transactions)); + const types = transactions.transactions.map((x) => x.type); + t.assertDeepEqual(types, ["withdrawal", "withdrawal"]); + const wtrans = transactions.transactions[1]; + t.assertTrue(wtrans.type === TransactionType.Withdrawal); + t.assertTrue(wtrans.txState.major === TransactionMajorState.Pending); + } + + // Now we also let the wallet time travel + + walletClient.call(WalletApiOperation.TestingSetTimetravel, { + offsetMs: Duration.toMilliseconds(timetravelDuration), + }); + + // The wallet should do denomination re-selection and succeed + + await wres2.withdrawalFinishedCond; +} + +runTimetravelWithdrawTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-tos-format.ts b/packages/taler-harness/src/integrationtests/test-tos-format.ts new file mode 100644 index 000000000..e6087af9d --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-tos-format.ts @@ -0,0 +1,101 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, +} from "../harness/helpers.js"; +import * as fs from "fs"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runTermOfServiceFormatTest(t: GlobalTestState) { + // Set up test environment + const tosDir = t.testDir + `/tos/`; + const langs = ["es", "en", "de"] + + langs.forEach(l => { + const langDir = tosDir + l + "/" + fs.mkdirSync(langDir, { recursive: true }); + fs.writeFileSync(langDir + "v1.txt", "text content"); + fs.writeFileSync(langDir + "v1.md", "markdown content"); + fs.writeFileSync(langDir + "v1.html", "html content"); + }); + + const { walletClient, exchange, } = + await createSimpleTestkudosEnvironmentV2(t, undefined, { + additionalExchangeConfig: (ex) => { + ex.changeConfig((cfg) => { + cfg.setString("exchange", "terms_etag", "v1") + cfg.setString("exchange", "terms_dir", tosDir) + }) + } + }); + + + { + const tos = await walletClient.client.call(WalletApiOperation.GetExchangeTos, { + exchangeBaseUrl: exchange.baseUrl, + }) + + t.assertDeepEqual(tos.content, "text content"); + } + + { + const tos = await walletClient.client.call(WalletApiOperation.GetExchangeTos, { + exchangeBaseUrl: exchange.baseUrl, + acceptedFormat: ["text/html"] + }) + + t.assertDeepEqual(tos.content, "html content"); + } + + { + const tos = await walletClient.client.call(WalletApiOperation.GetExchangeTos, { + exchangeBaseUrl: exchange.baseUrl, + acceptedFormat: ["text/markdown"] + }) + + t.assertDeepEqual(tos.content, "markdown content"); + } + + { + const tos = await walletClient.client.call(WalletApiOperation.GetExchangeTos, { + exchangeBaseUrl: exchange.baseUrl, + acceptedFormat: ["text/markdown", "text/html"] + }) + + // prefer markdown since its the first one in the list + t.assertDeepEqual(tos.content, "markdown content"); + } + + { + const tos = await walletClient.client.call(WalletApiOperation.GetExchangeTos, { + exchangeBaseUrl: exchange.baseUrl, + acceptedFormat: ["text/pdf", "text/html"] + }) + + // there is no pdf so fallback in html + t.assertDeepEqual(tos.content, "html content"); + } +} + +runTermOfServiceFormatTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-backup-basic.ts b/packages/taler-harness/src/integrationtests/test-wallet-backup-basic.ts new file mode 100644 index 000000000..94d43e195 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-backup-basic.ts @@ -0,0 +1,189 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; +import { SyncService } from "../harness/sync.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runWalletBackupBasicTest(t: GlobalTestState) { + // Set up test environment + + const { commonDb, merchant, walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + const sync = await SyncService.create(t, { + currency: "TESTKUDOS", + annualFee: "TESTKUDOS:0.5", + database: commonDb.connStr, + fulfillmentUrl: "taler://fulfillment-success", + httpPort: 8089, + name: "sync1", + paymentBackendUrl: merchant.makeInstanceBaseUrl(), + uploadLimitMb: 10, + }); + + await sync.start(); + await sync.pingUntilAvailable(); + + await walletClient.call(WalletApiOperation.AddBackupProvider, { + backupProviderBaseUrl: sync.baseUrl, + activate: false, + name: sync.baseUrl, + }); + + { + const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {}); + t.assertDeepEqual(bi.providers[0].active, false); + } + + await walletClient.call(WalletApiOperation.AddBackupProvider, { + backupProviderBaseUrl: sync.baseUrl, + activate: true, + name: sync.baseUrl, + }); + + { + const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {}); + t.assertDeepEqual(bi.providers[0].active, true); + } + + await walletClient.call(WalletApiOperation.RunBackupCycle, {}); + + { + const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {}); + console.log(bi); + t.assertDeepEqual( + bi.providers[0].paymentStatus.type, + "insufficient-balance", + ); + } + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:10", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + await walletClient.call(WalletApiOperation.RunBackupCycle, {}); + + { + const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {}); + console.log(bi); + } + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:5", + }); + + await walletClient.call(WalletApiOperation.RunBackupCycle, {}); + + { + const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {}); + console.log(bi); + } + + const backupRecovery = await walletClient.call( + WalletApiOperation.ExportBackupRecovery, + {}, + ); + + const txs = await walletClient.call(WalletApiOperation.GetTransactions, {}); + console.log(`backed up transactions ${j2s(txs)}`); + + const { walletClient: walletClient2 } = await createWalletDaemonWithClient( + t, + { + name: "w2", + }, + ); + + // Check that the second wallet is a fresh wallet. + { + const bal = await walletClient2.call(WalletApiOperation.GetBalances, {}); + t.assertTrue(bal.balances.length === 0); + } + + await walletClient2.call(WalletApiOperation.ImportBackupRecovery, { + recovery: backupRecovery, + }); + + await walletClient2.call(WalletApiOperation.RunBackupCycle, {}); + + // Check that now the old balance is available! + { + const bal = await walletClient2.call(WalletApiOperation.GetBalances, {}); + t.assertTrue(bal.balances.length === 1); + console.log(bal); + } + + // Now do some basic checks that the restored wallet is still functional + { + const txs = await walletClient2.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log(`restored transactions ${j2s(txs)}`); + const bal1 = await walletClient2.call(WalletApiOperation.GetBalances, {}); + + t.assertAmountEquals(bal1.balances[0].available, "TESTKUDOS:14.1"); + + await withdrawViaBankV3(t, { + walletClient: walletClient2, + bankClient, + exchange, + amount: "TESTKUDOS:10", + }); + + await exchange.runWirewatchOnce(); + + await walletClient2.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + + const txs2 = await walletClient2.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log(`tx after withdraw after restore ${j2s(txs2)}`); + + const bal2 = await walletClient2.call(WalletApiOperation.GetBalances, {}); + + t.assertAmountEquals(bal2.balances[0].available, "TESTKUDOS:23.82"); + } +} + +runWalletBackupBasicTest.suites = ["wallet", "wallet-backup"]; +// See https://bugs.taler.net/n/7598 +runWalletBackupBasicTest.experimental = true; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-backup-doublespend.ts b/packages/taler-harness/src/integrationtests/test-wallet-backup-doublespend.ts new file mode 100644 index 000000000..abcd71a3b --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-backup-doublespend.ts @@ -0,0 +1,183 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { MerchantApiClient, PreparePayResultType } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; +import { SyncService } from "../harness/sync.js"; + +export async function runWalletBackupDoublespendTest(t: GlobalTestState) { + // Set up test environment + + const { commonDb, merchant, walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const sync = await SyncService.create(t, { + currency: "TESTKUDOS", + annualFee: "TESTKUDOS:0.5", + database: commonDb.connStr, + fulfillmentUrl: "taler://fulfillment-success", + httpPort: 8089, + name: "sync1", + paymentBackendUrl: merchant.makeInstanceBaseUrl(), + uploadLimitMb: 10, + }); + + await sync.start(); + await sync.pingUntilAvailable(); + + await walletClient.call(WalletApiOperation.AddBackupProvider, { + backupProviderBaseUrl: sync.baseUrl, + activate: true, + name: sync.baseUrl, + }); + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:10", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + await walletClient.call(WalletApiOperation.RunBackupCycle, {}); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + await walletClient.call(WalletApiOperation.RunBackupCycle, {}); + + const backupRecovery = await walletClient.call( + WalletApiOperation.ExportBackupRecovery, + {}, + ); + + const { walletClient: walletClientTwo } = await createWalletDaemonWithClient( + t, + { name: "default" }, + ); + + await walletClientTwo.call(WalletApiOperation.ImportBackupRecovery, { + recovery: backupRecovery, + }); + + await walletClientTwo.call(WalletApiOperation.RunBackupCycle, {}); + + console.log( + "wallet1 balance before spend:", + await walletClient.call(WalletApiOperation.GetBalances, {}), + ); + + await makeTestPaymentV2(t, { + merchant, + walletClient, + order: { + summary: "foo", + amount: "TESTKUDOS:7", + }, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + console.log( + "wallet1 balance after spend:", + await walletClient.call(WalletApiOperation.GetBalances, {}), + ); + + { + console.log( + "wallet2 balance:", + await walletClientTwo.call(WalletApiOperation.GetBalances, {}), + ); + } + + // Now we double-spend with the second wallet + + { + const instance = "default"; + + const orderResp = await merchantClient.createOrder({ + order: { + amount: "TESTKUDOS:8", + summary: "bla", + fulfillment_url: "taler://fulfillment-success", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + { + console.log( + "wallet2 balance before preparePay:", + await walletClientTwo.call(WalletApiOperation.GetBalances, {}), + ); + } + + const preparePayResult = await walletClientTwo.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertDeepEqual( + preparePayResult.status, + PreparePayResultType.PaymentPossible, + ); + + const res = await walletClientTwo.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + console.log(res); + + // FIXME: wait for a notification that indicates insufficient funds! + + await withdrawViaBankV3(t, { + walletClient: walletClientTwo, + bankClient, + exchange, + amount: "TESTKUDOS:50", + }); + + const bal = await walletClientTwo.call(WalletApiOperation.GetBalances, {}); + console.log("bal", bal); + + await walletClientTwo.call( + WalletApiOperation.TestingWaitTransactionsFinal, + {}, + ); + } +} + +runWalletBackupDoublespendTest.suites = ["wallet", "wallet-backup"]; +// See https://bugs.taler.net/n/7598 +runWalletBackupDoublespendTest.experimental = true; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-balance-notifications.ts b/packages/taler-harness/src/integrationtests/test-wallet-balance-notifications.ts new file mode 100644 index 000000000..1586e2a72 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-balance-notifications.ts @@ -0,0 +1,114 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + NotificationType, + TalerCorebankApiClient, + TransactionMajorState, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +/** + * Test behavior when an order is deleted while the wallet is paying for it. + */ +export async function runWalletBalanceNotificationsTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, walletService } = + await createSimpleTestkudosEnvironmentV3(t); + + const amount = "TESTKUDOS:20"; + + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth({ + username: user.username, + password: user.password, + }); + + const wop = await bankClient.createWithdrawalOperation(user.username, amount); + + // Hand it to the wallet + + await walletClient.call(WalletApiOperation.GetWithdrawalDetailsForUri, { + talerWithdrawUri: wop.taler_withdraw_uri, + }); + + // Withdraw (AKA select) + + const balanceChangedNotif1 = walletClient.waitForNotificationCond( + (x) => x.type === NotificationType.BalanceChange, + ); + + const acceptRes = await walletClient.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + t.logStep("wait-balance-notif-1"); + await balanceChangedNotif1; + t.logStep("done-wait-balance-notif-1"); + + const withdrawalFinishedCond = walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Done && + x.transactionId === acceptRes.transactionId, + ); + + // Confirm it + + await bankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + await withdrawalFinishedCond; + + // Second withdrawal! + { + const wop2 = await bankClient.createWithdrawalOperation( + user.username, + "TESTKUDOS:5", + ); + + await walletClient.call(WalletApiOperation.GetWithdrawalDetailsForUri, { + talerWithdrawUri: wop2.taler_withdraw_uri, + }); + + const acceptRes = await walletClient.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop2.taler_withdraw_uri, + }, + ); + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals(bal.balances[0].available, "TESTKUDOS:19.53"); + t.assertAmountEquals(bal.balances[0].pendingIncoming, "TESTKUDOS:4.85"); + + await walletService.stop(); + } +} + +runWalletBalanceNotificationsTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-balance-zero.ts b/packages/taler-harness/src/integrationtests/test-wallet-balance-zero.ts new file mode 100644 index 000000000..01cf7c159 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-balance-zero.ts @@ -0,0 +1,64 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { makeNoFeeCoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Related bugs: + * https://bugs.taler.net/n/8323 + */ +export async function runWalletBalanceZeroTest(t: GlobalTestState) { + // Set up test environment + + const coinConfig = makeNoFeeCoinConfig("TESTKUDOS"); + console.log(`coin config ${j2s(coinConfig)}`); + const { merchant, walletClient, exchange, bankClient } = + await createSimpleTestkudosEnvironmentV3(t, coinConfig); + + const wres = await withdrawViaBankV3(t, { + amount: "TESTKUDOS:10", + bankClient, + exchange, + walletClient, + }); + await wres.withdrawalFinishedCond; + + await makeTestPaymentV2(t, { + walletClient, + merchant, + order: { + summary: "Hello, World!", + amount: "TESTKUDOS:10", + }, + }); + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(`${j2s(bal)}`); + t.assertAmountEquals(bal.balances[0].available, "TESTKUDOS:0"); +} + +runWalletBalanceZeroTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-balance.ts b/packages/taler-harness/src/integrationtests/test-wallet-balance.ts new file mode 100644 index 000000000..c37a6e482 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-balance.ts @@ -0,0 +1,130 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Amounts, + Duration, + MerchantApiClient, + MerchantContractTerms, + PreparePayResultType, + TalerMerchantApi, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test wallet: + * - balance error messages + * - different types of insufficient balance. + * + * Related bugs: + * https://bugs.taler.net/n/7299 + */ +export async function runWalletBalanceTest(t: GlobalTestState) { + // Set up test environment + + const { merchant, walletClient, exchange, bankClient } = + await createSimpleTestkudosEnvironmentV3(t); + + await merchant.addInstanceWithWireAccount({ + id: "myinst", + name: "My Instance", + paytoUris: ["payto://void/foo"], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const merchantClient = new MerchantApiClient( + merchant.makeInstanceBaseUrl("myinst"), + ); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + console.log("withdrawal finished"); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + console.log("creating order"); + + const orderResp = await merchantClient.createOrder({ + order, + }); + + console.log("created order with merchant"); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + console.log("queried order at merchant"); + + // Make wallet pay for the order + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.InsufficientBalance, + ); + + t.assertDeepEqual( + preparePayResult.status, + PreparePayResultType.InsufficientBalance, + ); + + t.assertTrue( + Amounts.isNonZero( + preparePayResult.balanceDetails.balanceReceiverAcceptable, + ), + ); + + t.assertTrue( + Amounts.isZero(preparePayResult.balanceDetails.balanceReceiverDepositable), + ); + + console.log("waiting for transactions to finalize"); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runWalletBalanceTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-blocked-deposit.ts b/packages/taler-harness/src/integrationtests/test-wallet-blocked-deposit.ts new file mode 100644 index 000000000..66f985114 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-blocked-deposit.ts @@ -0,0 +1,150 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + NotificationType, + TransactionMajorState, + TransactionMinorState, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState, generateRandomPayto } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +/** + * Run test for refreshe after a payment. + */ +export async function runWalletBlockedDepositTest(t: GlobalTestState) { + // Set up test environment + + const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, + { + ...coinCommon, + name: "n5", + value: "TESTKUDOS:5", + }, + ]; + + const { bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t, coinConfigList); + + // Withdraw digital cash into the wallet. + + const { walletClient: w1 } = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + config: { + testing: { + devModeActive: true, + }, + }, + }); + + await withdrawViaBankV3(t, { + walletClient: w1, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await w1.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Prevent the wallet from doing refreshes by injecting a 5xx + // status for all refresh requests. + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/start-block-refresh", + }); + + await makeTestPaymentV2(t, { + merchant, + walletClient: w1, + order: { + summary: "test", + amount: "TESTKUDOS:2", + }, + }); + + const userPayto = generateRandomPayto("foo"); + + const bal = await w1.call(WalletApiOperation.GetBalances, {}); + console.log(`balance: ${j2s(bal)}`); + + const balDet = await w1.call(WalletApiOperation.GetBalanceDetail, { + currency: "TESTKUDOS", + }); + console.log(`balance details: ${j2s(balDet)}`); + + const depositCheckResp = await w1.call(WalletApiOperation.PrepareDeposit, { + amount: "TESTKUDOS:18" as AmountString, + depositPaytoUri: userPayto, + }); + + console.log(`check resp: ${j2s(depositCheckResp)}`); + + const depositCreateResp = await w1.call( + WalletApiOperation.CreateDepositGroup, + { + amount: "TESTKUDOS:18" as AmountString, + depositPaytoUri: userPayto, + }, + ); + + console.log(`create resp: ${j2s(depositCreateResp)}`); + + const depositTrackCond = w1.waitForNotificationCond((n) => { + return ( + n.type === NotificationType.TransactionStateTransition && + n.transactionId === depositCreateResp.transactionId && + n.newTxState.major === TransactionMajorState.Pending && + n.newTxState.minor === TransactionMinorState.Track + ); + }); + + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/stop-block-refresh", + }); + + await depositTrackCond; +} + +runWalletBlockedDepositTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-merchant.ts b/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-merchant.ts new file mode 100644 index 000000000..004de87c8 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-merchant.ts @@ -0,0 +1,142 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + MerchantApiClient, + PreparePayResultType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, + { + ...coinCommon, + name: "n5", + value: "TESTKUDOS:5", + }, +]; + +/** + * Run test for paying a merchant with balance locked behind a pending refresh. + */ +export async function runWalletBlockedPayMerchantTest(t: GlobalTestState) { + // Set up test environment + + const { bank, exchange, merchant } = await createSimpleTestkudosEnvironmentV2( + t, + coinConfigList, + ); + + // Withdraw digital cash into the wallet. + + const { walletClient: w1 } = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + config: { + testing: { + devModeActive: true, + }, + }, + }); + + await withdrawViaBankV2(t, { + walletClient: w1, + bank, + exchange, + amount: "TESTKUDOS:20", + }); + + await w1.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Prevent the wallet from doing refreshes by injecting a 5xx + // status for all refresh requests. + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/start-block-refresh", + }); + + // Do a payment that causes a refresh. + await makeTestPaymentV2(t, { + merchant, + walletClient: w1, + order: { + summary: "test", + amount: "TESTKUDOS:2", + }, + }); + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const orderResp = await merchantClient.createOrder({ + order: { + summary: "My Payment", + amount: "TESTKUDOS:18", + }, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const preparePayResult = await w1.call(WalletApiOperation.PreparePayForUri, { + talerPayUri: orderStatus.taler_pay_uri, + }); + + console.log(`prepare pay result: ${j2s(preparePayResult)}`); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/stop-block-refresh", + }); + + await w1.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runWalletBlockedPayMerchantTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-peer-pull.ts b/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-peer-pull.ts new file mode 100644 index 000000000..36a6fea05 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-peer-pull.ts @@ -0,0 +1,177 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +/** + * Run test for a peer push payment with balance locked behind a pending refresh. + */ +export async function runWalletBlockedPayPeerPullTest(t: GlobalTestState) { + // Set up test environment + + const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, + { + ...coinCommon, + name: "n5", + value: "TESTKUDOS:5", + }, + ]; + + const { bank, exchange, merchant } = await createSimpleTestkudosEnvironmentV2( + t, + coinConfigList, + ); + + // Withdraw digital cash into the wallet. + + const { walletClient: w1 } = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + config: { + testing: { + devModeActive: true, + }, + }, + }); + + const { walletClient: w2 } = await createWalletDaemonWithClient(t, { + name: "w2", + persistent: true, + config: { + testing: { + devModeActive: true, + }, + }, + }); + + await withdrawViaBankV2(t, { + walletClient: w1, + bank, + exchange, + amount: "TESTKUDOS:20", + }); + + await w1.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Prevent the wallet from doing refreshes by injecting a 5xx + // status for all refresh requests. + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/start-block-refresh", + }); + + // Do a payment that causes a refresh. + await makeTestPaymentV2(t, { + merchant, + walletClient: w1, + order: { + summary: "test", + amount: "TESTKUDOS:2", + }, + }); + + await w2.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + const pullCreditReadyCond = w2.waitForNotificationCond((n) => { + return ( + n.type === NotificationType.TransactionStateTransition && + n.transactionId.startsWith("txn:peer-pull-credit:") && + n.newTxState.major === TransactionMajorState.Pending && + n.newTxState.minor === TransactionMinorState.Ready + ); + }); + + const initResp = await w2.call(WalletApiOperation.InitiatePeerPullCredit, { + partialContractTerms: { + summary: "hi!", + amount: "TESTKUDOS:18" as AmountString, + purse_expiration: AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ hours: 1 }), + ), + ), + }, + }); + + await pullCreditReadyCond; + + const initTx = await w2.call(WalletApiOperation.GetTransactionById, { + transactionId: initResp.transactionId, + }); + + t.assertDeepEqual(initTx.type, TransactionType.PeerPullCredit); + t.assertTrue(!!initTx.talerUri); + + const checkResp = await w1.call(WalletApiOperation.PreparePeerPullDebit, { + talerUri: initTx.talerUri, + }); + + console.log(`check resp ${j2s(checkResp)}`); + + const confirmResp = await w1.call(WalletApiOperation.ConfirmPeerPullDebit, { + transactionId: checkResp.transactionId, + }); + + console.log(`confirm resp ${j2s(confirmResp)}`); + + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/stop-block-refresh", + }); + + await w1.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runWalletBlockedPayPeerPullTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-peer-push.ts b/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-peer-push.ts new file mode 100644 index 000000000..7427f2b07 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-blocked-pay-peer-push.ts @@ -0,0 +1,149 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TransactionMajorState, + TransactionMinorState, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, + createWalletDaemonWithClient, + makeTestPaymentV2, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +/** + * Run test for a peer push payment with balance locked behind a pending refresh. + */ +export async function runWalletBlockedPayPeerPushTest(t: GlobalTestState) { + // Set up test environment + + const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, + { + ...coinCommon, + name: "n5", + value: "TESTKUDOS:5", + }, + ]; + + const { bank, exchange, merchant } = await createSimpleTestkudosEnvironmentV2( + t, + coinConfigList, + ); + + // Withdraw digital cash into the wallet. + + const { walletClient: w1 } = await createWalletDaemonWithClient(t, { + name: "w1", + persistent: true, + config: { + testing: { + devModeActive: true, + }, + }, + }); + + await withdrawViaBankV2(t, { + walletClient: w1, + bank, + exchange, + amount: "TESTKUDOS:20", + }); + + await w1.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Prevent the wallet from doing refreshes by injecting a 5xx + // status for all refresh requests. + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/start-block-refresh", + }); + + // Do a payment that causes a refresh. + await makeTestPaymentV2(t, { + merchant, + walletClient: w1, + order: { + summary: "test", + amount: "TESTKUDOS:2", + }, + }); + + const checkResp = await w1.call(WalletApiOperation.CheckPeerPushDebit, { + amount: "TESTKUDOS:18" as AmountString, + }); + + console.log(`check resp ${j2s(checkResp)}`); + + const readyCond = w1.waitForNotificationCond((n) => { + return ( + n.type === NotificationType.TransactionStateTransition && + n.transactionId.startsWith("txn:peer-push-debit:") && + n.newTxState.major === TransactionMajorState.Pending && + n.newTxState.minor === TransactionMinorState.Ready + ); + }); + + const confirmResp = await w1.call(WalletApiOperation.InitiatePeerPushDebit, { + partialContractTerms: { + summary: "hi!", + amount: "TESTKUDOS:18" as AmountString, + purse_expiration: AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ hours: 1 }), + ), + ), + }, + }); + + console.log(`confirm resp ${j2s(confirmResp)}`); + + await w1.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/stop-block-refresh", + }); + + await readyCond; +} + +runWalletBlockedPayPeerPushTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-cli-termination.ts b/packages/taler-harness/src/integrationtests/test-wallet-cli-termination.ts new file mode 100644 index 000000000..bcd7de74b --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-cli-termination.ts @@ -0,0 +1,101 @@ +/* + This file is part of GNU Taler + (C) 2024 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { AmountString } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + FakebankService, + GlobalTestState, + MerchantService, + WalletCli, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; + +/** + * Test that run-until-done of taler-wallet-cli terminates. + */ +export async function runWalletCliTerminationTest(t: GlobalTestState) { + const db = await setupDb(t); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + const exchangeBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + exchange.addBankAccount("1", exchangeBankAccount); + + bank.setSuggestedExchange(exchange, exchangeBankAccount.accountPaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + const wallet = new WalletCli(t, "wallet"); + + await wallet.client.call(WalletApiOperation.WithdrawTestBalance, { + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:20" as AmountString, + }); + + await wallet.runUntilDone(); +} + +runWalletCliTerminationTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-config.ts b/packages/taler-harness/src/integrationtests/test-wallet-config.ts new file mode 100644 index 000000000..461574031 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-config.ts @@ -0,0 +1,67 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createWalletDaemonWithClient } from "../harness/helpers.js"; + +export async function runWalletConfigTest(t: GlobalTestState) { + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + config: { + builtin: { + exchanges: [], + }, + }, + }); + + const exchangesResp1 = await w1.walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesResp1.exchanges.length, 0); + + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + config: { + builtin: { + exchanges: [ + { + exchangeBaseUrl: "https://exchange.demo.taler.net/", + currencyHint: "KUDOS", + }, + { + exchangeBaseUrl: "https://exchange.test.taler.net/", + currencyHint: "TESTKUDOS", + }, + ], + }, + }, + }); + + const exchangesResp2 = await w2.walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesResp2.exchanges.length, 2); +} + +runWalletConfigTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-cryptoworker.ts b/packages/taler-harness/src/integrationtests/test-wallet-cryptoworker.ts new file mode 100644 index 000000000..6c2006636 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-cryptoworker.ts @@ -0,0 +1,42 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + WalletApiOperation, +} from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, WalletCli } from "../harness/harness.js"; + +/** + * Run test for the different crypto workers. + */ +export async function runWalletCryptoWorkerTest(t: GlobalTestState) { + const wallet1 = new WalletCli(t, "w1", { + cryptoWorkerType: "sync", + }); + + await wallet1.client.call(WalletApiOperation.TestCrypto, {}); + + const wallet2 = new WalletCli(t, "w2", { + cryptoWorkerType: "node-worker-thread", + }); + + await wallet2.client.call(WalletApiOperation.TestCrypto, {}); +} + +runWalletCryptoWorkerTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-dbless.ts b/packages/taler-harness/src/integrationtests/test-wallet-dbless.ts new file mode 100644 index 000000000..a089d99b5 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-dbless.ts @@ -0,0 +1,160 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + encodeCrock, + getRandomBytes, + j2s, + TalerError, +} from "@gnu-taler/taler-util"; +import { + applyRunConfigDefaults, + CryptoDispatcher, + SynchronousCryptoWorkerFactoryPlain, +} from "@gnu-taler/taler-wallet-core"; +import { + checkReserve, + depositCoin, + downloadExchangeInfo, + findDenomOrThrow, + refreshCoin, + topupReserveWithBank, + withdrawCoin, +} from "@gnu-taler/taler-wallet-core/dbless"; +import { GlobalTestState, harnessHttpLib } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV2 } from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal and payment. + */ +export async function runWalletDblessTest(t: GlobalTestState) { + // Set up test environment + + const { bank, exchange } = await createSimpleTestkudosEnvironmentV2(t); + + const http = harnessHttpLib; + const cryptiDisp = new CryptoDispatcher( + new SynchronousCryptoWorkerFactoryPlain(), + ); + const cryptoApi = cryptiDisp.cryptoApi; + + try { + // Withdraw digital cash into the wallet. + + const exchangeInfo = await downloadExchangeInfo(exchange.baseUrl, http); + + const reserveKeyPair = await cryptoApi.createEddsaKeypair({}); + + let reserveUrl = new URL( + `reserves/${reserveKeyPair.pub}`, + exchange.baseUrl, + ); + reserveUrl.searchParams.set("timeout_ms", "30000"); + const longpollReq = http.fetch(reserveUrl.href, { + method: "GET", + }); + + await topupReserveWithBank({ + amount: "TESTKUDOS:10" as AmountString, + http, + reservePub: reserveKeyPair.pub, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeInfo, + }); + + console.log("waiting for longpoll request"); + const resp = await longpollReq; + console.log(`got response, status ${resp.status}`); + + console.log(exchangeInfo); + + await checkReserve(http, exchange.baseUrl, reserveKeyPair.pub); + + const defaultConfig = applyRunConfigDefaults(); + + const d1 = findDenomOrThrow(exchangeInfo, "TESTKUDOS:8" as AmountString, { + denomselAllowLate: defaultConfig.testing.denomselAllowLate, + }); + + const coin = await withdrawCoin({ + http, + cryptoApi, + reserveKeyPair: { + reservePriv: reserveKeyPair.priv, + reservePub: reserveKeyPair.pub, + }, + denom: d1, + exchangeBaseUrl: exchange.baseUrl, + }); + + const wireSalt = encodeCrock(getRandomBytes(16)); + const merchantPub = encodeCrock(getRandomBytes(32)); + const contractTermsHash = encodeCrock(getRandomBytes(64)); + + await depositCoin({ + contractTermsHash, + merchantPub, + wireSalt, + amount: "TESTKUDOS:4" as AmountString, + coin: coin, + cryptoApi, + exchangeBaseUrl: exchange.baseUrl, + http, + }); + + // Idempotency + await depositCoin({ + contractTermsHash, + merchantPub, + wireSalt, + amount: "TESTKUDOS:4" as AmountString, + coin: coin, + cryptoApi, + exchangeBaseUrl: exchange.baseUrl, + http, + }); + + const refreshDenoms = [ + findDenomOrThrow(exchangeInfo, "TESTKUDOS:1" as AmountString, { + denomselAllowLate: defaultConfig.testing.denomselAllowLate, + }), + findDenomOrThrow(exchangeInfo, "TESTKUDOS:1" as AmountString, { + denomselAllowLate: defaultConfig.testing.denomselAllowLate, + }), + ]; + + await refreshCoin({ + oldCoin: coin, + cryptoApi, + http, + newDenoms: refreshDenoms, + }); + } catch (e) { + if (e instanceof TalerError) { + console.log(e); + console.log(j2s(e.errorDetail)); + } else { + console.log(e); + } + throw e; + } +} + +runWalletDblessTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-dd48.ts b/packages/taler-harness/src/integrationtests/test-wallet-dd48.ts new file mode 100644 index 000000000..ba2b2670c --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-dd48.ts @@ -0,0 +1,206 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + ExchangeEntryStatus, + NotificationType, + TalerCorebankApiClient, + TalerError, + TalerErrorCode, + WalletNotification, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + WalletClient, + WalletService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { withdrawViaBankV3 } from "../harness/helpers.js"; + +/** + * Test for DD48 notifications. + */ +export async function runWalletDd48Test(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + const walletService = new WalletService(t, { + name: "wallet", + useInMemoryDb: true, + }); + await walletService.start(); + await walletService.pingUntilAvailable(); + + const allNotifications: WalletNotification[] = []; + + const walletClient = new WalletClient({ + name: "wallet", + unixPath: walletService.socketPath, + onNotification(n) { + console.log("got notification", n); + allNotifications.push(n); + }, + }); + await walletClient.connect(); + await walletClient.client.call(WalletApiOperation.InitWallet, { + config: { + testing: { + skipDefaults: true, + }, + }, + }); + + await walletClient.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + { + const exchangeEntry = await walletClient.call( + WalletApiOperation.GetExchangeEntryByUrl, + { + exchangeBaseUrl: exchange.baseUrl, + }, + ); + + t.assertDeepEqual( + exchangeEntry.exchangeEntryStatus, + ExchangeEntryStatus.Ephemeral, + ); + + const resources = await walletClient.call( + WalletApiOperation.GetExchangeResources, + { + exchangeBaseUrl: exchange.baseUrl, + }, + ); + t.assertDeepEqual(resources.hasResources, false); + } + + const wres = await withdrawViaBankV3(t, { + walletClient, + amount: "TESTKUDOS:20", + bankClient, + exchange, + }); + + await wres.withdrawalFinishedCond; + + const exchangeEntry = await walletClient.call( + WalletApiOperation.GetExchangeEntryByUrl, + { + exchangeBaseUrl: exchange.baseUrl, + }, + ); + + t.assertDeepEqual( + exchangeEntry.exchangeEntryStatus, + ExchangeEntryStatus.Used, + ); + + t.assertTrue( + !!allNotifications.find( + (x) => + x.type === NotificationType.ExchangeStateTransition && + x.oldExchangeState == null && + x.newExchangeState.exchangeEntryStatus === + ExchangeEntryStatus.Ephemeral, + ), + ); + + console.log(j2s(allNotifications)); + + const delErr = await t.assertThrowsAsync(async () => { + await walletClient.call(WalletApiOperation.DeleteExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + }); + + t.assertTrue(delErr instanceof TalerError); + t.assertDeepEqual( + delErr.errorDetail.code, + TalerErrorCode.WALLET_EXCHANGE_ENTRY_USED, + ); + + await walletClient.call(WalletApiOperation.DeleteExchange, { + exchangeBaseUrl: exchange.baseUrl, + purge: true, + }); +} + +runWalletDd48Test.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-denom-expire.ts b/packages/taler-harness/src/integrationtests/test-wallet-denom-expire.ts new file mode 100644 index 000000000..b9d028efd --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-denom-expire.ts @@ -0,0 +1,176 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { Duration, Logger, NotificationType, TalerCorebankApiClient, j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { makeNoFeeCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + FakebankService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + applyTimeTravelV2, + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +const logger = new Logger("test-exchange-timetravel.ts"); + +/** + * Test how the wallet handles an expired denomination. + */ +export async function runWalletDenomExpireTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + exchange.addCoinConfigList(makeNoFeeCoinConfig("TESTKUDOS")); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + console.log("merchant started, configuring instances"); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + }); + + console.log("setup done!"); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "default", + }); + + // Withdraw digital cash into the wallet. + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:15", + }); + await wres.withdrawalFinishedCond; + + const denomLossCond = walletClient.waitForNotificationCond((n) => { + return ( + n.type === NotificationType.TransactionStateTransition && + n.transactionId.startsWith("txn:denom-loss:") + ); + }); + + // Travel into the future, the deposit expiration is two years + // into the future. + console.log("applying first time travel"); + await applyTimeTravelV2( + Duration.toMilliseconds(Duration.fromSpec({ days: 800 })), + { + walletClient, + exchange, + merchant, + }, + ); + + t.logStep("before-wait-denom-loss"); + + // Should be detected automatically, as exchange entry is surely outdated. + await denomLossCond; + + const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); + console.log(`balances: ${j2s(bal)}`); + + const txns = await walletClient.call(WalletApiOperation.GetTransactions, { + sort: "stable-ascending", + includeRefreshes: true, + }); + console.log(`transactions: ${j2s(txns)}`); +} + +runWalletDenomExpireTest.suites = ["exchange"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-dev-experiments.ts b/packages/taler-harness/src/integrationtests/test-wallet-dev-experiments.ts new file mode 100644 index 000000000..1f1187d80 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-dev-experiments.ts @@ -0,0 +1,48 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createWalletDaemonWithClient } from "../harness/helpers.js"; + +export async function runWalletDevExperimentsTest(t: GlobalTestState) { + const w1 = await createWalletDaemonWithClient(t, { + name: "w1", + config: { + testing: { + devModeActive: true, + }, + }, + }); + + await w1.walletClient.call(WalletApiOperation.ApplyDevExperiment, { + devExperimentUri: "taler://dev-experiment/insert-pending-refresh", + }); + + const txnResp = await w1.walletClient.call( + WalletApiOperation.GetTransactions, + { + includeRefreshes: true, + }, + ); + + t.assertTrue(txnResp.transactions.length > 0); +} + +runWalletDevExperimentsTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-exchange-update.ts b/packages/taler-harness/src/integrationtests/test-wallet-exchange-update.ts new file mode 100644 index 000000000..b36e6ef61 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-exchange-update.ts @@ -0,0 +1,166 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + ExchangeUpdateStatus, + NotificationType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + FakebankService, + GlobalTestState, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +/** + * Test how the wallet reacts when an exchange unexpectedly updates + * properties like the master public key. + */ +export async function runWalletExchangeUpdateTest( + t: GlobalTestState, +): Promise<void> { + // Set up test environment + + const db = await setupDb(t); + const db2 = await setupDb(t, { + nameSuffix: "two", + }); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchangeOne = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + // Danger: The second exchange has the same port! + // That's because we want it to have the same base URL, + // and we'll only start on of them at a time. + const exchangeTwo = ExchangeService.create(t, { + name: "testexchange-2", + currency: "TESTKUDOS", + httpPort: 8081, + database: db2.connStr, + }); + + const exchangeBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + + await exchangeOne.addBankAccount("1", exchangeBankAccount); + await exchangeTwo.addBankAccount("1", exchangeBankAccount); + + // Same anyway. + bank.setSuggestedExchange(exchangeOne, exchangeBankAccount.accountPaytoUri); + + await bank.start(); + + exchangeOne.addCoinConfigList(defaultCoinConfig.map((x) => x("TESTKUDOS"))); + exchangeTwo.addCoinConfigList(defaultCoinConfig.map((x) => x("TESTKUDOS"))); + + // Only start first exchange. + await exchangeOne.start(); + + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "wallet", + persistent: true, + }); + + // Since the default exchanges can change, we start the wallet in tests + // with no built-in defaults. Thus the list of exchanges is empty here. + const exchangesListResult = await walletClient.call( + WalletApiOperation.ListExchanges, + {}, + ); + + t.assertDeepEqual(exchangesListResult.exchanges.length, 0); + + const wres = await withdrawViaBankV2(t, { + walletClient, + bank, + exchange: exchangeOne, + amount: "TESTKUDOS:10", + }); + + await wres.withdrawalFinishedCond; + + await exchangeOne.stop(); + + console.log("starting second exchange"); + await exchangeTwo.start(); + + console.log("updating exchange entry"); + + await t.assertThrowsAsync(async () => { + await walletClient.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: exchangeOne.baseUrl, + force: true, + }); + }); + + const exchangeEntry = await walletClient.call( + WalletApiOperation.GetExchangeEntryByUrl, + { + exchangeBaseUrl: exchangeOne.baseUrl, + }, + ); + + console.log(`exchange entry: ${j2s(exchangeEntry)}`); + + await t.assertThrowsAsync(async () => { + await walletClient.call(WalletApiOperation.GetWithdrawalDetailsForAmount, { + amount: "TESTKUDOS:10" as AmountString, + exchangeBaseUrl: exchangeOne.baseUrl, + }); + }); + + const exchangeAvailableCond = walletClient.waitForNotificationCond((n) => { + console.log(`got notif ${j2s(n)}`); + return ( + n.type === NotificationType.ExchangeStateTransition && + n.newExchangeState.exchangeUpdateStatus === ExchangeUpdateStatus.Ready + ); + }); + + await exchangeTwo.stop(); + + console.log("starting first exchange"); + await exchangeOne.start(); + + await exchangeAvailableCond; +} + +runWalletExchangeUpdateTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-gendb.ts b/packages/taler-harness/src/integrationtests/test-wallet-gendb.ts new file mode 100644 index 000000000..778f36432 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-gendb.ts @@ -0,0 +1,111 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + NotificationType, + TalerMerchantApi, + TransactionMajorState, + TransactionMinorState, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test that creates various transactions and exports the resulting + * database. Used to generate a database export file for DB compatibility + * testing. + */ +export async function runWalletGenDbTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:50", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:10", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const purseExpiration = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration( + AbsoluteTime.now(), + Duration.fromSpec({ days: 2 }), + ), + ); + + const peerPullIniResp = await walletClient.call( + WalletApiOperation.InitiatePeerPullCredit, + { + exchangeBaseUrl: exchange.baseUrl, + partialContractTerms: { + summary: "Hello World", + amount: "TESTKUDOS:5" as AmountString, + purse_expiration: purseExpiration, + }, + }, + ); + + const peerPullCreditReadyCond = walletClient.waitForNotificationCond( + (x) => + x.type === NotificationType.TransactionStateTransition && + x.transactionId === peerPullIniResp.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.Ready, + ); + + await peerPullCreditReadyCond; + + const checkResp = await walletClient.call( + WalletApiOperation.PreparePeerPullDebit, + { + talerUri: peerPullIniResp.talerUri, + }, + ); + + await walletClient.call(WalletApiOperation.ConfirmPeerPullDebit, { + transactionId: checkResp.transactionId, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runWalletGenDbTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts b/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts new file mode 100644 index 000000000..ac1244446 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-insufficient-balance.ts @@ -0,0 +1,166 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + Duration, + PaymentInsufficientBalanceDetails, + TalerErrorCode, + WalletNotification, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + FakebankService, + GlobalTestState, + MerchantService, + WalletClient, + WalletService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { withdrawViaBankV2 } from "../harness/helpers.js"; + +export async function runWalletInsufficientBalanceTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + const exchangeBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + exchangeBankAccount.skipWireFeeCreation = true; + exchange.addBankAccount("1", exchangeBankAccount); + + bank.setSuggestedExchange(exchange, exchangeBankAccount.accountPaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const walletService = new WalletService(t, { + name: "wallet", + useInMemoryDb: true, + }); + await walletService.start(); + await walletService.pingUntilAvailable(); + + const allNotifications: WalletNotification[] = []; + + const walletClient = new WalletClient({ + name: "wallet", + unixPath: walletService.socketPath, + onNotification(n) { + console.log("got notification", n); + allNotifications.push(n); + }, + }); + await walletClient.connect(); + await walletClient.client.call(WalletApiOperation.InitWallet, { + config: { + testing: { + skipDefaults: true, + }, + }, + }); + + const wres = await withdrawViaBankV2(t, { + amount: "TESTKUDOS:10", + bank, + exchange, + walletClient, + }); + 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", + }); + }); + 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", + ); +} + +runWalletInsufficientBalanceTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-notifications.ts b/packages/taler-harness/src/integrationtests/test-wallet-notifications.ts new file mode 100644 index 000000000..5088c8228 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-notifications.ts @@ -0,0 +1,195 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + TalerCorebankApiClient, + Duration, + NotificationType, + TransactionMajorState, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + WalletClient, + WalletService, + generateRandomPayto, + generateRandomTestIban, + setupDb, +} from "../harness/harness.js"; + +/** + * Test for wallet-core notifications. + */ +export async function runWalletNotificationsTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + // Fakebank uses x-taler-bank, but merchant is configured to only accept sepa! + const label = "mymerchant"; + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [ + `payto://iban/SANDBOXX/${generateRandomTestIban(label)}?receiver-name=${label}`, + ], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + console.log("setup done!"); + + const walletService = new WalletService(t, { + name: "wallet", + useInMemoryDb: true, + }); + await walletService.start(); + await walletService.pingUntilAvailable(); + + const walletClient = new WalletClient({ + name: "wallet", + unixPath: walletService.socketPath, + onNotification(n) { + console.log("got notification", n); + }, + }); + await walletClient.connect(); + await walletClient.client.call(WalletApiOperation.InitWallet, { + config: { + testing: { + skipDefaults: true, + } + } + }); + + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth(user); + const wop = await bankClient.createWithdrawalOperation( + user.username, + "TESTKUDOS:20", + ); + + // Hand it to the wallet + + await walletClient.client.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + // Withdraw (AKA select) + + const acceptRes = await walletClient.client.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + const withdrawalFinishedReceivedPromise = + walletClient.waitForNotificationCond((x) => { + return ( + x.type === NotificationType.TransactionStateTransition && + x.newTxState.major === TransactionMajorState.Done && + x.transactionId === acceptRes.transactionId + ); + }); + + // Confirm it + + await bankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + await withdrawalFinishedReceivedPromise; +} + +runWalletNotificationsTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-observability.ts b/packages/taler-harness/src/integrationtests/test-wallet-observability.ts new file mode 100644 index 000000000..55a60cb76 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-observability.ts @@ -0,0 +1,141 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { NotificationType, TalerCorebankApiClient, WalletNotification } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + WalletClient, + WalletService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { withdrawViaBankV3 } from "../harness/helpers.js"; + +export async function runWalletObservabilityTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + const walletService = new WalletService(t, { + name: "wallet", + useInMemoryDb: true, + }); + await walletService.start(); + await walletService.pingUntilAvailable(); + + const allNotifications: WalletNotification[] = []; + + const walletClient = new WalletClient({ + name: "wallet", + unixPath: walletService.socketPath, + onNotification(n) { + console.log("got notification", n); + allNotifications.push(n); + }, + }); + await walletClient.connect(); + await walletClient.client.call(WalletApiOperation.InitWallet, { + config: { + testing: { + skipDefaults: true, + emitObservabilityEvents: true, + }, + }, + }); + + const wres = await withdrawViaBankV3(t, { + amount: "TESTKUDOS:10", + bankClient, + exchange, + walletClient, + }); + await wres.withdrawalFinishedCond; + + const requestObsEvents = allNotifications.filter( + (x) => x.type === NotificationType.RequestObservabilityEvent, + ); + const taskObsEvents = allNotifications.filter( + (x) => x.type === NotificationType.TaskObservabilityEvent, + ); + + console.log(`req events: ${requestObsEvents.length}`); + console.log(`task events: ${taskObsEvents.length}`); + + t.assertTrue(requestObsEvents.length > 30); + t.assertTrue(taskObsEvents.length > 30); +} + +runWalletObservabilityTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-refresh-errors.ts b/packages/taler-harness/src/integrationtests/test-wallet-refresh-errors.ts new file mode 100644 index 000000000..0f1efd35e --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-refresh-errors.ts @@ -0,0 +1,107 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { AmountString } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV2, + withdrawViaBankV2, +} from "../harness/helpers.js"; + +const coinCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + feeDeposit: "TESTKUDOS:0", + feeRefresh: "TESTKUDOS:0", + feeRefund: "TESTKUDOS:0", + feeWithdraw: "TESTKUDOS:0", + rsaKeySize: 1024, +}; + +/** + * Run test for refreshe after a payment. + */ +export async function runWalletRefreshErrorsTest(t: GlobalTestState) { + // Set up test environment + + const coinConfigList: CoinConfig[] = [ + { + ...coinCommon, + name: "n1", + value: "TESTKUDOS:1", + }, + { + ...coinCommon, + name: "n5", + value: "TESTKUDOS:5", + }, + ]; + + const { walletClient, bank, exchange, merchant } = + await createSimpleTestkudosEnvironmentV2(t, coinConfigList); + + const wres = await withdrawViaBankV2(t, { + amount: "TESTKUDOS:5", + bank, + exchange, + walletClient, + }); + await wres.withdrawalFinishedCond; + + const backupResp = await walletClient.call( + WalletApiOperation.CreateStoredBackup, + {}, + ); + + const coinDump = await walletClient.call(WalletApiOperation.DumpCoins, {}); + + t.assertDeepEqual(coinDump.coins.length, 1); + + await walletClient.call(WalletApiOperation.ForceRefresh, { + refreshCoinSpecs: [ + { + coinPub: coinDump.coins[0].coin_pub, + amount: "TESTKUDOS:3" as AmountString, + }, + ], + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + await walletClient.call(WalletApiOperation.RecoverStoredBackup, { + name: backupResp.name, + }); + + await walletClient.call(WalletApiOperation.ForceRefresh, { + refreshCoinSpecs: [ + { + coinPub: coinDump.coins[0].coin_pub, + amount: "TESTKUDOS:3" as AmountString, + }, + ], + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); +} + +runWalletRefreshErrorsTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-refresh.ts b/packages/taler-harness/src/integrationtests/test-wallet-refresh.ts new file mode 100644 index 000000000..93fe94270 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-refresh.ts @@ -0,0 +1,201 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + NotificationType, + TalerMerchantApi, + TransactionIdStr, + TransactionMajorState, + TransactionType, + j2s, +} from "@gnu-taler/taler-util"; +import { + WalletApiOperation, + parseTransactionIdentifier, +} from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState, generateRandomPayto } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + makeTestPaymentV2, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Run test for refreshe after a payment. + */ +export async function runWalletRefreshTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, merchant } = + await createSimpleTestkudosEnvironmentV3(t); + + // Withdraw digital cash into the wallet. + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:5", + fulfillment_url: "taler://fulfillment-success/thx", + }; + + await makeTestPaymentV2(t, { walletClient, merchant, order }); + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const txns = await walletClient.call(WalletApiOperation.GetTransactions, { + includeRefreshes: true, + }); + + console.log(j2s(txns)); + + t.assertDeepEqual(txns.transactions.length, 3); + + const refreshListTx = txns.transactions.find( + (x) => x.type === TransactionType.Refresh, + ); + + t.assertTrue(!!refreshListTx); + + const refreshTx = await walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: refreshListTx.transactionId, + }, + ); + + t.assertDeepEqual(refreshTx.type, TransactionType.Refresh); + + // Now we test a pending refresh operation. + { + await exchange.stop(); + + const refreshCreatedCond = walletClient.waitForNotificationCond((x) => { + if ( + x.type === NotificationType.TransactionStateTransition && + parseTransactionIdentifier(x.transactionId)?.tag === + TransactionType.Refresh + ) { + return true; + } + return false; + }); + + const refreshDoneCond = walletClient.waitForNotificationCond((x) => { + if ( + x.type === NotificationType.TransactionStateTransition && + parseTransactionIdentifier(x.transactionId)?.tag === + TransactionType.Refresh && + x.newTxState.major === TransactionMajorState.Done + ) { + return true; + } + return false; + }); + + const depositGroupResult = await walletClient.client.call( + WalletApiOperation.CreateDepositGroup, + { + amount: "TESTKUDOS:10.5" as AmountString, + depositPaytoUri: generateRandomPayto("foo"), + }, + ); + + await refreshCreatedCond; + + // Here, the refresh operation should be in a pending state. + + const bal1 = await walletClient.call(WalletApiOperation.GetBalances, {}); + + await exchange.start(); + + await refreshDoneCond; + + const bal2 = await walletClient.call(WalletApiOperation.GetBalances, {}); + + // The refresh operation completing should not change the available balance, + // as we're accounting pending refreshes towards the available (but not material!) balance. + t.assertAmountEquals( + bal1.balances[0].available, + bal2.balances[0].available, + ); + } + + const wres = await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await wres.withdrawalFinishedCond; + + // Test failing a refresh transaction + { + await exchange.stop(); + + let refreshTransactionId: TransactionIdStr | undefined = undefined; + + const refreshCreatedCond = walletClient.waitForNotificationCond((x) => { + if ( + x.type === NotificationType.TransactionStateTransition && + parseTransactionIdentifier(x.transactionId)?.tag === + TransactionType.Refresh + ) { + refreshTransactionId = x.transactionId as TransactionIdStr; + return true; + } + return false; + }); + + const depositGroupResult = await walletClient.client.call( + WalletApiOperation.CreateDepositGroup, + { + amount: "TESTKUDOS:10.5" as AmountString, + depositPaytoUri: generateRandomPayto("foo"), + }, + ); + + await refreshCreatedCond; + + t.assertTrue(!!refreshTransactionId); + + await walletClient.call(WalletApiOperation.FailTransaction, { + transactionId: refreshTransactionId, + }); + + const txn = await walletClient.call(WalletApiOperation.GetTransactionById, { + transactionId: refreshTransactionId, + }); + + t.assertDeepEqual(txn.type, TransactionType.Refresh); + t.assertDeepEqual(txn.txState.major, TransactionMajorState.Failed); + + t.assertTrue(!!refreshTransactionId); + } +} + +runWalletRefreshTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallet-wirefees.ts b/packages/taler-harness/src/integrationtests/test-wallet-wirefees.ts new file mode 100644 index 000000000..c5a0fd363 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallet-wirefees.ts @@ -0,0 +1,210 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + Duration, + MerchantApiClient, + PreparePayResultType, + TalerCorebankApiClient, + TalerMerchantApi, + TransactionMajorState, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { + createWalletDaemonWithClient, + withdrawViaBankV3, +} from "../harness/helpers.js"; + +/** + * Test payment where the exchange charges wire fees. + */ +export async function runWalletWirefeesTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + // Ridiculously high wire fees! + overrideWireFee: "TESTKUDOS:5", + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL( + "accounts/exchange/taler-wire-gateway/", + bank.baseUrl, + ).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const { walletClient, walletService } = await createWalletDaemonWithClient( + t, + { name: "wallet", persistent: true }, + ); + + console.log("setup done!"); + + // Withdraw digital cash into the wallet. + + await withdrawViaBankV3(t, { + walletClient, + bankClient, + exchange, + amount: "TESTKUDOS:20", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const order: TalerMerchantApi.Order = { + summary: "Buy me!", + amount: "TESTKUDOS:1", + fulfillment_url: "taler://fulfillment-success/thx", + //max_wire_fee: "TESTKUDOS:0.1", + max_fee: "TESTKUDOS:0.1", + }; + + const merchantClient = new MerchantApiClient(merchant.makeInstanceBaseUrl()); + + const orderResp = await merchantClient.createOrder({ + order, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); + + t.assertTrue(orderStatus.order_status === "unpaid"); + + // Make wallet pay for the order + + const preparePayResult = await walletClient.call( + WalletApiOperation.PreparePayForUri, + { + talerPayUri: orderStatus.taler_pay_uri, + }, + ); + + t.assertTrue( + preparePayResult.status === PreparePayResultType.PaymentPossible, + ); + + console.log(`amountEffective: ${preparePayResult.amountEffective}`); + + t.assertAmountEquals(preparePayResult.amountEffective, "TESTKUDOS:6.4"); + + await walletClient.call(WalletApiOperation.ConfirmPay, { + transactionId: preparePayResult.transactionId, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const payTxn = await walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: preparePayResult.transactionId, + }, + ); + + t.assertTrue(payTxn.txState.major === TransactionMajorState.Done); +} + +runWalletWirefeesTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-wallettesting.ts b/packages/taler-harness/src/integrationtests/test-wallettesting.ts new file mode 100644 index 000000000..001081532 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-wallettesting.ts @@ -0,0 +1,247 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Integration test for the wallet testing functionality used by the exchange + * test cases. + */ + +/** + * Imports. + */ +import { AmountString, Amounts, CoinStatus } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + GlobalTestState, + MerchantService, + setupDb, + generateRandomPayto, + FakebankService, +} from "../harness/harness.js"; +import { + SimpleTestEnvironmentNg, + createWalletDaemonWithClient, +} from "../harness/helpers.js"; + +const merchantAuthToken = "secret-token:sandbox"; + +/** + * Run a test case with a simple TESTKUDOS Taler environment, consisting + * of one exchange, one bank and one merchant. + */ +export async function createMyEnvironment( + t: GlobalTestState, + coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")), +): Promise<SimpleTestEnvironmentNg> { + const db = await setupDb(t); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + const exchangeBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + exchange.addBankAccount("1", exchangeBankAccount); + + bank.setSuggestedExchange(exchange, exchangeBankAccount.accountPaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + }); + + console.log("setup done!"); + + const { walletClient, walletService } = await createWalletDaemonWithClient( + t, + { + name: "w1", + }, + ); + + return { + commonDb: db, + exchange, + merchant, + walletClient, + walletService, + bank, + exchangeBankAccount, + }; +} + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runWallettestingTest(t: GlobalTestState) { + const { walletClient, bank, exchange, merchant } = + await createMyEnvironment(t); + + await walletClient.call(WalletApiOperation.RunIntegrationTest, { + amountToSpend: "TESTKUDOS:5" as AmountString, + amountToWithdraw: "TESTKUDOS:10" as AmountString, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeBaseUrl: exchange.baseUrl, + merchantAuthToken: merchantAuthToken, + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + }); + + let txns = await walletClient.call(WalletApiOperation.GetTransactions, {}); + console.log(JSON.stringify(txns, undefined, 2)); + let txTypes = txns.transactions.map((x) => x.type); + + t.assertDeepEqual(txTypes, [ + "withdrawal", + "payment", + "withdrawal", + "payment", + "refund", + "payment", + ]); + + await walletClient.call(WalletApiOperation.ClearDb, {}); + + await walletClient.call(WalletApiOperation.WithdrawTestBalance, { + amount: "TESTKUDOS:10" as AmountString, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeBaseUrl: exchange.baseUrl, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + await walletClient.call(WalletApiOperation.TestPay, { + amount: "TESTKUDOS:5" as AmountString, + merchantAuthToken: merchantAuthToken, + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + summary: "foo", + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + txns = await walletClient.call(WalletApiOperation.GetTransactions, {}); + console.log(JSON.stringify(txns, undefined, 2)); + txTypes = txns.transactions.map((x) => x.type); + + t.assertDeepEqual(txTypes, ["withdrawal", "payment"]); + + await walletClient.call(WalletApiOperation.ClearDb, {}); + + await walletClient.call(WalletApiOperation.WithdrawTestBalance, { + amount: "TESTKUDOS:10" as AmountString, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeBaseUrl: exchange.baseUrl, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + const coinDump = await walletClient.call(WalletApiOperation.DumpCoins, {}); + + console.log("coin dump:", JSON.stringify(coinDump, undefined, 2)); + + let susp: string | undefined; + { + for (const c of coinDump.coins) { + if ( + c.coin_status === CoinStatus.Fresh && + 0 === Amounts.cmp(c.denom_value, "TESTKUDOS:8") + ) { + susp = c.coin_pub; + } + } + } + + t.assertTrue(susp !== undefined); + + console.log("suspending coin"); + + await walletClient.call(WalletApiOperation.SetCoinSuspended, { + coinPub: susp, + suspended: true, + }); + + // This should fail, as we've suspended a coin that we need + // to pay. + await t.assertThrowsAsync(async () => { + await walletClient.call(WalletApiOperation.TestPay, { + amount: "TESTKUDOS:5" as AmountString, + merchantAuthToken: merchantAuthToken, + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + summary: "foo", + }); + }); + + console.log("unsuspending coin"); + + await walletClient.call(WalletApiOperation.SetCoinSuspended, { + coinPub: susp, + suspended: false, + }); + + await walletClient.call(WalletApiOperation.TestPay, { + amount: "TESTKUDOS:5" as AmountString, + merchantAuthToken: merchantAuthToken, + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + summary: "foo", + }); + + await walletClient.call(WalletApiOperation.ClearDb, {}); + await walletClient.call(WalletApiOperation.RunIntegrationTestV2, { + amountToSpend: "TESTKUDOS:5" as AmountString, + amountToWithdraw: "TESTKUDOS:10" as AmountString, + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeBaseUrl: exchange.baseUrl, + merchantAuthToken: merchantAuthToken, + merchantBaseUrl: merchant.makeInstanceBaseUrl(), + }); +} + +runWallettestingTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-abort-bank.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-abort-bank.ts new file mode 100644 index 000000000..b87e67a68 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-abort-bank.ts @@ -0,0 +1,77 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { TalerErrorCode } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runWithdrawalAbortBankTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + // Create a withdrawal operation + + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth(user); + const wop = await bankClient.createWithdrawalOperation( + user.username, + "TESTKUDOS:10", + ); + + // Hand it to the wallet + + await walletClient.call(WalletApiOperation.GetWithdrawalDetailsForUri, { + talerWithdrawUri: wop.taler_withdraw_uri, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Abort it + + await bankClient.abortWithdrawalOperationV2(user.username, wop); + + // Withdraw + + // Difference: + // -> with euFin, the wallet selects + // -> with PyBank, the wallet stops _before_ + // + // WHY ?! + // + const e = await t.assertThrowsTalerErrorAsync(async () => { + await walletClient.call(WalletApiOperation.AcceptBankIntegratedWithdrawal, { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }); + }); + t.assertDeepEqual( + e.errorDetail.code, + TalerErrorCode.WALLET_WITHDRAWAL_OPERATION_ABORTED_BY_BANK, + ); + + await t.shutdown(); +} + +runWithdrawalAbortBankTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-amount.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-amount.ts new file mode 100644 index 000000000..cd6a1e325 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-amount.ts @@ -0,0 +1,94 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + Logger, + WireGatewayApiClient, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +const logger = new Logger("test-withdrawal-manual.ts"); + +/** + * Check what happens when the withdrawal amount unexpectedly changes. + */ +export async function runWithdrawalAmountTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, exchangeBankAccount } = + await createSimpleTestkudosEnvironmentV3(t); + + const wireGatewayApiClient = new WireGatewayApiClient( + exchangeBankAccount.wireGatewayApiBaseUrl, + { + auth: { + username: "admin", + password: "adminpw", + }, + }, + ); + + // Create a withdrawal operation + + const user = await bankClient.createRandomBankUser(); + + await walletClient.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + logger.info("starting AcceptManualWithdrawal request"); + + const wres = await walletClient.call( + WalletApiOperation.AcceptManualWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10" as AmountString, + }, + ); + + logger.info("AcceptManualWithdrawal finished"); + logger.info(`result: ${j2s(wres)}`); + + const reservePub: string = wres.reservePub; + + await wireGatewayApiClient.adminAddIncoming({ + amount: "TESTKUDOS:5", + debitAccountPayto: user.accountPaytoUri, + reservePub: reservePub, + }); + + await exchange.runWirewatchOnce(); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Check balance + + const balResp = await walletClient.call(WalletApiOperation.GetBalances, {}); + + // We managed to withdraw the actually transferred amount! + t.assertAmountEquals(balResp.balances[0].available, "TESTKUDOS:4.85"); + + await t.shutdown(); +} + +runWithdrawalAmountTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-bank-integrated.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-bank-integrated.ts new file mode 100644 index 000000000..a13095883 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-bank-integrated.ts @@ -0,0 +1,187 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + NotificationType, + TransactionMajorState, + TransactionMinorState, + TransactionType, + WithdrawalType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runWithdrawalBankIntegratedTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + // Create a withdrawal operation + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth(user); + const wop = await bankClient.createWithdrawalOperation( + user.username, + "TESTKUDOS:10", + ); + + // Hand it to the wallet + + const r1 = await walletClient.client.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + // Withdraw + + const r2 = await walletClient.client.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + const withdrawalBankConfirmedCond = walletClient.waitForNotificationCond( + (x) => { + return ( + x.type === NotificationType.TransactionStateTransition && + x.transactionId === r2.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.ExchangeWaitReserve + ); + }, + ); + + const withdrawalFinishedCond = walletClient.waitForNotificationCond((x) => { + return ( + x.type === NotificationType.TransactionStateTransition && + x.transactionId === r2.transactionId && + x.newTxState.major === TransactionMajorState.Done + ); + }); + + const withdrawalReserveReadyCond = walletClient.waitForNotificationCond( + (x) => { + return ( + x.type === NotificationType.TransactionStateTransition && + x.transactionId === r2.transactionId && + x.newTxState.major === TransactionMajorState.Pending && + x.newTxState.minor === TransactionMinorState.WithdrawCoins + ); + }, + ); + + // Do it twice to check idempotency + const r3 = await walletClient.client.call( + WalletApiOperation.AcceptBankIntegratedWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + await exchange.stopWirewatch(); + + // Check status before withdrawal is confirmed by bank. + { + const txn = await walletClient.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log("transactions before confirmation:", j2s(txn)); + const tx0 = txn.transactions[0]; + t.assertTrue(tx0.type === TransactionType.Withdrawal); + t.assertTrue( + tx0.withdrawalDetails.type === WithdrawalType.TalerBankIntegrationApi, + ); + t.assertTrue(tx0.withdrawalDetails.confirmed === false); + t.assertTrue(tx0.withdrawalDetails.reserveIsReady === false); + } + + // Confirm it + + await bankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + await withdrawalBankConfirmedCond; + + // Check status after withdrawal is confirmed by bank, + // but before funds are wired to the exchange. + { + const txn = await walletClient.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log("transactions after confirmation:", j2s(txn)); + const tx0 = txn.transactions[0]; + t.assertTrue(tx0.type === TransactionType.Withdrawal); + t.assertTrue( + tx0.withdrawalDetails.type === WithdrawalType.TalerBankIntegrationApi, + ); + t.assertTrue(tx0.withdrawalDetails.confirmed === true); + t.assertTrue(tx0.withdrawalDetails.reserveIsReady === false); + } + + await exchange.startWirewatch(); + + await withdrawalReserveReadyCond; + + // Check status after funds were wired. + { + const txn = await walletClient.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log("transactions after reserve ready:", j2s(txn)); + const tx0 = txn.transactions[0]; + t.assertTrue(tx0.type === TransactionType.Withdrawal); + t.assertTrue( + tx0.withdrawalDetails.type === WithdrawalType.TalerBankIntegrationApi, + ); + t.assertTrue(tx0.withdrawalDetails.confirmed === true); + t.assertTrue(tx0.withdrawalDetails.reserveIsReady === true); + } + + await withdrawalFinishedCond; + + // Check balance + + const balResp = await walletClient.client.call( + WalletApiOperation.GetBalances, + {}, + ); + t.assertAmountEquals("TESTKUDOS:9.72", balResp.balances[0].available); + + const txn = await walletClient.client.call( + WalletApiOperation.GetTransactions, + {}, + ); + console.log(`transactions: ${j2s(txn)}`); +} + +runWithdrawalBankIntegratedTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-conversion.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-conversion.ts new file mode 100644 index 000000000..615feafa7 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-conversion.ts @@ -0,0 +1,303 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + AmountString, + Duration, + Logger, + TalerBankConversionApi, + TalerCorebankApiClient, + TransactionType, + WireGatewayApiClient, + WithdrawalType, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import * as http from "node:http"; +import { defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + FakebankService, + GlobalTestState, + MerchantService, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; +import { createWalletDaemonWithClient } from "../harness/helpers.js"; + +const logger = new Logger("test-withdrawal-conversion.ts"); + +interface TestfakeConversionService { + stop: () => void; +} + +function splitInTwoAt(s: string, separator: string): [string, string] { + const idx = s.indexOf(separator); + if (idx === -1) { + return [s, ""]; + } + return [s.slice(0, idx), s.slice(idx + 1)]; +} + +/** + * Testfake for the kyc service that the exchange talks to. + */ +async function runTestfakeConversionService(): Promise<TestfakeConversionService> { + const server = http.createServer((req, res) => { + const requestUrl = req.url!; + logger.info(`kyc: got ${req.method} request, ${requestUrl}`); + + const [path, query] = splitInTwoAt(requestUrl, "?"); + + const qp = new URLSearchParams(query); + + if (path === "/config") { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + version: "0:0:0", + name: "taler-conversion-info", + regional_currency: "FOO", + fiat_currency: "BAR", + regional_currency_specification: { + alt_unit_names: {}, + name: "FOO", + num_fractional_input_digits: 2, + num_fractional_normal_digits: 2, + num_fractional_trailing_zero_digits: 2, + }, + fiat_currency_specification: { + alt_unit_names: {}, + name: "BAR", + num_fractional_input_digits: 2, + num_fractional_normal_digits: 2, + num_fractional_trailing_zero_digits: 2, + }, + conversion_rate: { + cashin_fee: "A:1" as AmountString, + cashin_min_amount: "A:0.1" as AmountString, + cashin_ratio: "1", + cashin_rounding_mode: "zero", + cashin_tiny_amount: "A:1" as AmountString, + cashout_fee: "A:1" as AmountString, + cashout_min_amount: "A:0.1" as AmountString, + cashout_ratio: "1", + cashout_rounding_mode: "zero", + cashout_tiny_amount: "A:1" as AmountString, + }, + } satisfies TalerBankConversionApi.IntegrationConfig), + ); + } else if (path === "/cashin-rate") { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + amount_debit: "FOO:123", + amount_credit: "BAR:123", + }), + ); + } else { + res.writeHead(400, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ code: 1, message: "bad request" })); + } + }); + await new Promise<void>((resolve, reject) => { + server.listen(8071, () => resolve()); + }); + return { + stop() { + server.close(); + }, + }; +} + +/** + * Test for currency conversion during manual withdrawal. + */ +export async function runWithdrawalConversionTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await FakebankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + const merchant = await MerchantService.create(t, { + name: "testmerchant-1", + currency: "TESTKUDOS", + httpPort: 8083, + database: db.connStr, + }); + + const exchangeBankAccount = await bank.createExchangeAccount( + "myexchange", + "x", + ); + exchangeBankAccount.conversionUrl = "http://localhost:8071/"; + await exchange.addBankAccount("1", exchangeBankAccount); + + await bank.start(); + + await bank.pingUntilAvailable(); + + exchange.addOfferedCoins(defaultCoinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + merchant.addExchange(exchange); + await merchant.start(); + await merchant.pingUntilAvailable(); + + await merchant.addInstanceWithWireAccount({ + id: "default", + name: "Default Instance", + paytoUris: [generateRandomPayto("merchant-default")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + await merchant.addInstanceWithWireAccount({ + id: "minst1", + name: "minst1", + paytoUris: [generateRandomPayto("minst1")], + defaultWireTransferDelay: Duration.toTalerProtocolDuration( + Duration.fromSpec({ minutes: 1 }), + ), + }); + + const { walletClient, walletService } = await createWalletDaemonWithClient( + t, + { name: "wallet" }, + ); + + await runTestfakeConversionService(); + + // Create a withdrawal operation + + const bankAccessApiClient = new TalerCorebankApiClient( + bank.corebankApiBaseUrl, + ); + + const user = await bankAccessApiClient.createRandomBankUser(); + + await walletClient.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + const infoRes = await walletClient.call( + WalletApiOperation.GetWithdrawalDetailsForAmount, + { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:20" as AmountString, + }, + ); + + console.log(`withdrawal details: ${j2s(infoRes)}`); + + const checkTransferAmount = infoRes.withdrawalAccountsList[0].transferAmount; + t.assertTrue(checkTransferAmount != null); + t.assertAmountEquals(checkTransferAmount, "FOO:123"); + + const tStart = AbsoluteTime.now(); + + logger.info("starting AcceptManualWithdrawal request"); + // We expect this to return immediately. + + const wres = await walletClient.call( + WalletApiOperation.AcceptManualWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10" as AmountString, + }, + ); + + logger.info("AcceptManualWithdrawal finished"); + logger.info(`result: ${j2s(wres)}`); + + const acceptedTransferAmount = wres.withdrawalAccountsList[0].transferAmount; + t.assertTrue(acceptedTransferAmount != null); + + t.assertAmountEquals(acceptedTransferAmount, "FOO:123"); + + const txInfo = await walletClient.call( + WalletApiOperation.GetTransactionById, + { + transactionId: wres.transactionId, + }, + ); + + t.assertDeepEqual(txInfo.type, TransactionType.Withdrawal); + t.assertDeepEqual( + txInfo.withdrawalDetails.type, + WithdrawalType.ManualTransfer, + ); + t.assertTrue(!!txInfo.withdrawalDetails.exchangeCreditAccountDetails); + t.assertDeepEqual( + txInfo.withdrawalDetails.exchangeCreditAccountDetails[0].transferAmount, + "FOO:123", + ); + + // Check that the request did not go into long-polling. + const duration = AbsoluteTime.difference(tStart, AbsoluteTime.now()); + if (typeof duration.d_ms !== "number" || duration.d_ms > 5 * 1000) { + throw Error("withdrawal took too long (longpolling issue)"); + } + + const reservePub: string = wres.reservePub; + + const wireGatewayApiClient = new WireGatewayApiClient( + exchangeBankAccount.wireGatewayApiBaseUrl, + { + auth: { + username: exchangeBankAccount.accountName, + password: exchangeBankAccount.accountPassword, + }, + }, + ); + + await wireGatewayApiClient.adminAddIncoming({ + amount: "TESTKUDOS:10", + debitAccountPayto: user.accountPaytoUri, + reservePub: reservePub, + }); + + await exchange.runWirewatchOnce(); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Check balance + + const balResp = await walletClient.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals("TESTKUDOS:9.72", balResp.balances[0].available); +} + +runWithdrawalConversionTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-fakebank.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-fakebank.ts new file mode 100644 index 000000000..1dc955649 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-fakebank.ts @@ -0,0 +1,101 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { AmountString, URL } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + ExchangeService, + FakebankService, + GlobalTestState, + WalletCli, + setupDb, +} from "../harness/harness.js"; + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runWithdrawalFakebankTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await FakebankService.create(t, { + currency: "TESTKUDOS", + httpPort: 8082, + allowRegistrations: true, + // Not used by fakebank + database: db.connStr, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + exchange.addBankAccount("1", { + accountName: "exchange", + accountPassword: "x", + wireGatewayApiBaseUrl: new URL( + "/accounts/exchange/taler-wire-gateway/", + bank.baseUrl, + ).href, + accountPaytoUri: + "payto://x-taler-bank/localhost/exchange?receiver-name=Exchange", + }); + + await bank.createExchangeAccount("exchange", "x"); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + console.log("setup done!"); + + const wallet = new WalletCli(t); + + await wallet.client.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + await wallet.client.call(WalletApiOperation.WithdrawTestBalance, { + corebankApiBaseUrl: bank.corebankApiBaseUrl, + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10" as AmountString, + }); + + await exchange.runWirewatchOnce(); + + await wallet.runUntilDone(); + + // Check balance + + const balResp = await wallet.client.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals("TESTKUDOS:9.72", balResp.balances[0].available); +} + +runWithdrawalFakebankTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-fees.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-fees.ts new file mode 100644 index 000000000..1c65de7d9 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-fees.ts @@ -0,0 +1,192 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { TalerCorebankApiClient, j2s } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig } from "../harness/denomStructures.js"; +import { + BankService, + ExchangeService, + GlobalTestState, + WalletCli, + generateRandomPayto, + setupDb, +} from "../harness/harness.js"; + +const coinRsaCommon = { + cipher: "RSA" as const, + durationLegal: "3 years", + durationSpend: "2 years", + durationWithdraw: "7 days", + rsaKeySize: 1024, +}; + +const coin_u1 = (curr: string): CoinConfig => ({ + ...coinRsaCommon, + name: `${curr}_u1`, + value: `${curr}:1`, + feeDeposit: `${curr}:0`, + feeRefresh: `${curr}:0`, + feeRefund: `${curr}:0`, + feeWithdraw: `${curr}:1`, +}); + +const coin_u5 = (curr: string): CoinConfig => ({ + ...coinRsaCommon, + name: `${curr}_u5`, + value: `${curr}:5`, + feeDeposit: `${curr}:0`, + feeRefresh: `${curr}:0`, + feeRefund: `${curr}:0`, + feeWithdraw: `${curr}:1`, +}); + +export const weirdCoinConfig = [coin_u1, coin_u5]; + +/** + * Test withdrawal with a weird denomination structure to + * make sure fees are computed as expected. + */ +export async function runWithdrawalFeesTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + allowRegistrations: true, + currency: "TESTKUDOS", + database: db.connStr, + httpPort: 8082, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + let receiverName = "Exchange"; + let exchangeBankUsername = "exchange"; + let exchangeBankPassword = "mypw"; + let exchangePaytoUri = generateRandomPayto(exchangeBankUsername); + + await exchange.addBankAccount("1", { + accountName: exchangeBankUsername, + accountPassword: exchangeBankPassword, + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: exchangePaytoUri, + }); + + bank.setSuggestedExchange(exchange, exchangePaytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + }, + }); + + await bankClient.registerAccountExtended({ + name: receiverName, + password: exchangeBankPassword, + username: exchangeBankUsername, + is_taler_exchange: true, + payto_uri: exchangePaytoUri, + }); + + const coinConfig: CoinConfig[] = weirdCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + console.log("setup done!"); + + const wallet = new WalletCli(t); + + await wallet.client.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + const amount = "TESTKUDOS:7.5"; + + const user = await bankClient.createRandomBankUser(); + bankClient.setAuth(user); + const wop = await bankClient.createWithdrawalOperation( + user.username, + amount, + ); + + // Hand it to the wallet + + const details = await wallet.client.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + console.log(j2s(details)); + + const amountDetails = await wallet.client.call( + WalletApiOperation.GetWithdrawalDetailsForAmount, + { + amount: details.amount, + exchangeBaseUrl: details.possibleExchanges[0].exchangeBaseUrl, + }, + ); + + console.log(j2s(amountDetails)); + + t.assertAmountEquals(amountDetails.amountEffective, "TESTKUDOS:5"); + t.assertAmountEquals(amountDetails.amountRaw, "TESTKUDOS:7.5"); + + await wallet.runPending(); + + // Withdraw (AKA select) + + await wallet.client.call(WalletApiOperation.AcceptBankIntegratedWithdrawal, { + exchangeBaseUrl: exchange.baseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }); + + // Confirm it + + await bankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + await wallet.runUntilDone(); + + // Check balance + + const balResp = await wallet.client.call(WalletApiOperation.GetBalances, {}); + console.log(j2s(balResp)); + + t.assertAmountEquals(balResp.balances[0].available, "TESTKUDOS:5"); + + const txns = await wallet.client.call(WalletApiOperation.GetTransactions, {}); + console.log(j2s(txns)); +} + +runWithdrawalFeesTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-handover.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-handover.ts new file mode 100644 index 000000000..82d551948 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-handover.ts @@ -0,0 +1,198 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + TalerCorebankApiClient, + TransactionIdStr, + TransactionMajorState, + TransactionMinorState, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, +} from "../harness/helpers.js"; + +/** + * Test handing over a withdrawal to another wallet. + */ +export async function runWithdrawalHandoverTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange } = + await createSimpleTestkudosEnvironmentV3(t); + + // Do one normal withdrawal with the new split API + { + // Create a withdrawal operation + + const user = await bankClient.createRandomBankUser(); + const userBankClient = new TalerCorebankApiClient(bankClient.baseUrl); + userBankClient.setAuth(user); + const amount = "TESTKUDOS:10"; + const wop = await userBankClient.createWithdrawalOperation( + user.username, + amount, + ); + + const checkResp = await walletClient.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + t.assertTrue(!!checkResp.defaultExchangeBaseUrl); + + const prepareResp = await walletClient.call( + WalletApiOperation.PrepareBankIntegratedWithdrawal, + { + // exchangeBaseUrl: checkResp.defaultExchangeBaseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + console.log(`prepareResp: ${j2s(prepareResp)}`); + + t.assertTrue(!!prepareResp.transactionId); + + const txns1 = await walletClient.call(WalletApiOperation.GetTransactions, { + sort: "stable-ascending", + }); + console.log(j2s(txns1)); + + await walletClient.call(WalletApiOperation.ConfirmWithdrawal, { + transactionId: prepareResp.transactionId, + amount, + exchangeBaseUrl: checkResp.defaultExchangeBaseUrl, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: prepareResp.transactionId as TransactionIdStr, + txState: { + major: TransactionMajorState.Pending, + minor: TransactionMinorState.BankConfirmTransfer, + }, + }); + + await userBankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: prepareResp.transactionId as TransactionIdStr, + txState: { + major: TransactionMajorState.Done, + }, + }); + } + + // Do one another withdrawal with handover. + { + t.logStep("start-subtest-handover"); + + const w2 = await createWalletDaemonWithClient(t, { + name: "w2", + }); + + // Create a withdrawal operation + + const user = await bankClient.createRandomBankUser(); + const userBankClient = new TalerCorebankApiClient(bankClient.baseUrl); + userBankClient.setAuth(user); + const amount = "TESTKUDOS:10"; + + const wop = await userBankClient.createWithdrawalOperation( + user.username, + amount, + ); + + const checkResp = await walletClient.call( + WalletApiOperation.GetWithdrawalDetailsForUri, + { + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + t.assertTrue(!!checkResp.defaultExchangeBaseUrl); + + const prepareRespW1 = await walletClient.call( + WalletApiOperation.PrepareBankIntegratedWithdrawal, + { + // exchangeBaseUrl: checkResp.defaultExchangeBaseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + const prepareRespW2 = await w2.walletClient.call( + WalletApiOperation.PrepareBankIntegratedWithdrawal, + { + // exchangeBaseUrl: checkResp.defaultExchangeBaseUrl, + talerWithdrawUri: wop.taler_withdraw_uri, + }, + ); + + t.assertTrue(!!prepareRespW2.transactionId); + + await w2.walletClient.call(WalletApiOperation.ConfirmWithdrawal, { + transactionId: prepareRespW2.transactionId, + amount, + exchangeBaseUrl: checkResp.defaultExchangeBaseUrl, + }); + + await w2.walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: prepareRespW2.transactionId as TransactionIdStr, + txState: { + major: TransactionMajorState.Pending, + minor: TransactionMinorState.BankConfirmTransfer, + }, + }); + + await userBankClient.confirmWithdrawalOperation(user.username, { + withdrawalOperationId: wop.withdrawal_id, + }); + + console.log(`wopid is ${wop.withdrawal_id}`); + + t.logStep("start-wait-w2-done"); + await w2.walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: prepareRespW2.transactionId as TransactionIdStr, + txState: { + major: TransactionMajorState.Done, + }, + }); + t.logStep("done-wait-w2-done"); + + t.logStep("start-wait-w1-done"); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: prepareRespW1.transactionId as TransactionIdStr, + txState: { + major: TransactionMajorState.Aborted, + minor: TransactionMinorState.CompletedByOtherWallet, + }, + }); + + t.logStep("done-wait-w1-done"); + } +} + +runWithdrawalHandoverTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-huge.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-huge.ts new file mode 100644 index 000000000..aaa6701f8 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-huge.ts @@ -0,0 +1,140 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + GlobalTestState, + setupDb, + ExchangeService, + WalletService, + WalletClient, + BankService, +} from "../harness/harness.js"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js"; +import { + AmountString, + NotificationType, + TalerCorebankApiClient, + TransactionMajorState, + URL, +} from "@gnu-taler/taler-util"; + +/** + * Withdraw a high amount. Mostly intended as a perf test. + * + * It is useful to see whether the wallet stays responsive while doing a huge withdrawal. + * (This is not automatic yet. Use taler-wallet-cli to connect to the daemon and make requests to check.) + */ +export async function runWithdrawalHugeTest(t: GlobalTestState) { + // Set up test environment + + const db = await setupDb(t); + + const bank = await BankService.create(t, { + currency: "TESTKUDOS", + httpPort: 8082, + allowRegistrations: true, + // Not used by fakebank + database: db.connStr, + }); + + const exchange = ExchangeService.create(t, { + name: "testexchange-1", + currency: "TESTKUDOS", + httpPort: 8081, + database: db.connStr, + }); + + let paytoUri = "payto://x-taler-bank/localhost/exchange"; + + await exchange.addBankAccount("1", { + accountName: "exchange", + accountPassword: "x", + wireGatewayApiBaseUrl: new URL("accounts/exchange/taler-wire-gateway/", bank.baseUrl).href, + accountPaytoUri: paytoUri, + }); + + bank.setSuggestedExchange(exchange, paytoUri); + + await bank.start(); + + await bank.pingUntilAvailable(); + + const bankClient = new TalerCorebankApiClient(bank.corebankApiBaseUrl, { + auth: { + username: "admin", + password: "adminpw", + } + }); + + await bankClient.registerAccountExtended({ + name: "Exchange", + password: "x", + username: "exchange", + is_taler_exchange: true, + payto_uri: paytoUri, + }); + + const coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")); + exchange.addCoinConfigList(coinConfig); + + await exchange.start(); + await exchange.pingUntilAvailable(); + + console.log("setup done!"); + + const walletService = new WalletService(t, { name: "w1" }); + await walletService.start(); + await walletService.pingUntilAvailable(); + + const wallet = new WalletClient({ + name: "wallet", + unixPath: walletService.socketPath, + }); + await wallet.connect(); + + const withdrawalFinishedCond = wallet.waitForNotificationCond( + (wn) => + wn.type === NotificationType.TransactionStateTransition && + wn.transactionId.startsWith("txn:withdrawal:") && + wn.newTxState.major === TransactionMajorState.Done, + ); + + await wallet.client.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + // Results in about 1K coins withdrawn + await wallet.client.call(WalletApiOperation.WithdrawTestBalance, { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10000" as AmountString, + corebankApiBaseUrl: bank.baseUrl, + }); + + await withdrawalFinishedCond; + + // Check balance + + const balResp = await wallet.client.call(WalletApiOperation.GetBalances, {}); + console.log(balResp); +} + +runWithdrawalHugeTest.suites = ["wallet-perf"]; +// FIXME: Should not be "experimental" but "slow" or something similar. +runWithdrawalHugeTest.experimental = true; diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-manual.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-manual.ts new file mode 100644 index 000000000..cd7d137cc --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-manual.ts @@ -0,0 +1,103 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AbsoluteTime, + TalerCorebankApiClient, + Logger, + WireGatewayApiClient, + j2s, + AmountString, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +const logger = new Logger("test-withdrawal-manual.ts"); + +/** + * Run test for basic, bank-integrated withdrawal. + */ +export async function runWithdrawalManualTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, exchangeBankAccount } = + await createSimpleTestkudosEnvironmentV3(t); + + // Create a withdrawal operation + + const user = await bankClient.createRandomBankUser(); + + await walletClient.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + const tStart = AbsoluteTime.now(); + + logger.info("starting AcceptManualWithdrawal request"); + // We expect this to return immediately. + + const wres = await walletClient.call( + WalletApiOperation.AcceptManualWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10" as AmountString, + }, + ); + + logger.info("AcceptManualWithdrawal finished"); + logger.info(`result: ${j2s(wres)}`); + + // Check that the request did not go into long-polling. + const duration = AbsoluteTime.difference(tStart, AbsoluteTime.now()); + if (typeof duration.d_ms !== "number" || duration.d_ms > 5 * 1000) { + throw Error("withdrawal took too long (longpolling issue)"); + } + + const reservePub: string = wres.reservePub; + + const wireGatewayApiClient = new WireGatewayApiClient( + exchangeBankAccount.wireGatewayApiBaseUrl, + { + auth: { + username: "admin", + password: "adminpw", + }, + }, + ); + + await wireGatewayApiClient.adminAddIncoming({ + amount: "TESTKUDOS:10", + debitAccountPayto: user.accountPaytoUri, + reservePub: reservePub, + }); + + await exchange.runWirewatchOnce(); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Check balance + + const balResp = await walletClient.call(WalletApiOperation.GetBalances, {}); + t.assertAmountEquals("TESTKUDOS:9.72", balResp.balances[0].available); + + await t.shutdown(); +} + +runWithdrawalManualTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/testrunner.ts b/packages/taler-harness/src/integrationtests/testrunner.ts new file mode 100644 index 000000000..eb2ae7fa6 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/testrunner.ts @@ -0,0 +1,599 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { CancellationToken, Logger, minimatch } from "@gnu-taler/taler-util"; +import * as child_process from "child_process"; +import { spawnSync } from "child_process"; +import * as fs from "fs"; +import * as os from "os"; +import * as path from "path"; +import url from "url"; +import { + GlobalTestState, + TestRunResult, + runTestWithState, + shouldLingerInTest, +} from "../harness/harness.js"; +import { getSharedTestDir } from "../harness/helpers.js"; +import { runAgeRestrictionsDepositTest } from "./test-age-restrictions-deposit.js"; +import { runAgeRestrictionsMerchantTest } from "./test-age-restrictions-merchant.js"; +import { runAgeRestrictionsMixedMerchantTest } from "./test-age-restrictions-mixed-merchant.js"; +import { runAgeRestrictionsPeerTest } from "./test-age-restrictions-peer.js"; +import { runBankApiTest } from "./test-bank-api.js"; +import { runClaimLoopTest } from "./test-claim-loop.js"; +import { runClauseSchnorrTest } from "./test-clause-schnorr.js"; +import { runCurrencyScopeTest } from "./test-currency-scope.js"; +import { runDenomLostTest } from "./test-denom-lost.js"; +import { runDenomUnofferedTest } from "./test-denom-unoffered.js"; +import { runDepositTest } from "./test-deposit.js"; +import { runExchangeDepositTest } from "./test-exchange-deposit.js"; +import { runExchangeManagementFaultTest } from "./test-exchange-management-fault.js"; +import { runExchangeManagementTest } from "./test-exchange-management.js"; +import { runExchangePurseTest } from "./test-exchange-purse.js"; +import { runExchangeTimetravelTest } from "./test-exchange-timetravel.js"; +import { runFeeRegressionTest } from "./test-fee-regression.js"; +import { runForcedSelectionTest } from "./test-forced-selection.js"; +import { runKycTest } from "./test-kyc.js"; +import { runLibeufinBankTest } from "./test-libeufin-bank.js"; +import { runMerchantExchangeConfusionTest } from "./test-merchant-exchange-confusion.js"; +import { runMerchantInstancesDeleteTest } from "./test-merchant-instances-delete.js"; +import { runMerchantInstancesUrlsTest } from "./test-merchant-instances-urls.js"; +import { runMerchantInstancesTest } from "./test-merchant-instances.js"; +import { runMerchantLongpollingTest } from "./test-merchant-longpolling.js"; +import { runMerchantRefundApiTest } from "./test-merchant-refund-api.js"; +import { runMerchantSpecPublicOrdersTest } from "./test-merchant-spec-public-orders.js"; +import { runMultiExchangeTest } from "./test-multiexchange.js"; +import { runOtpTest } from "./test-otp.js"; +import { runPayPaidTest } from "./test-pay-paid.js"; +import { runPaymentAbortTest } from "./test-payment-abort.js"; +import { runPaymentClaimTest } from "./test-payment-claim.js"; +import { runPaymentDeletedTest } from "./test-payment-deleted.js"; +import { runPaymentExpiredTest } from "./test-payment-expired.js"; +import { runPaymentFaultTest } from "./test-payment-fault.js"; +import { runPaymentForgettableTest } from "./test-payment-forgettable.js"; +import { runPaymentIdempotencyTest } from "./test-payment-idempotency.js"; +import { runPaymentMultipleTest } from "./test-payment-multiple.js"; +import { runPaymentShareTest } from "./test-payment-share.js"; +import { runPaymentTemplateTest } from "./test-payment-template.js"; +import { runPaymentTransientTest } from "./test-payment-transient.js"; +import { runPaymentZeroTest } from "./test-payment-zero.js"; +import { runPaymentTest } from "./test-payment.js"; +import { runPaywallFlowTest } from "./test-paywall-flow.js"; +import { runPeerPullLargeTest } from "./test-peer-pull-large.js"; +import { runPeerPushLargeTest } from "./test-peer-push-large.js"; +import { runPeerRepairTest } from "./test-peer-repair.js"; +import { runPeerToPeerPullTest } from "./test-peer-to-peer-pull.js"; +import { runPeerToPeerPushTest } from "./test-peer-to-peer-push.js"; +import { runRefundAutoTest } from "./test-refund-auto.js"; +import { runRefundGoneTest } from "./test-refund-gone.js"; +import { runRefundIncrementalTest } from "./test-refund-incremental.js"; +import { runRefundTest } from "./test-refund.js"; +import { runRevocationTest } from "./test-revocation.js"; +import { runSimplePaymentTest } from "./test-simple-payment.js"; +import { runStoredBackupsTest } from "./test-stored-backups.js"; +import { runTimetravelAutorefreshTest } from "./test-timetravel-autorefresh.js"; +import { runTimetravelWithdrawTest } from "./test-timetravel-withdraw.js"; +import { runTermOfServiceFormatTest } from "./test-tos-format.js"; +import { runWalletBackupBasicTest } from "./test-wallet-backup-basic.js"; +import { runWalletBackupDoublespendTest } from "./test-wallet-backup-doublespend.js"; +import { runWalletBalanceNotificationsTest } from "./test-wallet-balance-notifications.js"; +import { runWalletBalanceZeroTest } from "./test-wallet-balance-zero.js"; +import { runWalletBalanceTest } from "./test-wallet-balance.js"; +import { runWalletBlockedDepositTest } from "./test-wallet-blocked-deposit.js"; +import { runWalletBlockedPayMerchantTest } from "./test-wallet-blocked-pay-merchant.js"; +import { runWalletBlockedPayPeerPullTest } from "./test-wallet-blocked-pay-peer-pull.js"; +import { runWalletBlockedPayPeerPushTest } from "./test-wallet-blocked-pay-peer-push.js"; +import { runWalletCliTerminationTest } from "./test-wallet-cli-termination.js"; +import { runWalletConfigTest } from "./test-wallet-config.js"; +import { runWalletCryptoWorkerTest } from "./test-wallet-cryptoworker.js"; +import { runWalletDblessTest } from "./test-wallet-dbless.js"; +import { runWalletDd48Test } from "./test-wallet-dd48.js"; +import { runWalletDenomExpireTest } from "./test-wallet-denom-expire.js"; +import { runWalletDevExperimentsTest } from "./test-wallet-dev-experiments.js"; +import { runWalletExchangeUpdateTest } from "./test-wallet-exchange-update.js"; +import { runWalletGenDbTest } from "./test-wallet-gendb.js"; +import { runWalletInsufficientBalanceTest } from "./test-wallet-insufficient-balance.js"; +import { runWalletNotificationsTest } from "./test-wallet-notifications.js"; +import { runWalletObservabilityTest } from "./test-wallet-observability.js"; +import { runWalletRefreshErrorsTest } from "./test-wallet-refresh-errors.js"; +import { runWalletRefreshTest } from "./test-wallet-refresh.js"; +import { runWalletWirefeesTest } from "./test-wallet-wirefees.js"; +import { runWallettestingTest } from "./test-wallettesting.js"; +import { runWithdrawalAbortBankTest } from "./test-withdrawal-abort-bank.js"; +import { runWithdrawalBankIntegratedTest } from "./test-withdrawal-bank-integrated.js"; +import { runWithdrawalConversionTest } from "./test-withdrawal-conversion.js"; +import { runWithdrawalFakebankTest } from "./test-withdrawal-fakebank.js"; +import { runWithdrawalFeesTest } from "./test-withdrawal-fees.js"; +import { runWithdrawalHandoverTest } from "./test-withdrawal-handover.js"; +import { runWithdrawalHugeTest } from "./test-withdrawal-huge.js"; +import { runWithdrawalManualTest } from "./test-withdrawal-manual.js"; +import { runWithdrawalAmountTest } from "./test-withdrawal-amount.js"; + +/** + * Test runner. + */ +const logger = new Logger("testrunner.ts"); + +/** + * Spec for one test. + */ +interface TestMainFunction { + (t: GlobalTestState): Promise<void>; + timeoutMs?: number; + experimental?: boolean; + suites?: string[]; +} + +const allTests: TestMainFunction[] = [ + runAgeRestrictionsMerchantTest, + runAgeRestrictionsMixedMerchantTest, + runAgeRestrictionsPeerTest, + runAgeRestrictionsDepositTest, + runBankApiTest, + runClaimLoopTest, + runClauseSchnorrTest, + runDenomUnofferedTest, + runDepositTest, + runSimplePaymentTest, + runExchangeManagementFaultTest, + runExchangeTimetravelTest, + runFeeRegressionTest, + runForcedSelectionTest, + runKycTest, + runExchangePurseTest, + runExchangeDepositTest, + runMerchantExchangeConfusionTest, + runMerchantInstancesDeleteTest, + runMerchantInstancesTest, + runMerchantInstancesUrlsTest, + runMerchantLongpollingTest, + runMerchantRefundApiTest, + runMerchantSpecPublicOrdersTest, + runPaymentClaimTest, + runPaymentFaultTest, + runPaymentForgettableTest, + runPaymentIdempotencyTest, + runPaymentMultipleTest, + runPaymentTest, + runPaymentShareTest, + runPaymentTemplateTest, + runPaymentAbortTest, + runPaymentTransientTest, + runPaymentZeroTest, + runPayPaidTest, + runPeerRepairTest, + runMultiExchangeTest, + runWalletBalanceTest, + runPaywallFlowTest, + runPeerToPeerPullTest, + runPeerToPeerPushTest, + runRefundAutoTest, + runRefundGoneTest, + runRefundIncrementalTest, + runRefundTest, + runRevocationTest, + runWithdrawalManualTest, + runTimetravelAutorefreshTest, + runTimetravelWithdrawTest, + runWalletBackupBasicTest, + runWalletBackupDoublespendTest, + runWalletNotificationsTest, + runWalletCryptoWorkerTest, + runWalletDblessTest, + runWallettestingTest, + runWithdrawalAbortBankTest, + // runWithdrawalNotifyBeforeTxTest, + runWithdrawalBankIntegratedTest, + runWithdrawalFakebankTest, + runWithdrawalFeesTest, + runWithdrawalConversionTest, + runWithdrawalHugeTest, + runTermOfServiceFormatTest, + runStoredBackupsTest, + runPaymentExpiredTest, + runWalletGenDbTest, + runLibeufinBankTest, + runPaymentDeletedTest, + runWalletDd48Test, + runCurrencyScopeTest, + runWalletRefreshTest, + runWalletCliTerminationTest, + runOtpTest, + runWalletBalanceNotificationsTest, + runExchangeManagementTest, + runWalletConfigTest, + runWalletObservabilityTest, + runWalletDevExperimentsTest, + runWalletBalanceZeroTest, + runWalletInsufficientBalanceTest, + runWalletWirefeesTest, + runDenomLostTest, + runWalletDenomExpireTest, + runWalletBlockedDepositTest, + runWalletBlockedPayMerchantTest, + runWalletBlockedPayPeerPushTest, + runWalletBlockedPayPeerPullTest, + runWalletExchangeUpdateTest, + runWalletRefreshErrorsTest, + runPeerPullLargeTest, + runPeerPushLargeTest, + runWithdrawalHandoverTest, + runWithdrawalAmountTest, +]; + +export interface TestRunSpec { + includePattern?: string; + suiteSpec?: string; + dryRun?: boolean; + failFast?: boolean; + waitOnFail?: boolean; + includeExperimental: boolean; + noTimeout: boolean; + verbosity: number; +} + +export interface TestInfo { + name: string; + suites: string[]; + experimental: boolean; +} + +function updateCurrentSymlink(testDir: string): void { + const currLink = path.join( + os.tmpdir(), + `taler-integrationtests-${os.userInfo().username}-current`, + ); + try { + fs.unlinkSync(currLink); + } catch (e) { + // Ignore + } + try { + fs.symlinkSync(testDir, currLink); + } catch (e) { + console.log(e); + // Ignore + } +} + +export function getTestName(tf: TestMainFunction): string { + const res = tf.name.match(/run([a-zA-Z0-9]*)Test/); + if (!res) { + throw Error("invalid test name, must be 'run${NAME}Test'"); + } + return res[1] + .replace(/[a-z0-9][A-Z]/g, (x) => { + return x[0] + "-" + x[1]; + }) + .toLowerCase(); +} + +interface RunTestChildInstruction { + testName: string; + testRootDir: string; +} + +function purgeSharedTestEnvironment() { + const rmRes = spawnSync("rm", ["-rf", `${getSharedTestDir()}`]); + if (rmRes.status != 0) { + logger.warn("can't delete shared test directory"); + } + const psqlRes = spawnSync("psql", ["-Aqtl"], { + encoding: "utf-8", + }); + if (psqlRes.status != 0) { + logger.warn("could not list available postgres databases"); + return; + } + if (psqlRes.output[1]!!.indexOf("taler-integrationtest-shared") >= 0) { + const dropRes = spawnSync("dropdb", ["taler-integrationtest-shared"], { + encoding: "utf-8", + }); + if (dropRes.status != 0) { + logger.warn("could not drop taler-integrationtest-shared database"); + return; + } + } +} + +export async function runTests(spec: TestRunSpec) { + if (!process.env.TALER_HARNESS_KEEP) { + logger.info("purging shared test environment"); + purgeSharedTestEnvironment(); + } else { + logger.info("keeping shared test environment"); + } + + const testRootDir = fs.mkdtempSync( + path.join(os.tmpdir(), "taler-integrationtests-"), + ); + updateCurrentSymlink(testRootDir); + console.log(`testsuite root directory: ${testRootDir}`); + + const testResults: TestRunResult[] = []; + + let currentChild: child_process.ChildProcess | undefined; + + const handleSignal = (s: NodeJS.Signals) => { + console.log(`received signal ${s} in test parent`); + if (currentChild) { + currentChild.kill("SIGTERM"); + } + reportAndQuit(testRootDir, testResults, true); + }; + + process.on("SIGINT", (s) => handleSignal(s)); + process.on("SIGTERM", (s) => handleSignal(s)); + //process.on("unhandledRejection", handleSignal); + //process.on("uncaughtException", handleSignal); + + let suites: Set<string> | undefined; + + if (spec.suiteSpec) { + suites = new Set(spec.suiteSpec.split(",").map((x) => x.trim())); + } + + for (const [n, testCase] of allTests.entries()) { + const testName = getTestName(testCase); + if (spec.includePattern && !minimatch(testName, spec.includePattern)) { + continue; + } + + if (testCase.experimental && !spec.includeExperimental) { + continue; + } + + if (suites) { + const ts = new Set(testCase.suites ?? []); + const intersection = new Set([...suites].filter((x) => ts.has(x))); + if (intersection.size === 0) { + continue; + } + } + + if (spec.dryRun) { + console.log(`dry run: would run test ${testName}`); + continue; + } + + const testInstr: RunTestChildInstruction = { + testName, + testRootDir, + }; + + const myFilename = url.fileURLToPath(import.meta.url); + + currentChild = child_process.fork(myFilename, ["__TWCLI_TESTWORKER"], { + env: { + TWCLI_RUN_TEST_INSTRUCTION: JSON.stringify(testInstr), + ...process.env, + }, + stdio: ["pipe", "pipe", "pipe", "ipc"], + }); + + const testDir = path.join(testRootDir, testName); + fs.mkdirSync(testDir, { recursive: true }); + + const harnessLogFilename = path.join(testRootDir, testName, "harness.log"); + const harnessLogStream = fs.createWriteStream(harnessLogFilename); + + if (spec.verbosity > 0) { + currentChild.stderr?.pipe(process.stderr); + currentChild.stdout?.pipe(process.stdout); + } + + currentChild.stdout?.pipe(harnessLogStream); + currentChild.stderr?.pipe(harnessLogStream); + + // Default timeout when the test doesn't override it. + let defaultTimeout = 60000; + const overrideDefaultTimeout = process.env.TALER_TEST_TIMEOUT; + if (overrideDefaultTimeout) { + defaultTimeout = Number.parseInt(overrideDefaultTimeout, 10) * 1000; + } + + // Set the timeout to at least be the default timeout. + const testTimeoutMs = testCase.timeoutMs + ? Math.max(testCase.timeoutMs, defaultTimeout) + : defaultTimeout; + + if (spec.noTimeout) { + console.log(`running ${testName}, no timeout`); + } else { + console.log(`running ${testName} with timeout ${testTimeoutMs}ms`); + } + + const token = spec.noTimeout + ? CancellationToken.CONTINUE + : CancellationToken.timeout(testTimeoutMs).token; + + const resultPromise: Promise<TestRunResult> = new Promise( + (resolve, reject) => { + let msg: TestRunResult | undefined; + currentChild!.on("message", (m) => { + if (token.isCancelled) { + return; + } + msg = m as TestRunResult; + }); + currentChild!.on("exit", (code, signal) => { + if (token.isCancelled) { + return; + } + logger.info(`process exited code=${code} signal=${signal}`); + if (signal) { + reject(new Error(`test worker exited with signal ${signal}`)); + } else if (code != 0) { + reject(new Error(`test worker exited with code ${code}`)); + } else if (!msg) { + reject( + new Error( + `test worker exited without giving back the test results`, + ), + ); + } else { + resolve(msg); + } + }); + currentChild!.on("error", (err) => { + if (token.isCancelled) { + return; + } + reject(err); + }); + }, + ); + + let result: TestRunResult; + + try { + result = await token.racePromise(resultPromise); + if (result.status === "fail" && spec.failFast) { + logger.error("test failed and failing fast, exit!"); + throw Error("exit on fail fast"); + } + } catch (e: any) { + console.error(`test ${testName} timed out`); + if (token.isCancelled) { + result = { + status: "fail", + reason: "timeout", + timeSec: testTimeoutMs / 1000, + name: testName, + }; + currentChild.kill("SIGTERM"); + } else { + throw Error(e); + } + } + + harnessLogStream.close(); + + console.log(`parent: got result ${JSON.stringify(result)}`); + + testResults.push(result); + } + + reportAndQuit(testRootDir, testResults); +} + +export function reportAndQuit( + testRootDir: string, + testResults: TestRunResult[], + interrupted: boolean = false, +): never { + let numTotal = 0; + let numFail = 0; + let numSkip = 0; + let numPass = 0; + + for (const result of testResults) { + numTotal++; + if (result.status === "fail") { + numFail++; + } else if (result.status === "skip") { + numSkip++; + } else if (result.status === "pass") { + numPass++; + } + } + + const resultsFile = path.join(testRootDir, "results.json"); + fs.writeFileSync( + path.join(testRootDir, "results.json"), + JSON.stringify({ testResults, interrupted }, undefined, 2), + ); + if (interrupted) { + console.log("test suite was interrupted"); + } + console.log(`See ${resultsFile} for details`); + console.log(`Skipped: ${numSkip}/${numTotal}`); + console.log(`Failed: ${numFail}/${numTotal}`); + console.log(`Passed: ${numPass}/${numTotal}`); + + if (interrupted) { + process.exit(3); + } else if (numPass < numTotal - numSkip) { + process.exit(1); + } else { + process.exit(0); + } +} + +export function getTestInfo(): TestInfo[] { + return allTests.map((x) => ({ + name: getTestName(x), + suites: x.suites ?? [], + experimental: x.experimental ?? false, + })); +} + +const runTestInstrStr = process.env["TWCLI_RUN_TEST_INSTRUCTION"]; +if (runTestInstrStr && process.argv.includes("__TWCLI_TESTWORKER")) { + // Test will call taler-wallet-cli, so we must not propagate this variable. + delete process.env["TWCLI_RUN_TEST_INSTRUCTION"]; + const { testRootDir, testName } = JSON.parse( + runTestInstrStr, + ) as RunTestChildInstruction; + + process.on("disconnect", () => { + logger.trace("got disconnect from parent"); + process.exit(3); + }); + + const runTest = async () => { + let testMain: TestMainFunction | undefined; + for (const t of allTests) { + if (getTestName(t) === testName) { + testMain = t; + break; + } + } + + if (!process.send) { + logger.error("can't communicate with parent"); + process.exit(2); + } + + if (!testMain) { + logger.info(`test ${testName} not found`); + process.exit(2); + } + + const testDir = path.join(testRootDir, testName); + logger.info(`running test ${testName}`); + const gc = new GlobalTestState({ + testDir, + }); + const testResult = await runTestWithState(gc, testMain, testName); + logger.info(`done test ${testName}: ${testResult.status}`); + process.send(testResult); + }; + + runTest() + .then(() => { + logger.trace(`test ${testName} finished in worker`); + if (shouldLingerInTest()) { + logger.trace("lingering ..."); + return; + } + process.exit(0); + }) + .catch((e) => { + logger.error(e); + process.exit(1); + }); +} |