fix broken tests

This commit is contained in:
Sebastian 2023-04-04 15:11:08 -03:00
parent 68d711b2e7
commit 7090807fcb
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069
5 changed files with 112 additions and 53 deletions

View File

@ -16,14 +16,16 @@
import test from "ava"; import test from "ava";
import { import {
parsePayUri,
parseWithdrawUri,
parseRefundUri,
parseTipUri,
parsePayPushUri, parsePayPushUri,
constructPayPushUri,
parsePayTemplateUri, parsePayTemplateUri,
constructPayUri, parsePayUri,
parseRefundUri,
parseRestoreUri,
parseTipUri,
parseWithdrawUri,
stringifyPayPushUri,
stringifyPayUri,
stringifyRestoreUri,
} from "./taleruri.js"; } from "./taleruri.js";
test("taler pay url parsing: wrong scheme", (t) => { test("taler pay url parsing: wrong scheme", (t) => {
@ -220,22 +222,46 @@ test("taler peer to peer push URI (http)", (t) => {
t.is(r1.contractPriv, "foo"); t.is(r1.contractPriv, "foo");
}); });
test("taler peer to peer push URI (construction)", (t) => { test("taler peer to peer push URI (stringify)", (t) => {
const url = constructPayPushUri({ const url = stringifyPayPushUri({
exchangeBaseUrl: "https://foo.example.com/bla/", exchangeBaseUrl: "https://foo.example.com/bla/",
contractPriv: "123", contractPriv: "123",
}); });
t.deepEqual(url, "taler://pay-push/foo.example.com/bla/123"); t.deepEqual(url, "taler://pay-push/foo.example.com/bla/123");
}); });
test("taler pay URI (stringify)", (t) => {
test("taler pay URI (construction)", (t) => { const url1 = stringifyPayUri({
const url1 = constructPayUri("http://localhost:123/", "foo", ""); merchantBaseUrl: "http://localhost:123/",
orderId: "foo",
sessionId: "",
});
t.deepEqual(url1, "taler+http://pay/localhost:123/foo/"); t.deepEqual(url1, "taler+http://pay/localhost:123/foo/");
const url2 = constructPayUri("http://localhost:123/", "foo", "bla"); const url2 = stringifyPayUri({
merchantBaseUrl: "http://localhost:123/",
orderId: "foo",
sessionId: "bla",
});
t.deepEqual(url2, "taler+http://pay/localhost:123/foo/bla"); t.deepEqual(url2, "taler+http://pay/localhost:123/foo/bla");
}); });
test("taler pay URI (stringify with https)", (t) => {
const url1 = stringifyPayUri({
merchantBaseUrl: "https://localhost:123/",
orderId: "foo",
sessionId: "",
});
t.deepEqual(url1, "taler://pay/localhost:123/foo/");
const url2 = stringifyPayUri({
merchantBaseUrl: "https://localhost/",
orderId: "foo",
sessionId: "bla",
noncePriv: "123",
});
t.deepEqual(url2, "taler://pay/localhost/foo/bla?n=123");
});
test("taler pay template URI (parsing)", (t) => { test("taler pay template URI (parsing)", (t) => {
const url1 = const url1 =
"taler://pay-template/merchant.example.com/FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY?amount=KUDOS:5"; "taler://pay-template/merchant.example.com/FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY?amount=KUDOS:5";
@ -261,3 +287,45 @@ test("taler pay template URI (parsing, http with port)", (t) => {
t.deepEqual(r1.templateId, "FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY"); t.deepEqual(r1.templateId, "FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY");
t.deepEqual(r1.templateParams.amount, "KUDOS:5"); t.deepEqual(r1.templateParams.amount, "KUDOS:5");
}); });
test("taler restore URI (parsing, http with port)", (t) => {
const r1 = parseRestoreUri(
"taler+http://restore/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0/prov1.example.com,prov2.example.com:123",
);
if (!r1) {
t.fail();
return;
}
t.deepEqual(
r1.walletRootPriv,
"GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0",
);
t.deepEqual(r1.providers[0], "http://prov1.example.com/");
t.deepEqual(r1.providers[1], "http://prov2.example.com:123/");
});
test("taler restore URI (parsing, https with port)", (t) => {
const r1 = parseRestoreUri(
"taler://restore/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0/prov1.example.com,prov2.example.com:234,https%3A%2F%2Fprov1.example.com%2F",
);
if (!r1) {
t.fail();
return;
}
t.deepEqual(
r1.walletRootPriv,
"GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0",
);
t.deepEqual(r1.providers[0], "https://prov1.example.com/");
t.deepEqual(r1.providers[1], "https://prov2.example.com:234/");
});
test("taler restore URI (stringify)", (t) => {
const url = stringifyRestoreUri({
walletRootPriv: "GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0",
providers: ["http://prov1.example.com", "https://prov2.example.com:234/"],
});
t.deepEqual(
url,
"taler://restore/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0/http%3A%2F%2Fprov1.example.com%2F,https%3A%2F%2Fprov2.example.com%3A234%2F",
);
});

View File

@ -35,8 +35,8 @@ export interface PayUriResult {
merchantBaseUrl: string; merchantBaseUrl: string;
orderId: string; orderId: string;
sessionId: string; sessionId: string;
claimToken: string | undefined; claimToken?: string;
noncePriv: string | undefined; noncePriv?: string;
} }
export interface PayTemplateUriResult { export interface PayTemplateUriResult {
@ -96,10 +96,7 @@ export interface DevExperimentUri {
export interface BackupRestoreUri { export interface BackupRestoreUri {
type: TalerUriAction.Restore; type: TalerUriAction.Restore;
walletRootPriv: string; walletRootPriv: string;
providers: { providers: Array<string>;
name: string;
url: string;
}[];
} }
/** /**
@ -535,9 +532,9 @@ export function parseRefundUri(s: string): RefundUriResult | undefined {
return undefined; return undefined;
} }
const host = parts[0].toLowerCase(); const host = parts[0].toLowerCase();
// const sessionId = parts[parts.length - 1]; const sessionId = parts[parts.length - 1];
const orderId = parts[parts.length - 1]; const orderId = parts[parts.length - 2];
const pathSegments = parts.slice(1, parts.length - 1); const pathSegments = parts.slice(1, parts.length - 2);
const hostAndSegments = [host, ...pathSegments].join("/"); const hostAndSegments = [host, ...pathSegments].join("/");
const merchantBaseUrl = canonicalizeBaseUrl( const merchantBaseUrl = canonicalizeBaseUrl(
`${pi.innerProto}://${hostAndSegments}/`, `${pi.innerProto}://${hostAndSegments}/`,
@ -576,10 +573,12 @@ export function parseRestoreUri(uri: string): BackupRestoreUri | undefined {
const walletRootPriv = parts[0]; const walletRootPriv = parts[0];
if (!walletRootPriv) return undefined; if (!walletRootPriv) return undefined;
const providers = new Array<{ name: string; url: string }>(); const providers = new Array<string>();
parts[1].split(",").map((name) => { parts[1].split(",").map((name) => {
const url = canonicalizeBaseUrl(`${pi.innerProto}://${name}/`); const url = canonicalizeBaseUrl(
providers.push({ name, url }); `${pi.innerProto}://${decodeURIComponent(name)}/`,
);
providers.push(url);
}); });
return { return {
type: TalerUriAction.Restore, type: TalerUriAction.Restore,
@ -597,10 +596,7 @@ export function parseRestoreUri(uri: string): BackupRestoreUri | undefined {
*/ */
export function constructRecoveryUri(args: { export function constructRecoveryUri(args: {
walletRootPriv: string; walletRootPriv: string;
providers: { providers: string[];
name: string;
url: string;
}[];
}): string { }): string {
return stringifyRestoreUri(args); return stringifyRestoreUri(args);
} }
@ -652,26 +648,6 @@ export function stringifyPayUri({
claimToken, claimToken,
noncePriv, noncePriv,
}: Omit<PayUriResult, "type">): string { }: Omit<PayUriResult, "type">): string {
// const base = canonicalizeBaseUrl(merchantBaseUrl);
// const url = new URL(base);
// const isHttp = base.startsWith("http://");
// let result = isHttp ? `taler+http://pay/` : `taler://pay/`;
// result += url.hostname;
// if (url.port != "") {
// result += `:${url.port}`;
// }
// result += `${url.pathname}${orderId}/${sessionId}`;
// const qp = new URLSearchParams();
// if (claimToken) {
// qp.append("c", claimToken);
// }
// if (noncePriv) {
// qp.append("n", noncePriv);
// }
// const queryPart = qp.toString();
// if (queryPart) {
// result += "?" + queryPart;
// }
const { proto, path, query } = getUrlInfo(merchantBaseUrl, { const { proto, path, query } = getUrlInfo(merchantBaseUrl, {
c: claimToken, c: claimToken,
n: noncePriv, n: noncePriv,
@ -700,7 +676,9 @@ export function stringifyRestoreUri({
providers, providers,
walletRootPriv, walletRootPriv,
}: Omit<BackupRestoreUri, "type">): string { }: Omit<BackupRestoreUri, "type">): string {
const list = providers.map((p) => `${new URL(p.url).hostname}`).join("m"); const list = providers
.map((url) => `${encodeURIComponent(new URL(url).href)}`)
.join(",");
return `taler://restore/${walletRootPriv}/${list}`; return `taler://restore/${walletRootPriv}/${list}`;
} }

View File

@ -25,5 +25,5 @@ function expect(t: ExecutionContext, thing: any): any {
} }
test("should have a test", (t) => { test("should have a test", (t) => {
expect(t, true).equal(true); expect(t, true).deep.equal(true);
}); });

View File

@ -59,7 +59,13 @@ export function useComponentState({
async function recoverBackup(): Promise<void> { async function recoverBackup(): Promise<void> {
await api.wallet.call(WalletApiOperation.ImportBackupRecovery, { await api.wallet.call(WalletApiOperation.ImportBackupRecovery, {
recovery, recovery: {
walletRootPriv: recovery.walletRootPriv,
providers: recovery.providers.map((url) => ({
name: new URL(url).hostname,
url,
})),
},
}); });
onSuccess(); onSuccess();
} }

View File

@ -14,7 +14,11 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/ */
import { AbsoluteTime, constructRecoveryUri } from "@gnu-taler/taler-util"; import {
AbsoluteTime,
constructRecoveryUri,
stringifyRestoreUri,
} from "@gnu-taler/taler-util";
import { import {
ProviderInfo, ProviderInfo,
ProviderPaymentPaid, ProviderPaymentPaid,
@ -132,7 +136,10 @@ export function BackupPage({ onAddProvider }: Props): VNode {
WalletApiOperation.ExportBackupRecovery, WalletApiOperation.ExportBackupRecovery,
{}, {},
); );
const str = constructRecoveryUri(r); const str = stringifyRestoreUri({
walletRootPriv: r.walletRootPriv,
providers: r.providers.map((p) => p.url),
});
setRecoveryInfo(str); setRecoveryInfo(str);
} }