testing_api_cmd_patch_webhook.c (6994B)
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_webhook.c 21 * @brief command to test PATCH /webhook 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-webhooks-WEBHOOK_ID.h> 30 31 32 /** 33 * State of a "PATCH /webhook" CMD. 34 */ 35 struct PatchWebhookState 36 { 37 38 /** 39 * Handle for a "GET webhook" request. 40 */ 41 struct TALER_MERCHANT_PatchPrivateWebhookHandle *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 webhook to run GET for. 55 */ 56 const char *webhook_id; 57 58 /** 59 * event of the webhook 60 */ 61 const char *event_type; 62 63 /** 64 * url use by the customer 65 */ 66 const char *url; 67 68 /** 69 * http_method use by the merchant 70 */ 71 const char *http_method; 72 73 /** 74 * header of the webhook 75 */ 76 const char *header_template; 77 78 /** 79 * body_template of the webhook 80 */ 81 const char *body_template; 82 83 /** 84 * Expected HTTP response code. 85 */ 86 unsigned int http_status; 87 88 }; 89 90 91 /** 92 * Callback for a PATCH /webhooks/$ID operation. 93 * 94 * @param cls closure for this function 95 * @param hr response being processed 96 */ 97 static void 98 patch_webhook_cb (void *cls, 99 const struct TALER_MERCHANT_PatchPrivateWebhookResponse * 100 result) 101 { 102 struct PatchWebhookState *pis = cls; 103 const struct TALER_MERCHANT_HttpResponse *hr = &result->hr; 104 105 pis->iph = NULL; 106 if (pis->http_status != hr->http_status) 107 { 108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 109 "Unexpected response code %u (%d) to command %s\n", 110 hr->http_status, 111 (int) hr->ec, 112 TALER_TESTING_interpreter_get_current_label (pis->is)); 113 TALER_TESTING_interpreter_fail (pis->is); 114 return; 115 } 116 switch (hr->http_status) 117 { 118 case MHD_HTTP_NO_CONTENT: 119 break; 120 case MHD_HTTP_UNAUTHORIZED: 121 break; 122 case MHD_HTTP_FORBIDDEN: 123 break; 124 case MHD_HTTP_NOT_FOUND: 125 break; 126 case MHD_HTTP_CONFLICT: 127 break; 128 default: 129 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 130 "Unhandled HTTP status %u for PATCH /webhooks/ID.\n", 131 hr->http_status); 132 } 133 TALER_TESTING_interpreter_next (pis->is); 134 } 135 136 137 /** 138 * Run the "PATCH /webhooks/$ID" CMD. 139 * 140 * 141 * @param cls closure. 142 * @param cmd command being run now. 143 * @param is interpreter state. 144 */ 145 static void 146 patch_webhook_run (void *cls, 147 const struct TALER_TESTING_Command *cmd, 148 struct TALER_TESTING_Interpreter *is) 149 { 150 struct PatchWebhookState *pis = cls; 151 152 pis->is = is; 153 pis->iph = TALER_MERCHANT_patch_private_webhook_create ( 154 TALER_TESTING_interpreter_get_context (is), 155 pis->merchant_url, 156 pis->webhook_id, 157 pis->event_type, 158 pis->url, 159 pis->http_method); 160 GNUNET_assert (NULL != pis->iph); 161 if (NULL != pis->header_template) 162 TALER_MERCHANT_patch_private_webhook_set_options ( 163 pis->iph, 164 TALER_MERCHANT_patch_private_webhook_option_header_template ( 165 pis->header_template)); 166 if (NULL != pis->body_template) 167 TALER_MERCHANT_patch_private_webhook_set_options ( 168 pis->iph, 169 TALER_MERCHANT_patch_private_webhook_option_body_template ( 170 pis->body_template)); 171 { 172 enum TALER_ErrorCode ec; 173 174 ec = TALER_MERCHANT_patch_private_webhook_start ( 175 pis->iph, 176 &patch_webhook_cb, 177 pis); 178 GNUNET_assert (TALER_EC_NONE == ec); 179 } 180 } 181 182 183 /** 184 * Offers information from the PATCH /webhooks CMD state to other 185 * commands. 186 * 187 * @param cls closure 188 * @param[out] ret result (could be anything) 189 * @param trait name of the trait 190 * @param index index number of the object to extract. 191 * @return #GNUNET_OK on success 192 */ 193 static enum GNUNET_GenericReturnValue 194 patch_webhook_traits (void *cls, 195 const void **ret, 196 const char *trait, 197 unsigned int index) 198 { 199 struct PatchWebhookState *pws = cls; 200 struct TALER_TESTING_Trait traits[] = { 201 TALER_TESTING_make_trait_event_type (pws->event_type), 202 TALER_TESTING_make_trait_url (pws->url), 203 TALER_TESTING_make_trait_http_method (pws->http_method), 204 TALER_TESTING_make_trait_header_template (pws->header_template), 205 TALER_TESTING_make_trait_body_template (pws->body_template), 206 TALER_TESTING_make_trait_webhook_id (pws->webhook_id), 207 TALER_TESTING_trait_end (), 208 }; 209 210 return TALER_TESTING_get_trait (traits, 211 ret, 212 trait, 213 index); 214 } 215 216 217 /** 218 * Free the state of a "GET webhook" CMD, and possibly 219 * cancel a pending operation thereof. 220 * 221 * @param cls closure. 222 * @param cmd command being run. 223 */ 224 static void 225 patch_webhook_cleanup (void *cls, 226 const struct TALER_TESTING_Command *cmd) 227 { 228 struct PatchWebhookState *pis = cls; 229 230 if (NULL != pis->iph) 231 { 232 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 233 "PATCH /webhooks/$ID operation did not complete\n"); 234 TALER_MERCHANT_patch_private_webhook_cancel (pis->iph); 235 } 236 GNUNET_free (pis); 237 } 238 239 240 struct TALER_TESTING_Command 241 TALER_TESTING_cmd_merchant_patch_webhook ( 242 const char *label, 243 const char *merchant_url, 244 const char *webhook_id, 245 const char *event_type, 246 const char *url, 247 const char *http_method, 248 const char *header_template, 249 const char *body_template, 250 unsigned int http_status) 251 { 252 struct PatchWebhookState *pis; 253 254 pis = GNUNET_new (struct PatchWebhookState); 255 pis->merchant_url = merchant_url; 256 pis->webhook_id = webhook_id; 257 pis->http_status = http_status; 258 pis->event_type = event_type; 259 pis->url = url; 260 pis->http_method = http_method; 261 pis->header_template = (NULL == header_template) ? NULL : header_template; 262 pis->body_template = (NULL == body_template) ? NULL : body_template; 263 { 264 struct TALER_TESTING_Command cmd = { 265 .cls = pis, 266 .label = label, 267 .run = &patch_webhook_run, 268 .cleanup = &patch_webhook_cleanup, 269 .traits = &patch_webhook_traits 270 }; 271 272 return cmd; 273 } 274 } 275 276 277 /* end of testing_api_cmd_patch_webhook.c */