testing_api_cmd_post_donau_instances.c (5567B)
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 "taler/platform.h" 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/taler_merchant_donau.h" 32 #include <donau/donau_testing_lib.h> 33 34 35 /** 36 * State of a "POST /donau" CMD. 37 */ 38 struct PostDonauState 39 { 40 /** 41 * Handle for a "POST donau" request. 42 */ 43 struct TALER_MERCHANT_DonauInstancePostHandle *dph; 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 * Charity details. 57 */ 58 struct TALER_MERCHANT_Charity charity; 59 60 /** 61 * Merchant reference to fetch public key. 62 */ 63 const char *merchant_reference; 64 65 /** 66 * Authentication token for the request. 67 */ 68 const char *auth_token; 69 70 /** 71 * Expected HTTP response code. 72 */ 73 unsigned int http_status; 74 }; 75 76 /** 77 * Callback for a POST /donau operation. 78 * 79 * @param cls closure for this function 80 * @param hr response being processed 81 */ 82 static void 83 post_donau_cb (void *cls, 84 const struct TALER_MERCHANT_HttpResponse *hr) 85 { 86 struct PostDonauState *pds = cls; 87 88 pds->dph = NULL; 89 if (pds->http_status != hr->http_status) 90 { 91 TALER_TESTING_unexpected_status_with_body ( 92 pds->is, 93 hr->http_status, 94 pds->http_status, 95 hr->reply); 96 TALER_TESTING_interpreter_fail (pds->is); 97 return; 98 } 99 100 switch (hr->http_status) 101 { 102 case MHD_HTTP_NO_CONTENT: 103 break; 104 case MHD_HTTP_BAD_REQUEST: 105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 106 "POST /donau returned BAD REQUEST: %s\n", 107 json_dumps (hr->reply, JSON_INDENT (2))); 108 break; 109 case MHD_HTTP_UNAUTHORIZED: 110 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 111 "POST /donau returned UNAUTHORIZED\n"); 112 break; 113 case MHD_HTTP_NOT_FOUND: 114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 115 "POST /donau returned NOT FOUND\n"); 116 break; 117 default: 118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 119 "Unhandled HTTP status %u for POST /donau\n", 120 hr->http_status); 121 } 122 TALER_TESTING_interpreter_next (pds->is); 123 } 124 125 126 /** 127 * Run the "POST /donau" CMD. 128 * 129 * @param cls closure. 130 * @param cmd command being run now. 131 * @param is interpreter state. 132 */ 133 static void 134 post_donau_run (void *cls, 135 const struct TALER_TESTING_Command *cmd, 136 struct TALER_TESTING_Interpreter *is) 137 { 138 struct PostDonauState *pds = cls; 139 140 pds->is = is; 141 pds->charity.charity_url = TALER_TESTING_get_donau_url (is); 142 pds->dph = TALER_MERCHANT_donau_instances_post ( 143 TALER_TESTING_interpreter_get_context (is), 144 pds->merchant_url, 145 &pds->charity, 146 pds->auth_token, 147 &post_donau_cb, 148 pds); 149 150 if (NULL == pds->dph) 151 { 152 GNUNET_break (0); 153 TALER_TESTING_interpreter_fail (pds->is); 154 return; 155 } 156 } 157 158 159 /** 160 * Free the state of a "POST /donau" CMD, and possibly 161 * cancel a pending operation thereof. 162 * 163 * @param cls closure. 164 * @param cmd command being run. 165 */ 166 static void 167 post_donau_cleanup (void *cls, 168 const struct TALER_TESTING_Command *cmd) 169 { 170 struct PostDonauState *pds = cls; 171 172 if (NULL != pds->dph) 173 { 174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 175 "POST /donau operation did not complete\n"); 176 TALER_MERCHANT_donau_instances_post_cancel (pds->dph); 177 } 178 GNUNET_free (pds); 179 } 180 181 182 /** 183 * Create a new testing command for POST /donau. 184 */ 185 struct TALER_TESTING_Command 186 TALER_TESTING_cmd_merchant_post_donau_instance ( 187 const char *label, 188 const char *url, 189 const char *merchant_reference, 190 unsigned int 191 expected_http_status, 192 ...) 193 { 194 struct PostDonauState *pds = GNUNET_new (struct PostDonauState); 195 struct TALER_Amount max_amount; 196 struct TALER_Amount date_amount; 197 198 const char*mamount = "EUR:100"; 199 const char*damount = "EUR:20"; 200 201 GNUNET_assert (GNUNET_OK == 202 TALER_string_to_amount (mamount, 203 &max_amount)); 204 205 GNUNET_assert (GNUNET_OK == 206 TALER_string_to_amount (damount, 207 &date_amount)); 208 209 { 210 struct TALER_MERCHANT_Charity charity = { 211 .charity_url = "http://replaced.in.post_donau_run/", 212 .charity_id = 1, 213 }; 214 215 pds->merchant_reference = merchant_reference; 216 pds->merchant_url = url; 217 pds->charity = charity; 218 pds->http_status = expected_http_status; 219 pds->auth_token = NULL; 220 221 { 222 struct TALER_TESTING_Command cmd = { 223 .cls = pds, 224 .label = label, 225 .run = &post_donau_run, 226 .cleanup = &post_donau_cleanup 227 }; 228 229 return cmd; 230 } 231 232 } 233 }