Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing_api_cmd_merchant_get_tip.c
21 : * @brief command to test GET /private/tips/$TIP_ID.
22 : * @author Jonathan Buchanan
23 : */
24 : #include "platform.h"
25 : #include <taler/taler_exchange_service.h>
26 : #include <taler/taler_testing_lib.h>
27 : #include "taler_merchant_service.h"
28 : #include "taler_merchant_testing_lib.h"
29 :
30 : /**
31 : * State for a GET /private/tips/$TIP_ID CMD.
32 : */
33 : struct MerchantTipGetState
34 : {
35 :
36 : /**
37 : * The merchant base URL.
38 : */
39 : const char *merchant_url;
40 :
41 : /**
42 : * Expected HTTP response code for this CMD.
43 : */
44 : unsigned int http_status;
45 :
46 : /**
47 : * Whether to fetch and compare pickups.
48 : */
49 : bool fetch_pickups;
50 :
51 : /**
52 : * The length of @e pickups.
53 : */
54 : unsigned int pickups_length;
55 :
56 : /**
57 : * The NULL-terminated list of pickup commands associated with the tip.
58 : */
59 : const char **pickups;
60 :
61 : /**
62 : * The handle to the current GET /tips/$TIP_ID request.
63 : */
64 : struct TALER_MERCHANT_TipMerchantGetHandle *tgh;
65 :
66 : /**
67 : * The interpreter state.
68 : */
69 : struct TALER_TESTING_Interpreter *is;
70 :
71 : /**
72 : * Reference to a command that created a tip.
73 : */
74 : const char *tip_reference;
75 : };
76 :
77 :
78 : /**
79 : * Callback for a GET /private/tips/$TIP_ID operation.
80 : *
81 : * @param cls closure for this function
82 : * @param tsr response
83 : */
84 : static void
85 0 : merchant_get_tip_cb (void *cls,
86 : const struct TALER_MERCHANT_TipStatusResponse *tsr)
87 : {
88 0 : struct MerchantTipGetState *gts = cls;
89 : const struct TALER_TESTING_Command *authorize_cmd;
90 : struct TALER_Amount expected_total_picked_up;
91 :
92 0 : authorize_cmd = TALER_TESTING_interpreter_lookup_command (gts->is,
93 : gts->tip_reference);
94 :
95 0 : gts->tgh = NULL;
96 0 : if (gts->http_status != tsr->hr.http_status)
97 : {
98 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 : "Unexpected response code %u (%d) to command %s\n",
100 : tsr->hr.http_status,
101 : (int) tsr->hr.ec,
102 : TALER_TESTING_interpreter_get_current_label (gts->is));
103 0 : TALER_TESTING_interpreter_fail (gts->is);
104 0 : return;
105 : }
106 0 : switch (tsr->hr.http_status)
107 : {
108 0 : case MHD_HTTP_OK:
109 : {
110 : const struct TALER_Amount *initial_amount;
111 :
112 0 : GNUNET_assert (GNUNET_OK ==
113 : TALER_amount_set_zero (tsr->details.success.total_picked_up.currency,
114 : &expected_total_picked_up));
115 0 : if (GNUNET_OK !=
116 0 : TALER_TESTING_get_trait_amount (authorize_cmd,
117 : &initial_amount))
118 0 : TALER_TESTING_FAIL (gts->is);
119 0 : if ((GNUNET_OK !=
120 0 : TALER_amount_cmp_currency (&tsr->details.success.total_authorized,
121 0 : initial_amount)) ||
122 0 : (0 != TALER_amount_cmp (&tsr->details.success.total_authorized,
123 : initial_amount)))
124 : {
125 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126 : "Tip authorized amount does not match\n");
127 0 : TALER_TESTING_interpreter_fail (gts->is);
128 0 : return;
129 : }
130 : }
131 : {
132 : const char **justification;
133 :
134 0 : if (GNUNET_OK !=
135 0 : TALER_TESTING_get_trait_reason (authorize_cmd,
136 : &justification))
137 0 : TALER_TESTING_FAIL (gts->is);
138 0 : if (0 != strcmp (tsr->details.success.reason,
139 : *justification))
140 : {
141 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
142 : "Tip authorized reason does not match\n");
143 0 : TALER_TESTING_interpreter_fail (gts->is);
144 0 : return;
145 : }
146 : }
147 : {
148 : const struct GNUNET_TIME_Timestamp *tip_expiration;
149 :
150 0 : if (GNUNET_OK !=
151 0 : TALER_TESTING_get_trait_timestamp (authorize_cmd,
152 : 0,
153 : &tip_expiration))
154 0 : TALER_TESTING_FAIL (gts->is);
155 0 : if (GNUNET_TIME_timestamp_cmp (*tip_expiration,
156 : !=,
157 : tsr->details.success.expiration))
158 : {
159 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 : "Tip authorized expiration does not match\n");
161 0 : TALER_TESTING_interpreter_fail (gts->is);
162 0 : return;
163 : }
164 : }
165 0 : if (tsr->details.success.pickups_length != gts->pickups_length)
166 : {
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 : "Length of pickups array does not match\n");
169 0 : TALER_TESTING_interpreter_fail (gts->is);
170 0 : return;
171 : }
172 : {
173 0 : for (unsigned int i = 0; i < gts->pickups_length; ++i)
174 : {
175 : const struct TALER_TESTING_Command *pickup_cmd;
176 :
177 0 : pickup_cmd = TALER_TESTING_interpreter_lookup_command (gts->is,
178 0 : gts->pickups[i]);
179 : {
180 : const uint32_t *num_planchets;
181 :
182 0 : if (GNUNET_OK !=
183 0 : TALER_TESTING_get_trait_num_planchets (pickup_cmd,
184 : &num_planchets))
185 0 : TALER_TESTING_FAIL (gts->is);
186 :
187 0 : if (*num_planchets != tsr->details.success.pickups[i].num_planchets)
188 : {
189 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 : "Pickup planchet count does not match\n");
191 0 : TALER_TESTING_interpreter_fail (gts->is);
192 0 : return;
193 : }
194 : }
195 : {
196 : const struct TALER_Amount *total;
197 :
198 0 : if (GNUNET_OK !=
199 0 : TALER_TESTING_get_trait_amount (pickup_cmd,
200 : &total))
201 0 : TALER_TESTING_FAIL (gts->is);
202 :
203 0 : if ( (GNUNET_OK !=
204 0 : TALER_amount_cmp_currency (total,
205 0 : &tsr->details.success.pickups[i].requested_amount)) ||
206 0 : (0 != TALER_amount_cmp (total,
207 0 : &tsr->details.success.pickups[i].requested_amount)))
208 : {
209 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 : "Pickup planchet sum does not match\n");
211 0 : TALER_TESTING_interpreter_fail (gts->is);
212 0 : return;
213 : }
214 0 : GNUNET_assert (0 < TALER_amount_add (&expected_total_picked_up,
215 : &expected_total_picked_up,
216 : total));
217 : }
218 : }
219 0 : if ( (GNUNET_OK !=
220 0 : TALER_amount_cmp_currency (&expected_total_picked_up,
221 0 : &tsr->details.success.total_picked_up)) ||
222 : (0 !=
223 0 : TALER_amount_cmp (&expected_total_picked_up,
224 : &tsr->details.success.total_picked_up)) )
225 : {
226 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 : "Tip picked up amount does not match\n");
228 0 : TALER_TESTING_interpreter_fail (gts->is);
229 0 : return;
230 : }
231 : }
232 0 : break;
233 0 : default:
234 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
235 : "Unhandled HTTP status.\n");
236 : }
237 0 : TALER_TESTING_interpreter_next (gts->is);
238 : }
239 :
240 :
241 : /**
242 : * Run the "GET tip" CMD.
243 : *
244 : * @param cls closure.
245 : * @param cmd command being run now.
246 : * @param is interpreter state.
247 : */
248 : static void
249 0 : merchant_get_tip_run (void *cls,
250 : const struct TALER_TESTING_Command *cmd,
251 : struct TALER_TESTING_Interpreter *is)
252 : {
253 0 : struct MerchantTipGetState *tgs = cls;
254 : const struct TALER_TESTING_Command *tip_cmd;
255 : const struct TALER_TipIdentifierP *tip_id;
256 :
257 0 : tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
258 : tgs->tip_reference);
259 :
260 0 : if (GNUNET_OK !=
261 0 : TALER_TESTING_get_trait_tip_id (tip_cmd,
262 : &tip_id))
263 0 : TALER_TESTING_FAIL (is);
264 :
265 0 : tgs->is = is;
266 0 : tgs->tgh = TALER_MERCHANT_merchant_tip_get (is->ctx,
267 : tgs->merchant_url,
268 : tip_id,
269 : NULL,
270 0 : GNUNET_TIME_UNIT_ZERO,
271 0 : tgs->fetch_pickups,
272 : &merchant_get_tip_cb,
273 : tgs);
274 0 : GNUNET_assert (NULL != tgs->tgh);
275 : }
276 :
277 :
278 : /**
279 : * Free the state of a "GET tip" CMD, and possibly
280 : * cancel a pending operation thereof.
281 : *
282 : * @param cls closure.
283 : * @param cmd command being run.
284 : */
285 : static void
286 0 : merchant_get_tip_cleanup (void *cls,
287 : const struct TALER_TESTING_Command *cmd)
288 : {
289 0 : struct MerchantTipGetState *tgs = cls;
290 :
291 0 : if (NULL != tgs->tgh)
292 : {
293 0 : TALER_LOG_WARNING ("Get tip operation did not complete\n");
294 0 : TALER_MERCHANT_merchant_tip_get_cancel (tgs->tgh);
295 : }
296 0 : GNUNET_array_grow (tgs->pickups,
297 : tgs->pickups_length,
298 : 0);
299 0 : GNUNET_free (tgs);
300 0 : }
301 :
302 :
303 : struct TALER_TESTING_Command
304 0 : TALER_TESTING_cmd_merchant_get_tip (const char *label,
305 : const char *merchant_url,
306 : const char *tip_reference,
307 : unsigned int http_status)
308 : {
309 : struct MerchantTipGetState *tgs;
310 :
311 0 : tgs = GNUNET_new (struct MerchantTipGetState);
312 0 : tgs->merchant_url = merchant_url;
313 0 : tgs->tip_reference = tip_reference;
314 0 : tgs->http_status = http_status;
315 : {
316 0 : struct TALER_TESTING_Command cmd = {
317 : .cls = tgs,
318 : .label = label,
319 : .run = &merchant_get_tip_run,
320 : .cleanup = &merchant_get_tip_cleanup
321 : };
322 :
323 0 : return cmd;
324 : }
325 : }
326 :
327 :
328 : struct TALER_TESTING_Command
329 0 : TALER_TESTING_cmd_merchant_get_tip_with_pickups (const char *label,
330 : const char *merchant_url,
331 : const char *tip_reference,
332 : unsigned int http_status,
333 : ...)
334 : {
335 : struct MerchantTipGetState *tgs;
336 :
337 0 : tgs = GNUNET_new (struct MerchantTipGetState);
338 0 : tgs->merchant_url = merchant_url;
339 0 : tgs->tip_reference = tip_reference;
340 0 : tgs->fetch_pickups = true;
341 0 : tgs->http_status = http_status;
342 : {
343 : const char *clabel;
344 : va_list ap;
345 :
346 0 : va_start (ap, http_status);
347 0 : while (NULL != (clabel = va_arg (ap, const char *)))
348 : {
349 0 : GNUNET_array_append (tgs->pickups,
350 : tgs->pickups_length,
351 : clabel);
352 : }
353 0 : va_end (ap);
354 : }
355 : {
356 0 : struct TALER_TESTING_Command cmd = {
357 : .cls = tgs,
358 : .label = label,
359 : .run = &merchant_get_tip_run,
360 : .cleanup = &merchant_get_tip_cleanup
361 : };
362 :
363 0 : return cmd;
364 : }
365 : }
366 :
367 :
368 : /* end of testing_api_cmd_merchant_get_tip.c */
|