summaryrefslogtreecommitdiff
path: root/common/src/test/kotlin/PaytoTest.kt
blob: cfb33ada0c2b11df348cb28eebc1840f66ddd1fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2024 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin 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 Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

import org.junit.Test
import tech.libeufin.common.BankPaytoCtx
import tech.libeufin.common.CommonError
import tech.libeufin.common.Payto
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull

class PaytoTest {
    @Test
    fun wrongCases() {
        assertFailsWith<CommonError.Payto> { Payto.parse("http://iban/BIC123/IBAN123?receiver-name=The%20Name") }
        assertFailsWith<CommonError.Payto> { Payto.parse("payto:iban/BIC123/IBAN123?receiver-name=The%20Name&address=house") }
        assertFailsWith<CommonError.Payto> { Payto.parse("payto://wrong/BIC123/IBAN123?sender-name=Foo&receiver-name=Foo") }
    }

    @Test
    fun parsePaytoTest() {
        val withBic = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=The%20Name").expectIban()
        assertEquals(withBic.iban.value, "CH9300762011623852957")
        assertEquals(withBic.receiverName, "The Name")
        val complete = Payto.parse("payto://iban/BIC123/CH9300762011623852957?sender-name=The%20Name&amount=EUR:1&message=donation").expectIban()
        assertEquals(withBic.iban.value, "CH9300762011623852957")
        assertEquals(withBic.receiverName, "The Name")
        assertEquals(complete.message, "donation")
        assertEquals(complete.amount.toString(), "EUR:1")
        val withoutOptionals = Payto.parse("payto://iban/CH9300762011623852957").expectIban()
        assertNull(withoutOptionals.message)
        assertNull(withoutOptionals.receiverName)
        assertNull(withoutOptionals.amount)
    }

    @Test
    fun forms() {
        val ctx = BankPaytoCtx(
            bic = "TESTBIC"
        )
        val canonical = "payto://iban/CH9300762011623852957"
        val bank = "payto://iban/TESTBIC/CH9300762011623852957?receiver-name=Name"
        val inputs = listOf(
            "payto://iban/BIC/CH9300762011623852957?receiver-name=NotGiven",
            "payto://iban/CH9300762011623852957?receiver-name=Grothoff%20Hans",
            "payto://iban/ch%209300-7620-1162-3852-957",
        )
        val names = listOf(
            "NotGiven", "Grothoff Hans", null
        )
        val full = listOf(
            "payto://iban/BIC/CH9300762011623852957?receiver-name=NotGiven",
            "payto://iban/CH9300762011623852957?receiver-name=Grothoff%20Hans",
            "payto://iban/CH9300762011623852957?receiver-name=Santa",
        )
        for ((i, input) in inputs.withIndex()) {
            val payto = Payto.parse(input).expectIban()
            assertEquals(canonical, payto.canonical)
            assertEquals(bank, payto.bank("Name", ctx))
            assertEquals(full[i], payto.full("Santa"))
            assertEquals(names[i], payto.receiverName)
        }
    }
}