taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 778e31da67785fe48d9bd77b4b8f823a11a0b4fc
parent 3c416530e3176c11cb71c14e2b47ffd5f0bf148f
Author: Florian Dold <dold@taler.net>
Date:   Tue, 21 Jul 2026 00:40:41 +0200

util: test account restrictions

Diffstat:
Apackages/taler-util/src/account-restrictions.test.ts | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 64 insertions(+), 0 deletions(-)

diff --git a/packages/taler-util/src/account-restrictions.test.ts b/packages/taler-util/src/account-restrictions.test.ts @@ -0,0 +1,64 @@ +/* + This file is part of GNU Taler + (C) 2026 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { test } from "node:test"; +import assert from "node:assert"; +import { checkAccountRestriction } from "./account-restrictions.js"; +import { AccountRestriction } from "./types-taler-exchange.js"; + +const payto = "payto://iban/CH9300762011623852957"; + +test("account restriction: deny and regex", (t) => { + assert.strictEqual(checkAccountRestriction(payto, []).ok, true); + assert.strictEqual( + checkAccountRestriction(payto, [{ type: "deny" }]).ok, + false, + ); + assert.strictEqual( + checkAccountRestriction(payto, [ + { type: "regex", payto_regex: "^payto://iban/CH.*$", human_hint: "CH" }, + ]).ok, + true, + ); + assert.strictEqual( + checkAccountRestriction(payto, [ + { type: "regex", payto_regex: "^payto://iban/DE.*$", human_hint: "DE" }, + ]).ok, + false, + ); +}); + +test("account restriction: unknown type fails closed", (t) => { + // The C reference treats an unrecognized restriction type as a hard + // failure. Failing open means a wallet on an older protocol version + // silently permits whatever a newer exchange meant to block. + const unknown = { type: "country-deny", countries: ["RU"] } as unknown as + AccountRestriction; + assert.strictEqual(checkAccountRestriction(payto, [unknown]).ok, false); +}); + +test("account restriction: malformed regex fails closed, does not throw", (t) => { + const bad: AccountRestriction = { + type: "regex", + payto_regex: "payto://iban/CH(", + human_hint: "broken", + }; + let res; + assert.doesNotThrow(() => { + res = checkAccountRestriction(payto, [bad]); + }); + assert.strictEqual(res!.ok, false); +});