testing_api_cmd_post_instances.c (8303B)
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 post_instances_cb ( 111 struct PostInstancesState *pis, 112 const struct TALER_MERCHANT_PostManagementInstancesResponse *mir) 113 { 114 115 pis->iph = NULL; 116 if (pis->http_status != mir->hr.http_status) 117 { 118 TALER_TESTING_unexpected_status_with_body ( 119 pis->is, 120 mir->hr.http_status, 121 pis->http_status, 122 mir->hr.reply); 123 TALER_TESTING_interpreter_fail (pis->is); 124 return; 125 } 126 switch (mir->hr.http_status) 127 { 128 case MHD_HTTP_NO_CONTENT: 129 break; 130 case MHD_HTTP_BAD_REQUEST: 131 break; 132 case MHD_HTTP_UNAUTHORIZED: 133 break; 134 case MHD_HTTP_FORBIDDEN: 135 break; 136 case MHD_HTTP_NOT_FOUND: 137 break; 138 case MHD_HTTP_CONFLICT: 139 break; 140 default: 141 GNUNET_break (0); 142 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 143 "Unhandled HTTP status %u for POST instances.\n", 144 mir->hr.http_status); 145 } 146 TALER_TESTING_interpreter_next (pis->is); 147 } 148 149 150 /** 151 * Run the "POST /instances" CMD. 152 * 153 * 154 * @param cls closure. 155 * @param cmd command being run now. 156 * @param is interpreter state. 157 */ 158 static void 159 post_instances_run ( 160 void *cls, 161 const struct TALER_TESTING_Command *cmd, 162 struct TALER_TESTING_Interpreter *is) 163 { 164 struct PostInstancesState *pis = cls; 165 166 pis->is = is; 167 pis->iph = TALER_MERCHANT_post_management_instances_create ( 168 TALER_TESTING_interpreter_get_context (is), 169 pis->merchant_url, 170 pis->instance_id, 171 pis->name, 172 pis->address, 173 pis->jurisdiction, 174 pis->use_stefan); 175 TALER_MERCHANT_post_management_instances_set_options ( 176 pis->iph, 177 TALER_MERCHANT_post_management_instances_option_default_wire_transfer_delay ( 178 pis->default_wire_transfer_delay), 179 TALER_MERCHANT_post_management_instances_option_default_pay_delay ( 180 pis->default_pay_delay), 181 TALER_MERCHANT_post_management_instances_option_default_refund_delay ( 182 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS, 183 15)), 184 TALER_MERCHANT_post_management_instances_option_auth_password ( 185 pis->auth_token)); 186 { 187 enum TALER_ErrorCode ec; 188 189 ec = TALER_MERCHANT_post_management_instances_start ( 190 pis->iph, 191 &post_instances_cb, 192 pis); 193 if (TALER_EC_NONE != ec) 194 { 195 GNUNET_break (0); 196 TALER_TESTING_interpreter_fail (pis->is); 197 return; 198 } 199 } 200 } 201 202 203 /** 204 * Offers information from the POST /instances CMD state to other 205 * commands. 206 * 207 * @param cls closure 208 * @param[out] ret result (could be anything) 209 * @param trait name of the trait 210 * @param index index number of the object to extract. 211 * @return #GNUNET_OK on success 212 */ 213 static enum GNUNET_GenericReturnValue 214 post_instances_traits (void *cls, 215 const void **ret, 216 const char *trait, 217 unsigned int index) 218 { 219 struct PostInstancesState *pis = cls; 220 struct TALER_TESTING_Trait traits[] = { 221 TALER_TESTING_make_trait_instance_name (pis->name), 222 TALER_TESTING_make_trait_instance_id (pis->instance_id), 223 TALER_TESTING_make_trait_address (pis->address), 224 TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction), 225 TALER_TESTING_make_trait_use_stefan (&pis->use_stefan), 226 TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay), 227 TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay), 228 TALER_TESTING_trait_end () 229 }; 230 231 return TALER_TESTING_get_trait (traits, 232 ret, 233 trait, 234 index); 235 } 236 237 238 /** 239 * Free the state of a "POST /instances" CMD, and possibly 240 * cancel a pending operation thereof. 241 * 242 * @param cls closure. 243 * @param cmd command being run. 244 */ 245 static void 246 post_instances_cleanup (void *cls, 247 const struct TALER_TESTING_Command *cmd) 248 { 249 struct PostInstancesState *pis = cls; 250 251 if (NULL != pis->iph) 252 { 253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 254 "POST /instances operation did not complete\n"); 255 TALER_MERCHANT_post_management_instances_cancel (pis->iph); 256 } 257 json_decref (pis->address); 258 json_decref (pis->jurisdiction); 259 GNUNET_free (pis); 260 } 261 262 263 struct TALER_TESTING_Command 264 TALER_TESTING_cmd_merchant_post_instances2 ( 265 const char *label, 266 const char *merchant_url, 267 const char *instance_id, 268 const char *name, 269 json_t *address, 270 json_t *jurisdiction, 271 bool use_stefan, 272 struct GNUNET_TIME_Relative default_wire_transfer_delay, 273 struct GNUNET_TIME_Relative default_pay_delay, 274 const char *auth_token, 275 unsigned int http_status) 276 { 277 struct PostInstancesState *pis; 278 279 pis = GNUNET_new (struct PostInstancesState); 280 pis->merchant_url = merchant_url; 281 pis->instance_id = instance_id; 282 pis->http_status = http_status; 283 pis->name = name; 284 pis->address = address; /* ownership transfer! */ 285 pis->jurisdiction = jurisdiction; /* ownership transfer! */ 286 pis->use_stefan = use_stefan; 287 pis->default_wire_transfer_delay = default_wire_transfer_delay; 288 pis->default_pay_delay = default_pay_delay; 289 pis->auth_token = auth_token; 290 { 291 struct TALER_TESTING_Command cmd = { 292 .cls = pis, 293 .label = label, 294 .run = &post_instances_run, 295 .cleanup = &post_instances_cleanup, 296 .traits = &post_instances_traits 297 }; 298 299 return cmd; 300 } 301 } 302 303 304 struct TALER_TESTING_Command 305 TALER_TESTING_cmd_merchant_post_instances ( 306 const char *label, 307 const char *merchant_url, 308 const char *instance_id, 309 unsigned int http_status) 310 { 311 return TALER_TESTING_cmd_merchant_post_instances2 ( 312 label, 313 merchant_url, 314 instance_id, 315 instance_id, 316 json_pack ("{s:s}", "city", "shopcity"), 317 json_pack ("{s:s}", "city", "lawyercity"), 318 true, 319 GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */ 320 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 321 2), /* small pay delay */ 322 NULL, 323 http_status); 324 } 325 326 327 /* end of testing_api_cmd_post_instance.c */