testing_api_cmd_get_webhooks.c (6215B)
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 src/testing/testing_api_cmd_get_webhooks.c 21 * @brief command to test GET /webhooks 22 * @author Priscilla HUANG 23 */ 24 #include "platform.h" 25 struct GetWebhooksState; 26 #define TALER_MERCHANT_GET_PRIVATE_WEBHOOKS_RESULT_CLOSURE struct GetWebhooksState 27 #include <taler/taler_exchange_service.h> 28 #include <taler/taler_testing_lib.h> 29 #include "taler/taler_merchant_service.h" 30 #include "taler/taler_merchant_testing_lib.h" 31 #include <taler/merchant/get-private-webhooks.h> 32 33 34 /** 35 * State of a "GET webhooks" CMD. 36 */ 37 struct GetWebhooksState 38 { 39 40 /** 41 * Handle for a "GET webhook" request. 42 */ 43 struct TALER_MERCHANT_GetPrivateWebhooksHandle *igh; 44 45 /** 46 * The interpreter state. 47 */ 48 struct TALER_TESTING_Interpreter *is; 49 50 /** 51 * Base URL of the merchant serving the request. 52 */ 53 const char *merchant_url; 54 55 /** 56 * Expected HTTP response code. 57 */ 58 unsigned int http_status; 59 60 /** 61 * The list of webhook references. 62 */ 63 const char **webhooks; 64 65 /** 66 * Length of @e webhooks. 67 */ 68 unsigned int webhooks_length; 69 70 }; 71 72 73 /** 74 * Callback for a GET /webhooks operation. 75 * 76 * @param cls closure for this function 77 * @param wgr response details 78 */ 79 static void 80 get_webhooks_cb (struct GetWebhooksState *gis, 81 const struct TALER_MERCHANT_GetPrivateWebhooksResponse *wgr) 82 { 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 if (wgr->details.ok.webhooks_length != gis->webhooks_length) 99 { 100 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 101 "Length of webhooks found does not match\n"); 102 TALER_TESTING_interpreter_fail (gis->is); 103 return; 104 } 105 for (unsigned int i = 0; i < gis->webhooks_length; ++i) 106 { 107 const struct TALER_TESTING_Command *webhook_cmd; 108 109 webhook_cmd = TALER_TESTING_interpreter_lookup_command ( 110 gis->is, 111 gis->webhooks[i]); 112 113 { 114 const char *webhook_id; 115 116 if (GNUNET_OK != 117 TALER_TESTING_get_trait_webhook_id (webhook_cmd, 118 &webhook_id)) 119 { 120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 121 "Could not fetch webhook id\n"); 122 TALER_TESTING_interpreter_fail (gis->is); 123 return; 124 } 125 if (0 != strcmp (wgr->details.ok.webhooks[i].webhook_id, 126 webhook_id)) 127 { 128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 129 "Webhook id does not match\n"); 130 TALER_TESTING_interpreter_fail (gis->is); 131 return; 132 } 133 } 134 } 135 break; 136 case MHD_HTTP_UNAUTHORIZED: 137 break; 138 case MHD_HTTP_NOT_FOUND: 139 /* instance does not exist */ 140 break; 141 default: 142 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 143 "Unhandled HTTP status %u (%d).\n", 144 wgr->hr.http_status, 145 wgr->hr.ec); 146 } 147 TALER_TESTING_interpreter_next (gis->is); 148 } 149 150 151 /** 152 * Run the "GET /webhooks" CMD. 153 * 154 * 155 * @param cls closure. 156 * @param cmd command being run now. 157 * @param is interpreter state. 158 */ 159 static void 160 get_webhooks_run (void *cls, 161 const struct TALER_TESTING_Command *cmd, 162 struct TALER_TESTING_Interpreter *is) 163 { 164 struct GetWebhooksState *gis = cls; 165 166 gis->is = is; 167 gis->igh = TALER_MERCHANT_get_private_webhooks_create ( 168 TALER_TESTING_interpreter_get_context (is), 169 gis->merchant_url); 170 { 171 enum TALER_ErrorCode ec; 172 173 ec = TALER_MERCHANT_get_private_webhooks_start ( 174 gis->igh, 175 &get_webhooks_cb, 176 gis); 177 GNUNET_assert (TALER_EC_NONE == ec); 178 } 179 } 180 181 182 /** 183 * Free the state of a "GET webhook" CMD, and possibly 184 * cancel a pending operation thereof. 185 * 186 * @param cls closure. 187 * @param cmd command being run. 188 */ 189 static void 190 get_webhooks_cleanup (void *cls, 191 const struct TALER_TESTING_Command *cmd) 192 { 193 struct GetWebhooksState *gis = cls; 194 195 if (NULL != gis->igh) 196 { 197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 198 "GET /webhooks operation did not complete\n"); 199 TALER_MERCHANT_get_private_webhooks_cancel (gis->igh); 200 } 201 GNUNET_array_grow (gis->webhooks, 202 gis->webhooks_length, 203 0); 204 GNUNET_free (gis); 205 } 206 207 208 struct TALER_TESTING_Command 209 TALER_TESTING_cmd_merchant_get_webhooks (const char *label, 210 const char *merchant_url, 211 unsigned int http_status, 212 ...) 213 { 214 struct GetWebhooksState *gis; 215 216 gis = GNUNET_new (struct GetWebhooksState); 217 gis->merchant_url = merchant_url; 218 gis->http_status = http_status; 219 { 220 const char *clabel; 221 va_list ap; 222 223 va_start (ap, http_status); 224 while (NULL != (clabel = va_arg (ap, const char *))) 225 { 226 GNUNET_array_append (gis->webhooks, 227 gis->webhooks_length, 228 clabel); 229 } 230 va_end (ap); 231 } 232 { 233 struct TALER_TESTING_Command cmd = { 234 .cls = gis, 235 .label = label, 236 .run = &get_webhooks_run, 237 .cleanup = &get_webhooks_cleanup 238 }; 239 240 return cmd; 241 } 242 } 243 244 245 /* end of testing_api_cmd_get_webhooks.c */