diff options
author | Antoine A <> | 2024-01-22 11:40:18 +0100 |
---|---|---|
committer | Antoine A <> | 2024-01-22 11:40:18 +0100 |
commit | 277c533b1bc490596ef8831f32449d7319843461 (patch) | |
tree | 1e22360b9f567e908a2d75aac86c109e5b6fd892 | |
parent | 88b7a3bde2dd92417177f34dd0fe473c71b2f75e (diff) | |
download | libeufin-277c533b1bc490596ef8831f32449d7319843461.tar.gz libeufin-277c533b1bc490596ef8831f32449d7319843461.tar.bz2 libeufin-277c533b1bc490596ef8831f32449d7319843461.zip |
Fix stat test and improve logging
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt | 2 | ||||
-rw-r--r-- | bank/src/test/kotlin/StatsTest.kt | 6 | ||||
-rw-r--r-- | nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 55 | ||||
-rw-r--r-- | nexus/src/test/kotlin/CliTest.kt | 22 |
4 files changed, 44 insertions, 41 deletions
diff --git a/bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt b/bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt index 6e832703..fa376fd4 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt @@ -128,7 +128,7 @@ fun Routing.wireGatewayApi(db: Database, ctx: BankConfig) { TalerErrorCode.BANK_ACCOUNT_IS_NOT_EXCHANGE ) is AddIncomingResult.UnknownDebtor -> throw conflict( - "Debtor account was not found", + "Debtor account ${req.debit_account} was not found", TalerErrorCode.BANK_UNKNOWN_DEBTOR ) is AddIncomingResult.BothPartyAreExchange -> throw conflict( diff --git a/bank/src/test/kotlin/StatsTest.kt b/bank/src/test/kotlin/StatsTest.kt index 7ec5c7d0..4198dbe4 100644 --- a/bank/src/test/kotlin/StatsTest.kt +++ b/bank/src/test/kotlin/StatsTest.kt @@ -122,7 +122,7 @@ class StatsTest { @Test fun timeframe() = bankSetup { db -> db.conn { conn -> - suspend fun register(now: OffsetDateTime, amount: TalerAmount) { + suspend fun register(now: LocalDateTime, amount: TalerAmount) { val stmt = conn.prepareStatement( "CALL stats_register_payment('taler_out', ?::timestamp, (?, ?)::taler_amount, null)" ) @@ -133,7 +133,7 @@ class StatsTest { } suspend fun check( - now: OffsetDateTime, + now: LocalDateTime, timeframe: Timeframe, which: Int?, count: Long, @@ -165,7 +165,7 @@ class StatsTest { }!! } - val now = OffsetDateTime.now(ZoneOffset.UTC) + val now = LocalDateTime.now() val otherHour = now.withHour((now.hour + 1) % 24) val otherDay = now.withDayOfMonth((now.dayOfMonth) % 28 + 1) val otherMonth = now.withMonth((now.monthValue) % 12 + 1) diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt index 3854879e..da6e6237 100644 --- a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt +++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt @@ -282,62 +282,55 @@ fun expectFullKeys( ): Pair<ClientPrivateKeysFile, BankPublicKeysFile> { val clientKeys = loadPrivateKeysFromDisk(cfg.clientPrivateKeysFilename) if (clientKeys == null) { - throw Exception("Cannot operate without client keys. Missing '${cfg.clientPrivateKeysFilename}' file. Run 'libeufin-nexus ebics-setup' first") + throw Exception("Missing client private keys file at '${cfg.clientPrivateKeysFilename}', run 'libeufin-nexus ebics-setup' first") } else if (!clientKeys.submitted_ini || !clientKeys.submitted_hia) { - throw Exception("Cannot operate with unsubmitted client keys, run 'libeufin-nexus ebics-setup' first") + throw Exception("Unsubmitted client private keys, run 'libeufin-nexus ebics-setup' first") } val bankKeys = loadBankKeys(cfg.bankPublicKeysFilename) if (bankKeys == null) { - throw Exception("Cannot operate without bank keys. Missing '${cfg.bankPublicKeysFilename}' file. run 'libeufin-nexus ebics-setup' first") + throw Exception("Missing bank public keys at '${cfg.bankPublicKeysFilename}', run 'libeufin-nexus ebics-setup' first") } else if (!bankKeys.accepted) { - throw Exception("Cannot operate with unaccepted bank keys, run 'libeufin-nexus ebics-setup' until accepting the bank keys") + throw Exception("Unaccepted bank public keys, run 'libeufin-nexus ebics-setup' until accepting the bank keys") } return Pair(clientKeys, bankKeys) } -/** - * Load the bank keys file from disk. - * - * @param location the keys file location. - * @return the internal JSON representation of the keys file, - * or null if the file does not exist - */ -fun loadBankKeys(location: String): BankPublicKeysFile? { +private inline fun <reified T> loadJsonFile(path: String, name: String): T? { + val file = File(path) val content = try { - File(location).readText() - } catch (e: FileNotFoundException) { - return null + file.readText() } catch (e: Exception) { - throw Exception("Could not read the bank keys file from disk", e) + // FileNotFoundException can be thrown if the file exists, but is not accessible... + when { + !file.exists() -> return null + !file.canRead() -> throw Exception("Could not read $name at '$path': permission denied") + else -> throw Exception("Could not read $name at '$path'", e) + } } return try { myJson.decodeFromString(content) } catch (e: Exception) { - throw Exception("Could not decode bank keys", e) + throw Exception("Could not decode $name at '$path'", e) } } /** + * Load the bank keys file from disk. + * + * @param location the keys file location. + * @return the internal JSON representation of the keys file, + * or null if the file does not exist + */ +fun loadBankKeys(location: String): BankPublicKeysFile? = loadJsonFile(location, "bank public keys") + +/** * Load the client keys file from disk. * * @param location the keys file location. * @return the internal JSON representation of the keys file, * or null if the file does not exist */ -fun loadPrivateKeysFromDisk(location: String): ClientPrivateKeysFile? { - val content = try { - File(location).readText() - } catch (e: FileNotFoundException) { - return null - } catch (e: Exception) { - throw Exception("Could not read private keys from disk", e) - } - return try { - myJson.decodeFromString(content) - } catch (e: Exception) { - throw Exception("Could not decode private keys", e) - } -} +fun loadPrivateKeysFromDisk(location: String): ClientPrivateKeysFile? = loadJsonFile(location, "client private keys") /** * Abstracts the config loading diff --git a/nexus/src/test/kotlin/CliTest.kt b/nexus/src/test/kotlin/CliTest.kt index e6386e21..9ad737e3 100644 --- a/nexus/src/test/kotlin/CliTest.kt +++ b/nexus/src/test/kotlin/CliTest.kt @@ -58,17 +58,22 @@ class CliTest { // Missing client keys clientKeysPath.deleteIfExists() for (cmd in cmds) { - nexusCmd.testErr("$cmd -c $conf", "Cannot operate without client keys. Missing '$clientKeysPath' file. Run 'libeufin-nexus ebics-setup' first") + nexusCmd.testErr("$cmd -c $conf", "Missing client private keys file at '$clientKeysPath', run 'libeufin-nexus ebics-setup' first") } // Bad client json clientKeysPath.writeText("CORRUPTION", Charsets.UTF_8) for (cmd in allCmds) { - nexusCmd.testErr("$cmd -c $conf", "Could not decode private keys: Expected start of the object '{', but had 'EOF' instead at path: $\nJSON input: CORRUPTION") + nexusCmd.testErr("$cmd -c $conf", "Could not decode client private keys at '$clientKeysPath': Expected start of the object '{', but had 'EOF' instead at path: $\nJSON input: CORRUPTION") + } + // Missing permission + clientKeysPath.toFile().setReadable(false) + for (cmd in allCmds) { + nexusCmd.testErr("$cmd -c $conf", "Could not read client private keys at '$clientKeysPath': permission denied") } // Unfinished client syncJsonToDisk(generateNewKeys(), clientKeysPath.toString()) for (cmd in cmds) { - nexusCmd.testErr("$cmd -c $conf", "Cannot operate with unsubmitted client keys, run 'libeufin-nexus ebics-setup' first") + nexusCmd.testErr("$cmd -c $conf", "Unsubmitted client private keys, run 'libeufin-nexus ebics-setup' first") } // Missing bank keys @@ -78,12 +83,17 @@ class CliTest { }, clientKeysPath.toString()) bankKeysPath.deleteIfExists() for (cmd in cmds) { - nexusCmd.testErr("$cmd -c $conf", "Cannot operate without bank keys. Missing '$bankKeysPath' file. run 'libeufin-nexus ebics-setup' first") + nexusCmd.testErr("$cmd -c $conf", "Missing bank public keys at '$bankKeysPath', run 'libeufin-nexus ebics-setup' first") } // Bad bank json bankKeysPath.writeText("CORRUPTION", Charsets.UTF_8) for (cmd in allCmds) { - nexusCmd.testErr("$cmd -c $conf", "Could not decode bank keys: Expected start of the object '{', but had 'EOF' instead at path: $\nJSON input: CORRUPTION") + nexusCmd.testErr("$cmd -c $conf", "Could not decode bank public keys at '$bankKeysPath': Expected start of the object '{', but had 'EOF' instead at path: $\nJSON input: CORRUPTION") + } + // Missing permission + bankKeysPath.toFile().setReadable(false) + for (cmd in allCmds) { + nexusCmd.testErr("$cmd -c $conf", "Could not read bank public keys at '$bankKeysPath': permission denied") } // Unfinished bank syncJsonToDisk(BankPublicKeysFile( @@ -92,7 +102,7 @@ class CliTest { accepted = false ), bankKeysPath.toString()) for (cmd in cmds) { - nexusCmd.testErr("$cmd -c $conf", "Cannot operate with unaccepted bank keys, run 'libeufin-nexus ebics-setup' until accepting the bank keys") + nexusCmd.testErr("$cmd -c $conf", "Unaccepted bank public keys, run 'libeufin-nexus ebics-setup' until accepting the bank keys") } } }
\ No newline at end of file |