summaryrefslogtreecommitdiff
path: root/bank/src/test/kotlin/BankIntegrationApiTest.kt
blob: 9f8726a626747a28491e8d31fab9ed96948e8028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.HttpClient
import io.ktor.http.*
import io.ktor.server.testing.*
import kotlinx.serialization.json.*
import kotlinx.coroutines.*
import net.taler.wallet.crypto.Base32Crockford
import org.junit.Test
import tech.libeufin.bank.*
import tech.libeufin.util.CryptoUtil
import tech.libeufin.util.stripIbanPayto
import java.util.*
import java.time.Instant
import kotlin.test.*
import randHashCode

class BankIntegrationApiTest {
    // Selecting withdrawal details from the Integration API endpoint.
    @Test
    fun intSelect() = bankSetup { db ->
        val uuid = UUID.randomUUID()
        // insert new.
        assertEquals(WithdrawalCreationResult.SUCCESS, db.talerWithdrawalCreate(
            opUUID = uuid,
            walletAccountUsername = "merchant",
            amount = TalerAmount(1, 0, "KUDOS")
        ))
       
        val r = client.post("/taler-integration/withdrawal-operation/${uuid}") {
            jsonBody(BankWithdrawalOperationPostRequest(
                reserve_pub = randEddsaPublicKey(),
                selected_exchange = IbanPayTo("payto://iban/ABC123")
            ))
        }.assertOk()
        println(r.bodyAsText())
    }

    // Showing withdrawal details from the Integrtion API endpoint.
    @Test
    fun intGet() = bankSetup { db ->
        val uuid = UUID.randomUUID()
        // insert new.
        assertEquals(WithdrawalCreationResult.SUCCESS, db.talerWithdrawalCreate(
            opUUID = uuid,
            walletAccountUsername = "merchant",
            amount = TalerAmount(1, 0, "KUDOS")
        ))

        val r = client.get("/taler-integration/withdrawal-operation/${uuid}").assertOk()
        println(r.bodyAsText())
    }

    // Testing withdrawal abort
    @Test
    fun withdrawalAbort() = bankSetup { db ->
        val uuid = UUID.randomUUID()
        // insert new.
        assertEquals(WithdrawalCreationResult.SUCCESS, db.talerWithdrawalCreate(
            opUUID = uuid,
            walletAccountUsername = "merchant",
            amount = TalerAmount(1, 0, "KUDOS")
        ))
        val op = db.talerWithdrawalGet(uuid)
        assert(op?.aborted == false)
        assert(db.talerWithdrawalSetDetails(uuid, IbanPayTo("payto://iban/exchange-payto"), randEddsaPublicKey()))
       
        client.post("/withdrawals/${uuid}/abort") {
            basicAuth("merchant", "merchant-password")
        }.assertOk()
        
        val opAbo = db.talerWithdrawalGet(uuid)
        assert(opAbo?.aborted == true && opAbo.selectionDone == true)
    }

    // Testing withdrawal creation
    @Test
    fun withdrawalCreation() = bankSetup { _ ->
        // Creating the withdrawal as if the SPA did it.
        val r = client.post("/accounts/merchant/withdrawals") {
            basicAuth("merchant", "merchant-password")
            jsonBody(BankAccountCreateWithdrawalRequest(TalerAmount(value = 9, frac = 0, currency = "KUDOS"))) 
        }.assertOk()
        val opId = Json.decodeFromString<BankAccountCreateWithdrawalResponse>(r.bodyAsText())
        // Getting the withdrawal from the bank.  Throws (failing the test) if not found.
        client.get("/withdrawals/${opId.withdrawal_id}") {
            basicAuth("merchant", "merchant-password")
        }.assertOk()
    }

    // Testing withdrawal confirmation
    @Test
    fun withdrawalConfirmation() = bankSetup { db -> 
        // Artificially making a withdrawal operation for merchant.
        val uuid = UUID.randomUUID()
        assertEquals(WithdrawalCreationResult.SUCCESS, db.talerWithdrawalCreate(
            opUUID = uuid,
            walletAccountUsername = "merchant",
            amount = TalerAmount(1, 0, "KUDOS")
        ))
        // Specifying the exchange via its Payto URI.
        assert(db.talerWithdrawalSetDetails(
            opUuid = uuid,
            exchangePayto = IbanPayTo("payto://iban/EXCHANGE-IBAN-XYZ"),
            reservePub = randEddsaPublicKey()
        ))

        // Starting the bank and POSTing as Foo to /confirm the operation.
        client.post("/withdrawals/${uuid}/confirm") {
            basicAuth("merchant", "merchant-password")
        }.assertOk()
    }

    // Testing the generation of taler://withdraw-URIs.
    @Test
    fun testWithdrawUri() {
        // Checking the taler+http://-style.
        val withHttp = getTalerWithdrawUri(
            "http://example.com",
            "my-id"
        )
        assertEquals(withHttp, "taler+http://withdraw/example.com/taler-integration/my-id")
        // Checking the taler://-style
        val onlyTaler = getTalerWithdrawUri(
            "https://example.com/",
            "my-id"
        )
        // Note: this tests as well that no double slashes belong to the result
        assertEquals(onlyTaler, "taler://withdraw/example.com/taler-integration/my-id")
        // Checking the removal of subsequent slashes
        val manySlashes = getTalerWithdrawUri(
            "https://www.example.com//////",
            "my-id"
        )
        assertEquals(manySlashes, "taler://withdraw/www.example.com/taler-integration/my-id")
        // Checking with specified port number
        val withPort = getTalerWithdrawUri(
            "https://www.example.com:9876",
            "my-id"
        )
        assertEquals(withPort, "taler://withdraw/www.example.com:9876/taler-integration/my-id")
    }
}