diff options
author | Sebastian <sebasjm@gmail.com> | 2023-12-11 14:18:16 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-12-11 14:18:16 -0300 |
commit | acef3baeb50fa240c833dfe0dfb4863b176896f6 (patch) | |
tree | 61551b6699753bdec096271c0bab79279aae36a2 /packages/taler-util | |
parent | bad61fce24e55f117d28e7d6472d2ad288d1fbc6 (diff) | |
download | wallet-core-acef3baeb50fa240c833dfe0dfb4863b176896f6.tar.xz |
match 1:1 testing with taler-uri spec
Diffstat (limited to 'packages/taler-util')
-rw-r--r-- | packages/taler-util/src/taleruri.test.ts | 344 | ||||
-rw-r--r-- | packages/taler-util/src/taleruri.ts | 18 |
2 files changed, 295 insertions, 67 deletions
diff --git a/packages/taler-util/src/taleruri.test.ts b/packages/taler-util/src/taleruri.test.ts index 318f460da..1f0c37004 100644 --- a/packages/taler-util/src/taleruri.test.ts +++ b/packages/taler-util/src/taleruri.test.ts @@ -16,6 +16,10 @@ import test from "ava"; import { + parseAuditorUri, + parseDevExperimentUri, + parseExchangeUri, + parsePayPullUri, parsePayPushUri, parsePayTemplateUri, parsePayUri, @@ -24,23 +28,63 @@ import { parseRewardUri, parseWithdrawExchangeUri, parseWithdrawUri, + stringifyAuditorUri, + stringifyDevExperimentUri, + stringifyExchangeUri, + stringifyPayPullUri, stringifyPayPushUri, + stringifyPayTemplateUri, stringifyPayUri, + stringifyRefundUri, stringifyRestoreUri, + stringifyRewardUri, stringifyWithdrawExchange, + stringifyWithdrawUri, } from "./taleruri.js"; import { AmountString } from "./taler-types.js"; -test("taler pay url parsing: wrong scheme", (t) => { - const url1 = "talerfoo://"; - const r1 = parsePayUri(url1); - t.is(r1, undefined); - const url2 = "taler://refund/a/b/c/d/e/f"; - const r2 = parsePayUri(url2); - t.is(r2, undefined); +/** + * 5.1 action: withdraw https://lsd.gnunet.org/lsd0006/#name-action-withdraw + */ + +test("taler withdraw uri parsing", (t) => { + const url1 = "taler://withdraw/bank.example.com/12345"; + const r1 = parseWithdrawUri(url1); + if (!r1) { + t.fail(); + return; + } + t.is(r1.withdrawalOperationId, "12345"); + t.is(r1.bankIntegrationApiBaseUrl, "https://bank.example.com/"); }); +test("taler withdraw uri parsing (http)", (t) => { + const url1 = "taler+http://withdraw/bank.example.com/12345"; + const r1 = parseWithdrawUri(url1); + if (!r1) { + t.fail(); + return; + } + t.is(r1.withdrawalOperationId, "12345"); + t.is(r1.bankIntegrationApiBaseUrl, "http://bank.example.com/"); +}); + +test("taler withdraw URI (stringify)", (t) => { + const url = stringifyWithdrawUri({ + bankIntegrationApiBaseUrl: "https://bank.taler.test/integration-api/", + withdrawalOperationId: "123" + }); + t.deepEqual( + url, + "taler://withdraw/bank.taler.test/integration-api/123", + ); +}); + + +/** + * 5.2 action: pay https://lsd.gnunet.org/lsd0006/#name-action-pay + */ test("taler pay url parsing: defaults", (t) => { const url1 = "taler://pay/example.com/myorder/"; const r1 = parsePayUri(url1); @@ -84,17 +128,6 @@ test("taler pay url parsing (claim token)", (t) => { t.is(r1.claimToken, "ASDF"); }); -test("taler refund uri parsing: non-https #1", (t) => { - const url1 = "taler+http://refund/example.com/myorder/"; - const r1 = parseRefundUri(url1); - if (!r1) { - t.fail(); - return; - } - t.is(r1.merchantBaseUrl, "http://example.com/"); - t.is(r1.orderId, "myorder"); -}); - test("taler pay uri parsing: non-https", (t) => { const url1 = "taler+http://pay/example.com/myorder/"; const r1 = parsePayUri(url1); @@ -116,28 +149,55 @@ test("taler pay uri parsing: missing session component", (t) => { t.pass(); }); -test("taler withdraw uri parsing", (t) => { - const url1 = "taler://withdraw/bank.example.com/12345"; - const r1 = parseWithdrawUri(url1); - if (!r1) { - t.fail(); - return; - } - t.is(r1.withdrawalOperationId, "12345"); - t.is(r1.bankIntegrationApiBaseUrl, "https://bank.example.com/"); +test("taler pay URI (stringify)", (t) => { + const url1 = stringifyPayUri({ + merchantBaseUrl: "http://localhost:123/", + orderId: "foo", + sessionId: "", + }); + t.deepEqual(url1, "taler+http://pay/localhost:123/foo/"); + + const url2 = stringifyPayUri({ + merchantBaseUrl: "http://localhost:123/", + orderId: "foo", + sessionId: "bla", + }); + t.deepEqual(url2, "taler+http://pay/localhost:123/foo/bla"); }); -test("taler withdraw uri parsing (http)", (t) => { - const url1 = "taler+http://withdraw/bank.example.com/12345"; - const r1 = parseWithdrawUri(url1); +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"); +}); + +/** + * 5.3 action: refund https://lsd.gnunet.org/lsd0006/#name-action-refund + */ + +test("taler refund uri parsing: non-https #1", (t) => { + const url1 = "taler+http://refund/example.com/myorder/"; + const r1 = parseRefundUri(url1); if (!r1) { t.fail(); return; } - t.is(r1.withdrawalOperationId, "12345"); - t.is(r1.bankIntegrationApiBaseUrl, "http://bank.example.com/"); + t.is(r1.merchantBaseUrl, "http://example.com/"); + t.is(r1.orderId, "myorder"); }); + test("taler refund uri parsing", (t) => { const url1 = "taler://refund/merchant.example.com/1234/"; const r1 = parseRefundUri(url1); @@ -160,6 +220,23 @@ test("taler refund uri parsing with instance", (t) => { t.is(r1.merchantBaseUrl, "https://merchant.example.com/instances/myinst/"); }); +test("taler refund URI (stringify)", (t) => { + const url = stringifyRefundUri({ + merchantBaseUrl: "https://merchant.test/instance/pepe/", + orderId:"123" + }); + t.deepEqual( + url, + "taler://refund/merchant.test/instance/pepe/123/", + ); +}); + + +/** + * 5.4 action: reward https://lsd.gnunet.org/lsd0006/#name-action-tip + */ + + test("taler reward pickup uri", (t) => { const url1 = "taler://reward/merchant.example.com/tipid"; const r1 = parseRewardUri(url1); @@ -192,6 +269,22 @@ test("taler reward pickup uri with instance and prefix", (t) => { t.is(r1.merchantRewardId, "tipid"); }); +test("taler reward URI (stringify)", (t) => { + const url = stringifyRewardUri({ + merchantBaseUrl: "https://merchant.test/instance/pepe/", + merchantRewardId: "123" + }); + t.deepEqual( + url, + "taler://reward/merchant.test/instance/pepe/123/", + ); +}); + + +/** + * 5.5 action: pay-push https://lsd.gnunet.org/lsd0006/#name-action-pay-push + */ + test("taler peer to peer push URI", (t) => { const url1 = "taler://pay-push/exch.example.com/foo"; const r1 = parsePayPushUri(url1); @@ -232,39 +325,60 @@ test("taler peer to peer push URI (stringify)", (t) => { }); t.deepEqual(url, "taler://pay-push/foo.example.com/bla/123"); }); -test("taler pay URI (stringify)", (t) => { - const url1 = stringifyPayUri({ - merchantBaseUrl: "http://localhost:123/", - orderId: "foo", - sessionId: "", - }); - t.deepEqual(url1, "taler+http://pay/localhost:123/foo/"); - const url2 = stringifyPayUri({ - merchantBaseUrl: "http://localhost:123/", - orderId: "foo", - sessionId: "bla", - }); - t.deepEqual(url2, "taler+http://pay/localhost:123/foo/bla"); + +/** + * 5.6 action: pay-pull https://lsd.gnunet.org/lsd0006/#name-action-pay-pull + */ + +test("taler peer to peer pull URI", (t) => { + const url1 = "taler://pay-pull/exch.example.com/foo"; + const r1 = parsePayPullUri(url1); + if (!r1) { + t.fail(); + return; + } + t.is(r1.exchangeBaseUrl, "https://exch.example.com/"); + t.is(r1.contractPriv, "foo"); }); -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/"); +test("taler peer to peer pull URI (path)", (t) => { + const url1 = "taler://pay-pull/exch.example.com:123/bla/foo"; + const r1 = parsePayPullUri(url1); + if (!r1) { + t.fail(); + return; + } + t.is(r1.exchangeBaseUrl, "https://exch.example.com:123/bla/"); + t.is(r1.contractPriv, "foo"); +}); - const url2 = stringifyPayUri({ - merchantBaseUrl: "https://localhost/", - orderId: "foo", - sessionId: "bla", - noncePriv: "123", +test("taler peer to peer pull URI (http)", (t) => { + const url1 = "taler+http://pay-pull/exch.example.com:123/bla/foo"; + const r1 = parsePayPullUri(url1); + if (!r1) { + t.fail(); + return; + } + t.is(r1.exchangeBaseUrl, "http://exch.example.com:123/bla/"); + t.is(r1.contractPriv, "foo"); +}); + +test("taler peer to peer pull URI (stringify)", (t) => { + const url = stringifyPayPullUri({ + exchangeBaseUrl: "https://foo.example.com/bla/", + contractPriv: "123", }); - t.deepEqual(url2, "taler://pay/localhost/foo/bla?n=123"); + t.deepEqual(url, "taler://pay-pull/foo.example.com/bla/123"); }); + + +/** + * 5.7 action: pay-template https://lsd.gnunet.org/lsd0006/#name-action-pay-template + */ + + test("taler pay template URI (parsing)", (t) => { const url1 = "taler://pay-template/merchant.example.com/FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY?amount=KUDOS:5"; @@ -291,6 +405,72 @@ test("taler pay template URI (parsing, http with port)", (t) => { t.deepEqual(r1.templateParams.amount, "KUDOS:5"); }); +test("taler pay template URI (stringify)", (t) => { + const url1 = stringifyPayTemplateUri({ + merchantBaseUrl: "http://merchant.example.com:1234/", + templateId: "FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY", + templateParams: { + amount: "KUDOS:5" + }, + }); + t.deepEqual(url1, "taler+http://pay-template/merchant.example.com:1234/FEGHYJY48FEGU6WETYIOIDEDE2QW3OCZVY?amount=KUDOS%3A5"); +}); + + +/** + * 5.8 action: exchange https://lsd.gnunet.org/lsd0006/#name-action-exchange + */ + +test("taler exchange URI (parsing)", (t) => { + const url1 = + "taler://exchange/exchange.test/123"; + const r1 = parseExchangeUri(url1); + if (!r1) { + t.fail(); + return; + } + t.deepEqual(r1.exchangeBaseUrl, "https://exchange.test/"); + t.deepEqual(r1.exchangePub, "123"); +}); + +test("taler exchange URI (stringify)", (t) => { + const url1 = stringifyExchangeUri({ + exchangeBaseUrl: "https://exchange.test", + exchangePub: "123" + }); + t.deepEqual(url1, "taler://exchange/exchange.test/123"); +}); + + +/** + * 5.9 action: auditor https://lsd.gnunet.org/lsd0006/#name-action-auditor + */ + + + +test("taler auditor URI (parsing)", (t) => { + const url1 = + "taler://auditor/auditor.test/123"; + const r1 = parseAuditorUri(url1); + if (!r1) { + t.fail(); + return; + } + t.deepEqual(r1.auditorBaseUrl, "https://auditor.test/"); + t.deepEqual(r1.auditorPub, "123"); +}); + +test("taler auditor URI (stringify)", (t) => { + const url1 = stringifyAuditorUri({ + auditorBaseUrl: "https://auditor.test", + auditorPub: "123" + }); + t.deepEqual(url1, "taler://auditor/auditor.test/123"); +}); + +/** + * 5.10 action: restore https://lsd.gnunet.org/lsd0006/#name-action-restore + */ test("taler restore URI (parsing, http with port)", (t) => { const r1 = parseRestoreUri( "taler+http://restore/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0/prov1.example.com,prov2.example.com:123", @@ -333,6 +513,35 @@ test("taler restore URI (stringify)", (t) => { ); }); + + +/** + * 5.11 action: dev-experiment https://lsd.gnunet.org/lsd0006/#name-action-dev-experiment + */ + +test("taler dev exp URI (parsing)", (t) => { + const url1 = + "taler://dev-experiment/123"; + const r1 = parseDevExperimentUri(url1); + if (!r1) { + t.fail(); + return; + } + t.deepEqual(r1.devExperimentId, "123"); +}); + +test("taler dev exp URI (stringify)", (t) => { + const url1 = stringifyDevExperimentUri({ + devExperimentId: "123" + }); + t.deepEqual(url1, "taler://dev-experiment/123"); +}); + + +/** + * 5.12 action: withdraw-exchange https://lsd.gnunet.org/lsd0006/#name-action-withdraw-exchange + */ + test("taler withdraw exchange URI (parse)", (t) => { const r1 = parseWithdrawExchangeUri( "taler://withdraw-exchange/exchange.demo.taler.net/someroot/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0?a=KUDOS%3A2", @@ -371,3 +580,22 @@ test("taler withdraw exchange URI with amount (stringify)", (t) => { "taler://withdraw-exchange/exchange.demo.taler.net/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0?a=KUDOS%3A19", ); }); + + +/** + * wrong uris + */ +test("taler pay url parsing: wrong scheme", (t) => { + const url1 = "talerfoo://"; + const r1 = parsePayUri(url1); + t.is(r1, undefined); + + const url2 = "taler://refund/a/b/c/d/e/f"; + const r2 = parsePayUri(url2); + t.is(r2, undefined); +}); + + + + + diff --git a/packages/taler-util/src/taleruri.ts b/packages/taler-util/src/taleruri.ts index 47b646565..ff164dbbd 100644 --- a/packages/taler-util/src/taleruri.ts +++ b/packages/taler-util/src/taleruri.ts @@ -659,7 +659,7 @@ export function stringifyPayUri({ c: claimToken, n: noncePriv, }); - return `${proto}://pay/${path}${orderId}/${sessionId}/${query}`; + return `${proto}://pay/${path}${orderId}/${sessionId}${query}`; } export function stringifyPayPullUri({ @@ -667,7 +667,7 @@ export function stringifyPayPullUri({ exchangeBaseUrl, }: Omit<PayPullUriResult, "type">): string { const { proto, path } = getUrlInfo(exchangeBaseUrl); - return `${proto}://pay-pull/${path}${contractPriv}/`; + return `${proto}://pay-pull/${path}${contractPriv}`; } export function stringifyPayPushUri({ @@ -676,7 +676,7 @@ export function stringifyPayPushUri({ }: Omit<PayPushUriResult, "type">): string { const { proto, path } = getUrlInfo(exchangeBaseUrl); - return `${proto}://pay-push/${path}${contractPriv}/`; + return `${proto}://pay-push/${path}${contractPriv}`; } export function stringifyRestoreUri({ @@ -697,13 +697,13 @@ export function stringifyWithdrawExchange({ const { proto, path, query } = getUrlInfo(exchangeBaseUrl, { a: amount, }); - return `${proto}://withdraw-exchange/${path}${exchangePub}/${query}`; + return `${proto}://withdraw-exchange/${path}${exchangePub}${query}`; } export function stringifyDevExperimentUri({ devExperimentId, }: Omit<DevExperimentUri, "type">): string { - return `taler://dev-experiment/${devExperimentId}/`; + return `taler://dev-experiment/${devExperimentId}`; } export function stringifyPayTemplateUri({ @@ -712,7 +712,7 @@ export function stringifyPayTemplateUri({ templateParams, }: Omit<PayTemplateUriResult, "type">): string { const { proto, path, query } = getUrlInfo(merchantBaseUrl, templateParams); - return `${proto}://pay-template/${path}${templateId}/${query}`; + return `${proto}://pay-template/${path}${templateId}${query}`; } export function stringifyRefundUri({ merchantBaseUrl, @@ -734,7 +734,7 @@ export function stringifyExchangeUri({ exchangePub, }: Omit<ExchangeUri, "type">): string { const { proto, path } = getUrlInfo(exchangeBaseUrl); - return `${proto}://exchange/${path}${exchangePub}/`; + return `${proto}://exchange/${path}${exchangePub}`; } export function stringifyAuditorUri({ @@ -742,7 +742,7 @@ export function stringifyAuditorUri({ auditorPub, }: Omit<AuditorUri, "type">): string { const { proto, path } = getUrlInfo(auditorBaseUrl); - return `${proto}://auditor/${path}${auditorPub}/`; + return `${proto}://auditor/${path}${auditorPub}`; } export function stringifyWithdrawUri({ @@ -750,7 +750,7 @@ export function stringifyWithdrawUri({ withdrawalOperationId, }: Omit<WithdrawUriResult, "type">): string { const { proto, path } = getUrlInfo(bankIntegrationApiBaseUrl); - return `${proto}://withdraw/${path}${withdrawalOperationId}/`; + return `${proto}://withdraw/${path}${withdrawalOperationId}`; } /** |