summaryrefslogtreecommitdiff
path: root/nexus/src/test/kotlin/EbicsTest.kt
blob: a67a48d63717c147b639df3d689cd7e5744e41de (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
/*
 * 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 io.ktor.client.engine.mock.*
import io.ktor.http.*
import org.junit.Test
import tech.libeufin.nexus.*
import tech.libeufin.nexus.ebics.*
import kotlin.io.path.Path
import kotlin.io.path.writeBytes
import kotlin.test.*

class EbicsTest {
    // POSTs an EBICS message to the mock bank.  Tests
    // the main branches: unreachable bank, non-200 status
    // code, and 200.
    @Test
    fun postMessage() = conf { config ->
        assertFailsWith<EbicsError.Transport> {
            getMockedClient {
                respondError(HttpStatusCode.NotFound)
            }.postToBank("http://ignored.example.com/", ByteArray(0), "Test")
        }.run {
            assertEquals("Test: bank HTTP error: 404 Not Found", message)
        }
        assertFailsWith<EbicsError.Transport> {
            getMockedClient {
                throw Exception("Simulate failure")
            }.postToBank("http://ignored.example.com/", ByteArray(0), "Test")
        }.run {
            assertEquals("Test: failed to contact bank", message)
            assertEquals("Simulate failure", cause!!.message)
        }
        assertFailsWith<EbicsError.Protocol> {
            getMockedClient {
                respondOk("<ebics broken></ebics>")
            }.postToBank("http://ignored.example.com/", ByteArray(0), "Test")
        }.run {
            assertEquals("Test: invalid XML bank reponse", message)
            assertEquals("Attribute name \"broken\" associated with an element type \"ebics\" must be followed by the ' = ' character.", cause!!.message)
        }
        getMockedClient {
            respondOk("<ebics></ebics>")
        }.postToBank("http://ignored.example.com/", ByteArray(0), "Test")
    }

    // Tests that internal repr. of keys lead to valid PDF.
    // Mainly tests that the function does not throw any error.
    @Test
    fun keysPdf() = conf { config -> 
        val pdf = generateKeysPdf(clientKeys, config)
        Path("/tmp/libeufin-nexus-test-keys.pdf").writeBytes(pdf)
    }
}