summaryrefslogtreecommitdiff
path: root/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt
blob: 437697b69a61e3cbce0d9035be542b7104883136 (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
/*
 * This file is part of GNU Taler
 * (C) 2020 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.merchantlib

import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.runBlocking
import net.taler.common.Amount
import net.taler.common.ContractProduct
import net.taler.common.ContractTerms
import net.taler.merchantlib.MockHttpClient.giveJsonResponse
import net.taler.merchantlib.MockHttpClient.httpClient
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class MerchantApiTest {

    private val api = MerchantApi(httpClient)
    private val merchantConfig = MerchantConfig(
        baseUrl = "http://example.net/",
        instance = "testInstance",
        apiKey = "apiKeyFooBar"
    )

    @Test
    fun testGetConfig() = runBlocking {
        httpClient.giveJsonResponse("https://backend.int.taler.net/config") {
            """
            {
              "currency": "INTKUDOS",
              "version": "0:0:0"
            }
            """.trimIndent()
        }
        api.getConfig("https://backend.int.taler.net").assertSuccess {
            assertEquals(ConfigResponse("0:0:0", "INTKUDOS"), it)
        }
    }

    @Test
    fun testPostOrder() = runBlocking {
        val product = ContractProduct(
            productId = "foo",
            description = "bar",
            price = Amount("TEST", 1, 0),
            quantity = 2
        )
        val contractTerms = ContractTerms(
            summary = "test",
            amount = Amount("TEST", 2, 1),
            fulfillmentUrl = "http://example.org",
            products = listOf(product)
        )
        val request = PostOrderRequest(contractTerms)
        val contractTermsJson = """
            {
                "order": {
                    "summary": "${contractTerms.summary}",
                    "amount": "${contractTerms.amount.toJSONString()}",
                    "fulfillment_url": "${contractTerms.fulfillmentUrl}",
                    "products": [
                        {
                            "product_id": "${product.productId}",
                            "description": "${product.description}",
                            "price": "${product.price.toJSONString()}",
                            "quantity": ${product.quantity}
                        }
                    ]
                }
            }
        """.trimIndent()
        httpClient.giveJsonResponse(
            "http://example.net/instances/testInstance/private/orders",
            contractTermsJson
        ) {
            """{"order_id": "test"}"""
        }
        api.postOrder(merchantConfig, request).assertSuccess {
            assertEquals(PostOrderResponse("test"), it)
        }

        httpClient.giveJsonResponse(
            "http://example.net/instances/testInstance/private/orders",
            statusCode = HttpStatusCode.NotFound
        ) {
            """{
                "code": 2000,
                "hint": "merchant instance unknown"
            }"""
        }
        api.postOrder(merchantConfig, request).assertFailure {
            assertTrue(it.contains("2000"))
            assertTrue(it.contains("merchant instance unknown"))
        }
    }

    @Test
    fun testCheckOrder() = runBlocking {
        val orderId = "orderIdFoo"
        val unpaidResponse = CheckPaymentResponse.Unpaid(false, "http://taler.net/foo")
        httpClient.giveJsonResponse("http://example.net/instances/testInstance/private/orders/$orderId") {
            """{
                "order_status": "unpaid",
                "paid": ${unpaidResponse.paid},
                "taler_pay_uri": "${unpaidResponse.talerPayUri}"
            }""".trimIndent()
        }
        api.checkOrder(merchantConfig, orderId).assertSuccess {
            assertEquals(unpaidResponse, it)
        }

        httpClient.giveJsonResponse(
            "http://example.net/instances/testInstance/private/orders/$orderId",
            statusCode = HttpStatusCode.NotFound
        ) {
            """{
                "code": 2909,
                "hint": "Did not find contract terms for order in DB"
            }"""
        }
        api.checkOrder(merchantConfig, orderId).assertFailure {
            assertTrue(it.contains("2909"))
            assertTrue(it.contains("Did not find contract terms for order in DB"))
        }
    }

}