libeufin

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

helpers.kt (2898B)


      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 io.ktor.client.*
     21 import io.ktor.client.request.*
     22 import io.ktor.client.statement.*
     23 import io.ktor.http.*
     24 import io.ktor.server.testing.*
     25 import kotlinx.coroutines.runBlocking
     26 import tech.libeufin.ebics.*
     27 import tech.libeufin.ebics.test.*
     28 import tech.libeufin.ebisync.*
     29 import tech.libeufin.ebisync.db.*
     30 import tech.libeufin.common.*
     31 import tech.libeufin.common.test.*
     32 import tech.libeufin.common.db.dbInit
     33 import tech.libeufin.common.db.pgDataSource
     34 import java.nio.file.NoSuchFileException
     35 import kotlin.io.path.*
     36 import java.nio.file.FileAlreadyExistsException
     37 import java.nio.file.Path
     38 import java.nio.file.StandardOpenOption
     39 import kotlin.random.Random
     40 import kotlin.test.assertEquals
     41 import kotlin.test.assertIs
     42 import kotlin.test.assertNotNull
     43 
     44 /* ----- Setup ----- */
     45 
     46 fun setup(
     47     conf: String = "test.conf",
     48     lambda: suspend (Database, EbisyncConfig) -> Unit
     49 ) = runBlocking {
     50     val cfg = ebisyncConfig(Path("conf/$conf"))
     51     pgDataSource(cfg.dbCfg.dbConnStr).run {
     52         dbInit(cfg.dbCfg, "libeufin-ebisync", true)
     53     }
     54     cfg.withDb(lambda)
     55 }
     56 
     57 @OptIn(kotlin.io.path.ExperimentalPathApi::class)
     58 fun serverSetup(
     59     conf: String = "test.conf",
     60     lambda: suspend ApplicationTestBuilder.(Database, EbicsState) -> Unit
     61 ) = setup(conf) { db, cfg ->
     62     val bank = EbicsState()
     63 
     64     // Reset current keys
     65     val dir = Path("/tmp/ebics-test")
     66     dir.deleteRecursively()
     67     dir.createDirectories()
     68 
     69     // Set setup mock
     70     setMock(sequence {
     71         yield(bank::hev)
     72         yield(bank::ini)
     73         yield(bank::hia)
     74         yield(bank::hpb)
     75     })
     76 
     77     val client = httpClient()
     78     val ebicsLogger = EbicsLogger(null)
     79     
     80     val (clientKeys, bankKeys) = ebicsSetup(
     81         client,
     82         ebicsLogger,
     83         cfg,
     84         cfg,
     85         cfg.setup,
     86         false,
     87         false,
     88         true,
     89         true
     90     )
     91     val ebics = EbicsClient(
     92         cfg,
     93         client,
     94         db.ebics,
     95         ebicsLogger,
     96         clientKeys,
     97         bankKeys
     98     )
     99 
    100     testApplication {
    101         application {
    102             ebisyncApi((cfg.submit.source as Source.SyncAPI).auth, ebics, cfg.spa)
    103         }
    104         lambda(db, bank)
    105     }
    106 }