taler-typescript-core

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

commit da2a8b0bafacedae5a925d2ddb4ab541910386cd
parent 592980fbde5d47aa2847c2237482042c85c37f57
Author: Florian Dold <dold@taler.net>
Date:   Tue, 21 Jul 2026 11:40:46 +0200

util: test the corebank and merchant token listings separately

Diffstat:
Apackages/taler-util/src/types-taler-common.test.ts | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+), 0 deletions(-)

diff --git a/packages/taler-util/src/types-taler-common.test.ts b/packages/taler-util/src/types-taler-common.test.ts @@ -0,0 +1,86 @@ +/* + 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 assert from "node:assert"; +import { test } from "node:test"; +import { Codec } from "./codec.js"; +import * as common from "./types-taler-common.js"; + +// Resolved dynamically so this file compiles before the codec exists. +const codecForBankTokenInfoList = (common as any) + .codecForBankTokenInfoList as () => Codec<unknown>; + +// GET /accounts/$USERNAME/tokens of the corebank API. +const corebankTokenList = { + tokens: [ + { + creation_time: { t_s: 1000 }, + expiration: { t_s: 2000 }, + scope: "readonly", + refreshable: true, + description: "phone", + last_access: { t_s: 1500 }, + row_id: 7, + token_id: 7, + }, + ], +}; + +// GET [/instances/$INSTANCE]/private/tokens of the merchant API. +const merchantTokenList = { + tokens: [ + { + creation_time: { t_s: 1000 }, + expiration: { t_s: 2000 }, + scope: "readonly", + refreshable: true, + description: "cash register", + serial: 7, + }, + ], +}; + +test("the corebank token listing decodes", (t) => { + const decoded: any = codecForBankTokenInfoList().decode(corebankTokenList); + assert.strictEqual(decoded.tokens.length, 1); + assert.strictEqual(decoded.tokens[0].token_id, 7); + assert.deepStrictEqual(decoded.tokens[0].last_access, { t_s: 1500 }); +}); + +test("row_id is optional, since it is deprecated since corebank v9", (t) => { + const { row_id, ...withoutRowId } = corebankTokenList.tokens[0]; + assert.doesNotThrow(() => + codecForBankTokenInfoList().decode({ tokens: [withoutRowId] }), + ); +}); + +test("the merchant token listing decodes", (t) => { + const decoded: any = common + .codecForTokenInfoList() + .decode(merchantTokenList) as any; + assert.strictEqual(decoded.tokens[0].serial, 7); +}); + +test("the merchant codec rejects a corebank token listing", (t) => { + // The corebank response carries no serial. + assert.throws(() => common.codecForTokenInfoList().decode(corebankTokenList)); +}); + +test("the corebank codec rejects a merchant token listing", (t) => { + // The merchant response carries neither last_access nor token_id. + const codec = codecForBankTokenInfoList(); + assert.throws(() => codec.decode(merchantTokenList)); +});