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