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 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 : * Name of the instance.
59 : */
60 : const char *name;
61 :
62 : /**
63 : * Address to use.
64 : */
65 : json_t *address;
66 :
67 : /**
68 : * Jurisdiction to use.
69 : */
70 : json_t *jurisdiction;
71 :
72 : /**
73 : * Authentication token to require for this instance.
74 : */
75 : const char *auth_token;
76 :
77 : /**
78 : * Use STEFAN curves?
79 : */
80 : bool use_stefan;
81 :
82 : /**
83 : * Wire transfer delay to use.
84 : */
85 : struct GNUNET_TIME_Relative default_wire_transfer_delay;
86 :
87 : /**
88 : * Order validity default duration to use.
89 : */
90 : struct GNUNET_TIME_Relative default_pay_delay;
91 :
92 : /**
93 : * Expected HTTP response code.
94 : */
95 : unsigned int http_status;
96 :
97 : };
98 :
99 :
100 : /**
101 : * Callback for a POST /instances operation.
102 : *
103 : * @param cls closure for this function
104 : * @param hr response being processed
105 : */
106 : static void
107 17 : post_instances_cb (
108 : void *cls,
109 : const struct TALER_MERCHANT_HttpResponse *hr)
110 : {
111 17 : struct PostInstancesState *pis = cls;
112 :
113 17 : pis->iph = NULL;
114 17 : if (pis->http_status != hr->http_status)
115 : {
116 0 : TALER_TESTING_unexpected_status_with_body (
117 : pis->is,
118 : hr->http_status,
119 : pis->http_status,
120 : hr->reply);
121 0 : TALER_TESTING_interpreter_fail (pis->is);
122 0 : return;
123 : }
124 17 : switch (hr->http_status)
125 : {
126 17 : case MHD_HTTP_NO_CONTENT:
127 17 : break;
128 0 : case MHD_HTTP_BAD_REQUEST:
129 0 : break;
130 0 : case MHD_HTTP_UNAUTHORIZED:
131 0 : break;
132 0 : case MHD_HTTP_FORBIDDEN:
133 0 : break;
134 0 : case MHD_HTTP_NOT_FOUND:
135 0 : break;
136 0 : case MHD_HTTP_CONFLICT:
137 0 : break;
138 0 : default:
139 0 : GNUNET_break (0);
140 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
141 : "Unhandled HTTP status %u for POST instances.\n",
142 : hr->http_status);
143 : }
144 17 : TALER_TESTING_interpreter_next (pis->is);
145 : }
146 :
147 :
148 : /**
149 : * Run the "POST /instances" CMD.
150 : *
151 : *
152 : * @param cls closure.
153 : * @param cmd command being run now.
154 : * @param is interpreter state.
155 : */
156 : static void
157 17 : post_instances_run (
158 : void *cls,
159 : const struct TALER_TESTING_Command *cmd,
160 : struct TALER_TESTING_Interpreter *is)
161 : {
162 17 : struct PostInstancesState *pis = cls;
163 :
164 17 : pis->is = is;
165 17 : pis->iph = TALER_MERCHANT_instances_post (
166 : TALER_TESTING_interpreter_get_context (is),
167 : pis->merchant_url,
168 : pis->instance_id,
169 : pis->name,
170 17 : pis->address,
171 17 : pis->jurisdiction,
172 17 : pis->use_stefan,
173 : pis->default_wire_transfer_delay,
174 : pis->default_pay_delay,
175 : pis->auth_token,
176 : &post_instances_cb,
177 : pis);
178 17 : if (NULL == pis->iph)
179 : {
180 0 : GNUNET_break (0);
181 0 : TALER_TESTING_interpreter_fail (pis->is);
182 0 : return;
183 : }
184 : }
185 :
186 :
187 : /**
188 : * Offers information from the POST /instances CMD state to other
189 : * commands.
190 : *
191 : * @param cls closure
192 : * @param[out] ret result (could be anything)
193 : * @param trait name of the trait
194 : * @param index index number of the object to extract.
195 : * @return #GNUNET_OK on success
196 : */
197 : static enum GNUNET_GenericReturnValue
198 26 : post_instances_traits (void *cls,
199 : const void **ret,
200 : const char *trait,
201 : unsigned int index)
202 : {
203 26 : struct PostInstancesState *pis = cls;
204 : struct TALER_TESTING_Trait traits[] = {
205 26 : TALER_TESTING_make_trait_instance_name (pis->name),
206 26 : TALER_TESTING_make_trait_instance_id (pis->instance_id),
207 26 : TALER_TESTING_make_trait_address (pis->address),
208 26 : TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction),
209 26 : TALER_TESTING_make_trait_use_stefan (&pis->use_stefan),
210 26 : TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay),
211 26 : TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay),
212 26 : TALER_TESTING_trait_end ()
213 : };
214 :
215 26 : return TALER_TESTING_get_trait (traits,
216 : ret,
217 : trait,
218 : index);
219 : }
220 :
221 :
222 : /**
223 : * Free the state of a "POST /instances" CMD, and possibly
224 : * cancel a pending operation thereof.
225 : *
226 : * @param cls closure.
227 : * @param cmd command being run.
228 : */
229 : static void
230 17 : post_instances_cleanup (void *cls,
231 : const struct TALER_TESTING_Command *cmd)
232 : {
233 17 : struct PostInstancesState *pis = cls;
234 :
235 17 : if (NULL != pis->iph)
236 : {
237 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
238 : "POST /instances operation did not complete\n");
239 0 : TALER_MERCHANT_instances_post_cancel (pis->iph);
240 : }
241 17 : json_decref (pis->address);
242 17 : json_decref (pis->jurisdiction);
243 17 : GNUNET_free (pis);
244 17 : }
245 :
246 :
247 : struct TALER_TESTING_Command
248 17 : TALER_TESTING_cmd_merchant_post_instances2 (
249 : const char *label,
250 : const char *merchant_url,
251 : const char *instance_id,
252 : const char *name,
253 : json_t *address,
254 : json_t *jurisdiction,
255 : bool use_stefan,
256 : struct GNUNET_TIME_Relative default_wire_transfer_delay,
257 : struct GNUNET_TIME_Relative default_pay_delay,
258 : const char *auth_token,
259 : unsigned int http_status)
260 : {
261 : struct PostInstancesState *pis;
262 :
263 17 : pis = GNUNET_new (struct PostInstancesState);
264 17 : pis->merchant_url = merchant_url;
265 17 : pis->instance_id = instance_id;
266 17 : pis->http_status = http_status;
267 17 : pis->name = name;
268 17 : pis->address = address; /* ownership transfer! */
269 17 : pis->jurisdiction = jurisdiction; /* ownership transfer! */
270 17 : pis->use_stefan = use_stefan;
271 17 : pis->default_wire_transfer_delay = default_wire_transfer_delay;
272 17 : pis->default_pay_delay = default_pay_delay;
273 17 : pis->auth_token = auth_token;
274 : {
275 17 : struct TALER_TESTING_Command cmd = {
276 : .cls = pis,
277 : .label = label,
278 : .run = &post_instances_run,
279 : .cleanup = &post_instances_cleanup,
280 : .traits = &post_instances_traits
281 : };
282 :
283 17 : return cmd;
284 : }
285 : }
286 :
287 :
288 : struct TALER_TESTING_Command
289 15 : TALER_TESTING_cmd_merchant_post_instances (
290 : const char *label,
291 : const char *merchant_url,
292 : const char *instance_id,
293 : unsigned int http_status)
294 : {
295 30 : return TALER_TESTING_cmd_merchant_post_instances2 (
296 : label,
297 : merchant_url,
298 : instance_id,
299 : instance_id,
300 : json_pack ("{s:s}", "city", "shopcity"),
301 : json_pack ("{s:s}", "city", "lawyercity"),
302 : true,
303 15 : GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */
304 : GNUNET_TIME_UNIT_MINUTES,
305 : NULL,
306 : http_status);
307 : }
308 :
309 :
310 : /* end of testing_api_cmd_post_instance.c */
|