testing_api_cmd_get_instance.c (9620B)
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 get_instance_cb (struct GetInstanceState *gis, 86 const struct TALER_MERCHANT_GetManagementInstanceResponse *igr) 87 { 88 const struct TALER_TESTING_Command *instance_cmd; 89 90 instance_cmd = TALER_TESTING_interpreter_lookup_command ( 91 gis->is, 92 gis->instance_reference); 93 94 gis->igh = NULL; 95 if (gis->http_status != igr->hr.http_status) 96 { 97 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 TALER_TESTING_interpreter_fail (gis->is); 103 return; 104 } 105 switch (igr->hr.http_status) 106 { 107 case MHD_HTTP_OK: 108 { 109 const struct TALER_MERCHANT_GetManagementInstanceDetails *details = 110 &igr->details.ok.details; 111 112 gis->account_pub.merchant_pub 113 = details->merchant_pub; 114 { 115 const char *name; 116 117 if (GNUNET_OK != 118 TALER_TESTING_get_trait_instance_name (instance_cmd, 119 &name)) 120 TALER_TESTING_interpreter_fail (gis->is); 121 if (0 != strcmp (details->name, 122 name)) 123 { 124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 125 "Instance name does not match: Got `%s', wanted `%s'\n", 126 details->name, 127 name); 128 TALER_TESTING_interpreter_fail (gis->is); 129 return; 130 } 131 } 132 { 133 const json_t *address; 134 135 if (GNUNET_OK != 136 TALER_TESTING_get_trait_address (instance_cmd, 137 &address)) 138 TALER_TESTING_interpreter_fail (gis->is); 139 if (1 != json_equal (details->address, 140 address)) 141 { 142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 143 "Instance address does not match\n"); 144 TALER_TESTING_interpreter_fail (gis->is); 145 return; 146 } 147 } 148 { 149 const json_t *jurisdiction; 150 151 if (GNUNET_OK != 152 TALER_TESTING_get_trait_jurisdiction (instance_cmd, 153 &jurisdiction)) 154 TALER_TESTING_interpreter_fail (gis->is); 155 if (1 != json_equal (details->jurisdiction, 156 jurisdiction)) 157 { 158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 159 "Instance jurisdiction does not match\n"); 160 TALER_TESTING_interpreter_fail (gis->is); 161 return; 162 } 163 } 164 { 165 const bool *use_stefan; 166 167 if (GNUNET_OK != 168 TALER_TESTING_get_trait_use_stefan (instance_cmd, 169 &use_stefan)) 170 TALER_TESTING_interpreter_fail (gis->is); 171 if (*use_stefan != details->use_stefan) 172 { 173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 174 "Instance use_stefan value does not match\n"); 175 TALER_TESTING_interpreter_fail (gis->is); 176 return; 177 } 178 } 179 { 180 const struct GNUNET_TIME_Relative *default_wire_transfer_delay; 181 182 if (GNUNET_OK != 183 TALER_TESTING_get_trait_wire_delay (instance_cmd, 184 &default_wire_transfer_delay)) 185 TALER_TESTING_interpreter_fail (gis->is); 186 if (details->default_wire_transfer_delay.rel_value_us != 187 default_wire_transfer_delay->rel_value_us) 188 { 189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 190 "Instance default wire transfer delay does not match\n"); 191 TALER_TESTING_interpreter_fail (gis->is); 192 return; 193 } 194 } 195 { 196 const struct GNUNET_TIME_Relative *default_pay_delay; 197 198 if (GNUNET_OK != 199 TALER_TESTING_get_trait_pay_delay (instance_cmd, 200 &default_pay_delay)) 201 TALER_TESTING_interpreter_fail (gis->is); 202 if (details->default_pay_delay.rel_value_us != 203 default_pay_delay->rel_value_us) 204 { 205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 206 "Instance default pay delay does not match\n"); 207 TALER_TESTING_interpreter_fail (gis->is); 208 return; 209 } 210 } 211 } 212 break; 213 case MHD_HTTP_UNAUTHORIZED: 214 break; 215 case MHD_HTTP_NOT_FOUND: 216 break; 217 default: 218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 219 "Unhandled HTTP status %u for GET instance ID.\n", 220 igr->hr.http_status); 221 } 222 TALER_TESTING_interpreter_next (gis->is); 223 } 224 225 226 /** 227 * Run the "GET instance" CMD. 228 * 229 * 230 * @param cls closure. 231 * @param cmd command being run now. 232 * @param is interpreter state. 233 */ 234 static void 235 get_instance_run (void *cls, 236 const struct TALER_TESTING_Command *cmd, 237 struct TALER_TESTING_Interpreter *is) 238 { 239 struct GetInstanceState *gis = cls; 240 241 gis->is = is; 242 gis->igh = TALER_MERCHANT_get_management_instance_create ( 243 TALER_TESTING_interpreter_get_context (is), 244 gis->merchant_url, 245 gis->instance_id); 246 { 247 enum TALER_ErrorCode ec; 248 249 ec = TALER_MERCHANT_get_management_instance_start ( 250 gis->igh, 251 &get_instance_cb, 252 gis); 253 GNUNET_assert (TALER_EC_NONE == ec); 254 } 255 } 256 257 258 /** 259 * Free the state of a "GET instance" CMD, and possibly 260 * cancel a pending operation thereof. 261 * 262 * @param cls closure. 263 * @param cmd command being run. 264 */ 265 static void 266 get_instance_cleanup (void *cls, 267 const struct TALER_TESTING_Command *cmd) 268 { 269 struct GetInstanceState *gis = cls; 270 271 if (NULL != gis->igh) 272 { 273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 274 "GET /instances/$ID operation did not complete\n"); 275 TALER_MERCHANT_get_management_instance_cancel (gis->igh); 276 } 277 GNUNET_free (gis); 278 } 279 280 281 /** 282 * Offers information from the GET /instance/$ID CMD state to other 283 * commands. 284 * 285 * @param cls closure 286 * @param[out] ret result (could be anything) 287 * @param trait name of the trait 288 * @param index index number of the object to extract. 289 * @return #GNUNET_OK on success 290 */ 291 static enum GNUNET_GenericReturnValue 292 get_instance_traits (void *cls, 293 const void **ret, 294 const char *trait, 295 unsigned int index) 296 { 297 struct GetInstanceState *pps = cls; 298 struct TALER_TESTING_Trait traits[] = { 299 TALER_TESTING_make_trait_merchant_base_url ( 300 pps->merchant_url), 301 TALER_TESTING_make_trait_merchant_pub ( 302 &pps->account_pub.merchant_pub), 303 TALER_TESTING_make_trait_account_pub ( 304 &pps->account_pub), 305 TALER_TESTING_trait_end (), 306 }; 307 308 return TALER_TESTING_get_trait (traits, 309 ret, 310 trait, 311 index); 312 } 313 314 315 struct TALER_TESTING_Command 316 TALER_TESTING_cmd_merchant_get_instance ( 317 const char *label, 318 const char *merchant_url, 319 const char *instance_id, 320 unsigned int http_status, 321 const char *instance_reference) 322 { 323 struct GetInstanceState *gis; 324 325 gis = GNUNET_new (struct GetInstanceState); 326 gis->merchant_url = merchant_url; 327 gis->instance_id = instance_id; 328 gis->http_status = http_status; 329 gis->instance_reference = instance_reference; 330 { 331 struct TALER_TESTING_Command cmd = { 332 .cls = gis, 333 .label = label, 334 .run = &get_instance_run, 335 .cleanup = &get_instance_cleanup, 336 .traits = &get_instance_traits 337 }; 338 339 return cmd; 340 } 341 } 342 343 344 /* end of testing_api_cmd_get_instance.c */