From a60a1d867cfe6a12f1e6fadfa037f022e9385107 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 5 Sep 2023 10:46:06 +0200 Subject: harness: remove deprecated testing APIs --- packages/taler-harness/src/harness/harness.ts | 103 ++++++-- packages/taler-harness/src/harness/helpers.ts | 326 +++----------------------- 2 files changed, 115 insertions(+), 314 deletions(-) (limited to 'packages/taler-harness/src/harness') diff --git a/packages/taler-harness/src/harness/harness.ts b/packages/taler-harness/src/harness/harness.ts index 337f3ca44..b5197afd7 100644 --- a/packages/taler-harness/src/harness/harness.ts +++ b/packages/taler-harness/src/harness/harness.ts @@ -56,6 +56,7 @@ import { TippingReserveStatus, WalletNotification, codecForAny, + AccountAddDetails, } from "@gnu-taler/taler-util"; import { createPlatformHttpLib, @@ -760,19 +761,19 @@ export class ExchangeService implements ExchangeServiceInterface { return new ExchangeService(gc, ec, cfgFilename, keyPair); } - private currentTimetravel: Duration | undefined; + private currentTimetravelOffsetMs: number | undefined; - setTimetravel(t: Duration | undefined): void { + setTimetravel(t: number | undefined): void { if (this.isRunning()) { throw Error("can't set time travel while the exchange is running"); } - this.currentTimetravel = t; + this.currentTimetravelOffsetMs = t; } private get timetravelArg(): string | undefined { - if (this.currentTimetravel && this.currentTimetravel.d_ms !== "forever") { + if (this.currentTimetravelOffsetMs != null) { // Convert to microseconds - return `--timetravel=+${this.currentTimetravel.d_ms * 1000}`; + return `--timetravel=+${this.currentTimetravelOffsetMs * 1000}`; } return undefined; } @@ -1360,12 +1361,26 @@ export const harnessHttpLib = createPlatformHttpLib({ }); export class MerchantApiClient { - constructor( - private baseUrl: string, - public readonly auth: MerchantAuthConfiguration, - ) {} + /** + * Base URL for the particular instance that this merchant API client + * is for. + */ + private baseUrl: string; - httpClient = createPlatformHttpLib({ allowHttp: true, enableThrottling: false }); + readonly auth: MerchantAuthConfiguration; + + constructor(baseUrl: string, auth?: MerchantAuthConfiguration) { + this.baseUrl = baseUrl; + + this.auth = auth ?? { + method: "external", + }; + } + + httpClient = createPlatformHttpLib({ + allowHttp: true, + enableThrottling: false, + }); async changeAuth(auth: MerchantAuthConfiguration): Promise { const url = new URL("private/auth", this.baseUrl); @@ -1463,7 +1478,38 @@ export class MerchantApiClient { } } - makeAuthHeader(): Record { + async createOrder( + req: MerchantPostOrderRequest, + ): Promise { + let url = new URL("private/orders", this.baseUrl); + const resp = await harnessHttpLib.fetch(url.href, { + method: "POST", + body: req, + headers: this.makeAuthHeader(), + }); + return readSuccessResponseJsonOrThrow( + resp, + codecForMerchantPostOrderResponse(), + ); + } + + async queryPrivateOrderStatus( + query: PrivateOrderStatusQuery, + ): Promise { + const reqUrl = new URL(`private/orders/${query.orderId}`, this.baseUrl); + if (query.sessionId) { + reqUrl.searchParams.set("session_id", query.sessionId); + } + const resp = await harnessHttpLib.fetch(reqUrl.href, { + headers: this.makeAuthHeader(), + }); + return readSuccessResponseJsonOrThrow( + resp, + codecForMerchantOrderPrivateStatusResponse(), + ); + } + + private makeAuthHeader(): Record { switch (this.auth.method) { case "external": return {}; @@ -1633,23 +1679,23 @@ export class MerchantService implements MerchantServiceInterface { private configFilename: string, ) {} - private currentTimetravel: Duration | undefined; + private currentTimetravelOffsetMs: number | undefined; private isRunning(): boolean { return !!this.proc; } - setTimetravel(t: Duration | undefined): void { + setTimetravel(t: number | undefined): void { if (this.isRunning()) { throw Error("can't set time travel while the exchange is running"); } - this.currentTimetravel = t; + this.currentTimetravelOffsetMs = t; } private get timetravelArg(): string | undefined { - if (this.currentTimetravel && this.currentTimetravel.d_ms !== "forever") { + if (this.currentTimetravelOffsetMs != null) { // Convert to microseconds - return `--timetravel=+${this.currentTimetravel.d_ms * 1000}`; + return `--timetravel=+${this.currentTimetravelOffsetMs * 1000}`; } return undefined; } @@ -1756,7 +1802,7 @@ export class MerchantService implements MerchantServiceInterface { } async addDefaultInstance(): Promise { - return await this.addInstance({ + return await this.addInstanceWithWireAccount({ id: "default", name: "Default Instance", paytoUris: [getPayto("merchant-default")], @@ -1766,7 +1812,10 @@ export class MerchantService implements MerchantServiceInterface { }); } - async addInstance( + /** + * Add an instance together with a wire account. + */ + async addInstanceWithWireAccount( instanceConfig: PartialMerchantInstanceConfig, ): Promise { if (!this.proc) { @@ -1798,12 +1847,20 @@ export class MerchantService implements MerchantServiceInterface { instanceConfig.defaultPayDelay ?? Duration.toTalerProtocolDuration(Duration.getForever()), }; - const httpLib = createPlatformHttpLib({ - allowHttp: true, - enableThrottling: false, - }); - const resp = await httpLib.fetch(url, { method: "POST", body }); + const resp = await harnessHttpLib.fetch(url, { method: "POST", body }); await expectSuccessResponseOrThrow(resp); + + const accountCreateUrl = `http://localhost:${this.merchantConfig.httpPort}/instances/${instanceConfig.id}/private/accounts`; + for (const paytoUri of instanceConfig.paytoUris) { + const accountReq: AccountAddDetails = { + payto_uri: paytoUri, + }; + const acctResp = await harnessHttpLib.fetch(accountCreateUrl, { + method: "POST", + body: accountReq, + }); + await expectSuccessResponseOrThrow(acctResp); + } } makeInstanceBaseUrl(instanceName?: string): string { diff --git a/packages/taler-harness/src/harness/helpers.ts b/packages/taler-harness/src/harness/helpers.ts index d1d0ea104..39d411a46 100644 --- a/packages/taler-harness/src/harness/helpers.ts +++ b/packages/taler-harness/src/harness/helpers.ts @@ -53,6 +53,7 @@ import { FakebankService, getPayto, GlobalTestState, + MerchantApiClient, MerchantPrivateApi, MerchantService, MerchantServiceInterface, @@ -107,114 +108,6 @@ export interface EnvOptions { additionalBankConfig?(b: BankService): void; } -/** - * Run a test case with a simple TESTKUDOS Taler environment, consisting - * of one exchange, one bank and one merchant. - * - * @deprecated use {@link createSimpleTestkudosEnvironmentV2} instead - */ -export async function createSimpleTestkudosEnvironment( - t: GlobalTestState, - coinConfig: CoinConfig[] = defaultCoinConfig.map((x) => x("TESTKUDOS")), - opts: EnvOptions = {}, -): Promise { - 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 exchangeBankAccount = await bank.createExchangeAccount( - "myexchange", - "x", - ); - await exchange.addBankAccount("1", exchangeBankAccount); - - bank.setSuggestedExchange(exchange, exchangeBankAccount.accountPaytoUri); - - await bank.start(); - - await bank.pingUntilAvailable(); - - 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.start(); - await exchange.pingUntilAvailable(); - - merchant.addExchange(exchange); - - await merchant.start(); - await merchant.pingUntilAvailable(); - - await merchant.addInstance({ - id: "default", - name: "Default Instance", - paytoUris: [getPayto("merchant-default")], - defaultWireTransferDelay: Duration.toTalerProtocolDuration( - Duration.fromSpec({ minutes: 1 }), - ), - }); - - await merchant.addInstance({ - id: "minst1", - name: "minst1", - paytoUris: [getPayto("minst1")], - defaultWireTransferDelay: Duration.toTalerProtocolDuration( - Duration.fromSpec({ minutes: 1 }), - ), - }); - - console.log("setup done!"); - - const wallet = new WalletCli(t); - - return { - commonDb: db, - exchange, - merchant, - wallet, - bank, - exchangeBankAccount, - }; -} - export function getSharedTestDir(): string { return `/tmp/taler-harness@${process.env.USER}`; } @@ -344,7 +237,7 @@ export async function useSharedTestkudosEnvironment(t: GlobalTestState) { await merchant.pingUntilAvailable(); if (!prevSetupDone) { - await merchant.addInstance({ + await merchant.addInstanceWithWireAccount({ id: "default", name: "Default Instance", paytoUris: [getPayto("merchant-default")], @@ -353,7 +246,7 @@ export async function useSharedTestkudosEnvironment(t: GlobalTestState) { ), }); - await merchant.addInstance({ + await merchant.addInstanceWithWireAccount({ id: "minst1", name: "minst1", paytoUris: [getPayto("minst1")], @@ -476,7 +369,7 @@ export async function createSimpleTestkudosEnvironmentV2( await merchant.start(); await merchant.pingUntilAvailable(); - await merchant.addInstance({ + await merchant.addInstanceWithWireAccount({ id: "default", name: "Default Instance", paytoUris: [getPayto("merchant-default")], @@ -485,7 +378,7 @@ export async function createSimpleTestkudosEnvironmentV2( ), }); - await merchant.addInstance({ + await merchant.addInstanceWithWireAccount({ id: "minst1", name: "minst1", paytoUris: [getPayto("minst1")], @@ -554,7 +447,7 @@ export interface FaultyMerchantTestEnvironment { exchangeBankAccount: HarnessExchangeBankAccount; merchant: MerchantService; faultyMerchant: FaultInjectedMerchantService; - wallet: WalletCli; + walletClient: WalletClient; } /** @@ -620,13 +513,13 @@ export async function createFaultInjectedMerchantTestkudosEnvironment( await merchant.start(); await merchant.pingUntilAvailable(); - await merchant.addInstance({ + await merchant.addInstanceWithWireAccount({ id: "default", name: "Default Instance", paytoUris: [getPayto("merchant-default")], }); - await merchant.addInstance({ + await merchant.addInstanceWithWireAccount({ id: "minst1", name: "minst1", paytoUris: [getPayto("minst1")], @@ -634,13 +527,15 @@ export async function createFaultInjectedMerchantTestkudosEnvironment( console.log("setup done!"); - const wallet = new WalletCli(t); + const { walletClient } = await createWalletDaemonWithClient(t, { + name: "default", + }); return { commonDb: db, exchange, merchant, - wallet, + walletClient, bank, exchangeBankAccount, faultyMerchant, @@ -648,51 +543,6 @@ export async function createFaultInjectedMerchantTestkudosEnvironment( }; } -/** - * Start withdrawing into the wallet. - * - * Only starts the operation, does not wait for it to finish. - */ -export async function startWithdrawViaBank( - t: GlobalTestState, - p: { - wallet: WalletCli; - bank: BankService; - exchange: ExchangeServiceInterface; - amount: AmountString; - restrictAge?: number; - }, -): Promise { - const { wallet, bank, exchange, amount } = p; - - const user = await BankApi.createRandomBankUser(bank); - const wop = await BankAccessApi.createWithdrawalOperation(bank, user, amount); - - // Hand it to the wallet - - await wallet.client.call(WalletApiOperation.GetWithdrawalDetailsForUri, { - talerWithdrawUri: wop.taler_withdraw_uri, - restrictAge: p.restrictAge, - }); - - await wallet.runPending(); - - // Withdraw (AKA select) - - await wallet.client.call(WalletApiOperation.AcceptBankIntegratedWithdrawal, { - exchangeBaseUrl: exchange.baseUrl, - talerWithdrawUri: wop.taler_withdraw_uri, - restrictAge: p.restrictAge, - }); - - // Confirm it - - await BankApi.confirmWithdrawalOperation(bank, user, wop); - - // We do *not* call runPending / runUntilDone on the wallet here. - // Some tests rely on the final withdraw failing. -} - export interface WithdrawViaBankResult { withdrawalFinishedCond: Promise; } @@ -751,131 +601,35 @@ export async function withdrawViaBankV2( }; } -/** - * Withdraw balance. - * - * @deprecated use {@link withdrawViaBankV2 instead} - */ -export async function withdrawViaBank( - t: GlobalTestState, - p: { - wallet: WalletCli; - bank: BankService; - exchange: ExchangeServiceInterface; - amount: AmountString; - restrictAge?: number; - }, -): Promise { - const { wallet } = p; - - await startWithdrawViaBank(t, p); - - await wallet.runUntilDone(); - - // Check balance - - await wallet.client.call(WalletApiOperation.GetBalances, {}); -} - -export async function applyTimeTravel( - timetravelDuration: Duration, +export async function applyTimeTravelV2( + timetravelOffsetMs: number, s: { exchange?: ExchangeService; merchant?: MerchantService; - wallet?: WalletCli; + walletClient?: WalletClient; }, ): Promise { if (s.exchange) { await s.exchange.stop(); - s.exchange.setTimetravel(timetravelDuration); + s.exchange.setTimetravel(timetravelOffsetMs); await s.exchange.start(); await s.exchange.pingUntilAvailable(); } if (s.merchant) { await s.merchant.stop(); - s.merchant.setTimetravel(timetravelDuration); + s.merchant.setTimetravel(timetravelOffsetMs); await s.merchant.start(); await s.merchant.pingUntilAvailable(); } - if (s.wallet) { - s.wallet.setTimetravel(timetravelDuration); + if (s.walletClient) { + s.walletClient.call(WalletApiOperation.TestingSetTimetravel, { + offsetMs: timetravelOffsetMs, + }); } } -/** - * Make a simple payment and check that it succeeded. - * - * @deprecated - */ -export async function makeTestPayment( - t: GlobalTestState, - args: { - merchant: MerchantServiceInterface; - wallet: WalletCli; - order: Partial; - instance?: string; - }, - auth: WithAuthorization = {}, -): Promise { - // Set up order. - - const { wallet, merchant } = args; - const instance = args.instance ?? "default"; - - const orderResp = await MerchantPrivateApi.createOrder( - merchant, - instance, - { - order: args.order, - }, - auth, - ); - - let orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus( - merchant, - { - orderId: orderResp.order_id, - }, - auth, - ); - - t.assertTrue(orderStatus.order_status === "unpaid"); - - // Make wallet pay for the order - - const preparePayResult = await wallet.client.call( - WalletApiOperation.PreparePayForUri, - { - talerPayUri: orderStatus.taler_pay_uri, - }, - ); - - t.assertTrue( - preparePayResult.status === PreparePayResultType.PaymentPossible, - ); - - const r2 = await wallet.client.call(WalletApiOperation.ConfirmPay, { - proposalId: preparePayResult.proposalId, - }); - - t.assertTrue(r2.type === ConfirmPayResultType.Done); - - // Check if payment was successful. - - orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus( - merchant, - { - orderId: orderResp.order_id, - instance, - }, - auth, - ); - - t.assertTrue(orderStatus.order_status === "paid"); -} - /** * Make a simple payment and check that it succeeded. */ @@ -891,25 +645,19 @@ export async function makeTestPaymentV2( ): Promise { // Set up order. - const { walletClient, merchant } = args; - const instance = args.instance ?? "default"; + const { walletClient, merchant, instance } = args; - const orderResp = await MerchantPrivateApi.createOrder( - merchant, - instance, - { - order: args.order, - }, - auth, + const merchantClient = new MerchantApiClient( + merchant.makeInstanceBaseUrl(instance), ); - let orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus( - merchant, - { - orderId: orderResp.order_id, - }, - auth, - ); + const orderResp = await merchantClient.createOrder({ + order: args.order, + }); + + let orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + }); t.assertTrue(orderStatus.order_status === "unpaid"); @@ -934,14 +682,10 @@ export async function makeTestPaymentV2( // Check if payment was successful. - orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus( - merchant, - { - orderId: orderResp.order_id, - instance, - }, - auth, - ); + orderStatus = await merchantClient.queryPrivateOrderStatus({ + orderId: orderResp.order_id, + instance, + }); t.assertTrue(orderStatus.order_status === "paid"); } -- cgit v1.2.3