diff options
author | Florian Dold <florian@dold.me> | 2023-08-29 09:02:16 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-08-29 09:03:19 +0200 |
commit | b13bd85215ad64e7a2764ac7e7fee5945ffa1c07 (patch) | |
tree | 70643c7e76baeb9f61857add603dc381b6f03766 /packages/taler-harness/src/harness/libeufin-apis.ts | |
parent | 8941f29cb457f86235b73f125e77a88cb762f353 (diff) | |
download | wallet-core-b13bd85215ad64e7a2764ac7e7fee5945ffa1c07.tar.xz |
taler-harness: remove axios usage, renovate some tests
Diffstat (limited to 'packages/taler-harness/src/harness/libeufin-apis.ts')
-rw-r--r-- | packages/taler-harness/src/harness/libeufin-apis.ts | 519 |
1 files changed, 216 insertions, 303 deletions
diff --git a/packages/taler-harness/src/harness/libeufin-apis.ts b/packages/taler-harness/src/harness/libeufin-apis.ts index cb9acdaa4..3c57eee07 100644 --- a/packages/taler-harness/src/harness/libeufin-apis.ts +++ b/packages/taler-harness/src/harness/libeufin-apis.ts @@ -6,8 +6,21 @@ */ import { URL } from "@gnu-taler/taler-util"; -import axiosImp from "axios"; -const axios = axiosImp.default; +import { + createPlatformHttpLib, + makeBasicAuthHeader, +} from "@gnu-taler/taler-util/http"; +import { + LibeufinNexusTransactions, + LibeufinSandboxAdminBankAccountBalance, + NexusBankConnections, + NexusFacadeListResponse, + NexusGetPermissionsResponse, + NexusNewTransactionsInfo, + NexusTask, + NexusTaskCollection, + NexusUserResponse, +} from "./libeufin.js"; export interface LibeufinSandboxServiceInterface { baseUrl: string; @@ -163,30 +176,13 @@ export interface LibeufinSandboxAddIncomingRequest { direction: string; } +const libeufinHttpLib = createPlatformHttpLib(); + /** * APIs spread across Legacy and Access, it is therefore * the "base URL" relative to which API every call addresses. */ export namespace LibeufinSandboxApi { - // Need Access API base URL. - export async function demobankAccountInfo( - username: string, - password: string, - libeufinSandboxService: LibeufinSandboxServiceInterface, - accountLabel: string, - ) { - let url = new URL( - `accounts/${accountLabel}`, - libeufinSandboxService.baseUrl, - ); - return await axios.get(url.href, { - auth: { - username: username, - password: password, - }, - }); - } - // Creates one bank account via the Access API. // Need the /demobanks/$id/access-api as the base URL export async function createDemobankAccount( @@ -194,12 +190,15 @@ export namespace LibeufinSandboxApi { password: string, libeufinSandboxService: LibeufinSandboxServiceInterface, iban: string | null = null, - ) { + ): Promise<void> { let url = new URL("testing/register", libeufinSandboxService.baseUrl); - await axios.post(url.href, { - username: username, - password: password, - iban: iban, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: { + username: username, + password: password, + iban: iban, + }, }); } // Need /demobanks/$id as the base URL @@ -209,75 +208,57 @@ export namespace LibeufinSandboxApi { libeufinSandboxService: LibeufinSandboxServiceInterface, username: string = "admin", password: string = "secret", - ) { + ): Promise<void> { // baseUrl should already be pointed to one demobank. let url = new URL("ebics/subscribers", libeufinSandboxService.baseUrl); - await axios.post( - url.href, - { + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: { userID: req.userID, hostID: req.hostID, partnerID: req.partnerID, demobankAccountLabel: demobankAccountLabel, }, - { - auth: { - username: "admin", - password: "secret", - }, - }, - ); + }); } export async function rotateKeys( libeufinSandboxService: LibeufinSandboxServiceInterface, hostID: string, - ) { + ): Promise<void> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL(`admin/ebics/hosts/${hostID}/rotate-keys`, baseUrl); - await axios.post( - url.href, - {}, - { - auth: { - username: "admin", - password: "secret", - }, - }, - ); + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: {}, + }); } export async function createEbicsHost( libeufinSandboxService: LibeufinSandboxServiceInterface, hostID: string, - ) { + ): Promise<void> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL("admin/ebics/hosts", baseUrl); - await axios.post( - url.href, - { + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: { hostID, ebicsVersion: "2.5", }, - { - auth: { - username: "admin", - password: "secret", - }, - }, - ); + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + }); } export async function createBankAccount( libeufinSandboxService: LibeufinSandboxServiceInterface, req: BankAccountInfo, - ) { + ): Promise<void> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL(`admin/bank-accounts/${req.label}`, baseUrl); - await axios.post(url.href, req, { - auth: { - username: "admin", - password: "secret", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: req, + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); } @@ -288,14 +269,13 @@ export namespace LibeufinSandboxApi { export async function createEbicsSubscriber( libeufinSandboxService: LibeufinSandboxServiceInterface, req: CreateEbicsSubscriberRequest, - ) { + ): Promise<void> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL("admin/ebics/subscribers", baseUrl); - await axios.post(url.href, req, { - auth: { - username: "admin", - password: "secret", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: req, + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); } @@ -306,14 +286,13 @@ export namespace LibeufinSandboxApi { export async function createEbicsBankAccount( libeufinSandboxService: LibeufinSandboxServiceInterface, req: CreateEbicsBankAccountRequest, - ) { + ): Promise<void> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL("admin/ebics/bank-accounts", baseUrl); - await axios.post(url.href, req, { - auth: { - username: "admin", - password: "secret", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: req, + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); } @@ -321,17 +300,16 @@ export namespace LibeufinSandboxApi { libeufinSandboxService: LibeufinSandboxServiceInterface, accountLabel: string, req: SimulateIncomingTransactionRequest, - ) { + ): Promise<void> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL( `admin/bank-accounts/${accountLabel}/simulate-incoming-transaction`, baseUrl, ); - await axios.post(url.href, req, { - auth: { - username: "admin", - password: "secret", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + body: req, + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); } @@ -344,13 +322,10 @@ export namespace LibeufinSandboxApi { `admin/bank-accounts/${accountLabel}/transactions`, baseUrl, ); - const res = await axios.get(url.href, { - auth: { - username: "admin", - password: "secret", - }, + const res = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); - return res.data as SandboxAccountTransactions; + return (await res.json()) as SandboxAccountTransactions; } export async function getCamt053( @@ -359,61 +334,50 @@ export namespace LibeufinSandboxApi { ): Promise<any> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL("admin/payments/camt", baseUrl); - return await axios.post( - url.href, - { + return await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: { bankaccount: accountLabel, type: 53, }, - { - auth: { - username: "admin", - password: "secret", - }, - }, - ); + }); } export async function getAccountInfoWithBalance( libeufinSandboxService: LibeufinSandboxServiceInterface, accountLabel: string, - ): Promise<any> { + ): Promise<LibeufinSandboxAdminBankAccountBalance> { const baseUrl = libeufinSandboxService.baseUrl; let url = new URL(`admin/bank-accounts/${accountLabel}`, baseUrl); - return await axios.get(url.href, { - auth: { - username: "admin", - password: "secret", - }, + const res = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + return res.json(); } } export namespace LibeufinNexusApi { export async function getAllConnections( nexus: LibeufinNexusServiceInterface, - ): Promise<any> { + ): Promise<NexusBankConnections> { let url = new URL("bank-connections", nexus.baseUrl); - const res = await axios.get(url.href, { - auth: { - username: "admin", - password: "test", - }, + const res = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); - return res; + return res.json(); } export async function deleteBankConnection( libeufinNexusService: LibeufinNexusServiceInterface, req: DeleteBankConnectionRequest, - ): Promise<any> { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL("bank-connections/delete-connection", baseUrl); - return await axios.post(url.href, req, { - auth: { - username: "admin", - password: "test", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: req, }); } @@ -423,9 +387,10 @@ export namespace LibeufinNexusApi { ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL("bank-connections", baseUrl); - await axios.post( - url.href, - { + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: { source: "new", type: "ebics", name: req.name, @@ -437,13 +402,7 @@ export namespace LibeufinNexusApi { systemID: req.systemID, }, }, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + }); } export async function getBankAccount( @@ -452,12 +411,10 @@ export namespace LibeufinNexusApi { ): Promise<any> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`bank-accounts/${accountName}`, baseUrl); - return await axios.get(url.href, { - auth: { - username: "admin", - password: "test", - }, + const resp = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + return resp.json(); } export async function submitInitiatedPayment( @@ -470,16 +427,11 @@ export namespace LibeufinNexusApi { `bank-accounts/${accountName}/payment-initiations/${paymentId}/submit`, baseUrl, ); - await axios.post( - url.href, - {}, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: {}, + }); } export async function fetchAccounts( @@ -491,16 +443,11 @@ export namespace LibeufinNexusApi { `bank-connections/${connectionName}/fetch-accounts`, baseUrl, ); - await axios.post( - url.href, - {}, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: {}, + }); } export async function importConnectionAccount( @@ -514,37 +461,27 @@ export namespace LibeufinNexusApi { `bank-connections/${connectionName}/import-account`, baseUrl, ); - await axios.post( - url.href, - { + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: { offeredAccountId, nexusBankAccountId, }, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + }); } export async function connectBankConnection( libeufinNexusService: LibeufinNexusServiceInterface, connectionName: string, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`bank-connections/${connectionName}/connect`, baseUrl); - await axios.post( - url.href, - {}, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: {}, + }); } export async function getPaymentInitiations( @@ -558,43 +495,33 @@ export namespace LibeufinNexusApi { `/bank-accounts/${accountName}/payment-initiations`, baseUrl, ); - let response = await axios.get(url.href, { - auth: { - username: username, - password: password, - }, + let response = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + const respJson = await response.json(); console.log( `Payment initiations of: ${accountName}`, - JSON.stringify(response.data, null, 2), + JSON.stringify(respJson, null, 2), ); } - export async function getConfig( - libeufinNexusService: LibeufinNexusServiceInterface, - ): Promise<void> { - const baseUrl = libeufinNexusService.baseUrl; - let url = new URL(`/config`, baseUrl); - let response = await axios.get(url.href); - } - // Uses the Anastasis API to get a list of transactions. export async function getAnastasisTransactions( libeufinNexusService: LibeufinNexusServiceInterface, anastasisBaseUrl: string, + // FIXME: Nail down type! params: {}, // of the request: {delta: 5, ..} username: string = "admin", password: string = "test", ): Promise<any> { let url = new URL("history/incoming", anastasisBaseUrl); - let response = await axios.get(url.href, { - params: params, - auth: { - username: username, - password: password, - }, + for (const [k, v] of Object.entries(params)) { + url.searchParams.set(k, String(v)); + } + let response = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); - return response; + return response.json(); } // FIXME: this function should return some structured @@ -604,16 +531,13 @@ export namespace LibeufinNexusApi { accountName: string, username: string = "admin", password: string = "test", - ): Promise<any> { + ): Promise<LibeufinNexusTransactions> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/bank-accounts/${accountName}/transactions`, baseUrl); - let response = await axios.get(url.href, { - auth: { - username: username, - password: password, - }, + let response = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); - return response; + return response.json(); } export async function fetchTransactions( @@ -623,25 +547,21 @@ export namespace LibeufinNexusApi { level: string = "report", username: string = "admin", password: string = "test", - ): Promise<any> { + ): Promise<NexusNewTransactionsInfo> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL( `/bank-accounts/${accountName}/fetch-transactions`, baseUrl, ); - return await axios.post( - url.href, - { + const resp = await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: { rangeType: rangeType, level: level, }, - { - auth: { - username: username, - password: password, - }, - }, - ); + }); + return resp.json(); } export async function changePassword( @@ -649,97 +569,109 @@ export namespace LibeufinNexusApi { username: string, req: UpdateNexusUserRequest, auth: NexusAuth, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/users/${username}/password`, baseUrl); - await axios.post(url.href, req, auth); + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: req, + }); } export async function getUser( libeufinNexusService: LibeufinNexusServiceInterface, auth: NexusAuth, - ): Promise<any> { + ): Promise<NexusUserResponse> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/user`, baseUrl); - return await axios.get(url.href, auth); + const resp = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + }); + return resp.json(); } export async function createUser( libeufinNexusService: LibeufinNexusServiceInterface, req: CreateNexusUserRequest, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/users`, baseUrl); - await axios.post(url.href, req, { - auth: { - username: "admin", - password: "test", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: req, }); } export async function getAllPermissions( libeufinNexusService: LibeufinNexusServiceInterface, - ): Promise<any> { + ): Promise<NexusGetPermissionsResponse> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/permissions`, baseUrl); - return await axios.get(url.href, { - auth: { - username: "admin", - password: "test", - }, + const resp = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + return resp.json(); } export async function postPermission( libeufinNexusService: LibeufinNexusServiceInterface, req: PostNexusPermissionRequest, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/permissions`, baseUrl); - await axios.post(url.href, req, { - auth: { - username: "admin", - password: "test", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: req, + }); + } + + export async function getAllTasks( + libeufinNexusService: LibeufinNexusServiceInterface, + bankAccountName: string, + ): Promise<NexusTaskCollection> { + const baseUrl = libeufinNexusService.baseUrl; + let url = new URL(`/bank-accounts/${bankAccountName}/schedule`, baseUrl); + const resp = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + return resp.json(); } - export async function getTasks( + export async function getTask( libeufinNexusService: LibeufinNexusServiceInterface, bankAccountName: string, // When void, the request returns the list of all the // tasks under this bank account. - taskName: string | void, - ): Promise<any> { + taskName: string, + ): Promise<NexusTask> { const baseUrl = libeufinNexusService.baseUrl; - let url = new URL(`/bank-accounts/${bankAccountName}/schedule`, baseUrl); + let url = new URL( + `/bank-accounts/${bankAccountName}/schedule/${taskName}`, + baseUrl, + ); if (taskName) url = new URL(taskName, `${url.href}/`); - - // It's caller's responsibility to interpret the response. - return await axios.get(url.href, { - auth: { - username: "admin", - password: "test", - }, + const resp = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + return resp.json(); } export async function deleteTask( libeufinNexusService: LibeufinNexusServiceInterface, bankAccountName: string, taskName: string, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL( `/bank-accounts/${bankAccountName}/schedule/${taskName}`, baseUrl, ); - await axios.delete(url.href, { - auth: { - username: "admin", - password: "test", - }, + await libeufinHttpLib.fetch(url.href, { + method: "DELETE", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); } @@ -747,53 +679,50 @@ export namespace LibeufinNexusApi { libeufinNexusService: LibeufinNexusServiceInterface, bankAccountName: string, req: PostNexusTaskRequest, - ): Promise<any> { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`/bank-accounts/${bankAccountName}/schedule`, baseUrl); - return await axios.post(url.href, req, { - auth: { - username: "admin", - password: "test", - }, + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: req, }); } export async function deleteFacade( libeufinNexusService: LibeufinNexusServiceInterface, facadeName: string, - ): Promise<any> { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL(`facades/${facadeName}`, baseUrl); - return await axios.delete(url.href, { - auth: { - username: "admin", - password: "test", - }, + await libeufinHttpLib.fetch(url.href, { + method: "DELETE", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); } export async function getAllFacades( libeufinNexusService: LibeufinNexusServiceInterface, - ): Promise<any> { + ): Promise<NexusFacadeListResponse> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL("facades", baseUrl); - return await axios.get(url.href, { - auth: { - username: "admin", - password: "test", - }, + const resp = await libeufinHttpLib.fetch(url.href, { + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, }); + // FIXME: Just return validated, typed response here! + return resp.json(); } export async function createAnastasisFacade( libeufinNexusService: LibeufinNexusServiceInterface, req: CreateAnastasisFacadeRequest, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL("facades", baseUrl); - await axios.post( - url.href, - { + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: { name: req.name, type: "anastasis", config: { @@ -803,24 +732,19 @@ export namespace LibeufinNexusApi { reserveTransferLevel: req.reserveTransferLevel, }, }, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + }); } export async function createTwgFacade( libeufinNexusService: LibeufinNexusServiceInterface, req: CreateTalerWireGatewayFacadeRequest, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL("facades", baseUrl); - await axios.post( - url.href, - { + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: { name: req.name, type: "taler-wire-gateway", config: { @@ -830,33 +754,22 @@ export namespace LibeufinNexusApi { reserveTransferLevel: req.reserveTransferLevel, }, }, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + }); } export async function submitAllPaymentInitiations( libeufinNexusService: LibeufinNexusServiceInterface, accountId: string, - ) { + ): Promise<void> { const baseUrl = libeufinNexusService.baseUrl; let url = new URL( `/bank-accounts/${accountId}/submit-all-payment-initiations`, baseUrl, ); - await axios.post( - url.href, - {}, - { - auth: { - username: "admin", - password: "test", - }, - }, - ); + await libeufinHttpLib.fetch(url.href, { + method: "POST", + headers: { Authorization: makeBasicAuthHeader("admin", "secret") }, + body: {}, + }); } } |