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