testing_api_cmd_get_webhook.c (8173B)
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_get_webhook.c 21 * @brief command to test GET /webhooks/$ID 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 "GET webhook" CMD. 33 */ 34 struct GetWebhookState 35 { 36 37 /** 38 * Handle for a "GET webhook" request. 39 */ 40 struct TALER_MERCHANT_WebhookGetHandle *igh; 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 GET for. 54 */ 55 const char *webhook_id; 56 57 /** 58 * Reference for a POST or PATCH /webhooks CMD (optional). 59 */ 60 const char *webhook_reference; 61 62 /** 63 * Expected HTTP response code. 64 */ 65 unsigned int http_status; 66 67 }; 68 69 70 /** 71 * Callback for a /get/webhooks/$ID operation. 72 * 73 * @param cls closure for this function 74 * @param hr HTTP response details 75 * @param event_type event of the webhook 76 * @param url use by the customer 77 * @param http_method method use by the merchant 78 * @param header_template of the webhook 79 * @param body_template of the webhook 80 */ 81 static void 82 get_webhook_cb (void *cls, 83 const struct TALER_MERCHANT_HttpResponse *hr, 84 const char *event_type, 85 const char *url, 86 const char *http_method, 87 const char *header_template, 88 const char *body_template) 89 { 90 struct GetWebhookState *gis = cls; 91 const struct TALER_TESTING_Command *webhook_cmd; 92 93 gis->igh = NULL; 94 if (gis->http_status != hr->http_status) 95 { 96 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 97 "Unexpected response code %u (%d) to command %s\n", 98 hr->http_status, 99 (int) hr->ec, 100 TALER_TESTING_interpreter_get_current_label (gis->is)); 101 TALER_TESTING_interpreter_fail (gis->is); 102 return; 103 } 104 switch (hr->http_status) 105 { 106 case MHD_HTTP_OK: 107 { 108 const char *expected_event_type; 109 110 webhook_cmd = TALER_TESTING_interpreter_lookup_command ( 111 gis->is, 112 gis->webhook_reference); 113 if (GNUNET_OK != 114 TALER_TESTING_get_trait_event_type (webhook_cmd, 115 &expected_event_type)) 116 TALER_TESTING_interpreter_fail (gis->is); 117 if (0 != strcmp (event_type, 118 expected_event_type)) 119 { 120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 121 "Event type does not match\n"); 122 TALER_TESTING_interpreter_fail (gis->is); 123 return; 124 } 125 } 126 { 127 const char *expected_url; 128 129 if (GNUNET_OK != 130 TALER_TESTING_get_trait_url (webhook_cmd, 131 &expected_url)) 132 TALER_TESTING_interpreter_fail (gis->is); 133 if (0 != strcmp (url, 134 expected_url)) 135 { 136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 137 "URL does not match\n"); 138 TALER_TESTING_interpreter_fail (gis->is); 139 return; 140 } 141 } 142 { 143 const char *expected_http_method; 144 145 if (GNUNET_OK != 146 TALER_TESTING_get_trait_http_method (webhook_cmd, 147 &expected_http_method)) 148 TALER_TESTING_interpreter_fail (gis->is); 149 if (0 != strcmp (http_method, 150 expected_http_method)) 151 { 152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 153 "http_method does not match\n"); 154 TALER_TESTING_interpreter_fail (gis->is); 155 return; 156 } 157 } 158 { 159 const char *expected_header_template; 160 161 if (GNUNET_OK != 162 TALER_TESTING_get_trait_header_template (webhook_cmd, 163 &expected_header_template)) 164 TALER_TESTING_interpreter_fail (gis->is); 165 if ( ( (NULL == header_template) && (NULL != expected_header_template)) || 166 ( (NULL != header_template) && (NULL == expected_header_template)) || 167 ( (NULL != header_template) && 168 (0 != strcmp (header_template, 169 expected_header_template)) ) ) 170 { 171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 172 "header template does not match\n"); 173 TALER_TESTING_interpreter_fail (gis->is); 174 return; 175 } 176 } 177 { 178 const char *expected_body_template; 179 180 if (GNUNET_OK != 181 TALER_TESTING_get_trait_body_template (webhook_cmd, 182 &expected_body_template)) 183 TALER_TESTING_interpreter_fail (gis->is); 184 if ( ( (NULL == body_template) && (NULL != expected_body_template)) || 185 ( (NULL != body_template) && (NULL == expected_body_template)) || 186 ( (NULL != body_template) && 187 (0 != strcmp (body_template, 188 expected_body_template)) ) ) 189 { 190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 191 "body template does not match\n"); 192 TALER_TESTING_interpreter_fail (gis->is); 193 return; 194 } 195 } 196 break; 197 case MHD_HTTP_UNAUTHORIZED: 198 break; 199 case MHD_HTTP_NOT_FOUND: 200 break; 201 default: 202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 203 "Unhandled HTTP status.\n"); 204 } 205 TALER_TESTING_interpreter_next (gis->is); 206 } 207 208 209 /** 210 * Run the "GET webhook" CMD. 211 * 212 * 213 * @param cls closure. 214 * @param cmd command being run now. 215 * @param is interpreter state. 216 */ 217 static void 218 get_webhook_run (void *cls, 219 const struct TALER_TESTING_Command *cmd, 220 struct TALER_TESTING_Interpreter *is) 221 { 222 struct GetWebhookState *gis = cls; 223 224 gis->is = is; 225 gis->igh = TALER_MERCHANT_webhook_get (TALER_TESTING_interpreter_get_context ( 226 is), 227 gis->merchant_url, 228 gis->webhook_id, 229 &get_webhook_cb, 230 gis); 231 GNUNET_assert (NULL != gis->igh); 232 } 233 234 235 /** 236 * Free the state of a "GET webhook" CMD, and possibly 237 * cancel a pending operation thereof. 238 * 239 * @param cls closure. 240 * @param cmd command being run. 241 */ 242 static void 243 get_webhook_cleanup (void *cls, 244 const struct TALER_TESTING_Command *cmd) 245 { 246 struct GetWebhookState *gis = cls; 247 248 if (NULL != gis->igh) 249 { 250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 251 "GET /webhooks/$ID operation did not complete\n"); 252 TALER_MERCHANT_webhook_get_cancel (gis->igh); 253 } 254 GNUNET_free (gis); 255 } 256 257 258 struct TALER_TESTING_Command 259 TALER_TESTING_cmd_merchant_get_webhook (const char *label, 260 const char *merchant_url, 261 const char *webhook_id, 262 unsigned int http_status, 263 const char *webhook_reference) 264 { 265 struct GetWebhookState *gis; 266 267 gis = GNUNET_new (struct GetWebhookState); 268 gis->merchant_url = merchant_url; 269 gis->webhook_id = webhook_id; 270 gis->http_status = http_status; 271 gis->webhook_reference = webhook_reference; 272 { 273 struct TALER_TESTING_Command cmd = { 274 .cls = gis, 275 .label = label, 276 .run = &get_webhook_run, 277 .cleanup = &get_webhook_cleanup 278 }; 279 280 return cmd; 281 } 282 } 283 284 285 /* end of testing_api_cmd_get_webhook.c */