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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* This file is part of LibEuFin.
* Copyright (C) 2023 Taler Systems S.A.
* LibEuFin is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3, or
* (at your option) any later version.
* LibEuFin 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 Affero General
* Public License for more details.
* You should have received a copy of the GNU Affero General Public
* License along with LibEuFin; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>
*/
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.json.JsonObject
import tech.libeufin.bank.BankAccountCreateWithdrawalResponse
import tech.libeufin.bank.WithdrawalStatus
import tech.libeufin.common.*
import kotlin.test.assertEquals
// Test endpoint is correctly authenticated
suspend fun ApplicationTestBuilder.authRoutine(
method: HttpMethod,
path: String,
body: JsonObject? = null,
requireExchange: Boolean = false,
requireAdmin: Boolean = false,
allowAdmin: Boolean = false
) {
// No body when authentication must happen before parsing the body
// Unknown account
client.request(path) {
this.method = method
basicAuth("unknown", "password")
}.assertUnauthorized()
// Wrong password
client.request(path) {
this.method = method
basicAuth("merchant", "wrong-password")
}.assertUnauthorized()
// Wrong account
client.request(path) {
this.method = method
basicAuth("exchange", "merchant-password")
}.assertUnauthorized()
if (requireAdmin) {
// Not exchange account
client.request(path) {
this.method = method
pwAuth("merchant")
}.assertUnauthorized()
} else if (!allowAdmin) {
// Check no admin
client.request(path) {
this.method = method
pwAuth("admin")
}.assertUnauthorized()
}
if (requireExchange) {
// Not exchange account
client.request(path) {
this.method = method
if (body != null) json(body)
pwAuth(if (requireAdmin) "admin" else "merchant")
}.assertConflict(TalerErrorCode.BANK_ACCOUNT_IS_NOT_EXCHANGE)
}
}
suspend inline fun <reified B> ApplicationTestBuilder.historyRoutine(
url: String,
crossinline ids: (B) -> List<Long>,
registered: List<suspend () -> Unit>,
ignored: List<suspend () -> Unit> = listOf(),
polling: Boolean = true,
auth: String? = null
) {
// Get history
val history: suspend (String) -> HttpResponse = { params: String ->
client.get("$url?$params") {
pwAuth(auth)
}
}
// Check history is following specs
val assertHistory: suspend HttpResponse.(Int) -> Unit = { size: Int ->
assertHistoryIds<B>(size, ids)
}
// Get latest registered id
val latestId: suspend () -> Long = {
history("delta=-1").assertOkJson<B>().run { ids(this)[0] }
}
// Check error when no transactions
history("delta=7").assertNoContent()
// Run interleaved registered and ignore transactions
val registered_iter = registered.iterator()
val ignored_iter = ignored.iterator()
while (registered_iter.hasNext() || ignored_iter.hasNext()) {
if (registered_iter.hasNext()) registered_iter.next()()
if (ignored_iter.hasNext()) ignored_iter.next()()
}
val nbRegistered = registered.size
val nbIgnored = ignored.size
val nbTotal = nbRegistered + nbIgnored
// Check ignored
history("delta=$nbTotal").assertHistory(nbRegistered)
// Check skip ignored
history("delta=$nbRegistered").assertHistory(nbRegistered)
if (polling) {
// Check no polling when we cannot have more transactions
assertTime(0, 100) {
history("delta=-${nbRegistered+1}&long_poll_ms=1000")
.assertHistory(nbRegistered)
}
// Check no polling when already find transactions even if less than delta
assertTime(0, 100) {
history("delta=${nbRegistered+1}&long_poll_ms=1000")
.assertHistory(nbRegistered)
}
// Check polling
coroutineScope {
val id = latestId()
launch { // Check polling succeed
assertTime(100, 200) {
history("delta=2&start=$id&long_poll_ms=1000")
.assertHistory(1)
}
}
launch { // Check polling timeout
assertTime(200, 300) {
history("delta=1&start=${id+nbTotal*3}&long_poll_ms=200")
.assertNoContent()
}
}
delay(100)
registered[0]()
}
// Test triggers
for (register in registered) {
coroutineScope {
val id = latestId()
launch {
assertTime(100, 200) {
history("delta=7&start=$id&long_poll_ms=1000")
.assertHistory(1)
}
}
delay(100)
register()
}
}
// Test doesn't trigger
coroutineScope {
val id = latestId()
launch {
assertTime(200, 300) {
history("delta=7&start=$id&long_poll_ms=200")
.assertNoContent()
}
}
delay(100)
for (ignore in ignored) {
ignore()
}
}
}
// Testing ranges.
repeat(20) {
registered[0]()
}
val id = latestId()
// Default
history("").assertHistory(20)
// forward range:
history("delta=10").assertHistory(10)
history("delta=10&start=4").assertHistory(10)
// backward range:
history("delta=-10").assertHistory(10)
history("delta=-10&start=${id-4}").assertHistory(10)
}
suspend inline fun <reified B> ApplicationTestBuilder.statusRoutine(
url: String,
crossinline status: (B) -> WithdrawalStatus
) {
val amount = TalerAmount("KUDOS:9.0")
client.postA("/accounts/customer/withdrawals") {
json { "amount" to amount }
}.assertOkJson<BankAccountCreateWithdrawalResponse> { resp ->
val aborted_uuid = resp.taler_withdraw_uri.split("/").last()
val confirmed_uuid = client.postA("/accounts/customer/withdrawals") {
json { "amount" to amount }
}.assertOkJson<BankAccountCreateWithdrawalResponse>()
.taler_withdraw_uri.split("/").last()
// Check no useless polling
assertTime(0, 100) {
client.get("$url/$confirmed_uuid?long_poll_ms=1000&old_state=selected")
.assertOkJson<B> { assertEquals(WithdrawalStatus.pending, status(it)) }
}
// Polling selected
coroutineScope {
launch { // Check polling succeed
assertTime(100, 200) {
client.get("$url/$confirmed_uuid?long_poll_ms=1000")
.assertOkJson<B> { assertEquals(WithdrawalStatus.selected, status(it)) }
}
}
launch { // Check polling succeed
assertTime(100, 200) {
client.get("$url/$aborted_uuid?long_poll_ms=1000")
.assertOkJson<B> { assertEquals(WithdrawalStatus.selected, status(it)) }
}
}
delay(100)
withdrawalSelect(confirmed_uuid)
withdrawalSelect(aborted_uuid)
}
// Polling confirmed
coroutineScope {
launch { // Check polling succeed
assertTime(100, 200) {
client.get("$url/$confirmed_uuid?long_poll_ms=1000&old_state=selected")
.assertOkJson<B> { assertEquals(WithdrawalStatus.confirmed, status(it))}
}
}
launch { // Check polling timeout
assertTime(200, 300) {
client.get("$url/$aborted_uuid?long_poll_ms=200&old_state=selected")
.assertOkJson<B> { assertEquals(WithdrawalStatus.selected, status(it)) }
}
}
delay(100)
client.postA("/accounts/customer/withdrawals/$confirmed_uuid/confirm").assertNoContent()
}
// Polling abort
coroutineScope {
launch {
assertTime(200, 300) {
client.get("$url/$confirmed_uuid?long_poll_ms=200&old_state=confirmed")
.assertOkJson<B> { assertEquals(WithdrawalStatus.confirmed, status(it))}
}
}
launch {
assertTime(100, 200) {
client.get("$url/$aborted_uuid?long_poll_ms=1000&old_state=selected")
.assertOkJson<B> { assertEquals(WithdrawalStatus.aborted, status(it)) }
}
}
delay(100)
client.post("/taler-integration/withdrawal-operation/$aborted_uuid/abort").assertNoContent()
}
}
}
|