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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/*
This file is part of TALER
Copyright (C) 2020 Taler Systems SA
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.
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 TALER; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>
*/
/**
* @file testing_api_cmd_get_orders.c
* @brief command to test GET /orders
* @author Jonathan Buchanan
*/
#include "platform.h"
#include <taler/taler_exchange_service.h>
#include <taler/taler_testing_lib.h>
#include "taler_merchant_service.h"
#include "taler_merchant_testing_lib.h"
/**
* State of a "GET orders" CMD.
*/
struct GetOrdersState
{
/**
* Handle for a "GET orders" request.
*/
struct TALER_MERCHANT_OrdersGetHandle *ogh;
/**
* The interpreter state.
*/
struct TALER_TESTING_Interpreter *is;
/**
* Base URL of the merchant serving the request.
*/
const char *merchant_url;
/**
* Expected HTTP response code.
*/
unsigned int http_status;
/**
* A NULL-terminated array of CMD labels that created orders.
*/
const char **orders;
/**
* The length of @e orders.
*/
unsigned int orders_length;
};
/**
* Callback for a GET /orders operation.
*
* @param cls closure for this function
* @param hr HTTP response
* @param orders_length how many orders are returned
* @param orders all the orders' details
*/
static void
get_orders_cb (void *cls,
const struct TALER_MERCHANT_HttpResponse *hr,
unsigned int orders_length,
const struct TALER_MERCHANT_OrderEntry orders[])
{
struct GetOrdersState *gos = cls;
gos->ogh = NULL;
if (gos->http_status != hr->http_status)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unexpected response code %u (%d) to command %s\n",
hr->http_status,
(int) hr->ec,
TALER_TESTING_interpreter_get_current_label (gos->is));
TALER_TESTING_interpreter_fail (gos->is);
return;
}
switch (hr->http_status)
{
case MHD_HTTP_OK:
if (orders_length != gos->orders_length)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Number of orders found does not match\n");
TALER_TESTING_interpreter_fail (gos->is);
return;
}
for (unsigned int i = 0; i < orders_length; ++i)
{
const struct TALER_TESTING_Command *order_cmd;
order_cmd = TALER_TESTING_interpreter_lookup_command (
gos->is,
gos->orders[i]);
{
const char *order_id;
if (GNUNET_OK !=
TALER_TESTING_get_trait_order_id (order_cmd,
0,
&order_id))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Could not fetch order id\n");
TALER_TESTING_interpreter_fail (gos->is);
return;
}
if (0 != strcmp (orders[i].order_id,
order_id))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Order id does not match\n");
TALER_TESTING_interpreter_fail (gos->is);
return;
}
}
{
const json_t *contract_terms;
struct TALER_Amount amount;
const char *summary;
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_string ("summary",
&summary),
TALER_JSON_spec_amount ("amount",
&amount),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
TALER_TESTING_get_trait_contract_terms (order_cmd,
0,
&contract_terms))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Could not fetch order contract terms\n");
TALER_TESTING_interpreter_fail (gos->is);
return;
}
if (GNUNET_OK !=
GNUNET_JSON_parse (contract_terms,
spec,
NULL, NULL))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Could not parse order contract terms\n");
TALER_TESTING_interpreter_fail (gos->is);
return;
}
if ((0 != strcmp (summary,
orders[i].summary)) ||
(GNUNET_OK != TALER_amount_cmp_currency (&amount,
&orders[i].amount)) ||
(0 != TALER_amount_cmp (&amount,
&orders[i].amount)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Order summary and/or amount does not match\n");
TALER_TESTING_interpreter_fail (gos->is);
return;
}
}
}
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Unhandled HTTP status.\n");
}
TALER_TESTING_interpreter_next (gos->is);
}
/**
* Run the "GET /orders" CMD.
*
*
* @param cls closure.
* @param cmd command being run now.
* @param is interpreter state.
*/
static void
get_orders_run (void *cls,
const struct TALER_TESTING_Command *cmd,
struct TALER_TESTING_Interpreter *is)
{
struct GetOrdersState *gos = cls;
gos->is = is;
gos->ogh = TALER_MERCHANT_orders_get (is->ctx,
gos->merchant_url,
&get_orders_cb,
gos);
GNUNET_assert (NULL != gos->ogh);
}
/**
* Free the state of a "GET orders" CMD, and possibly
* cancel a pending operation thereof.
*
* @param cls closure.
* @param cmd command being run.
*/
static void
get_orders_cleanup (void *cls,
const struct TALER_TESTING_Command *cmd)
{
struct GetOrdersState *gos = cls;
if (NULL != gos->ogh)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"GET /orders operation did not complete\n");
TALER_MERCHANT_orders_get_cancel (gos->ogh);
}
GNUNET_array_grow (gos->orders,
gos->orders_length,
0);
GNUNET_free (gos);
}
struct TALER_TESTING_Command
TALER_TESTING_cmd_merchant_get_orders (const char *label,
const char *merchant_url,
unsigned int http_status,
...)
{
struct GetOrdersState *gos;
gos = GNUNET_new (struct GetOrdersState);
gos->merchant_url = merchant_url;
gos->http_status = http_status;
{
const char *clabel;
va_list ap;
va_start (ap, http_status);
while (NULL != (clabel = va_arg (ap, const char *)))
{
GNUNET_array_append (gos->orders,
gos->orders_length,
clabel);
}
va_end (ap);
}
{
struct TALER_TESTING_Command cmd = {
.cls = gos,
.label = label,
.run = &get_orders_run,
.cleanup = &get_orders_cleanup
};
return cmd;
}
}
struct MerchantPollOrdersConcludeState
{
/**
* The interpreter state.
*/
struct TALER_TESTING_Interpreter *is;
/**
* Reference to a command that can provide a poll orders start command.
*/
const char *start_reference;
/**
* Task to wait for the deadline.
*/
struct GNUNET_SCHEDULER_Task *task;
/**
* Expected HTTP response status code.
*/
unsigned int expected_http_status;
};
struct MerchantPollOrdersStartState
{
/**
* The merchant base URL.
*/
const char *merchant_url;
/**
* The handle to the current GET /private/orders request.
*/
struct TALER_MERCHANT_OrdersGetHandle *ogh;
/**
* The interpreter state.
*/
struct TALER_TESTING_Interpreter *is;
/**
* How long to wait for server to return a response.
*/
struct GNUNET_TIME_Relative timeout;
/**
* Conclude state waiting for completion (if any).
*/
struct MerchantPollOrdersConcludeState *cs;
/**
* The HTTP status code returned by the backend.
*/
unsigned int http_status;
/**
* When the request should be completed by.
*/
struct GNUNET_TIME_Absolute deadline;
};
/**
* Task called when either the timeout for the get orders
* command expired or we got a response. Checks if the
* result is what we expected.
*
* @param cls a `struct MerchantPollOrdersConcludeState`
*/
static void
conclude_task (void *cls)
{
struct MerchantPollOrdersConcludeState *poc = cls;
const struct TALER_TESTING_Command *poll_cmd;
struct MerchantPollOrdersStartState *pos;
struct GNUNET_TIME_Absolute now;
poc->task = NULL;
poll_cmd =
TALER_TESTING_interpreter_lookup_command (poc->is,
poc->start_reference);
if (NULL == poll_cmd)
TALER_TESTING_FAIL (poc->is);
pos = poll_cmd->cls;
if (NULL != pos->ogh)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Expected poll GET /private/orders to have completed, but it did not!\n");
TALER_TESTING_FAIL (poc->is);
}
if (pos->http_status != poc->expected_http_status)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Expected HTTP status %u, got %u\n",
poc->expected_http_status,
pos->http_status);
TALER_TESTING_FAIL (poc->is);
}
now = GNUNET_TIME_absolute_get ();
if ((GNUNET_TIME_absolute_add (pos->deadline,
GNUNET_TIME_UNIT_SECONDS).abs_value_us <
now.abs_value_us) )
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Expected answer to be delayed until %llu, but got response at %llu\n",
(unsigned long long) pos->deadline.abs_value_us,
(unsigned long long) now.abs_value_us);
TALER_TESTING_FAIL (poc->is);
}
TALER_TESTING_interpreter_next (poc->is);
}
/**
* Callback to process a GET /orders request
*
* @param cls closure
* @param hr HTTP response details
* @param orders_length how many orders are returned
* @param orders the returned orders
*/
static void
merchant_poll_orders_cb (
void *cls,
const struct TALER_MERCHANT_HttpResponse *hr,
unsigned int orders_length,
const struct TALER_MERCHANT_OrderEntry orders[])
{
/* FIXME, deeper checks should be implemented here. */
struct MerchantPollOrdersStartState *pos = cls;
pos->ogh = NULL;
if (MHD_HTTP_OK != hr->http_status)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unexpected response code %u (%d) to command %s\n",
hr->http_status,
(int) hr->ec,
TALER_TESTING_interpreter_get_current_label (pos->is));
TALER_TESTING_interpreter_fail (pos->is);
return;
}
switch (hr->http_status)
{
case MHD_HTTP_OK:
// FIXME: use order references
// check if the data returned matches that from the POST / PATCH
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Unhandled HTTP status.\n");
}
pos->http_status = hr->http_status;
if (NULL != pos->cs)
{
GNUNET_SCHEDULER_cancel (pos->cs->task);
pos->cs->task = GNUNET_SCHEDULER_add_now (&conclude_task,
pos->cs);
}
}
/**
* Run the "GET orders" CMD.
*
* @param cls closure.
* @param cmd command being run now.
* @param is interpreter state.
*/
static void
merchant_poll_orders_start_run (void *cls,
const struct TALER_TESTING_Command *cmd,
struct TALER_TESTING_Interpreter *is)
{
struct MerchantPollOrdersStartState *pos = cls;
/* add 1s grace time to timeout */
pos->deadline
= GNUNET_TIME_absolute_add (GNUNET_TIME_relative_to_absolute (pos->timeout),
GNUNET_TIME_UNIT_SECONDS);
pos->is = is;
pos->ogh = TALER_MERCHANT_orders_get2 (is->ctx,
pos->merchant_url,
TALER_EXCHANGE_YNA_ALL,
TALER_EXCHANGE_YNA_ALL,
TALER_EXCHANGE_YNA_ALL,
GNUNET_TIME_UNIT_ZERO_ABS,
1,
2,
pos->timeout,
&merchant_poll_orders_cb,
pos);
GNUNET_assert (NULL != pos->ogh);
/* We CONTINUE to run the interpreter while the long-polled command
completes asynchronously! */
TALER_TESTING_interpreter_next (pos->is);
}
/**
* Free the state of a "GET orders" CMD, and possibly
* cancel a pending operation thereof.
*
* @param cls closure.
* @param cmd command being run.
*/
static void
merchant_poll_orders_start_cleanup (void *cls,
const struct TALER_TESTING_Command *cmd)
{
struct MerchantPollOrdersStartState *pos = cls;
if (NULL != pos->ogh)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Command `%s' was not terminated\n",
TALER_TESTING_interpreter_get_current_label (
pos->is));
TALER_MERCHANT_orders_get_cancel (pos->ogh);
}
GNUNET_free (pos);
}
/**
* Start a long poll for GET /private/orders.
*/
struct TALER_TESTING_Command
TALER_TESTING_cmd_poll_orders_start (const char *label,
const char *merchant_url,
struct GNUNET_TIME_Relative timeout)
{
struct MerchantPollOrdersStartState *pos;
pos = GNUNET_new (struct MerchantPollOrdersStartState);
pos->merchant_url = merchant_url;
pos->timeout = timeout;
{
struct TALER_TESTING_Command cmd = {
.cls = pos,
.label = label,
.run = &merchant_poll_orders_start_run,
.cleanup = &merchant_poll_orders_start_cleanup
};
return cmd;
}
}
/**
* Run the "GET orders" CMD.
*
* @param cls closure.
* @param cmd command being run now.
* @param is interpreter state.
*/
static void
merchant_poll_orders_conclude_run (void *cls,
const struct TALER_TESTING_Command *cmd,
struct TALER_TESTING_Interpreter *is)
{
struct MerchantPollOrdersConcludeState *poc = cls;
const struct TALER_TESTING_Command *poll_cmd;
struct MerchantPollOrdersStartState *pos;
poc->is = is;
poll_cmd =
TALER_TESTING_interpreter_lookup_command (is,
poc->start_reference);
if (NULL == poll_cmd)
TALER_TESTING_FAIL (poc->is);
GNUNET_assert (poll_cmd->run == &merchant_poll_orders_start_run);
pos = poll_cmd->cls;
pos->cs = poc;
if (NULL == pos->ogh)
poc->task = GNUNET_SCHEDULER_add_now (&conclude_task,
poc);
else
poc->task = GNUNET_SCHEDULER_add_at (pos->deadline,
&conclude_task,
poc);
}
/**
* Free the state of a "GET orders" CMD, and possibly
* cancel a pending operation thereof.
*
* @param cls closure.
* @param cmd command being run.
*/
static void
merchant_poll_orders_conclude_cleanup (void *cls,
const struct TALER_TESTING_Command *cmd)
{
struct MerchantPollOrdersConcludeState *poc = cls;
if (NULL != poc->task)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Command `%s' was not terminated\n",
TALER_TESTING_interpreter_get_current_label (
poc->is));
GNUNET_SCHEDULER_cancel (poc->task);
poc->task = NULL;
}
GNUNET_free (poc);
}
/**
* Complete a long poll for GET /private/orders.
*/
struct TALER_TESTING_Command
TALER_TESTING_cmd_poll_orders_conclude (const char *label,
unsigned int http_status,
const char *poll_start_reference)
{
struct MerchantPollOrdersConcludeState *poc;
poc = GNUNET_new (struct MerchantPollOrdersConcludeState);
poc->start_reference = poll_start_reference;
poc->expected_http_status = http_status;
{
struct TALER_TESTING_Command cmd = {
.cls = poc,
.label = label,
.run = &merchant_poll_orders_conclude_run,
.cleanup = &merchant_poll_orders_conclude_cleanup
};
return cmd;
}
}
/* end of testing_api_cmd_get_orders.c */
|