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