adapt to corebank API change, minor refactoring

This commit is contained in:
Florian Dold 2023-09-24 13:01:42 +02:00
parent 6b63ecc49e
commit bdd906c887
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
9 changed files with 50 additions and 132 deletions

View File

@ -592,7 +592,9 @@ export async function withdrawViaBankV2(
// Confirm it // Confirm it
await bankClient.confirmWithdrawalOperation(user.username, wop); await bankClient.confirmWithdrawalOperation(user.username, {
withdrawalOperationId: wop.withdrawal_id,
});
return { return {
withdrawalFinishedCond, withdrawalFinishedCond,

View File

@ -331,7 +331,9 @@ export async function runKycTest(t: GlobalTestState) {
// Confirm it // Confirm it
await bankClient.confirmWithdrawalOperation(user.username, wop); await bankClient.confirmWithdrawalOperation(user.username, {
withdrawalOperationId: wop.withdrawal_id,
});
const kycNotificationCond = walletClient.waitForNotificationCond((x) => { const kycNotificationCond = walletClient.waitForNotificationCond((x) => {
if ( if (

View File

@ -153,7 +153,9 @@ export async function runPaymentFaultTest(t: GlobalTestState) {
// Confirm it // Confirm it
await bankClient.confirmWithdrawalOperation(user.username, wop); await bankClient.confirmWithdrawalOperation(user.username, {
withdrawalOperationId: wop.withdrawal_id,
});
await wallet.runUntilDone(); await wallet.runUntilDone();

View File

@ -161,7 +161,9 @@ export async function runWalletNotificationsTest(t: GlobalTestState) {
// Confirm it // Confirm it
await bankAccessApiClient.confirmWithdrawalOperation(user.username, wop); await bankAccessApiClient.confirmWithdrawalOperation(user.username, {
withdrawalOperationId: wop.withdrawal_id,
});
await withdrawalFinishedReceivedPromise; await withdrawalFinishedReceivedPromise;
} }

View File

@ -129,7 +129,9 @@ export async function runWithdrawalBankIntegratedTest(t: GlobalTestState) {
// Confirm it // Confirm it
await bankAccessApiClient.confirmWithdrawalOperation(user.username, wop); await bankAccessApiClient.confirmWithdrawalOperation(user.username, {
withdrawalOperationId: wop.withdrawal_id,
});
await withdrawalBankConfirmedCond; await withdrawalBankConfirmedCond;

View File

@ -152,7 +152,9 @@ export async function runWithdrawalFeesTest(t: GlobalTestState) {
// Confirm it // Confirm it
await bankAccessApiClient.confirmWithdrawalOperation(user.username, wop); await bankAccessApiClient.confirmWithdrawalOperation(user.username, {
withdrawalOperationId: wop.withdrawal_id,
});
await wallet.runUntilDone(); await wallet.runUntilDone();
// Check balance // Check balance

View File

@ -223,6 +223,10 @@ export interface AccountData {
cashout_payto_uri?: string; cashout_payto_uri?: string;
} }
export interface ConfirmWithdrawalArgs {
withdrawalOperationId: string;
}
/** /**
* Client for the Taler corebank API. * Client for the Taler corebank API.
*/ */
@ -356,10 +360,10 @@ export class TalerCorebankApiClient {
async confirmWithdrawalOperation( async confirmWithdrawalOperation(
username: string, username: string,
wopi: WithdrawalOperationInfo, wopi: ConfirmWithdrawalArgs,
): Promise<void> { ): Promise<void> {
const url = new URL( const url = new URL(
`accounts/${username}/withdrawals/${wopi.withdrawal_id}/confirm`, `withdrawals/${wopi.withdrawalOperationId}/confirm`,
this.baseUrl, this.baseUrl,
); );
logger.info(`confirming withdrawal operation via ${url.href}`); logger.info(`confirming withdrawal operation via ${url.href}`);

View File

@ -142,7 +142,9 @@ export async function topupReserveWithDemobank(
httpResp, httpResp,
codecForBankWithdrawalOperationPostResponse(), codecForBankWithdrawalOperationPostResponse(),
); );
await bankClient.confirmWithdrawalOperation(bankUser.username, wopi); await bankClient.confirmWithdrawalOperation(bankUser.username, {
withdrawalOperationId: wopi.withdrawal_id,
});
} }
export async function withdrawCoin(args: { export async function withdrawCoin(args: {

View File

@ -27,6 +27,7 @@ import {
NotificationType, NotificationType,
RegisterAccountRequest, RegisterAccountRequest,
stringToBytes, stringToBytes,
TalerCorebankApiClient,
TestPayResult, TestPayResult,
TransactionMajorState, TransactionMajorState,
TransactionMinorState, TransactionMinorState,
@ -74,16 +75,6 @@ import { getTransactionById, getTransactions } from "./transactions.js";
const logger = new Logger("operations/testing.ts"); const logger = new Logger("operations/testing.ts");
interface BankUser {
username: string;
password: string;
}
interface BankWithdrawalResponse {
taler_withdraw_uri: string;
withdrawal_id: string;
}
interface MerchantBackendInfo { interface MerchantBackendInfo {
baseUrl: string; baseUrl: string;
authToken?: string; authToken?: string;
@ -103,16 +94,6 @@ function makeId(length: number): string {
return result; return result;
} }
/**
* Helper function to generate the "Authorization" HTTP header.
* FIXME: redundant, put in taler-util
*/
function makeBasicAuthHeader(username: string, password: string): string {
const auth = `${username}:${password}`;
const authEncoded: string = base64FromArrayBuffer(stringToBytes(auth));
return `Basic ${authEncoded}`;
}
export async function withdrawTestBalance( export async function withdrawTestBalance(
ws: InternalWalletState, ws: InternalWalletState,
req: WithdrawTestBalanceRequest, req: WithdrawTestBalanceRequest,
@ -122,15 +103,18 @@ export async function withdrawTestBalance(
const bankAccessApiBaseUrl = req.bankAccessApiBaseUrl; const bankAccessApiBaseUrl = req.bankAccessApiBaseUrl;
logger.trace( logger.trace(
`Registered bank user, bank access base url ${bankAccessApiBaseUrl}`, `Registering bank user, bank access base url ${bankAccessApiBaseUrl}`,
); );
const bankUser = await registerRandomBankUser(ws.http, bankAccessApiBaseUrl);
const corebankClient = new TalerCorebankApiClient(bankAccessApiBaseUrl);
const bankUser = await corebankClient.createRandomBankUser();
logger.trace(`Registered bank user ${JSON.stringify(bankUser)}`); logger.trace(`Registered bank user ${JSON.stringify(bankUser)}`);
const wresp = await createDemoBankWithdrawalUri( corebankClient.setAuth(bankUser);
ws.http,
bankAccessApiBaseUrl, const wresp = await corebankClient.createWithdrawalOperation(
bankUser, bankUser.username,
amount, amount,
); );
@ -140,14 +124,14 @@ export async function withdrawTestBalance(
forcedDenomSel: req.forcedDenomSel, forcedDenomSel: req.forcedDenomSel,
}); });
await confirmBankWithdrawalUri( await corebankClient.confirmWithdrawalOperation(bankUser.username, {
ws.http, withdrawalOperationId: wresp.withdrawal_id,
bankAccessApiBaseUrl, });
bankUser,
wresp.withdrawal_id,
);
} }
/**
* FIXME: User MerchantApiClient instead.
*/
function getMerchantAuthHeader(m: MerchantBackendInfo): Record<string, string> { function getMerchantAuthHeader(m: MerchantBackendInfo): Record<string, string> {
if (m.authToken) { if (m.authToken) {
return { return {
@ -158,88 +142,8 @@ function getMerchantAuthHeader(m: MerchantBackendInfo): Record<string, string> {
} }
/** /**
* Use the testing API of a demobank to create a taler://withdraw URI * FIXME: User MerchantApiClient instead.
* that the wallet can then use to make a withdrawal.
*/ */
export async function createDemoBankWithdrawalUri(
http: HttpRequestLibrary,
bankAccessApiBaseUrl: string,
bankUser: BankUser,
amount: AmountString,
): Promise<BankWithdrawalResponse> {
const reqUrl = new URL(
`accounts/${bankUser.username}/withdrawals`,
bankAccessApiBaseUrl,
).href;
const resp = await http.postJson(
reqUrl,
{
amount,
},
{
headers: {
Authorization: makeBasicAuthHeader(
bankUser.username,
bankUser.password,
),
},
},
);
const respJson = await readSuccessResponseJsonOrThrow(resp, codecForAny());
return respJson;
}
async function confirmBankWithdrawalUri(
http: HttpRequestLibrary,
bankAccessApiBaseUrl: string,
bankUser: BankUser,
withdrawalId: string,
): Promise<void> {
const reqUrl = new URL(
`accounts/${bankUser.username}/withdrawals/${withdrawalId}/confirm`,
bankAccessApiBaseUrl,
).href;
const resp = await http.postJson(
reqUrl,
{},
{
headers: {
Authorization: makeBasicAuthHeader(
bankUser.username,
bankUser.password,
),
},
},
);
await readSuccessResponseJsonOrThrow(resp, codecForAny());
return;
}
async function registerRandomBankUser(
http: HttpRequestLibrary,
corebankApiBaseUrl: string,
): Promise<BankUser> {
const reqUrl = new URL("accounts", corebankApiBaseUrl).href;
const randId = makeId(8);
const username = `testuser-${randId.toLowerCase()}`;
const password = `testpw-${randId}`;
const bankUser: BankUser = {
username,
password,
};
const userReq: RegisterAccountRequest = {
username,
password,
name: username,
};
const resp = await http.fetch(reqUrl, { method: "POST", body: userReq });
await checkSuccessResponseOrThrow(resp);
return bankUser;
}
async function refund( async function refund(
http: HttpRequestLibrary, http: HttpRequestLibrary,
merchantBackend: MerchantBackendInfo, merchantBackend: MerchantBackendInfo,
@ -267,6 +171,9 @@ async function refund(
return refundUri; return refundUri;
} }
/**
* FIXME: User MerchantApiClient instead.
*/
async function createOrder( async function createOrder(
http: HttpRequestLibrary, http: HttpRequestLibrary,
merchantBackend: MerchantBackendInfo, merchantBackend: MerchantBackendInfo,
@ -296,6 +203,9 @@ async function createOrder(
return { orderId }; return { orderId };
} }
/**
* FIXME: User MerchantApiClient instead.
*/
async function checkPayment( async function checkPayment(
http: HttpRequestLibrary, http: HttpRequestLibrary,
merchantBackend: MerchantBackendInfo, merchantBackend: MerchantBackendInfo,
@ -309,16 +219,6 @@ async function checkPayment(
return readSuccessResponseJsonOrThrow(resp, codecForCheckPaymentResponse()); return readSuccessResponseJsonOrThrow(resp, codecForCheckPaymentResponse());
} }
interface BankUser {
username: string;
password: string;
}
interface BankWithdrawalResponse {
taler_withdraw_uri: string;
withdrawal_id: string;
}
async function makePayment( async function makePayment(
ws: InternalWalletState, ws: InternalWalletState,
merchant: MerchantBackendInfo, merchant: MerchantBackendInfo,