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_post_instances.c
21 : * @brief command to test POST /instances
22 : * @author Christian Grothoff
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 : /**
32 : * State of a "POST /instances" CMD.
33 : */
34 : struct PostInstancesState
35 : {
36 :
37 : /**
38 : * Handle for a "POST instance" request.
39 : */
40 : struct TALER_MERCHANT_InstancesPostHandle *iph;
41 :
42 : /**
43 : * The interpreter state.
44 : */
45 : struct TALER_TESTING_Interpreter *is;
46 :
47 : /**
48 : * Base URL of the merchant serving the request.
49 : */
50 : const char *merchant_url;
51 :
52 : /**
53 : * ID of the instance to run POST for.
54 : */
55 : const char *instance_id;
56 :
57 : /**
58 : * Length of the @payto_uris array
59 : */
60 : unsigned int payto_uris_length;
61 :
62 : /**
63 : * Array of payto URIs.
64 : */
65 : const char **payto_uris;
66 :
67 : /**
68 : * Name of the instance.
69 : */
70 : const char *name;
71 :
72 : /**
73 : * Address to use.
74 : */
75 : json_t *address;
76 :
77 : /**
78 : * Jurisdiction to use.
79 : */
80 : json_t *jurisdiction;
81 :
82 : /**
83 : * Authentication token to require for this instance.
84 : */
85 : const char *auth_token;
86 :
87 : /**
88 : * Wire fee to use.
89 : */
90 : struct TALER_Amount default_max_wire_fee;
91 :
92 : /**
93 : * Amortization to use.
94 : */
95 : uint32_t default_wire_fee_amortization;
96 :
97 : /**
98 : * Deposit fee ceiling to use.
99 : */
100 : struct TALER_Amount default_max_deposit_fee;
101 :
102 : /**
103 : * Wire transfer delay to use.
104 : */
105 : struct GNUNET_TIME_Relative default_wire_transfer_delay;
106 :
107 : /**
108 : * Order validity default duration to use.
109 : */
110 : struct GNUNET_TIME_Relative default_pay_delay;
111 :
112 : /**
113 : * Expected HTTP response code.
114 : */
115 : unsigned int http_status;
116 :
117 : };
118 :
119 :
120 : /**
121 : * Callback for a POST /instances operation.
122 : *
123 : * @param cls closure for this function
124 : * @param hr response being processed
125 : */
126 : static void
127 0 : post_instances_cb (void *cls,
128 : const struct TALER_MERCHANT_HttpResponse *hr)
129 : {
130 0 : struct PostInstancesState *pis = cls;
131 :
132 0 : pis->iph = NULL;
133 0 : if (pis->http_status != hr->http_status)
134 : {
135 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 : "Unexpected response code %u (%d) to command %s\n",
137 : hr->http_status,
138 : (int) hr->ec,
139 : TALER_TESTING_interpreter_get_current_label (pis->is));
140 0 : TALER_TESTING_interpreter_fail (pis->is);
141 0 : return;
142 : }
143 0 : switch (hr->http_status)
144 : {
145 0 : case MHD_HTTP_NO_CONTENT:
146 0 : break;
147 0 : case MHD_HTTP_BAD_REQUEST:
148 0 : break;
149 0 : case MHD_HTTP_UNAUTHORIZED:
150 0 : break;
151 0 : case MHD_HTTP_FORBIDDEN:
152 0 : break;
153 0 : case MHD_HTTP_NOT_FOUND:
154 0 : break;
155 0 : case MHD_HTTP_CONFLICT:
156 0 : break;
157 0 : default:
158 0 : GNUNET_break (0);
159 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
160 : "Unhandled HTTP status %u for POST instances.\n",
161 : hr->http_status);
162 : }
163 0 : TALER_TESTING_interpreter_next (pis->is);
164 : }
165 :
166 :
167 : /**
168 : * Run the "POST /instances" CMD.
169 : *
170 : *
171 : * @param cls closure.
172 : * @param cmd command being run now.
173 : * @param is interpreter state.
174 : */
175 : static void
176 0 : post_instances_run (void *cls,
177 : const struct TALER_TESTING_Command *cmd,
178 : struct TALER_TESTING_Interpreter *is)
179 : {
180 0 : struct PostInstancesState *pis = cls;
181 :
182 0 : pis->is = is;
183 0 : pis->iph = TALER_MERCHANT_instances_post (is->ctx,
184 : pis->merchant_url,
185 : pis->instance_id,
186 : pis->payto_uris_length,
187 : pis->payto_uris,
188 : pis->name,
189 0 : pis->address,
190 0 : pis->jurisdiction,
191 0 : &pis->default_max_wire_fee,
192 : pis->default_wire_fee_amortization,
193 0 : &pis->default_max_deposit_fee,
194 : pis->default_wire_transfer_delay,
195 : pis->default_pay_delay,
196 : pis->auth_token,
197 : &post_instances_cb,
198 : pis);
199 0 : if (NULL == pis->iph)
200 : {
201 0 : GNUNET_break (0);
202 0 : TALER_TESTING_interpreter_fail (pis->is);
203 0 : return;
204 : }
205 : }
206 :
207 :
208 : /**
209 : * Offers information from the POST /instances CMD state to other
210 : * commands.
211 : *
212 : * @param cls closure
213 : * @param[out] ret result (could be anything)
214 : * @param trait name of the trait
215 : * @param index index number of the object to extract.
216 : * @return #GNUNET_OK on success
217 : */
218 : static enum GNUNET_GenericReturnValue
219 0 : post_instances_traits (void *cls,
220 : const void **ret,
221 : const char *trait,
222 : unsigned int index)
223 0 : {
224 0 : struct PostInstancesState *pis = cls;
225 : #define NUM_TRAITS (pis->payto_uris_length) + 11
226 0 : struct TALER_TESTING_Trait traits[NUM_TRAITS];
227 : traits[0] =
228 0 : TALER_TESTING_make_trait_instance_name (&pis->name);
229 : traits[1] =
230 0 : TALER_TESTING_make_trait_instance_id (&pis->instance_id);
231 : traits[2] =
232 0 : TALER_TESTING_make_trait_address (pis->address);
233 : traits[3] =
234 0 : TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction);
235 : traits[4] =
236 0 : TALER_TESTING_make_trait_max_wire_fee (&pis->default_max_wire_fee);
237 : traits[5] =
238 0 : TALER_TESTING_make_trait_wire_fee_amortization (
239 0 : &pis->default_wire_fee_amortization);
240 : traits[6] =
241 0 : TALER_TESTING_make_trait_max_deposit_fee (&pis->default_max_deposit_fee);
242 : traits[7] =
243 0 : TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay);
244 : traits[8] =
245 0 : TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay);
246 : traits[9] =
247 0 : TALER_TESTING_make_trait_payto_length (&pis->payto_uris_length);
248 0 : traits[NUM_TRAITS - 1] =
249 0 : TALER_TESTING_trait_end ();
250 0 : for (unsigned int i = 0; i < pis->payto_uris_length; ++i)
251 : {
252 0 : traits[10 + i] =
253 0 : TALER_TESTING_make_trait_payto_uris (i,
254 0 : &pis->payto_uris[i]);
255 : }
256 :
257 0 : return TALER_TESTING_get_trait (traits,
258 : ret,
259 : trait,
260 : index);
261 : }
262 :
263 :
264 : /**
265 : * Free the state of a "POST /instances" CMD, and possibly
266 : * cancel a pending operation thereof.
267 : *
268 : * @param cls closure.
269 : * @param cmd command being run.
270 : */
271 : static void
272 0 : post_instances_cleanup (void *cls,
273 : const struct TALER_TESTING_Command *cmd)
274 : {
275 0 : struct PostInstancesState *pis = cls;
276 :
277 0 : if (NULL != pis->iph)
278 : {
279 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
280 : "POST /instances operation did not complete\n");
281 0 : TALER_MERCHANT_instances_post_cancel (pis->iph);
282 : }
283 0 : json_decref (pis->address);
284 0 : json_decref (pis->jurisdiction);
285 0 : GNUNET_free (pis->payto_uris);
286 0 : GNUNET_free (pis);
287 0 : }
288 :
289 :
290 : struct TALER_TESTING_Command
291 0 : TALER_TESTING_cmd_merchant_post_instances2 (
292 : const char *label,
293 : const char *merchant_url,
294 : const char *instance_id,
295 : unsigned int payto_uris_length,
296 : const char *payto_uris[],
297 : const char *name,
298 : json_t *address,
299 : json_t *jurisdiction,
300 : const char *default_max_wire_fee,
301 : uint32_t default_wire_fee_amortization,
302 : const char *default_max_deposit_fee,
303 : struct GNUNET_TIME_Relative default_wire_transfer_delay,
304 : struct GNUNET_TIME_Relative default_pay_delay,
305 : const char *auth_token,
306 : unsigned int http_status)
307 : {
308 : struct PostInstancesState *pis;
309 :
310 0 : pis = GNUNET_new (struct PostInstancesState);
311 0 : pis->merchant_url = merchant_url;
312 0 : pis->instance_id = instance_id;
313 0 : pis->http_status = http_status;
314 0 : pis->payto_uris_length = payto_uris_length;
315 0 : pis->payto_uris = GNUNET_new_array (payto_uris_length,
316 : const char *);
317 0 : memcpy (pis->payto_uris,
318 : payto_uris,
319 : sizeof (const char *) * payto_uris_length);
320 0 : pis->name = name;
321 0 : pis->address = address; /* ownership transfer! */
322 0 : pis->jurisdiction = jurisdiction; /* ownership transfer! */
323 0 : GNUNET_assert (GNUNET_OK ==
324 : TALER_string_to_amount (default_max_wire_fee,
325 : &pis->default_max_wire_fee));
326 0 : pis->default_wire_fee_amortization = default_wire_fee_amortization;
327 0 : GNUNET_assert (GNUNET_OK ==
328 : TALER_string_to_amount (default_max_deposit_fee,
329 : &pis->default_max_deposit_fee));
330 0 : pis->default_wire_transfer_delay = default_wire_transfer_delay;
331 0 : pis->default_pay_delay = default_pay_delay;
332 0 : pis->auth_token = auth_token;
333 : {
334 0 : struct TALER_TESTING_Command cmd = {
335 : .cls = pis,
336 : .label = label,
337 : .run = &post_instances_run,
338 : .cleanup = &post_instances_cleanup,
339 : .traits = &post_instances_traits
340 : };
341 :
342 0 : return cmd;
343 : }
344 : }
345 :
346 :
347 : struct TALER_TESTING_Command
348 0 : TALER_TESTING_cmd_merchant_post_instances (const char *label,
349 : const char *merchant_url,
350 : const char *instance_id,
351 : const char *payto_uri,
352 : const char *currency,
353 : unsigned int http_status)
354 : {
355 0 : const char *payto_uris[] = {
356 : payto_uri
357 : };
358 : struct TALER_Amount default_max_fee;
359 : const char *default_max_fee_s;
360 :
361 0 : GNUNET_assert (GNUNET_OK ==
362 : TALER_amount_set_zero (currency,
363 : &default_max_fee));
364 0 : default_max_fee.value = 1;
365 0 : default_max_fee_s = TALER_amount2s (&default_max_fee);
366 :
367 0 : return TALER_TESTING_cmd_merchant_post_instances2 (
368 : label,
369 : merchant_url,
370 : instance_id,
371 : 1,
372 : payto_uris,
373 : instance_id,
374 : json_pack ("{s:s}", "city", "shopcity"),
375 : json_pack ("{s:s}", "city", "lawyercity"),
376 : default_max_fee_s,
377 : 10,
378 : default_max_fee_s,
379 0 : GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */
380 : GNUNET_TIME_UNIT_MINUTES,
381 : NULL,
382 : http_status);
383 : }
384 :
385 :
386 : /* end of testing_api_cmd_post_instance.c */
|