EbicsTest.kt (3467B)
1 /* 2 * This file is part of LibEuFin. 3 * Copyright (C) 2025 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.w3c.dom.Document 21 import com.github.ajalt.clikt.core.CliktCommand 22 import com.github.ajalt.clikt.testing.test 23 import kotlinx.coroutines.runBlocking 24 import io.ktor.http.* 25 import io.ktor.http.content.* 26 import io.ktor.client.engine.mock.* 27 import org.junit.Test 28 import tech.libeufin.common.* 29 import tech.libeufin.common.crypto.CryptoUtil 30 import tech.libeufin.ebics.* 31 import tech.libeufin.ebics.test.* 32 import kotlin.io.path.* 33 import kotlin.test.* 34 import java.time.LocalDate 35 36 @OptIn(kotlin.io.path.ExperimentalPathApi::class) 37 class EbicsTest { 38 private val ebicsLogger = EbicsLogger(null).tx("test").step("step") 39 40 // POSTs an EBICS message to the mock bank. Tests 41 // the main branches: unreachable bank, non-200 status 42 // code, and 200. 43 @Test 44 fun postMessage() {runBlocking { 45 assertFailsWith<EbicsError.HTTP> { 46 getMockedClient { 47 respondError(HttpStatusCode.NotFound) 48 }.postToBank("http://ignored.example.com/", ByteArray(0), "Test", ebicsLogger) 49 }.run { 50 assertEquals(HttpStatusCode.NotFound, status) 51 assertEquals("Test: bank HTTP error: 404 Not Found", message) 52 } 53 assertFailsWith<EbicsError.Network> { 54 getMockedClient { 55 throw Exception("Simulate failure") 56 }.postToBank("http://ignored.example.com/", ByteArray(0), "Test", ebicsLogger) 57 }.run { 58 assertEquals("Test: failed to contact bank", message) 59 assertEquals("Simulate failure", cause!!.message) 60 } 61 assertFailsWith<EbicsError.Protocol> { 62 getMockedClient { 63 respondOk("<ebics broken></ebics>") 64 }.postToBank("http://ignored.example.com/", ByteArray(0), "Test", ebicsLogger) 65 }.run { 66 assertEquals("Test: invalid XML bank response", message) 67 assertEquals("Attribute name \"broken\" associated with an element type \"ebics\" must be followed by the ' = ' character.", cause!!.message) 68 } 69 getMockedClient { 70 respondOk("<ebics></ebics>") 71 }.postToBank("http://ignored.example.com/", ByteArray(0), "Test", ebicsLogger) 72 }} 73 74 // Tests that internal repr. of keys lead to valid PDF. 75 // Mainly tests that the function does not throw any error. 76 @Test 77 fun keysPdf() { 78 val pdf = generateKeysPdf(generateNewKeys(), object: EbicsHostConfig { 79 override val baseUrl = "https://isotest.postfinance.ch/ebicsweb/ebicsweb" 80 override val hostId = "PFEBICS" 81 override val userId = "PFC00563" 82 override val partnerId = "PFC00563" 83 }) 84 Path("/tmp/libeufin-nexus-test-keys.pdf").writeBytes(pdf) 85 } 86 }