summaryrefslogtreecommitdiff
path: root/nexus/src/test/kotlin/NexusApiTest.kt
diff options
context:
space:
mode:
authorMS <ms@taler.net>2023-04-16 09:19:46 +0200
committerMS <ms@taler.net>2023-04-16 10:13:54 +0200
commitd7cecd35a5f7ab3ab2491e4c4ad4f07e041e9944 (patch)
tree56ab7ef93a92a0f3f743c53c10ec56db9425f37b /nexus/src/test/kotlin/NexusApiTest.kt
parent1602c0b6cd3cad8d8b8f14d68f509842e72146b6 (diff)
downloadlibeufin-d7cecd35a5f7ab3ab2491e4c4ad4f07e041e9944.tar.gz
libeufin-d7cecd35a5f7ab3ab2491e4c4ad4f07e041e9944.tar.bz2
libeufin-d7cecd35a5f7ab3ab2491e4c4ad4f07e041e9944.zip
Conversion service.
Implementing the cash-out monitor. The monitor watches one particular bank account (the admin's by default) and submits a fiat payment initiation to Nexus upon every new incoming transaction. Also implementing idempotence for payment initiations at Nexus. This helps in case the cash-out monitor fails at keeping track of the submitted payments and accidentally submits multiple times the same payment.
Diffstat (limited to 'nexus/src/test/kotlin/NexusApiTest.kt')
-rw-r--r--nexus/src/test/kotlin/NexusApiTest.kt60
1 files changed, 60 insertions, 0 deletions
diff --git a/nexus/src/test/kotlin/NexusApiTest.kt b/nexus/src/test/kotlin/NexusApiTest.kt
index 1b5caaec..d1ccad47 100644
--- a/nexus/src/test/kotlin/NexusApiTest.kt
+++ b/nexus/src/test/kotlin/NexusApiTest.kt
@@ -1,14 +1,18 @@
+import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
+import io.netty.handler.codec.http.HttpResponseStatus
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.runBlocking
+import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Test
+import tech.libeufin.nexus.PaymentInitiationEntity
import tech.libeufin.nexus.server.nexusApp
/**
@@ -16,6 +20,7 @@ import tech.libeufin.nexus.server.nexusApp
* documented here: https://docs.taler.net/libeufin/api-nexus.html
*/
class NexusApiTest {
+ private val jMapper = ObjectMapper()
// Testing long-polling on GET /transactions
@Test
fun getTransactions() {
@@ -102,4 +107,59 @@ class NexusApiTest {
}
}
}
+ /**
+ * Testing the idempotence of payment submissions. That
+ * helps Sandbox not to create multiple payment initiations
+ * in case it fails at keeping track of what it submitted
+ * already.
+ */
+ @Test
+ fun paymentInitIdempotence() {
+ withTestDatabase {
+ prepNexusDb()
+ testApplication {
+ application(nexusApp)
+ // Check no pay. ini. exist.
+ transaction { PaymentInitiationEntity.all().count() == 0L }
+ // Create one.
+ fun f(futureThis: HttpRequestBuilder, subject: String = "idempotence pay. init. test") {
+ futureThis.basicAuth("foo", "foo")
+ futureThis.expectSuccess = true
+ futureThis.contentType(ContentType.Application.Json)
+ futureThis.setBody("""
+ {"iban": "TESTIBAN",
+ "bic": "SANDBOXX",
+ "name": "TEST NAME",
+ "amount": "TESTKUDOS:3",
+ "subject": "$subject",
+ "uid": "salt"
+ }
+ """.trimIndent())
+ }
+ val R = client.post("/bank-accounts/foo/payment-initiations") { f(this) }
+ println(jMapper.readTree(R.bodyAsText()).get("uuid"))
+ // Submit again
+ client.post("/bank-accounts/foo/payment-initiations") { f(this) }
+ // Checking that Nexus serves it.
+ client.get("/bank-accounts/foo/payment-initiations/1") {
+ basicAuth("foo", "foo")
+ expectSuccess = true
+ }
+ // Checking that the database has only one, despite the double submission.
+ transaction {
+ assert(PaymentInitiationEntity.all().count() == 1L)
+ }
+ /**
+ * Causing a conflict by changing one payment detail
+ * (the subject in this case) but not the "uid".
+ */
+ val maybeConflict = client.post("/bank-accounts/foo/payment-initiations") {
+ f(this, "different-subject")
+ expectSuccess = false
+ }
+ assert(maybeConflict.status.value == HttpStatusCode.Conflict.value)
+
+ }
+ }
+ }
} \ No newline at end of file