libeufin

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

commit 96f44f2a12d4e5c71d043a9bbc737ca20a6d4d0b
parent 1ced32c441385a44aed7237a01c1953cdd6ee6a4
Author: Antoine A <>
Date:   Wed, 22 Nov 2023 15:00:40 +0000

Improve admin account creation and log conversion config in db

Diffstat:
Mbank/src/main/kotlin/tech/libeufin/bank/Main.kt | 33+++++++++++++++++++++++++--------
Mbank/src/main/kotlin/tech/libeufin/bank/helpers.kt | 11++---------
Mbank/src/test/kotlin/DatabaseTest.kt | 5+++--
Mbank/src/test/kotlin/helpers.kt | 2+-
4 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/bank/src/main/kotlin/tech/libeufin/bank/Main.kt b/bank/src/main/kotlin/tech/libeufin/bank/Main.kt @@ -289,11 +289,35 @@ class BankDbInit : CliktCommand("Initialize the libeufin-bank database", name = ).flag() override fun run() { - val cfg = talerConfig(configFile).loadDbConfig() + val config = talerConfig(configFile) + val cfg = config.loadDbConfig() if (requestReset) { resetDatabaseTables(cfg, sqlFilePrefix = "libeufin-bank") } initializeDatabaseTables(cfg, sqlFilePrefix = "libeufin-bank") + val ctx = config.loadBankConfig(); + val db = Database(cfg.dbConnStr, ctx.regionalCurrency, ctx.fiatCurrency) + runBlocking { + // Create admin account if missing + val res = maybeCreateAdminAccount(db, ctx) // logs provided by the helper + when (res) { + AccountCreationResult.BonusBalanceInsufficient -> {} + AccountCreationResult.LoginReuse -> {} + AccountCreationResult.PayToReuse -> { + logger.error("Failed to create admin's account") + exitProcess(1) + } + AccountCreationResult.Success -> { + logger.info("Admin's account created") + } + } + + // Load conversion config + ctx.conversionInfo?.run { + logger.info("load conversion config in DB") + db.conversion.updateConfig(this) + } + } } } @@ -313,10 +337,6 @@ class ServeBank : CliktCommand("Run libeufin-bank HTTP server", name = "serve") exitProcess(1) } val db = Database(dbCfg.dbConnStr, ctx.regionalCurrency, ctx.fiatCurrency) - runBlocking { - if (!maybeCreateAdminAccount(db, ctx)) // logs provided by the helper - exitProcess(1) - } embeddedServer(Netty, port = serverCfg.port) { corebankWebApp(db, ctx) }.start(wait = true) @@ -337,9 +357,6 @@ class ChangePw : CliktCommand("Change account password", name = "passwd") { val dbCfg = cfg.loadDbConfig() val db = Database(dbCfg.dbConnStr, ctx.regionalCurrency, ctx.fiatCurrency) runBlocking { - if (!maybeCreateAdminAccount(db, ctx)) // logs provided by the helper - exitProcess(1) - if (db.account.reconfigPassword(account, password, null) != AccountPatchAuthResult.Success) { println("password change failed") exitProcess(1) diff --git a/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt b/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt @@ -117,8 +117,7 @@ fun ApplicationCall.longUriComponent(name: String): Long { * * It returns false in case of problems, true otherwise. */ -suspend fun maybeCreateAdminAccount(db: Database, ctx: BankConfig, pw: String? = null): Boolean { - logger.debug("Creating admin's account") +suspend fun maybeCreateAdminAccount(db: Database, ctx: BankConfig, pw: String? = null): AccountCreationResult { var pwStr = pw; if (pwStr == null) { val pwBuf = ByteArray(32) @@ -126,7 +125,7 @@ suspend fun maybeCreateAdminAccount(db: Database, ctx: BankConfig, pw: String? = pwStr = String(pwBuf, Charsets.UTF_8) } - val res = db.account.create( + return db.account.create( login = "admin", password = pwStr, name = "Bank administrator", @@ -136,12 +135,6 @@ suspend fun maybeCreateAdminAccount(db: Database, ctx: BankConfig, pw: String? = maxDebt = ctx.defaultAdminDebtLimit, bonus = null ) - return when (res) { - AccountCreationResult.BonusBalanceInsufficient -> false - AccountCreationResult.LoginReuse -> true - AccountCreationResult.PayToReuse -> false - AccountCreationResult.Success -> true - } } fun Route.intercept(callback: Route.() -> Unit, interceptor: suspend PipelineContext<Unit, ApplicationCall>.() -> Unit): Route { diff --git a/bank/src/test/kotlin/DatabaseTest.kt b/bank/src/test/kotlin/DatabaseTest.kt @@ -29,6 +29,7 @@ import kotlin.test.* import kotlinx.coroutines.* import org.junit.Test import tech.libeufin.bank.* +import tech.libeufin.bank.AccountDAO.* import tech.libeufin.util.* class DatabaseTest { @@ -44,9 +45,9 @@ class DatabaseTest { @Test fun createAdmin() = setup { db, ctx -> // Create admin account - assert(maybeCreateAdminAccount(db, ctx)) + assertEquals(AccountCreationResult.Success, maybeCreateAdminAccount(db, ctx)) // Checking idempotency - assert(maybeCreateAdminAccount(db, ctx)) + assertEquals(AccountCreationResult.LoginReuse, maybeCreateAdminAccount(db, ctx)) } @Test diff --git a/bank/src/test/kotlin/helpers.kt b/bank/src/test/kotlin/helpers.kt @@ -100,7 +100,7 @@ fun bankSetup( bonus = null )) // Create admin account - assert(maybeCreateAdminAccount(db, ctx, "admin-password")) + assertEquals(AccountCreationResult.Success, maybeCreateAdminAccount(db, ctx, "admin-password")) testApplication { application { corebankWebApp(db, ctx)