commit d8438142db43975ea348f00bdf51106ecef67d69
parent ffd714d263da95e59088906e1d7fe5165e39985c
Author: MS <ms@taler.net>
Date: Tue, 20 Dec 2022 17:29:28 +0100
test latest changes
Diffstat:
3 files changed, 122 insertions(+), 2 deletions(-)
diff --git a/nexus/src/test/kotlin/MakeEnv.kt b/nexus/src/test/kotlin/MakeEnv.kt
@@ -176,7 +176,7 @@ fun prepSandboxDb() {
}
DemobankCustomerEntity.new {
username = "foo"
- passwordHash = "foo"
+ passwordHash = CryptoUtil.hashpw("foo")
name = "Foo"
}
}
diff --git a/nexus/src/test/kotlin/SandboxAccessApiTest.kt b/nexus/src/test/kotlin/SandboxAccessApiTest.kt
@@ -12,8 +12,50 @@ import tech.libeufin.sandbox.sandboxApp
import tech.libeufin.util.buildBasicAuthLine
class SandboxAccessApiTest {
-
val mapper = ObjectMapper()
+ // Check successful and failing case due to insufficient funds.
+ @Test
+ fun debitWithdraw() {
+ withTestDatabase {
+ prepSandboxDb()
+ withTestApplication(sandboxApp) {
+ runBlocking {
+ // Normal, successful withdrawal.
+ client.post<Any>("/demobanks/default/access-api/accounts/foo/withdrawals") {
+ expectSuccess = true
+ headers {
+ append(
+ HttpHeaders.ContentType,
+ ContentType.Application.Json
+ )
+ append(
+ HttpHeaders.Authorization,
+ buildBasicAuthLine("foo", "foo")
+ )
+ }
+ this.body = "{\"amount\": \"TESTKUDOS:1\"}"
+ }
+ // Withdrawal over the debit threshold.
+ val r: HttpStatusCode = client.post("/demobanks/default/access-api/accounts/foo/withdrawals") {
+ expectSuccess = false
+ headers {
+ append(
+ HttpHeaders.ContentType,
+ ContentType.Application.Json
+ )
+ append(
+ HttpHeaders.Authorization,
+ buildBasicAuthLine("foo", "foo")
+ )
+ }
+ this.body = "{\"amount\": \"TESTKUDOS:99999999999\"}"
+ }
+ assert(HttpStatusCode.Forbidden.value == r.value)
+ }
+ }
+ }
+ }
+
@Test
fun registerTest() {
// Test IBAN conflict detection.
diff --git a/nexus/src/test/kotlin/SandboxBankAccountTest.kt b/nexus/src/test/kotlin/SandboxBankAccountTest.kt
@@ -0,0 +1,77 @@
+import io.ktor.client.features.*
+import io.ktor.client.request.*
+import io.ktor.client.statement.*
+import io.ktor.http.*
+import io.ktor.server.testing.*
+import kotlinx.coroutines.runBlocking
+import org.junit.Test
+import tech.libeufin.sandbox.SandboxError
+import tech.libeufin.sandbox.getBalance
+import tech.libeufin.sandbox.sandboxApp
+import tech.libeufin.sandbox.wireTransfer
+import tech.libeufin.util.buildBasicAuthLine
+import tech.libeufin.util.parseDecimal
+
+class SandboxBankAccountTest {
+ // Check if the balance shows debit.
+ @Test
+ fun debitBalance() {
+ withTestDatabase {
+ prepSandboxDb()
+ wireTransfer(
+ "bank",
+ "foo",
+ "default",
+ "Show up in logging!",
+ "TESTKUDOS:1"
+ )
+ /**
+ * Bank gave 1 to foo, should be -1 debit now. Because
+ * the payment is still pending (= not booked), the pending
+ * transactions must be included in the calculation.
+ */
+ var bankBalance = getBalance("bank", true)
+ assert(bankBalance == parseDecimal("-1"))
+ wireTransfer(
+ "foo",
+ "bank",
+ "default",
+ "Show up in logging!",
+ "TESTKUDOS:5"
+ )
+ bankBalance = getBalance("bank", true)
+ assert(bankBalance == parseDecimal("4"))
+ // Trigger Insufficient funds case for users.
+ try {
+ wireTransfer(
+ "foo",
+ "bank",
+ "default",
+ "Show up in logging!",
+ "TESTKUDOS:5000"
+ )
+ } catch (e: SandboxError) {
+ // Future versions may wrap this case into a dedicate exception type.
+ assert(e.statusCode == HttpStatusCode.PreconditionFailed)
+ }
+ // Trigger Insufficient funds case for the bank.
+ try {
+ wireTransfer(
+ "bank",
+ "foo",
+ "default",
+ "Show up in logging!",
+ "TESTKUDOS:5000000"
+ )
+ } catch (e: SandboxError) {
+ // Future versions may wrap this case into a dedicate exception type.
+ assert(e.statusCode == HttpStatusCode.PreconditionFailed)
+ }
+ // Check balance didn't change for both parties.
+ bankBalance = getBalance("bank", true)
+ assert(bankBalance == parseDecimal("4"))
+ val fooBalance = getBalance("foo", true)
+ assert(fooBalance == parseDecimal("-4"))
+ }
+ }
+}
+\ No newline at end of file