Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020-2023 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 src/testing/testing_api_cmd_post_instances.c
21 : * @brief command to test POST /instances
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : struct PostInstancesState;
26 : #define TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_RESULT_CLOSURE struct PostInstancesState
27 : #include <taler/taler_exchange_service.h>
28 : #include <taler/taler_testing_lib.h>
29 : #include "taler/taler_merchant_service.h"
30 : #include "taler/taler_merchant_testing_lib.h"
31 : #include <taler/merchant/post-management-instances.h>
32 :
33 :
34 : /**
35 : * State of a "POST /instances" CMD.
36 : */
37 : struct PostInstancesState
38 : {
39 :
40 : /**
41 : * Handle for a "POST instance" request.
42 : */
43 : struct TALER_MERCHANT_PostManagementInstancesHandle *iph;
44 :
45 : /**
46 : * The interpreter state.
47 : */
48 : struct TALER_TESTING_Interpreter *is;
49 :
50 : /**
51 : * Base URL of the merchant serving the request.
52 : */
53 : const char *merchant_url;
54 :
55 : /**
56 : * ID of the instance to run POST for.
57 : */
58 : const char *instance_id;
59 :
60 : /**
61 : * Name of the instance.
62 : */
63 : const char *name;
64 :
65 : /**
66 : * Address to use.
67 : */
68 : json_t *address;
69 :
70 : /**
71 : * Jurisdiction to use.
72 : */
73 : json_t *jurisdiction;
74 :
75 : /**
76 : * Authentication token to require for this instance.
77 : */
78 : const char *auth_token;
79 :
80 : /**
81 : * Use STEFAN curves?
82 : */
83 : bool use_stefan;
84 :
85 : /**
86 : * Wire transfer delay to use.
87 : */
88 : struct GNUNET_TIME_Relative default_wire_transfer_delay;
89 :
90 : /**
91 : * Order validity default duration to use.
92 : */
93 : struct GNUNET_TIME_Relative default_pay_delay;
94 :
95 : /**
96 : * Expected HTTP response code.
97 : */
98 : unsigned int http_status;
99 :
100 : };
101 :
102 :
103 : /**
104 : * Callback for a POST /instances operation.
105 : *
106 : * @param cls closure for this function
107 : * @param mir response being processed
108 : */
109 : static void
110 19 : post_instances_cb (
111 : struct PostInstancesState *pis,
112 : const struct TALER_MERCHANT_PostManagementInstancesResponse *mir)
113 : {
114 :
115 19 : pis->iph = NULL;
116 19 : if (pis->http_status != mir->hr.http_status)
117 : {
118 0 : TALER_TESTING_unexpected_status_with_body (
119 : pis->is,
120 : mir->hr.http_status,
121 : pis->http_status,
122 : mir->hr.reply);
123 0 : return;
124 : }
125 19 : switch (mir->hr.http_status)
126 : {
127 19 : case MHD_HTTP_NO_CONTENT:
128 19 : break;
129 0 : case MHD_HTTP_BAD_REQUEST:
130 0 : break;
131 0 : case MHD_HTTP_UNAUTHORIZED:
132 0 : break;
133 0 : case MHD_HTTP_FORBIDDEN:
134 0 : break;
135 0 : case MHD_HTTP_NOT_FOUND:
136 0 : break;
137 0 : case MHD_HTTP_CONFLICT:
138 0 : break;
139 0 : default:
140 0 : GNUNET_break (0);
141 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
142 : "Unhandled HTTP status %u for POST instances.\n",
143 : mir->hr.http_status);
144 : }
145 19 : TALER_TESTING_interpreter_next (pis->is);
146 : }
147 :
148 :
149 : /**
150 : * Run the "POST /instances" CMD.
151 : *
152 : *
153 : * @param cls closure.
154 : * @param cmd command being run now.
155 : * @param is interpreter state.
156 : */
157 : static void
158 19 : post_instances_run (
159 : void *cls,
160 : const struct TALER_TESTING_Command *cmd,
161 : struct TALER_TESTING_Interpreter *is)
162 : {
163 19 : struct PostInstancesState *pis = cls;
164 :
165 19 : pis->is = is;
166 19 : pis->iph = TALER_MERCHANT_post_management_instances_create (
167 : TALER_TESTING_interpreter_get_context (is),
168 : pis->merchant_url,
169 : pis->instance_id,
170 : pis->name,
171 19 : pis->address,
172 19 : pis->jurisdiction,
173 19 : pis->use_stefan);
174 19 : TALER_MERCHANT_post_management_instances_set_options (
175 : pis->iph,
176 : TALER_MERCHANT_post_management_instances_option_default_wire_transfer_delay (
177 : pis->default_wire_transfer_delay),
178 : TALER_MERCHANT_post_management_instances_option_default_pay_delay (
179 : pis->default_pay_delay),
180 : TALER_MERCHANT_post_management_instances_option_default_refund_delay (
181 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
182 : 15)),
183 : TALER_MERCHANT_post_management_instances_option_auth_password (
184 : pis->auth_token));
185 : {
186 : enum TALER_ErrorCode ec;
187 :
188 19 : ec = TALER_MERCHANT_post_management_instances_start (
189 : pis->iph,
190 : &post_instances_cb,
191 : pis);
192 19 : if (TALER_EC_NONE != ec)
193 : {
194 0 : GNUNET_break (0);
195 0 : TALER_TESTING_interpreter_fail (pis->is);
196 0 : return;
197 : }
198 : }
199 : }
200 :
201 :
202 : /**
203 : * Offers information from the POST /instances CMD state to other
204 : * commands.
205 : *
206 : * @param cls closure
207 : * @param[out] ret result (could be anything)
208 : * @param trait name of the trait
209 : * @param index index number of the object to extract.
210 : * @return #GNUNET_OK on success
211 : */
212 : static enum GNUNET_GenericReturnValue
213 26 : post_instances_traits (void *cls,
214 : const void **ret,
215 : const char *trait,
216 : unsigned int index)
217 : {
218 26 : struct PostInstancesState *pis = cls;
219 : struct TALER_TESTING_Trait traits[] = {
220 26 : TALER_TESTING_make_trait_instance_name (pis->name),
221 26 : TALER_TESTING_make_trait_instance_id (pis->instance_id),
222 26 : TALER_TESTING_make_trait_address (pis->address),
223 26 : TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction),
224 26 : TALER_TESTING_make_trait_use_stefan (&pis->use_stefan),
225 26 : TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay),
226 26 : TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay),
227 26 : TALER_TESTING_trait_end ()
228 : };
229 :
230 26 : return TALER_TESTING_get_trait (traits,
231 : ret,
232 : trait,
233 : index);
234 : }
235 :
236 :
237 : /**
238 : * Free the state of a "POST /instances" CMD, and possibly
239 : * cancel a pending operation thereof.
240 : *
241 : * @param cls closure.
242 : * @param cmd command being run.
243 : */
244 : static void
245 19 : post_instances_cleanup (void *cls,
246 : const struct TALER_TESTING_Command *cmd)
247 : {
248 19 : struct PostInstancesState *pis = cls;
249 :
250 19 : if (NULL != pis->iph)
251 : {
252 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
253 : "POST /instances operation did not complete\n");
254 0 : TALER_MERCHANT_post_management_instances_cancel (pis->iph);
255 : }
256 19 : json_decref (pis->address);
257 19 : json_decref (pis->jurisdiction);
258 19 : GNUNET_free (pis);
259 19 : }
260 :
261 :
262 : struct TALER_TESTING_Command
263 19 : TALER_TESTING_cmd_merchant_post_instances2 (
264 : const char *label,
265 : const char *merchant_url,
266 : const char *instance_id,
267 : const char *name,
268 : json_t *address,
269 : json_t *jurisdiction,
270 : bool use_stefan,
271 : struct GNUNET_TIME_Relative default_wire_transfer_delay,
272 : struct GNUNET_TIME_Relative default_pay_delay,
273 : const char *auth_token,
274 : unsigned int http_status)
275 : {
276 : struct PostInstancesState *pis;
277 :
278 19 : pis = GNUNET_new (struct PostInstancesState);
279 19 : pis->merchant_url = merchant_url;
280 19 : pis->instance_id = instance_id;
281 19 : pis->http_status = http_status;
282 19 : pis->name = name;
283 19 : pis->address = address; /* ownership transfer! */
284 19 : pis->jurisdiction = jurisdiction; /* ownership transfer! */
285 19 : pis->use_stefan = use_stefan;
286 19 : pis->default_wire_transfer_delay = default_wire_transfer_delay;
287 19 : pis->default_pay_delay = default_pay_delay;
288 19 : pis->auth_token = auth_token;
289 : {
290 19 : struct TALER_TESTING_Command cmd = {
291 : .cls = pis,
292 : .label = label,
293 : .run = &post_instances_run,
294 : .cleanup = &post_instances_cleanup,
295 : .traits = &post_instances_traits
296 : };
297 :
298 19 : return cmd;
299 : }
300 : }
301 :
302 :
303 : struct TALER_TESTING_Command
304 17 : TALER_TESTING_cmd_merchant_post_instances (
305 : const char *label,
306 : const char *merchant_url,
307 : const char *instance_id,
308 : unsigned int http_status)
309 : {
310 34 : return TALER_TESTING_cmd_merchant_post_instances2 (
311 : label,
312 : merchant_url,
313 : instance_id,
314 : instance_id,
315 : json_pack ("{s:s}", "city", "shopcity"),
316 : json_pack ("{s:s}", "city", "lawyercity"),
317 : true,
318 17 : GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */
319 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
320 : 2), /* small pay delay */
321 : NULL,
322 : http_status);
323 : }
324 :
325 :
326 : /* end of testing_api_cmd_post_instance.c */
|