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