2020-09-08 22:55:48 +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/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fault injection test to check aborting partial payment
|
|
|
|
* via refunds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2021-03-17 17:56:37 +01:00
|
|
|
import { PreparePayResultType, TalerErrorCode } from "@gnu-taler/taler-util";
|
2021-06-17 13:34:59 +02:00
|
|
|
import { URL, WalletApiOperation } from "@gnu-taler/taler-wallet-core";
|
2021-02-03 14:35:29 +01:00
|
|
|
import {
|
|
|
|
FaultInjectionRequestContext,
|
|
|
|
FaultInjectionResponseContext,
|
|
|
|
} from "./faultInjection";
|
2021-01-13 01:19:23 +01:00
|
|
|
import { GlobalTestState, MerchantPrivateApi, setupDb } from "./harness";
|
2020-09-08 22:55:48 +02:00
|
|
|
import {
|
2021-01-13 01:19:23 +01:00
|
|
|
createFaultInjectedMerchantTestkudosEnvironment,
|
|
|
|
withdrawViaBank,
|
|
|
|
} from "./helpers";
|
2020-09-08 22:55:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run test for basic, bank-integrated withdrawal.
|
|
|
|
*/
|
2021-01-12 20:04:16 +01:00
|
|
|
export async function runPayAbortTest(t: GlobalTestState) {
|
2021-01-13 01:19:23 +01:00
|
|
|
const {
|
|
|
|
bank,
|
|
|
|
faultyExchange,
|
|
|
|
wallet,
|
|
|
|
faultyMerchant,
|
|
|
|
} = await createFaultInjectedMerchantTestkudosEnvironment(t);
|
2020-09-08 22:55:48 +02:00
|
|
|
// Set up test environment
|
|
|
|
|
|
|
|
await withdrawViaBank(t, {
|
|
|
|
wallet,
|
|
|
|
exchange: faultyExchange,
|
|
|
|
amount: "TESTKUDOS:20",
|
|
|
|
bank,
|
|
|
|
});
|
|
|
|
|
2021-01-13 01:19:23 +01:00
|
|
|
const orderResp = await MerchantPrivateApi.createOrder(
|
|
|
|
faultyMerchant,
|
|
|
|
"default",
|
|
|
|
{
|
|
|
|
order: {
|
|
|
|
summary: "Buy me!",
|
|
|
|
amount: "TESTKUDOS:15",
|
|
|
|
fulfillment_url: "taler://fulfillment-success/thx",
|
|
|
|
},
|
2020-09-08 22:55:48 +02:00
|
|
|
},
|
2021-01-13 01:19:23 +01:00
|
|
|
);
|
2020-09-08 22:55:48 +02:00
|
|
|
|
2021-01-13 01:19:23 +01:00
|
|
|
let orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus(
|
|
|
|
faultyMerchant,
|
|
|
|
{
|
|
|
|
orderId: orderResp.order_id,
|
|
|
|
},
|
|
|
|
);
|
2020-09-08 22:55:48 +02:00
|
|
|
|
|
|
|
t.assertTrue(orderStatus.order_status === "unpaid");
|
|
|
|
|
|
|
|
// Make wallet pay for the order
|
|
|
|
|
2021-06-17 13:34:59 +02:00
|
|
|
const preparePayResult = await wallet.client.call(
|
|
|
|
WalletApiOperation.PreparePayForUri,
|
|
|
|
{
|
|
|
|
talerPayUri: orderStatus.taler_pay_uri,
|
|
|
|
},
|
|
|
|
);
|
2020-09-08 22:55:48 +02:00
|
|
|
|
|
|
|
t.assertTrue(
|
|
|
|
preparePayResult.status === PreparePayResultType.PaymentPossible,
|
|
|
|
);
|
|
|
|
|
|
|
|
// We let only the first deposit through!
|
|
|
|
let firstDepositUrl: string | undefined;
|
|
|
|
|
|
|
|
faultyExchange.faultProxy.addFault({
|
2021-02-08 13:02:12 +01:00
|
|
|
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
2020-09-08 22:55:48 +02:00
|
|
|
const url = new URL(ctx.requestUrl);
|
|
|
|
if (url.pathname.endsWith("/deposit")) {
|
|
|
|
if (!firstDepositUrl) {
|
|
|
|
firstDepositUrl = url.href;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (url.href != firstDepositUrl) {
|
|
|
|
url.pathname = "/doesntexist";
|
|
|
|
ctx.requestUrl = url.href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-02-08 13:02:12 +01:00
|
|
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
2020-09-08 22:55:48 +02:00
|
|
|
const url = new URL(ctx.request.requestUrl);
|
|
|
|
if (url.pathname.endsWith("/deposit") && url.href != firstDepositUrl) {
|
|
|
|
ctx.responseBody = Buffer.from("{}");
|
|
|
|
ctx.statusCode = 500;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-13 01:19:23 +01:00
|
|
|
faultyMerchant.faultProxy.addFault({
|
2021-02-08 13:02:12 +01:00
|
|
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
2021-01-13 01:19:23 +01:00
|
|
|
const url = new URL(ctx.request.requestUrl);
|
|
|
|
if (url.pathname.endsWith("/pay") && url.href != firstDepositUrl) {
|
|
|
|
ctx.responseBody = Buffer.from("{}");
|
|
|
|
ctx.statusCode = 400;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-09-08 22:55:48 +02:00
|
|
|
await t.assertThrowsOperationErrorAsync(async () => {
|
2021-06-17 13:34:59 +02:00
|
|
|
await wallet.client.call(WalletApiOperation.ConfirmPay, {
|
2020-09-08 22:55:48 +02:00
|
|
|
proposalId: preparePayResult.proposalId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-17 13:34:59 +02:00
|
|
|
let txr = await wallet.client.call(WalletApiOperation.GetTransactions, {});
|
2020-09-08 22:55:48 +02:00
|
|
|
console.log(JSON.stringify(txr, undefined, 2));
|
|
|
|
|
|
|
|
t.assertDeepEqual(txr.transactions[1].type, "payment");
|
|
|
|
t.assertDeepEqual(txr.transactions[1].pending, true);
|
|
|
|
t.assertDeepEqual(
|
|
|
|
txr.transactions[1].error?.code,
|
2021-01-13 01:19:23 +01:00
|
|
|
TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE,
|
2020-09-08 22:55:48 +02:00
|
|
|
);
|
|
|
|
|
2021-06-17 13:34:59 +02:00
|
|
|
await wallet.client.call(WalletApiOperation.AbortFailedPayWithRefund, {
|
2020-09-08 22:55:48 +02:00
|
|
|
proposalId: preparePayResult.proposalId,
|
|
|
|
});
|
|
|
|
|
|
|
|
await wallet.runUntilDone();
|
|
|
|
|
2021-06-17 13:34:59 +02:00
|
|
|
txr = await wallet.client.call(WalletApiOperation.GetTransactions, {});
|
2020-09-08 22:55:48 +02:00
|
|
|
console.log(JSON.stringify(txr, undefined, 2));
|
|
|
|
|
|
|
|
const txTypes = txr.transactions.map((x) => x.type);
|
|
|
|
|
|
|
|
t.assertDeepEqual(txTypes, ["withdrawal", "payment", "refund"]);
|
2021-01-12 20:04:16 +01:00
|
|
|
}
|
2021-05-21 11:47:20 +02:00
|
|
|
|
2021-06-09 15:26:18 +02:00
|
|
|
runPayAbortTest.suites = ["wallet"];
|