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