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