testing_api_cmd_post_donau_charity_merchant.c (6507B)
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_charity_merchant.c 21 * @brief command to test POST /donau charity with merchant_pub 22 * @author Bohdan Potuzhnyi 23 * @author Vlada Svirsh 24 */ 25 #include "taler/platform.h" 26 #include <taler/taler_exchange_service.h> 27 #include <taler/taler_testing_lib.h> 28 #include <taler/taler_signatures.h> 29 #include "taler/taler_merchant_service.h" 30 #include "taler/taler_merchant_testing_lib.h" 31 #include <donau/donau_service.h> 32 #include <donau/donau_testing_lib.h> 33 34 35 /** 36 * State for a "status" CMD. 37 */ 38 struct StatusState 39 { 40 /** 41 * Handle to the "charity status" operation. 42 */ 43 struct DONAU_CharityPostHandle *cph; 44 45 /** 46 * name of the charity 47 */ 48 const char *charity_name; 49 50 /** 51 * charity url 52 */ 53 const char *charity_url; 54 55 /** 56 * public key of the charity 57 */ 58 struct DONAU_CharityPublicKeyP charity_pub; 59 60 /** 61 * Max donation amount for this charitiy and @e current_year. 62 */ 63 struct TALER_Amount max_per_year; 64 65 /** 66 * The bearer token for authorization. 67 */ 68 const struct DONAU_BearerToken *bearer; 69 70 /** 71 * Expected HTTP response code. 72 */ 73 unsigned int expected_response_code; 74 75 /** 76 * Interpreter state. 77 */ 78 struct TALER_TESTING_Interpreter *is; 79 80 /** 81 * charity id 82 */ 83 uint64_t charity_id; 84 85 /** 86 * Merchant reference to fetch public key. 87 */ 88 const char *merchant_reference; 89 }; 90 91 /** 92 * Offer internal data from a CMD, to other commands. 93 * 94 * @param cls closure. 95 * @param[out] ret result. 96 * @param trait name of the trait. 97 * @param index index number of the object to offer. 98 * @return #GNUNET_OK on success. 99 */ 100 static enum GNUNET_GenericReturnValue 101 charity_post_traits (void *cls, 102 const void **ret, 103 const char *trait, 104 unsigned int index) 105 { 106 struct StatusState *ss = cls; 107 struct TALER_TESTING_Trait traits[] = { 108 TALER_TESTING_make_trait_charity_pub (&ss->charity_pub), 109 TALER_TESTING_make_trait_charity_id (&ss->charity_id), 110 TALER_TESTING_trait_end () 111 }; 112 113 return TALER_TESTING_get_trait (traits, 114 ret, 115 trait, 116 index); 117 } 118 119 120 /** 121 * Check that the reserve balance and HTTP response code are 122 * both acceptable. 123 * 124 * @param cls closure. 125 * @param gcr HTTP response details 126 */ 127 static void 128 charity_status_cb (void *cls, 129 const struct DONAU_PostCharityResponse *gcr) 130 { 131 struct StatusState *ss = cls; 132 133 ss->cph = NULL; 134 if (ss->expected_response_code != gcr->hr.http_status) 135 { 136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 137 "Unexpected HTTP response code: %d in %s:%u\n", 138 gcr->hr.http_status, 139 __FILE__, 140 __LINE__); 141 json_dumpf (gcr->hr.reply, 142 stderr, 143 0); 144 TALER_TESTING_interpreter_fail (ss->is); 145 return; 146 } 147 if (ss->expected_response_code == gcr->hr.http_status) 148 ss->charity_id = (unsigned long long) gcr->details.ok.charity_id; 149 TALER_TESTING_interpreter_next (ss->is); 150 } 151 152 153 /** 154 * Run the command. 155 * 156 * @param cls closure. 157 * @param cmd the command being executed. 158 * @param is the interpreter state. 159 */ 160 static void 161 charity_status_run (void *cls, 162 const struct TALER_TESTING_Command *cmd, 163 struct TALER_TESTING_Interpreter *is) 164 { 165 struct StatusState *ss = cls; 166 167 ss->is = is; 168 169 if (NULL != ss->merchant_reference) 170 { 171 const struct TALER_TESTING_Command *mc; 172 const struct TALER_MerchantPublicKeyP *mpub; 173 174 mc = TALER_TESTING_interpreter_lookup_command (is, 175 ss->merchant_reference); 176 GNUNET_assert (NULL != mc); 177 GNUNET_assert (GNUNET_OK == 178 TALER_TESTING_get_trait_merchant_pub (mc, 179 &mpub)); 180 181 ss->charity_pub.eddsa_pub = mpub->eddsa_pub; 182 } 183 184 ss->cph = DONAU_charity_post ( 185 TALER_TESTING_interpreter_get_context (is), 186 TALER_TESTING_get_donau_url (is), 187 ss->charity_name, 188 ss->charity_url, 189 &ss->max_per_year, 190 &ss->charity_pub, 191 ss->bearer, 192 &charity_status_cb, 193 ss); 194 } 195 196 197 /** 198 * Cleanup the state from a "reserve status" CMD, and possibly 199 * cancel a pending operation thereof. 200 * 201 * @param cls closure. 202 * @param cmd the command which is being cleaned up. 203 */ 204 static void 205 cleanup (void *cls, 206 const struct TALER_TESTING_Command *cmd) 207 { 208 struct StatusState *ss = cls; 209 210 if (NULL != ss->cph) 211 { 212 // log incomplete command 213 TALER_TESTING_command_incomplete (ss->is, 214 cmd->label); 215 DONAU_charity_post_cancel (ss->cph); 216 ss->cph = NULL; 217 } 218 GNUNET_free (ss); 219 } 220 221 222 struct TALER_TESTING_Command 223 TALER_TESTING_cmd_charity_post_merchant ( 224 const char *label, 225 const char *name, 226 const char *url, 227 const char *max_per_year, 228 const struct DONAU_BearerToken *bearer, 229 const char *merchant_reference, 230 unsigned int expected_response_code) 231 { 232 struct StatusState *ss; 233 234 ss = GNUNET_new (struct StatusState); 235 ss->merchant_reference = merchant_reference; 236 ss->charity_name = name; 237 ss->charity_url = url; 238 // parse string max_per_year to amount 239 if (GNUNET_OK != 240 TALER_string_to_amount (max_per_year, 241 &ss->max_per_year)) 242 { 243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 244 "Failed to parse amount `%s' at %s\n", 245 max_per_year, 246 label); 247 GNUNET_assert (0); 248 } 249 ss->expected_response_code = expected_response_code; 250 ss->bearer = bearer; 251 { 252 struct TALER_TESTING_Command cmd = { 253 .cls = ss, 254 .label = label, 255 .run = &charity_status_run, 256 .cleanup = &cleanup, 257 .traits = &charity_post_traits 258 }; 259 260 return cmd; 261 } 262 }