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 src/testing/testing_api_cmd_get_instance.c
21 : * @brief command to test GET /instance/$ID
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : struct GetInstanceState;
26 : #define TALER_MERCHANT_GET_MANAGEMENT_INSTANCE_RESULT_CLOSURE struct GetInstanceState
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/get-management-instances-INSTANCE.h>
32 :
33 :
34 : /**
35 : * State of a "GET instance" CMD.
36 : */
37 : struct GetInstanceState
38 : {
39 :
40 : /**
41 : * Handle for a "GET instance" request.
42 : */
43 : struct TALER_MERCHANT_GetManagementInstanceHandle *igh;
44 :
45 : /**
46 : * The interpreter state.
47 : */
48 : struct TALER_TESTING_Interpreter *is;
49 :
50 : /**
51 : * Public key of the merchant instance.
52 : */
53 : union TALER_AccountPublicKeyP account_pub;
54 :
55 : /**
56 : * Base URL of the merchant serving the request.
57 : */
58 : const char *merchant_url;
59 :
60 : /**
61 : * ID of the instance to run GET for.
62 : */
63 : const char *instance_id;
64 :
65 : /**
66 : * Reference for a POST or PATCH /instances CMD (optional).
67 : */
68 : const char *instance_reference;
69 :
70 : /**
71 : * Expected HTTP response code.
72 : */
73 : unsigned int http_status;
74 :
75 : };
76 :
77 :
78 : /**
79 : * Callback for a /get/instance/$ID operation.
80 : *
81 : * @param cls closure for this function
82 : * @param igr response
83 : */
84 : static void
85 9 : get_instance_cb (struct GetInstanceState *gis,
86 : const struct TALER_MERCHANT_GetManagementInstanceResponse *igr)
87 : {
88 : const struct TALER_TESTING_Command *instance_cmd;
89 :
90 9 : instance_cmd = TALER_TESTING_interpreter_lookup_command (
91 : gis->is,
92 : gis->instance_reference);
93 :
94 9 : gis->igh = NULL;
95 9 : if (gis->http_status != igr->hr.http_status)
96 : {
97 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98 : "Unexpected response code %u (%d) to command %s\n",
99 : igr->hr.http_status,
100 : (int) igr->hr.ec,
101 : TALER_TESTING_interpreter_get_current_label (gis->is));
102 0 : TALER_TESTING_interpreter_fail (gis->is);
103 0 : return;
104 : }
105 9 : switch (igr->hr.http_status)
106 : {
107 5 : case MHD_HTTP_OK:
108 : {
109 5 : const struct TALER_MERCHANT_GetManagementInstanceDetails *details =
110 : &igr->details.ok.details;
111 :
112 : gis->account_pub.merchant_pub
113 5 : = details->merchant_pub;
114 : {
115 : const char *name;
116 :
117 5 : if (GNUNET_OK !=
118 5 : TALER_TESTING_get_trait_instance_name (instance_cmd,
119 : &name))
120 : {
121 0 : TALER_TESTING_interpreter_fail (gis->is);
122 0 : return;
123 : }
124 5 : if (0 != strcmp (details->name,
125 : name))
126 : {
127 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 : "Instance name does not match: Got `%s', wanted `%s'\n",
129 : details->name,
130 : name);
131 0 : TALER_TESTING_interpreter_fail (gis->is);
132 0 : return;
133 : }
134 : }
135 : {
136 : const json_t *address;
137 :
138 5 : if (GNUNET_OK !=
139 5 : TALER_TESTING_get_trait_address (instance_cmd,
140 : &address))
141 : {
142 0 : TALER_TESTING_interpreter_fail (gis->is);
143 0 : return;
144 : }
145 5 : if (1 != json_equal (details->address,
146 : address))
147 : {
148 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 : "Instance address does not match\n");
150 0 : TALER_TESTING_interpreter_fail (gis->is);
151 0 : return;
152 : }
153 : }
154 : {
155 : const json_t *jurisdiction;
156 :
157 5 : if (GNUNET_OK !=
158 5 : TALER_TESTING_get_trait_jurisdiction (instance_cmd,
159 : &jurisdiction))
160 : {
161 0 : TALER_TESTING_interpreter_fail (gis->is);
162 0 : return;
163 : }
164 5 : if (1 != json_equal (details->jurisdiction,
165 : jurisdiction))
166 : {
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 : "Instance jurisdiction does not match\n");
169 0 : TALER_TESTING_interpreter_fail (gis->is);
170 0 : return;
171 : }
172 : }
173 : {
174 : const bool *use_stefan;
175 :
176 5 : if (GNUNET_OK !=
177 5 : TALER_TESTING_get_trait_use_stefan (instance_cmd,
178 : &use_stefan))
179 : {
180 0 : TALER_TESTING_interpreter_fail (gis->is);
181 0 : return;
182 : }
183 5 : if (*use_stefan != details->use_stefan)
184 : {
185 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 : "Instance use_stefan value does not match\n");
187 0 : TALER_TESTING_interpreter_fail (gis->is);
188 0 : return;
189 : }
190 : }
191 : {
192 : const struct GNUNET_TIME_Relative *default_wire_transfer_delay;
193 :
194 5 : if (GNUNET_OK !=
195 5 : TALER_TESTING_get_trait_wire_delay (instance_cmd,
196 : &default_wire_transfer_delay))
197 : {
198 0 : TALER_TESTING_interpreter_fail (gis->is);
199 0 : return;
200 : }
201 5 : if (details->default_wire_transfer_delay.rel_value_us !=
202 5 : default_wire_transfer_delay->rel_value_us)
203 : {
204 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205 : "Instance default wire transfer delay does not match\n");
206 0 : TALER_TESTING_interpreter_fail (gis->is);
207 0 : return;
208 : }
209 : }
210 : {
211 : const struct GNUNET_TIME_Relative *default_pay_delay;
212 :
213 5 : if (GNUNET_OK !=
214 5 : TALER_TESTING_get_trait_pay_delay (instance_cmd,
215 : &default_pay_delay))
216 : {
217 0 : TALER_TESTING_interpreter_fail (gis->is);
218 0 : return;
219 : }
220 5 : if (details->default_pay_delay.rel_value_us !=
221 5 : default_pay_delay->rel_value_us)
222 : {
223 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
224 : "Instance default pay delay does not match\n");
225 0 : TALER_TESTING_interpreter_fail (gis->is);
226 0 : return;
227 : }
228 : }
229 : }
230 5 : break;
231 0 : case MHD_HTTP_UNAUTHORIZED:
232 0 : break;
233 4 : case MHD_HTTP_NOT_FOUND:
234 4 : break;
235 0 : default:
236 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
237 : "Unhandled HTTP status %u for GET instance ID.\n",
238 : igr->hr.http_status);
239 : }
240 9 : TALER_TESTING_interpreter_next (gis->is);
241 : }
242 :
243 :
244 : /**
245 : * Run the "GET instance" CMD.
246 : *
247 : *
248 : * @param cls closure.
249 : * @param cmd command being run now.
250 : * @param is interpreter state.
251 : */
252 : static void
253 9 : get_instance_run (void *cls,
254 : const struct TALER_TESTING_Command *cmd,
255 : struct TALER_TESTING_Interpreter *is)
256 : {
257 9 : struct GetInstanceState *gis = cls;
258 :
259 9 : gis->is = is;
260 9 : gis->igh = TALER_MERCHANT_get_management_instance_create (
261 : TALER_TESTING_interpreter_get_context (is),
262 : gis->merchant_url,
263 : gis->instance_id);
264 : {
265 : enum TALER_ErrorCode ec;
266 :
267 9 : ec = TALER_MERCHANT_get_management_instance_start (
268 : gis->igh,
269 : &get_instance_cb,
270 : gis);
271 9 : GNUNET_assert (TALER_EC_NONE == ec);
272 : }
273 9 : }
274 :
275 :
276 : /**
277 : * Free the state of a "GET instance" CMD, and possibly
278 : * cancel a pending operation thereof.
279 : *
280 : * @param cls closure.
281 : * @param cmd command being run.
282 : */
283 : static void
284 9 : get_instance_cleanup (void *cls,
285 : const struct TALER_TESTING_Command *cmd)
286 : {
287 9 : struct GetInstanceState *gis = cls;
288 :
289 9 : if (NULL != gis->igh)
290 : {
291 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
292 : "GET /instances/$ID operation did not complete\n");
293 0 : TALER_MERCHANT_get_management_instance_cancel (gis->igh);
294 : }
295 9 : GNUNET_free (gis);
296 9 : }
297 :
298 :
299 : /**
300 : * Offers information from the GET /instance/$ID CMD state to other
301 : * commands.
302 : *
303 : * @param cls closure
304 : * @param[out] ret result (could be anything)
305 : * @param trait name of the trait
306 : * @param index index number of the object to extract.
307 : * @return #GNUNET_OK on success
308 : */
309 : static enum GNUNET_GenericReturnValue
310 2 : get_instance_traits (void *cls,
311 : const void **ret,
312 : const char *trait,
313 : unsigned int index)
314 : {
315 2 : struct GetInstanceState *pps = cls;
316 : struct TALER_TESTING_Trait traits[] = {
317 2 : TALER_TESTING_make_trait_merchant_base_url (
318 : pps->merchant_url),
319 2 : TALER_TESTING_make_trait_merchant_pub (
320 2 : &pps->account_pub.merchant_pub),
321 2 : TALER_TESTING_make_trait_account_pub (
322 2 : &pps->account_pub),
323 2 : TALER_TESTING_trait_end (),
324 : };
325 :
326 2 : return TALER_TESTING_get_trait (traits,
327 : ret,
328 : trait,
329 : index);
330 : }
331 :
332 :
333 : struct TALER_TESTING_Command
334 9 : TALER_TESTING_cmd_merchant_get_instance (
335 : const char *label,
336 : const char *merchant_url,
337 : const char *instance_id,
338 : unsigned int http_status,
339 : const char *instance_reference)
340 : {
341 : struct GetInstanceState *gis;
342 :
343 9 : gis = GNUNET_new (struct GetInstanceState);
344 9 : gis->merchant_url = merchant_url;
345 9 : gis->instance_id = instance_id;
346 9 : gis->http_status = http_status;
347 9 : gis->instance_reference = instance_reference;
348 : {
349 9 : struct TALER_TESTING_Command cmd = {
350 : .cls = gis,
351 : .label = label,
352 : .run = &get_instance_run,
353 : .cleanup = &get_instance_cleanup,
354 : .traits = &get_instance_traits
355 : };
356 :
357 9 : return cmd;
358 : }
359 : }
360 :
361 :
362 : /* end of testing_api_cmd_get_instance.c */
|