testing_api_cmd_patch_template.c (6388B)
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_patch_template.c 21 * @brief command to test PATCH /template 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/patch-private-templates-TEMPLATE_ID.h> 30 31 32 /** 33 * State of a "PATCH /template" CMD. 34 */ 35 struct PatchTemplateState 36 { 37 38 /** 39 * Handle for a "GET template" request. 40 */ 41 struct TALER_MERCHANT_PatchPrivateTemplateHandle *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 GET 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 PATCH /templates/$ID operation. 83 * 84 * @param cls closure for this function 85 * @param hr response being processed 86 */ 87 static void 88 patch_template_cb (void *cls, 89 const struct TALER_MERCHANT_PatchPrivateTemplateResponse * 90 result) 91 { 92 struct PatchTemplateState *pis = cls; 93 const struct TALER_MERCHANT_HttpResponse *hr = &result->hr; 94 95 pis->iph = NULL; 96 if (pis->http_status != hr->http_status) 97 { 98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 99 "Unexpected response code %u (%d) to command %s\n", 100 hr->http_status, 101 (int) hr->ec, 102 TALER_TESTING_interpreter_get_current_label (pis->is)); 103 TALER_TESTING_interpreter_fail (pis->is); 104 return; 105 } 106 switch (hr->http_status) 107 { 108 case MHD_HTTP_NO_CONTENT: 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_log (GNUNET_ERROR_TYPE_WARNING, 120 "Unhandled HTTP status %u for PATCH /templates/ID.\n", 121 hr->http_status); 122 } 123 TALER_TESTING_interpreter_next (pis->is); 124 } 125 126 127 /** 128 * Run the "PATCH /templates/$ID" CMD. 129 * 130 * 131 * @param cls closure. 132 * @param cmd command being run now. 133 * @param is interpreter state. 134 */ 135 static void 136 patch_template_run (void *cls, 137 const struct TALER_TESTING_Command *cmd, 138 struct TALER_TESTING_Interpreter *is) 139 { 140 struct PatchTemplateState *pis = cls; 141 142 pis->is = is; 143 pis->iph = TALER_MERCHANT_patch_private_template_create ( 144 TALER_TESTING_interpreter_get_context (is), 145 pis->merchant_url, 146 pis->template_id, 147 pis->template_description, 148 pis->otp_id, 149 pis->template_contract); 150 GNUNET_assert (NULL != pis->iph); 151 { 152 enum TALER_ErrorCode ec; 153 154 ec = TALER_MERCHANT_patch_private_template_start ( 155 pis->iph, 156 &patch_template_cb, 157 pis); 158 GNUNET_assert (TALER_EC_NONE == ec); 159 } 160 } 161 162 163 /** 164 * Offers information from the PATCH /templates CMD state to other 165 * commands. 166 * 167 * @param cls closure 168 * @param[out] ret result (could be anything) 169 * @param trait name of the trait 170 * @param index index number of the object to extract. 171 * @return #GNUNET_OK on success 172 */ 173 static enum GNUNET_GenericReturnValue 174 patch_template_traits (void *cls, 175 const void **ret, 176 const char *trait, 177 unsigned int index) 178 { 179 struct PatchTemplateState *pts = cls; 180 struct TALER_TESTING_Trait traits[] = { 181 TALER_TESTING_make_trait_template_description (pts->template_description), 182 TALER_TESTING_make_trait_otp_id (pts->otp_id), 183 TALER_TESTING_make_trait_template_contract (pts->template_contract), 184 TALER_TESTING_make_trait_template_id (pts->template_id), 185 TALER_TESTING_trait_end (), 186 }; 187 188 return TALER_TESTING_get_trait (traits, 189 ret, 190 trait, 191 index); 192 } 193 194 195 /** 196 * Free the state of a "GET template" CMD, and possibly 197 * cancel a pending operation thereof. 198 * 199 * @param cls closure. 200 * @param cmd command being run. 201 */ 202 static void 203 patch_template_cleanup (void *cls, 204 const struct TALER_TESTING_Command *cmd) 205 { 206 struct PatchTemplateState *pis = cls; 207 208 if (NULL != pis->iph) 209 { 210 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 211 "PATCH /templates/$ID operation did not complete\n"); 212 TALER_MERCHANT_patch_private_template_cancel (pis->iph); 213 } 214 GNUNET_free (pis->otp_id); 215 json_decref (pis->template_contract); 216 GNUNET_free (pis); 217 } 218 219 220 struct TALER_TESTING_Command 221 TALER_TESTING_cmd_merchant_patch_template ( 222 const char *label, 223 const char *merchant_url, 224 const char *template_id, 225 const char *template_description, 226 const char *otp_id, 227 json_t *template_contract, 228 unsigned int http_status) 229 { 230 struct PatchTemplateState *pis; 231 232 pis = GNUNET_new (struct PatchTemplateState); 233 pis->merchant_url = merchant_url; 234 pis->template_id = template_id; 235 pis->http_status = http_status; 236 pis->template_description = template_description; 237 pis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id); 238 pis->template_contract = template_contract; /* ownership taken */ 239 { 240 struct TALER_TESTING_Command cmd = { 241 .cls = pis, 242 .label = label, 243 .run = &patch_template_run, 244 .cleanup = &patch_template_cleanup, 245 .traits = &patch_template_traits 246 }; 247 248 return cmd; 249 } 250 } 251 252 253 /* end of testing_api_cmd_patch_template.c */