PaytoTest.kt (4077B)
1 /* 2 * This file is part of LibEuFin. 3 * Copyright (C) 2024, 2025, 2026 Taler Systems S.A. 4 5 * LibEuFin is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation; either version 3, or 8 * (at your option) any later version. 9 10 * LibEuFin is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General 13 * Public License for more details. 14 15 * You should have received a copy of the GNU Affero General Public 16 * License along with LibEuFin; see the file COPYING. If not, see 17 * <http://www.gnu.org/licenses/> 18 */ 19 20 import org.junit.Test 21 import tech.libeufin.common.BankPaytoCtx 22 import tech.libeufin.common.CommonError 23 import tech.libeufin.common.Payto 24 import kotlin.test.assertEquals 25 import kotlin.test.assertFailsWith 26 import kotlin.test.assertNull 27 import java.net.URL 28 29 class PaytoTest { 30 @Test 31 fun wrongCases() { 32 assertFailsWith<CommonError.Payto> { Payto.parse("http://iban/BIC123/IBAN123?receiver-name=The%20Name") } 33 assertFailsWith<CommonError.Payto> { Payto.parse("payto:iban/BIC123/IBAN123?receiver-name=The%20Name&address=house") } 34 assertFailsWith<CommonError.Payto> { Payto.parse("payto://wrong/BIC123/IBAN123?sender-name=Foo&receiver-name=Foo") } 35 } 36 37 @Test 38 fun parsePaytoTest() { 39 val withBic = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=The%20Name").expectIban() 40 assertEquals(withBic.iban.value, "CH9300762011623852957") 41 assertEquals(withBic.receiverName, "The Name") 42 val complete = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=The%20Name&amount=EUR:1&message=donation").expectIban() 43 assertEquals(complete.iban.value, "CH9300762011623852957") 44 assertEquals(complete.receiverName, "The Name") 45 assertEquals(complete.message, "donation") 46 assertEquals(complete.amount.toString(), "EUR:1") 47 val plusEncode = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=Santa+Claus&amount=EUR:1&message=donation").expectIban() 48 assertEquals(plusEncode.iban.value, "CH9300762011623852957") 49 assertEquals(plusEncode.receiverName, "Santa Claus") 50 val withoutOptionals = Payto.parse("payto://iban/CH9300762011623852957").expectIban() 51 assertNull(withoutOptionals.message) 52 assertNull(withoutOptionals.receiverName) 53 assertNull(withoutOptionals.amount) 54 val malformed = Payto.parse("payto://iban/CH0400766000103138557?receiver-name=NYM%20Technologies%SA").expectIban() 55 assertEquals(malformed.iban.value, "CH0400766000103138557") 56 assertEquals(malformed.receiverName, "NYM Technologies%SA") 57 } 58 59 @Test 60 fun forms() { 61 val ctx = BankPaytoCtx( 62 bic = "TESTBIC", 63 hostname = "test.com" 64 ) 65 val canonical = "payto://iban/CH9300762011623852957" 66 val bank = "payto://iban/TESTBIC/CH9300762011623852957?receiver-name=Name" 67 val inputs = listOf( 68 "payto://iban/BIC/CH9300762011623852957?receiver-name=NotGiven", 69 "payto://iban/CH9300762011623852957?receiver-name=Grothoff%20Hans", 70 "payto://iban/ch%209300-7620-1162-3852-957", 71 ) 72 val names = listOf( 73 "NotGiven", "Grothoff Hans", null 74 ) 75 val full = listOf( 76 "payto://iban/BIC/CH9300762011623852957?receiver-name=Santa", 77 "payto://iban/CH9300762011623852957?receiver-name=Santa", 78 "payto://iban/CH9300762011623852957?receiver-name=Santa", 79 ) 80 for ((i, input) in inputs.withIndex()) { 81 val payto = Payto.parse(input).expectIban() 82 assertEquals(canonical, payto.canonical) 83 assertEquals(bank, payto.bank("Name", ctx)) 84 assertEquals(full[i], payto.full("Santa")) 85 assertEquals(names[i], payto.receiverName) 86 } 87 } 88 }