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