testing_api_cmd_post_donau_instances.c (6672B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020-2024 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, 11 but 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_donau_instances.c 21 * @brief command to test POST /donau request 22 * @author Bohdan Potuzhnyi 23 * @author Vlada Svirsh 24 */ 25 26 #include "platform.h" 27 #include <taler/taler_exchange_service.h> 28 #include <taler/taler_testing_lib.h> 29 #include "taler_merchant_service.h" 30 #include "taler_merchant_testing_lib.h" 31 #include "taler_merchant_donau.h" 32 #include <donau/donau_testing_lib.h> 33 34 /** 35 * State of a "POST /donau" CMD. 36 */ 37 struct PostDonauState 38 { 39 /** 40 * Handle for a "POST donau" request. 41 */ 42 struct TALER_MERCHANT_DonauInstancePostHandle *dph; 43 44 /** 45 * The interpreter state. 46 */ 47 struct TALER_TESTING_Interpreter *is; 48 49 /** 50 * Base URL of the merchant serving the request. 51 */ 52 const char *merchant_url; 53 54 /** 55 * Charity details. 56 */ 57 struct TALER_MERCHANT_DONAU_Charity charity; 58 59 /** 60 * Merchant reference to fetch public key. 61 */ 62 const char *merchant_reference; 63 64 /** 65 * Authentication token for the request. 66 */ 67 const char *auth_token; 68 69 /** 70 * Expected HTTP response code. 71 */ 72 unsigned int http_status; 73 }; 74 75 /** 76 * Callback for a POST /donau operation. 77 * 78 * @param cls closure for this function 79 * @param hr response being processed 80 */ 81 static void 82 post_donau_cb (void *cls, 83 const struct TALER_MERCHANT_HttpResponse *hr) 84 { 85 struct PostDonauState *pds = cls; 86 87 pds->dph = NULL; 88 if (pds->http_status != hr->http_status) 89 { 90 TALER_TESTING_unexpected_status_with_body ( 91 pds->is, 92 hr->http_status, 93 pds->http_status, 94 hr->reply); 95 TALER_TESTING_interpreter_fail (pds->is); 96 return; 97 } 98 99 switch (hr->http_status) 100 { 101 case MHD_HTTP_NO_CONTENT: 102 break; 103 case MHD_HTTP_BAD_REQUEST: 104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 105 "POST /donau returned BAD REQUEST: %s\n", 106 json_dumps (hr->reply, JSON_INDENT (2))); 107 break; 108 case MHD_HTTP_UNAUTHORIZED: 109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 110 "POST /donau returned UNAUTHORIZED\n"); 111 break; 112 case MHD_HTTP_NOT_FOUND: 113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 114 "POST /donau returned NOT FOUND\n"); 115 break; 116 default: 117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 118 "Unhandled HTTP status %u for POST /donau\n", 119 hr->http_status); 120 } 121 TALER_TESTING_interpreter_next (pds->is); 122 } 123 124 125 /** 126 * Run the "POST /donau" CMD. 127 * 128 * @param cls closure. 129 * @param cmd command being run now. 130 * @param is interpreter state. 131 */ 132 static void 133 post_donau_run (void *cls, 134 const struct TALER_TESTING_Command *cmd, 135 struct TALER_TESTING_Interpreter *is) 136 { 137 struct PostDonauState *pds = cls; 138 139 pds->is = is; 140 pds->charity.charity_url = TALER_TESTING_get_donau_url (is); 141 if (NULL != pds->merchant_reference) 142 { 143 const struct TALER_TESTING_Command *mc; 144 const struct TALER_MerchantPublicKeyP *merchant_pub; 145 146 mc = TALER_TESTING_interpreter_lookup_command (is, 147 pds->merchant_reference); 148 GNUNET_assert (NULL != mc); 149 GNUNET_assert (GNUNET_OK == 150 TALER_TESTING_get_trait_merchant_pub (mc, 151 &merchant_pub)); 152 pds->charity.charity_pub.eddsa_pub = merchant_pub->eddsa_pub; 153 } 154 155 pds->dph = TALER_MERCHANT_donau_instances_post ( 156 TALER_TESTING_interpreter_get_context (is), 157 pds->merchant_url, 158 &pds->charity, 159 pds->auth_token, 160 &post_donau_cb, 161 pds); 162 163 if (NULL == pds->dph) 164 { 165 GNUNET_break (0); 166 TALER_TESTING_interpreter_fail (pds->is); 167 return; 168 } 169 } 170 171 172 /** 173 * Free the state of a "POST /donau" CMD, and possibly 174 * cancel a pending operation thereof. 175 * 176 * @param cls closure. 177 * @param cmd command being run. 178 */ 179 static void 180 post_donau_cleanup (void *cls, 181 const struct TALER_TESTING_Command *cmd) 182 { 183 struct PostDonauState *pds = cls; 184 185 if (NULL != pds->dph) 186 { 187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 188 "POST /donau operation did not complete\n"); 189 TALER_MERCHANT_donau_instances_post_cancel (pds->dph); 190 } 191 GNUNET_free (pds); 192 } 193 194 195 /** 196 * Create a new testing command for POST /donau. 197 */ 198 struct TALER_TESTING_Command 199 TALER_TESTING_cmd_merchant_post_donau_instance (const char *label, 200 const char *url, 201 const char *merchant_reference, 202 unsigned int 203 expected_http_status, 204 ...) 205 { 206 struct PostDonauState *pds = GNUNET_new (struct PostDonauState); 207 struct DONAU_CharityPublicKeyP *charity_pub = GNUNET_new (struct 208 DONAU_CharityPublicKeyP); 209 210 struct TALER_Amount max_amount; 211 struct TALER_Amount date_amount; 212 213 const char*mamount = "EUR:100"; 214 const char*damount = "EUR:20"; 215 216 GNUNET_assert (GNUNET_OK == 217 TALER_string_to_amount (mamount, 218 &max_amount)); 219 220 GNUNET_assert (GNUNET_OK == 221 TALER_string_to_amount (damount, 222 &date_amount)); 223 224 { 225 struct TALER_MERCHANT_DONAU_Charity charity = { 226 .charity_url = "http://replaced.in.post_donau_run/", 227 .name = "example", 228 .charity_pub = *charity_pub, 229 .charity_id = 1, 230 .max_per_year = max_amount, 231 .receipts_to_date = date_amount, 232 .current_year = 2025 233 }; 234 GNUNET_free (charity_pub); 235 236 pds->merchant_reference = merchant_reference; 237 pds->merchant_url = url; 238 pds->charity = charity; 239 pds->http_status = expected_http_status; 240 pds->auth_token = NULL; 241 242 { 243 struct TALER_TESTING_Command cmd = { 244 .cls = pds, 245 .label = label, 246 .run = &post_donau_run, 247 .cleanup = &post_donau_cleanup 248 }; 249 250 return cmd; 251 } 252 253 } 254 }