gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated (dd22bcca -> f1fead3a)


From: gnunet
Subject: [libeufin] branch master updated (dd22bcca -> f1fead3a)
Date: Mon, 12 Dec 2022 23:12:22 +0100

This is an automated email from the git hooks/post-receive script.

ms pushed a change to branch master
in repository libeufin.

    from dd22bcca Reduce logging.
     new 83f3fc7c check IBAN conflict
     new f1fead3a test IBAN conflict

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 nexus/src/test/kotlin/MakeEnv.kt                   | 28 +++++--
 nexus/src/test/kotlin/SandboxAccessApiTest.kt      | 96 ++++++++++++++++++++++
 .../src/main/kotlin/tech/libeufin/sandbox/Main.kt  | 15 +++-
 util/src/main/kotlin/JSON.kt                       |  2 +-
 4 files changed, 131 insertions(+), 10 deletions(-)
 create mode 100644 nexus/src/test/kotlin/SandboxAccessApiTest.kt

diff --git a/nexus/src/test/kotlin/MakeEnv.kt b/nexus/src/test/kotlin/MakeEnv.kt
index b4a11254..58b2affb 100644
--- a/nexus/src/test/kotlin/MakeEnv.kt
+++ b/nexus/src/test/kotlin/MakeEnv.kt
@@ -36,20 +36,19 @@ val userKeys = EbicsKeys(
  * Cleans up the DB file afterwards.
  */
 fun withTestDatabase(f: () -> Unit) {
-    val dbfile = TEST_DB_CONN
-    File(dbfile).also {
+    File(TEST_DB_FILE).also {
         if (it.exists()) {
             it.delete()
         }
     }
-    Database.connect("jdbc:sqlite:$dbfile")
-    dbDropTables(dbfile)
+    Database.connect("jdbc:sqlite:$TEST_DB_FILE")
+    dbDropTables(TEST_DB_CONN)
     tech.libeufin.sandbox.dbDropTables(TEST_DB_CONN)
     try {
         f()
     }
     finally {
-        File(dbfile).also {
+        File(TEST_DB_FILE).also {
             if (it.exists()) {
                 it.delete()
             }
@@ -189,4 +188,23 @@ fun withNexusAndSandboxUser(f: () -> Unit) {
         prepSandboxDb()
         f()
     }
+}
+
+// Creates tables and the default demobank.
+fun withSandboxTestDatabase(f: () -> Unit) {
+    withTestDatabase {
+        tech.libeufin.sandbox.dbCreateTables(TEST_DB_CONN)
+        transaction {
+            DemobankConfigEntity.new {
+                currency = "TESTKUDOS"
+                bankDebtLimit = 10000
+                usersDebtLimit = 1000
+                allowRegistrations = true
+                name = "default"
+                this.withSignupBonus = false
+                captchaUrl = "http://example.com/"; // unused
+            }
+        }
+        f()
+    }
 }
\ No newline at end of file
diff --git a/nexus/src/test/kotlin/SandboxAccessApiTest.kt 
b/nexus/src/test/kotlin/SandboxAccessApiTest.kt
new file mode 100644
index 00000000..ac230203
--- /dev/null
+++ b/nexus/src/test/kotlin/SandboxAccessApiTest.kt
@@ -0,0 +1,96 @@
+import com.fasterxml.jackson.databind.ObjectMapper
+import io.ktor.client.features.*
+import io.ktor.client.request.*
+import io.ktor.client.statement.*
+import io.ktor.client.utils.*
+import io.ktor.http.*
+import io.ktor.server.testing.*
+import io.netty.handler.codec.http.HttpResponseStatus
+import kotlinx.coroutines.runBlocking
+import org.junit.Test
+import tech.libeufin.sandbox.sandboxApp
+import tech.libeufin.util.buildBasicAuthLine
+
+class SandboxAccessApiTest {
+
+    val mapper = ObjectMapper()
+    @Test
+    fun registerTest() {
+        // Test IBAN conflict detection.
+        withSandboxTestDatabase {
+            withTestApplication(sandboxApp) {
+                runBlocking {
+                    val bodyFoo = mapper.writeValueAsString(object {
+                        val username = "x"
+                        val password = "y"
+                        val iban = FOO_USER_IBAN
+                    })
+                    val bodyBar = mapper.writeValueAsString(object {
+                        val username = "y"
+                        val password = "y"
+                        val iban = FOO_USER_IBAN // conflicts
+                    })
+                    val bodyBaz = mapper.writeValueAsString(object {
+                        val username = "y"
+                        val password = "y"
+                        val iban = BAR_USER_IBAN
+                    })
+                    // The following block would allow to save many LOC,
+                    // but gets somehow ignored.
+                    /*client.config {
+                        this.defaultRequest {
+                            headers {
+                                append(
+                                    HttpHeaders.ContentType,
+                                    ContentType.Application.Json
+                                )
+                            }
+                            expectSuccess = false
+                        }
+                    }*/
+                    // Succeeds.
+                    client.post<HttpResponse>(
+                        urlString = 
"/demobanks/default/access-api/testing/register",
+                    ) {
+                        this.body = bodyFoo
+                        expectSuccess = true
+                        headers {
+                            append(
+                                HttpHeaders.ContentType,
+                                ContentType.Application.Json
+                            )
+                        }
+                    }
+                    // Hits conflict, because of the same IBAN.
+                    val r = client.post<HttpResponse>(
+                        "/demobanks/default/access-api/testing/register"
+                    ) {
+                        this.body = bodyBar
+                        expectSuccess = false
+                        headers {
+                            append(
+                                HttpHeaders.ContentType,
+                                ContentType.Application.Json
+                            )
+                        }
+                    }
+                    assert(r.status.value == 
HttpResponseStatus.CONFLICT.code())
+                    // Succeeds, because of a new IBAN.
+                    client.post<HttpResponse>(
+                        "/demobanks/default/access-api/testing/register"
+                    ) {
+                        this.body = bodyBaz
+                        expectSuccess = true
+                        headers {
+                            append(
+                                HttpHeaders.ContentType,
+                                ContentType.Application.Json
+                            )
+                        }
+                    }
+                }
+            }
+
+        }
+    }
+}
\ No newline at end of file
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
index e93dce8d..cde8e6b4 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -54,7 +54,6 @@ import com.github.ajalt.clikt.parameters.options.*
 import com.github.ajalt.clikt.parameters.types.int
 import execThrowableOrTerminate
 import io.ktor.application.*
-import io.ktor.client.statement.*
 import io.ktor.features.*
 import io.ktor.http.*
 import io.ktor.jackson.*
@@ -1517,7 +1516,7 @@ val sandboxApp: Application.() -> Unit = {
                     // Forbid 'admin' or 'bank' usernames.
                     if (req.username == "bank" || req.username == "admin")
                         throw forbidden("Unallowed username: ${req.username}")
-                    val checkExist = transaction {
+                    val checkCustomerExist = transaction {
                         DemobankCustomerEntity.find {
                             DemobankCustomersTable.username eq req.username
                         }.firstOrNull()
@@ -1526,17 +1525,25 @@ val sandboxApp: Application.() -> Unit = {
                      * Not allowing 'bank' username, as it's been assigned
                      * to the default bank's bank account.
                      */
-                    if (checkExist != null) {
+                    if (checkCustomerExist != null) {
                         throw SandboxError(
                             HttpStatusCode.Conflict,
                             "Username ${req.username} not available."
                         )
                     }
+                    val newIban = req.iban ?: getIban()
+                    // Double-check if IBAN was taken already.
+                    val checkIbanExist = transaction {
+                        BankAccountEntity.find(BankAccountsTable.iban eq 
newIban).firstOrNull()
+                    }
+                    if (checkIbanExist != null)
+                        throw conflict("Proposed IBAN not available.")
+
                     // Create new customer.
                     requireValidResourceName(req.username)
                     val bankAccount = transaction {
                         val bankAccount = BankAccountEntity.new {
-                            iban = req.iban ?: getIban()
+                            iban = newIban
                             /**
                              * For now, keep same semantics of Pybank: a 
username
                              * is AS WELL a bank account label.  In other 
words, it
diff --git a/util/src/main/kotlin/JSON.kt b/util/src/main/kotlin/JSON.kt
index c4a97fcb..e54cdd77 100644
--- a/util/src/main/kotlin/JSON.kt
+++ b/util/src/main/kotlin/JSON.kt
@@ -36,7 +36,7 @@ data class RawPayment(
     val amount: String,
     val currency: String,
     val subject: String,
-    val date: String? = null,
+    val date: String,
     val uid: String, // FIXME: explain this value.
     val direction: String, // FIXME: this following value should be restricted 
to only DBIT/CRDT.
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]