libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 6045f4885b5020fc052b1407965f343e2868ecfc
parent 831eed3d6cbcff7ce9a2e2e9d230f16ef250f88a
Author: MS <ms@taler.net>
Date:   Tue, 15 Dec 2020 01:23:46 +0100

fix payto-parser

Diffstat:
Mutil/src/main/kotlin/Payto.kt | 19++++++++++---------
Mutil/src/test/kotlin/PaytoTest.kt | 29++++++++++++++++++++++-------
2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/util/src/main/kotlin/Payto.kt b/util/src/main/kotlin/Payto.kt @@ -1,7 +1,5 @@ package tech.libeufin.util -import io.ktor.http.HttpStatusCode -import tech.libeufin.util.EbicsProtocolError import java.net.URI /** @@ -10,28 +8,31 @@ import java.net.URI data class Payto( val name: String, val iban: String, - val bic: String = "NOTGIVEN" + val bic: String ) +class InvalidPaytoError(msg: String) : Exception(msg) fun parsePayto(paytoLine: String): Payto { + if (!"^payto://".toRegex().containsMatchIn(paytoLine)) throw InvalidPaytoError("Invalid payto line: $paytoLine") val javaParsedUri = try { URI(paytoLine) } catch (e: java.lang.Exception) { - throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' is not a valid URI") + throw InvalidPaytoError("'${paytoLine}' is not a valid URI") } if (javaParsedUri.scheme != "payto") { - throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' is not payto") + throw InvalidPaytoError("'${paytoLine}' is not payto") } - val iban = javaParsedUri.path.split("/").last() val queryStringAsList = javaParsedUri.query.split("&") // admit only ONE parameter: receiver-name. if (queryStringAsList.size != 1) { - throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' has unsupported query string") + throw InvalidPaytoError("'${paytoLine}' has unsupported query string") } val splitParameter = queryStringAsList.first().split("=") if (splitParameter.first() != "receiver-name" && splitParameter.first() != "sender-name") { - throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' has unsupported query string") + throw InvalidPaytoError("'${paytoLine}' has unsupported query string") } val receiverName = splitParameter.last() - return Payto(iban = iban, name = receiverName) + val split_path = javaParsedUri.path.split("/").filter { it.isNotEmpty() } + if (split_path.size != 2) throw InvalidPaytoError("BIC and IBAN are both mandatory ($split_path)") + return Payto(iban = split_path[1], bic = split_path[0], name = receiverName) } \ No newline at end of file diff --git a/util/src/test/kotlin/PaytoTest.kt b/util/src/test/kotlin/PaytoTest.kt @@ -1,26 +1,41 @@ import org.junit.Test import tech.libeufin.util.EbicsProtocolError +import tech.libeufin.util.InvalidPaytoError import tech.libeufin.util.parsePayto class PaytoTest { @Test - fun parsePaytoTest() { - val noBic = parsePayto("payto://iban/IBAN123?receiver-name=The%20Name") - assert(noBic.iban == "IBAN123" && noBic.name == "The Name") - val withBic = parsePayto("payto://iban/BIC123/IBAN123?receiver-name=The%20Name") - assert(withBic.iban == "IBAN123" && withBic.name == "The Name") + fun wrongCases() { try { parsePayto("http://iban/BIC123/IBAN123?receiver-name=The%20Name") - } catch (e: EbicsProtocolError) { + } catch (e: InvalidPaytoError) { + println(e) println("wrong scheme was caught") } try { parsePayto( "payto://iban/BIC123/IBAN123?receiver-name=The%20Name&address=house" ) - } catch (e: EbicsProtocolError) { + } catch (e: InvalidPaytoError) { + println(e) println("more than one parameter isn't allowed") } + try { + parsePayto( + "payto:iban/BIC123/IBAN123?receiver-name=The%20Name&address=house" + ) + } catch (e: InvalidPaytoError) { + println(e) + println("'://' missing, invalid Payto") + } + } + + @Test + fun parsePaytoTest() { + val withBic = parsePayto("payto://iban/BIC123/IBAN123?receiver-name=The%20Name") + assert(withBic.iban == "IBAN123") + assert(withBic.bic == "BIC123") + assert(withBic.name == "The Name") } } \ No newline at end of file