testing_api_cmd_post_templates.c (7439B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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_templates.c 21 * @brief command to test POST /templates 22 * @author Priscilla HUANG 23 */ 24 #include "platform.h" 25 struct PostTemplatesState; 26 #define TALER_MERCHANT_POST_PRIVATE_TEMPLATES_RESULT_CLOSURE struct PostTemplatesState 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-private-templates.h> 32 33 34 /** 35 * State of a "POST /templates" CMD. 36 */ 37 struct PostTemplatesState 38 { 39 40 /** 41 * Handle for a "GET template" request. 42 */ 43 struct TALER_MERCHANT_PostPrivateTemplatesHandle *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 template to run POST for. 57 */ 58 const char *template_id; 59 60 /** 61 * description of the template 62 */ 63 const char *template_description; 64 65 /** 66 * OTP device ID. 67 */ 68 char *otp_id; 69 70 /** 71 * Contract of the company 72 */ 73 json_t *template_contract; 74 75 /** 76 * Expected HTTP response code. 77 */ 78 unsigned int http_status; 79 80 }; 81 82 83 /** 84 * Callback for a POST /templates operation. 85 * 86 * @param cls closure for this function 87 * @param ptr response being processed 88 */ 89 static void 90 post_templates_cb (struct PostTemplatesState *tis, 91 const struct TALER_MERCHANT_PostPrivateTemplatesResponse *ptr 92 ) 93 { 94 95 tis->iph = NULL; 96 if (tis->http_status != ptr->hr.http_status) 97 { 98 TALER_TESTING_unexpected_status_with_body (tis->is, 99 ptr->hr.http_status, 100 tis->http_status, 101 ptr->hr.reply); 102 return; 103 } 104 switch (ptr->hr.http_status) 105 { 106 case MHD_HTTP_NO_CONTENT: 107 break; 108 case MHD_HTTP_BAD_REQUEST: 109 break; 110 case MHD_HTTP_UNAUTHORIZED: 111 break; 112 case MHD_HTTP_FORBIDDEN: 113 break; 114 case MHD_HTTP_NOT_FOUND: 115 break; 116 case MHD_HTTP_CONFLICT: 117 break; 118 default: 119 GNUNET_break (0); 120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 121 "Unhandled HTTP status %u for POST /templates.\n", 122 ptr->hr.http_status); 123 } 124 TALER_TESTING_interpreter_next (tis->is); 125 } 126 127 128 /** 129 * Run the "POST /templates" CMD. 130 * 131 * 132 * @param cls closure. 133 * @param cmd command being run now. 134 * @param is interpreter state. 135 */ 136 static void 137 post_templates_run (void *cls, 138 const struct TALER_TESTING_Command *cmd, 139 struct TALER_TESTING_Interpreter *is) 140 { 141 struct PostTemplatesState *tis = cls; 142 143 tis->is = is; 144 tis->iph = TALER_MERCHANT_post_private_templates_create ( 145 TALER_TESTING_interpreter_get_context (is), 146 tis->merchant_url, 147 tis->template_id, 148 tis->template_description, 149 tis->template_contract); 150 if (NULL != tis->otp_id) 151 TALER_MERCHANT_post_private_templates_set_options ( 152 tis->iph, 153 TALER_MERCHANT_post_private_templates_option_otp_id ( 154 tis->otp_id)); 155 { 156 enum TALER_ErrorCode ec; 157 158 ec = TALER_MERCHANT_post_private_templates_start ( 159 tis->iph, 160 &post_templates_cb, 161 tis); 162 if (TALER_EC_NONE != ec) 163 { 164 GNUNET_break (0); 165 TALER_TESTING_interpreter_fail (tis->is); 166 return; 167 } 168 } 169 } 170 171 172 /** 173 * Offers information from the POST /templates 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 post_templates_traits (void *cls, 184 const void **ret, 185 const char *trait, 186 unsigned int index) 187 { 188 struct PostTemplatesState *pts = cls; 189 struct TALER_TESTING_Trait traits[] = { 190 TALER_TESTING_make_trait_template_description (pts->template_description), 191 TALER_TESTING_make_trait_otp_id (pts->otp_id), 192 TALER_TESTING_make_trait_template_contract (pts->template_contract), 193 TALER_TESTING_make_trait_template_id (pts->template_id), 194 TALER_TESTING_trait_end (), 195 }; 196 197 return TALER_TESTING_get_trait (traits, 198 ret, 199 trait, 200 index); 201 } 202 203 204 /** 205 * Free the state of a "POST template" CMD, and possibly 206 * cancel a pending operation thereof. 207 * 208 * @param cls closure. 209 * @param cmd command being run. 210 */ 211 static void 212 post_templates_cleanup (void *cls, 213 const struct TALER_TESTING_Command *cmd) 214 { 215 struct PostTemplatesState *tis = cls; 216 217 if (NULL != tis->iph) 218 { 219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 220 "POST /templates operation did not complete\n"); 221 TALER_MERCHANT_post_private_templates_cancel (tis->iph); 222 } 223 GNUNET_free (tis->otp_id); 224 json_decref (tis->template_contract); 225 GNUNET_free (tis); 226 } 227 228 229 struct TALER_TESTING_Command 230 TALER_TESTING_cmd_merchant_post_templates2 ( 231 const char *label, 232 const char *merchant_url, 233 const char *template_id, 234 const char *template_description, 235 const char *otp_id, 236 json_t *template_contract, 237 unsigned int http_status) 238 { 239 struct PostTemplatesState *tis; 240 241 GNUNET_assert ((NULL == template_contract) || 242 json_is_object (template_contract)); 243 244 tis = GNUNET_new (struct PostTemplatesState); 245 tis->merchant_url = merchant_url; 246 tis->template_id = template_id; 247 tis->http_status = http_status; 248 tis->template_description = template_description; 249 tis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id); 250 tis->template_contract = template_contract; 251 { 252 struct TALER_TESTING_Command cmd = { 253 .cls = tis, 254 .label = label, 255 .run = &post_templates_run, 256 .cleanup = &post_templates_cleanup, 257 .traits = &post_templates_traits 258 }; 259 260 return cmd; 261 } 262 } 263 264 265 struct TALER_TESTING_Command 266 TALER_TESTING_cmd_merchant_post_templates (const char *label, 267 const char *merchant_url, 268 const char *template_id, 269 const char *template_description, 270 unsigned int http_status) 271 { 272 return TALER_TESTING_cmd_merchant_post_templates2 ( 273 label, 274 merchant_url, 275 template_id, 276 template_description, 277 NULL, 278 GNUNET_JSON_PACK ( 279 GNUNET_JSON_pack_uint64 ("minimum_age", 0), 280 GNUNET_JSON_pack_time_rel ("pay_duration", 281 GNUNET_TIME_UNIT_MINUTES)), 282 http_status); 283 } 284 285 286 /* end of testing_api_cmd_post_templates.c */