testing_api_cmd_post_donau_instances.c (5698B)
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 src/testing/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 struct PostDonauState; 28 #define TALER_MERCHANT_POST_PRIVATE_DONAU_RESULT_CLOSURE struct PostDonauState 29 #include <taler/taler_exchange_service.h> 30 #include <taler/taler_testing_lib.h> 31 #include "taler/taler_merchant_service.h" 32 #include "taler/taler_merchant_testing_lib.h" 33 #include <donau/donau_testing_lib.h> 34 #include <taler/merchant/post-private-donau.h> 35 36 /** 37 * State of a "POST /donau" CMD. 38 */ 39 struct PostDonauState 40 { 41 /** 42 * Handle for a "POST donau" request. 43 */ 44 struct TALER_MERCHANT_PostPrivateDonauHandle *dph; 45 46 /** 47 * The interpreter state. 48 */ 49 struct TALER_TESTING_Interpreter *is; 50 51 /** 52 * Base URL of the merchant serving the request. 53 */ 54 const char *merchant_url; 55 56 /** 57 * Charity details. 58 */ 59 struct TALER_MERCHANT_Charity charity; 60 61 /** 62 * Merchant reference to fetch public key. 63 */ 64 const char *merchant_reference; 65 66 /** 67 * Expected HTTP response code. 68 */ 69 unsigned int http_status; 70 }; 71 72 /** 73 * Callback for a POST /donau operation. 74 * 75 * @param cls closure for this function 76 * @param pdr response being processed 77 */ 78 static void 79 post_donau_cb (struct PostDonauState *pds, 80 const struct TALER_MERCHANT_PostPrivateDonauResponse *pdr) 81 { 82 83 pds->dph = NULL; 84 if (pds->http_status != pdr->hr.http_status) 85 { 86 TALER_TESTING_unexpected_status_with_body ( 87 pds->is, 88 pdr->hr.http_status, 89 pds->http_status, 90 pdr->hr.reply); 91 return; 92 } 93 94 switch (pdr->hr.http_status) 95 { 96 case MHD_HTTP_NO_CONTENT: 97 break; 98 case MHD_HTTP_BAD_REQUEST: 99 { 100 char *s = json_dumps (pdr->hr.reply, JSON_INDENT (2)); 101 102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 103 "POST /donau returned BAD REQUEST: %s\n", 104 s); 105 free (s); 106 } 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 pdr->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 pds->dph = TALER_MERCHANT_post_private_donau_create ( 142 TALER_TESTING_interpreter_get_context (is), 143 pds->merchant_url, 144 &pds->charity); 145 { 146 enum TALER_ErrorCode ec; 147 148 ec = TALER_MERCHANT_post_private_donau_start (pds->dph, 149 &post_donau_cb, 150 pds); 151 GNUNET_assert (TALER_EC_NONE == ec); 152 } 153 } 154 155 156 /** 157 * Free the state of a "POST /donau" CMD, and possibly 158 * cancel a pending operation thereof. 159 * 160 * @param cls closure. 161 * @param cmd command being run. 162 */ 163 static void 164 post_donau_cleanup (void *cls, 165 const struct TALER_TESTING_Command *cmd) 166 { 167 struct PostDonauState *pds = cls; 168 169 if (NULL != pds->dph) 170 { 171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 172 "POST /donau operation did not complete\n"); 173 TALER_MERCHANT_post_private_donau_cancel (pds->dph); 174 } 175 GNUNET_free (pds); 176 } 177 178 179 /** 180 * Create a new testing command for POST /donau. 181 */ 182 struct TALER_TESTING_Command 183 TALER_TESTING_cmd_merchant_post_donau_instance ( 184 const char *label, 185 const char *url, 186 const char *merchant_reference, 187 unsigned int 188 expected_http_status, 189 ...) 190 { 191 struct PostDonauState *pds = GNUNET_new (struct PostDonauState); 192 struct TALER_Amount max_amount; 193 struct TALER_Amount date_amount; 194 195 const char*mamount = "EUR:100"; 196 const char*damount = "EUR:20"; 197 198 GNUNET_assert (GNUNET_OK == 199 TALER_string_to_amount (mamount, 200 &max_amount)); 201 202 GNUNET_assert (GNUNET_OK == 203 TALER_string_to_amount (damount, 204 &date_amount)); 205 206 { 207 struct TALER_MERCHANT_Charity charity = { 208 .charity_url = "http://replaced.in.post_donau_run/", 209 .charity_id = 1, 210 }; 211 212 pds->merchant_reference = merchant_reference; 213 pds->merchant_url = url; 214 pds->charity = charity; 215 pds->http_status = expected_http_status; 216 217 { 218 struct TALER_TESTING_Command cmd = { 219 .cls = pds, 220 .label = label, 221 .run = &post_donau_run, 222 .cleanup = &post_donau_cleanup 223 }; 224 225 return cmd; 226 } 227 228 } 229 }