wallet-core: test insufficient balance response, also use it for deposit groups

This commit is contained in:
Florian Dold 2023-01-06 10:32:39 +01:00
parent db1b824818
commit 80639429a2
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
6 changed files with 627 additions and 8 deletions

View File

@ -17,7 +17,7 @@
/**
* Imports.
*/
import { Duration, PreparePayResultType } from "@gnu-taler/taler-util";
import { Amounts, Duration, PreparePayResultType } from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { CoinConfig, defaultCoinConfig } from "../harness/denomStructures.js";
import {
@ -133,11 +133,23 @@ export async function runWalletBalanceTest(t: GlobalTestState) {
},
);
t.assertTrue(
preparePayResult.status === PreparePayResultType.InsufficientBalance,
);
t.assertDeepEqual(
preparePayResult.status,
PreparePayResultType.InsufficientBalance,
);
t.assertTrue(
Amounts.isNonZero(preparePayResult.balanceDetails.balanceMerchantAcceptable),
);
t.assertTrue(
Amounts.isZero(preparePayResult.balanceDetails.balanceMerchantDepositable),
);
await wallet.runUntilDone();
}

View File

@ -303,7 +303,8 @@ export class Amounts {
/**
* Check if an amount is non-zero.
*/
static isNonZero(a: AmountJson): boolean {
static isNonZero(a: AmountLike): boolean {
a = Amounts.jsonifyAmount(a);
return a.value > 0 || a.fraction > 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -2141,4 +2141,6 @@ const codecForPayMerchantInsufficientBalanceDetails =
.property("balanceMerchantAcceptable", codecForAmountString())
.property("balanceMerchantDepositable", codecForAmountString())
.property("feeGapEstimate", codecForAmountString())
.build("PayMerchantInsufficientBalanceDetails");
.build("PayMerchantInsufficientBalanceDetails");

View File

@ -24,6 +24,7 @@
* Imports.
*/
import {
PayMerchantInsufficientBalanceDetails,
TalerErrorCode,
TalerErrorDetail,
TransactionType,
@ -83,6 +84,9 @@ export interface DetailsMap {
[TalerErrorCode.WALLET_WITHDRAWAL_KYC_REQUIRED]: {
// FIXME!
};
[TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE]: {
insufficientBalanceDetails: PayMerchantInsufficientBalanceDetails;
};
}
type ErrBody<Y> = Y extends keyof DetailsMap ? DetailsMap[Y] : never;

View File

@ -45,12 +45,14 @@ import {
TrackDepositGroupResponse,
TransactionType,
URL,
TalerErrorCode,
} from "@gnu-taler/taler-util";
import {
DenominationRecord,
DepositGroupRecord,
OperationStatus,
} from "../db.js";
import { TalerError } from "../errors.js";
import { InternalWalletState } from "../internal-wallet-state.js";
import { readSuccessResponseJsonOrThrow } from "../util/http.js";
import { OperationAttemptResult } from "../util/retries.js";
@ -269,7 +271,12 @@ export async function getFeeForDeposit(
});
if (payCoinSel.type !== "success") {
throw Error("insufficient funds");
throw TalerError.fromDetail(
TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE,
{
insufficientBalanceDetails: payCoinSel.insufficientBalanceDetails,
},
);
}
return await getTotalFeesForDepositAmount(
@ -356,7 +363,12 @@ export async function prepareDepositGroup(
});
if (payCoinSel.type !== "success") {
throw Error("insufficient funds");
throw TalerError.fromDetail(
TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE,
{
insufficientBalanceDetails: payCoinSel.insufficientBalanceDetails,
},
);
}
const totalDepositCost = await getTotalPaymentCost(ws, payCoinSel.coinSel);
@ -453,7 +465,12 @@ export async function createDepositGroup(
});
if (payCoinSel.type !== "success") {
throw Error("insufficient funds");
throw TalerError.fromDetail(
TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE,
{
insufficientBalanceDetails: payCoinSel.insufficientBalanceDetails,
},
);
}
const totalDepositCost = await getTotalPaymentCost(ws, payCoinSel.coinSel);