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_patch_instance.c
21 : * @brief command to test PATCH /instance
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : struct PatchInstanceState;
26 : #define TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_RESULT_CLOSURE struct PatchInstanceState
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/patch-management-instances-INSTANCE.h>
32 :
33 :
34 : /**
35 : * State of a "PATCH /instance" CMD.
36 : */
37 : struct PatchInstanceState
38 : {
39 :
40 : /**
41 : * Handle for a "PATCH /instance/$ID" request.
42 : */
43 : struct TALER_MERCHANT_PatchManagementInstancesHandle *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 PATCH 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 : * Use STEFAN curve?
77 : */
78 : bool use_stefan;
79 :
80 : /**
81 : * Wire transfer delay to use.
82 : */
83 : struct GNUNET_TIME_Relative default_wire_transfer_delay;
84 :
85 : /**
86 : * Order validity default duration to use.
87 : */
88 : struct GNUNET_TIME_Relative default_pay_delay;
89 :
90 : /**
91 : * Expected HTTP response code.
92 : */
93 : unsigned int http_status;
94 :
95 : };
96 :
97 :
98 : /**
99 : * Callback for a PATCH /instances/$ID operation.
100 : *
101 : * @param cls closure for this function
102 : * @param result response being processed
103 : */
104 : static void
105 4 : patch_instance_cb (struct PatchInstanceState *pis,
106 : const struct
107 : TALER_MERCHANT_PatchManagementInstancesResponse *result)
108 : {
109 4 : const struct TALER_MERCHANT_HttpResponse *hr = &result->hr;
110 :
111 4 : pis->iph = NULL;
112 4 : if (pis->http_status != hr->http_status)
113 : {
114 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 : "Unexpected response code %u (%d) to command %s\n",
116 : hr->http_status,
117 : (int) hr->ec,
118 : TALER_TESTING_interpreter_get_current_label (pis->is));
119 0 : TALER_TESTING_interpreter_fail (pis->is);
120 0 : return;
121 : }
122 4 : switch (hr->http_status)
123 : {
124 4 : case MHD_HTTP_NO_CONTENT:
125 4 : break;
126 0 : case MHD_HTTP_BAD_REQUEST:
127 : /* happens also for currency mismatch */
128 0 : break;
129 0 : case MHD_HTTP_UNAUTHORIZED:
130 0 : break;
131 0 : case MHD_HTTP_NOT_FOUND:
132 0 : break;
133 0 : case MHD_HTTP_CONFLICT:
134 0 : break;
135 0 : default:
136 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
137 : "Unhandled HTTP status %u for PATCH instance.\n",
138 : hr->http_status);
139 : }
140 4 : TALER_TESTING_interpreter_next (pis->is);
141 : }
142 :
143 :
144 : /**
145 : * Run the "PATCH /instances/$ID" CMD.
146 : *
147 : *
148 : * @param cls closure.
149 : * @param cmd command being run now.
150 : * @param is interpreter state.
151 : */
152 : static void
153 4 : patch_instance_run (void *cls,
154 : const struct TALER_TESTING_Command *cmd,
155 : struct TALER_TESTING_Interpreter *is)
156 : {
157 4 : struct PatchInstanceState *pis = cls;
158 :
159 4 : pis->is = is;
160 4 : pis->iph = TALER_MERCHANT_patch_management_instances_create (
161 : TALER_TESTING_interpreter_get_context (is),
162 : pis->merchant_url,
163 : pis->instance_id,
164 : pis->name,
165 4 : pis->address,
166 4 : pis->jurisdiction,
167 4 : pis->use_stefan);
168 4 : GNUNET_assert (
169 : GNUNET_OK ==
170 : TALER_MERCHANT_patch_management_instances_set_options (
171 : pis->iph,
172 : TALER_MERCHANT_patch_management_instances_option_default_wire_transfer_delay (
173 : pis->default_wire_transfer_delay),
174 : TALER_MERCHANT_patch_management_instances_option_default_pay_delay (
175 : pis->default_pay_delay),
176 : TALER_MERCHANT_patch_management_instances_option_default_refund_delay (
177 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
178 : 15))));
179 4 : GNUNET_assert (NULL != pis->iph);
180 : {
181 : enum TALER_ErrorCode ec;
182 :
183 4 : ec = TALER_MERCHANT_patch_management_instances_start (
184 : pis->iph,
185 : &patch_instance_cb,
186 : pis);
187 4 : GNUNET_assert (TALER_EC_NONE == ec);
188 : }
189 4 : }
190 :
191 :
192 : /**
193 : * Offers information from the PATCH /instances CMD state to other
194 : * commands.
195 : *
196 : * @param cls closure
197 : * @param[out] ret result (could be anything)
198 : * @param trait name of the trait
199 : * @param index index number of the object to extract.
200 : * @return #GNUNET_OK on success
201 : */
202 : static enum GNUNET_GenericReturnValue
203 12 : patch_instance_traits (void *cls,
204 : const void **ret,
205 : const char *trait,
206 : unsigned int index)
207 : {
208 12 : struct PatchInstanceState *pis = cls;
209 : struct TALER_TESTING_Trait traits[] = {
210 12 : TALER_TESTING_make_trait_instance_name (pis->name),
211 12 : TALER_TESTING_make_trait_instance_id (pis->instance_id),
212 12 : TALER_TESTING_make_trait_address (pis->address),
213 12 : TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction),
214 12 : TALER_TESTING_make_trait_use_stefan (&pis->use_stefan),
215 12 : TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay),
216 12 : TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay),
217 12 : TALER_TESTING_trait_end ()
218 : };
219 :
220 12 : return TALER_TESTING_get_trait (traits,
221 : ret,
222 : trait,
223 : index);
224 : }
225 :
226 :
227 : /**
228 : * Free the state of a "PATCH /instances/$ID" CMD, and possibly
229 : * cancel a pending operation thereof.
230 : *
231 : * @param cls closure.
232 : * @param cmd command being run.
233 : */
234 : static void
235 4 : patch_instance_cleanup (void *cls,
236 : const struct TALER_TESTING_Command *cmd)
237 : {
238 4 : struct PatchInstanceState *pis = cls;
239 :
240 4 : if (NULL != pis->iph)
241 : {
242 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243 : "PATCH /instance/$ID operation did not complete\n");
244 0 : TALER_MERCHANT_patch_management_instances_cancel (pis->iph);
245 : }
246 4 : json_decref (pis->jurisdiction);
247 4 : json_decref (pis->address);
248 4 : GNUNET_free (pis);
249 4 : }
250 :
251 :
252 : struct TALER_TESTING_Command
253 4 : TALER_TESTING_cmd_merchant_patch_instance (
254 : const char *label,
255 : const char *merchant_url,
256 : const char *instance_id,
257 : const char *name,
258 : json_t *address,
259 : json_t *jurisdiction,
260 : bool use_stefan,
261 : struct GNUNET_TIME_Relative default_wire_transfer_delay,
262 : struct GNUNET_TIME_Relative default_pay_delay,
263 : unsigned int http_status)
264 : {
265 : struct PatchInstanceState *pis;
266 :
267 4 : pis = GNUNET_new (struct PatchInstanceState);
268 4 : pis->merchant_url = merchant_url;
269 4 : pis->instance_id = instance_id;
270 4 : pis->http_status = http_status;
271 4 : pis->name = name;
272 4 : pis->address = address; /* ownership transfer! */
273 4 : pis->jurisdiction = jurisdiction; /* ownership transfer! */
274 4 : pis->use_stefan = use_stefan;
275 4 : pis->default_wire_transfer_delay = default_wire_transfer_delay;
276 4 : pis->default_pay_delay = default_pay_delay;
277 : {
278 4 : struct TALER_TESTING_Command cmd = {
279 : .cls = pis,
280 : .label = label,
281 : .run = &patch_instance_run,
282 : .cleanup = &patch_instance_cleanup,
283 : .traits = &patch_instance_traits
284 : };
285 :
286 4 : return cmd;
287 : }
288 : }
289 :
290 :
291 : /* end of testing_api_cmd_patch_instance.c */
|