aboutsummaryrefslogtreecommitdiff
path: root/nexus/src/test/kotlin/Common.kt
blob: d35315d6a9f92d08248ce25da71a9a9612ec25d4 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.request.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import tech.libeufin.nexus.*
import tech.libeufin.util.*
import java.security.interfaces.RSAPrivateCrtKey
import java.time.Instant

val j = Json {
    this.serializersModule = SerializersModule {
        contextual(RSAPrivateCrtKey::class) { RSAPrivateCrtKeySerializer }
    }
}

val config: EbicsSetupConfig = run {
    val handle = TalerConfig(NEXUS_CONFIG_SOURCE)
    handle.load()
    EbicsSetupConfig(handle)
}

fun prepDb(cfg: TalerConfig): Database {
    cfg.loadDefaults()
    val dbCfg = DatabaseConfig(
        dbConnStr = "postgresql:///libeufincheck",
        sqlDir = cfg.requirePath("paths", "datadir") + "sql"
    )
    pgDataSource(dbCfg.dbConnStr).pgConnection().use { conn ->
        println("SQL dir for testing: ${dbCfg.sqlDir}")
        try {
            resetDatabaseTables(conn, dbCfg, "libeufin-nexus")
        } catch (e: Exception) {
            logger.warn("Resetting an empty database throws, tolerating this...")
            logger.warn(e.message)
        }
        initializeDatabaseTables(conn, dbCfg, "libeufin-nexus")
    }
   
    return Database(dbCfg.dbConnStr)
}

val clientKeys = generateNewKeys()

// Gets an HTTP client whose requests are going to be served by 'handler'.
fun getMockedClient(
    handler: MockRequestHandleScope.(HttpRequestData) -> HttpResponseData
): HttpClient {
    return HttpClient(MockEngine) {
        followRedirects = false
        engine {
            addHandler {
                    request -> handler(request)
            }
        }
    }
}

// Partial config to talk to PostFinance.
fun getPofiConfig(
    userId: String,
    partnerId: String,
    accountOwner: String? = "NotGiven"
    ) = """
    [nexus-ebics]
    CURRENCY = KUDOS
    HOST_BASE_URL = https://isotest.postfinance.ch/ebicsweb/ebicsweb
    HOST_ID = PFEBICS
    USER_ID = $userId
    PARTNER_ID = $partnerId
    SYSTEM_ID = not-used
    IBAN = CH9789144829733648596
    BIC = POFICHBE
    NAME = LibEuFin
    BANK_PUBLIC_KEYS_FILE = /tmp/pofi-testplatform-bank-keys.json
    CLIENT_PRIVATE_KEYS_FILE = /tmp/pofi-testplatform-subscriber-keys.json
    BANK_DIALECT = postfinance
""".trimIndent()

// Generates a payment initiation, given its subject.
fun genInitPay(
    subject: String = "init payment",
    requestUid: String = "unique"
) =
    InitiatedPayment(
        amount = TalerAmount(44, 0, "KUDOS"),
        creditPaytoUri = "payto://iban/TEST-IBAN?receiver-name=Test",
        wireTransferSubject = subject,
        initiationTime = Instant.now(),
        requestUid = requestUid
    )

// Generates an incoming payment, given its subject.
fun genInPay(subject: String) =
    IncomingPayment(
        amount = TalerAmount(44, 0, "KUDOS"),
        debitPaytoUri = "payto://iban/not-used",
        wireTransferSubject = subject,
        executionTime = Instant.now(),
        bankTransferId = "entropic"
    )

// Generates an outgoing payment, given its subject.
fun genOutPay(subject: String, bankTransferId: String) =
    OutgoingPayment(
        amount = TalerAmount(44, 0, "KUDOS"),
        creditPaytoUri = "payto://iban/TEST-IBAN?receiver-name=Test",
        wireTransferSubject = subject,
        executionTime = Instant.now(),
        bankTransferId = bankTransferId
    )