2020-08-05 21:00:36 +02:00
|
|
|
/*
|
|
|
|
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 {
|
|
|
|
GlobalTestState,
|
|
|
|
MerchantService,
|
|
|
|
ExchangeService,
|
|
|
|
setupDb,
|
|
|
|
BankService,
|
|
|
|
WalletCli,
|
2020-08-19 17:26:10 +02:00
|
|
|
MerchantPrivateApi,
|
2020-08-20 10:25:03 +02:00
|
|
|
BankApi,
|
|
|
|
BankAccessApi,
|
2020-08-05 21:00:36 +02:00
|
|
|
} from "./harness";
|
2020-08-12 09:11:00 +02:00
|
|
|
import {
|
|
|
|
FaultInjectedExchangeService,
|
|
|
|
FaultInjectionRequestContext,
|
|
|
|
FaultInjectionResponseContext,
|
|
|
|
} from "./faultInjection";
|
2021-03-17 17:56:37 +01:00
|
|
|
import { CoreApiResponse } from "@gnu-taler/taler-util";
|
2020-09-08 19:14:01 +02:00
|
|
|
import { defaultCoinConfig } from "./denomStructures";
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run test for basic, bank-integrated withdrawal.
|
|
|
|
*/
|
2021-01-12 20:04:16 +01:00
|
|
|
export async function runPaymentFaultTest(t: GlobalTestState) {
|
2020-08-05 21:00:36 +02:00
|
|
|
// 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,
|
|
|
|
});
|
|
|
|
|
2020-08-12 09:11:00 +02:00
|
|
|
const exchangeBankAccount = await bank.createExchangeAccount(
|
|
|
|
"MyExchange",
|
|
|
|
"x",
|
|
|
|
);
|
2020-08-08 18:57:26 +02:00
|
|
|
|
|
|
|
bank.setSuggestedExchange(exchange, exchangeBankAccount.accountPaytoUri);
|
2020-08-07 19:36:52 +02:00
|
|
|
|
|
|
|
await bank.start();
|
|
|
|
|
|
|
|
await bank.pingUntilAvailable();
|
|
|
|
|
2020-08-08 18:57:26 +02:00
|
|
|
await exchange.addBankAccount("1", exchangeBankAccount);
|
2020-08-07 19:36:52 +02:00
|
|
|
exchange.addOfferedCoins(defaultCoinConfig);
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
await exchange.start();
|
|
|
|
await exchange.pingUntilAvailable();
|
|
|
|
|
|
|
|
const faultyExchange = new FaultInjectedExchangeService(t, exchange, 8091);
|
|
|
|
|
|
|
|
// Print all requests to the exchange
|
|
|
|
faultyExchange.faultProxy.addFault({
|
2021-02-08 13:02:12 +01:00
|
|
|
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
2020-08-05 21:00:36 +02:00
|
|
|
console.log("got request", ctx);
|
|
|
|
},
|
2021-02-08 13:02:12 +01:00
|
|
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
2020-08-05 21:00:36 +02:00
|
|
|
console.log("got response", ctx);
|
2020-08-12 09:11:00 +02:00
|
|
|
},
|
2020-08-05 21:00:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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.addInstance({
|
|
|
|
id: "default",
|
|
|
|
name: "Default Instance",
|
|
|
|
paytoUris: [`payto://x-taler-bank/merchant-default`],
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("setup done!");
|
|
|
|
|
|
|
|
const wallet = new WalletCli(t);
|
|
|
|
|
|
|
|
// Create withdrawal operation
|
|
|
|
|
2020-08-20 10:25:03 +02:00
|
|
|
const user = await BankApi.createRandomBankUser(bank);
|
2020-09-01 15:37:14 +02:00
|
|
|
const wop = await BankAccessApi.createWithdrawalOperation(
|
|
|
|
bank,
|
|
|
|
user,
|
|
|
|
"TESTKUDOS:20",
|
|
|
|
);
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
// Hand it to the wallet
|
|
|
|
|
|
|
|
const r1 = await wallet.apiRequest("getWithdrawalDetailsForUri", {
|
|
|
|
talerWithdrawUri: wop.taler_withdraw_uri,
|
|
|
|
});
|
|
|
|
t.assertTrue(r1.type === "response");
|
|
|
|
|
|
|
|
await wallet.runPending();
|
|
|
|
|
|
|
|
// Confirm it
|
|
|
|
|
2020-08-20 10:25:03 +02:00
|
|
|
await BankApi.confirmWithdrawalOperation(bank, user, wop);
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
// Withdraw
|
|
|
|
|
|
|
|
const r2 = await wallet.apiRequest("acceptBankIntegratedWithdrawal", {
|
|
|
|
exchangeBaseUrl: faultyExchange.baseUrl,
|
|
|
|
talerWithdrawUri: wop.taler_withdraw_uri,
|
|
|
|
});
|
|
|
|
t.assertTrue(r2.type === "response");
|
|
|
|
await wallet.runUntilDone();
|
|
|
|
|
|
|
|
// Check balance
|
|
|
|
|
2021-02-05 11:56:17 +01:00
|
|
|
await wallet.getBalances();
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
// Set up order.
|
|
|
|
|
2020-08-19 17:26:10 +02:00
|
|
|
const orderResp = await MerchantPrivateApi.createOrder(merchant, "default", {
|
2020-08-05 21:00:36 +02:00
|
|
|
order: {
|
|
|
|
summary: "Buy me!",
|
|
|
|
amount: "TESTKUDOS:5",
|
|
|
|
fulfillment_url: "taler://fulfillment-success/thx",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-08-19 17:26:10 +02:00
|
|
|
let orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus(merchant, {
|
2020-08-12 20:56:55 +02:00
|
|
|
orderId: orderResp.order_id,
|
|
|
|
});
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
t.assertTrue(orderStatus.order_status === "unpaid");
|
|
|
|
|
|
|
|
// Make wallet pay for the order
|
|
|
|
|
|
|
|
let apiResp: CoreApiResponse;
|
|
|
|
|
|
|
|
apiResp = await wallet.apiRequest("preparePay", {
|
|
|
|
talerPayUri: orderStatus.taler_pay_uri,
|
|
|
|
});
|
|
|
|
t.assertTrue(apiResp.type === "response");
|
|
|
|
|
|
|
|
const proposalId = (apiResp.result as any).proposalId;
|
|
|
|
|
|
|
|
await wallet.runPending();
|
|
|
|
|
2021-01-11 00:46:28 +01:00
|
|
|
// Drop 3 responses from the exchange.
|
2020-08-05 21:00:36 +02:00
|
|
|
let faultCount = 0;
|
|
|
|
faultyExchange.faultProxy.addFault({
|
2021-02-08 13:02:12 +01:00
|
|
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
2021-02-05 11:56:17 +01:00
|
|
|
if (!ctx.request.requestUrl.endsWith("/deposit")) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-11 00:46:28 +01:00
|
|
|
if (faultCount < 3) {
|
2020-08-05 21:00:36 +02:00
|
|
|
faultCount++;
|
|
|
|
ctx.dropResponse = true;
|
|
|
|
}
|
2020-08-12 09:11:00 +02:00
|
|
|
},
|
2020-08-05 21:00:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// confirmPay won't work, as the exchange is unreachable
|
|
|
|
|
|
|
|
apiResp = await wallet.apiRequest("confirmPay", {
|
|
|
|
// FIXME: should be validated, don't cast!
|
|
|
|
proposalId: proposalId,
|
|
|
|
});
|
|
|
|
|
|
|
|
await wallet.runUntilDone();
|
|
|
|
|
|
|
|
// Check if payment was successful.
|
|
|
|
|
2020-08-19 17:26:10 +02:00
|
|
|
orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus(merchant, {
|
2020-08-12 20:56:55 +02:00
|
|
|
orderId: orderResp.order_id,
|
|
|
|
});
|
2020-08-05 21:00:36 +02:00
|
|
|
|
|
|
|
t.assertTrue(orderStatus.order_status === "paid");
|
2021-01-12 20:04:16 +01:00
|
|
|
}
|