testing_api_cmd_charity_post.c (6108B)
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 #include "donau_testing_lib.h" 29 30 31 /** 32 * State for a "status" CMD. 33 */ 34 struct StatusState 35 { 36 /** 37 * Handle to the "charity status" operation. 38 */ 39 struct DONAU_CharityPostHandle *cph; 40 41 /** 42 * Name of the charity. 43 */ 44 const char *charity_name; 45 46 /** 47 * Web site of the charity. 48 */ 49 const char *charity_url; 50 51 /** 52 * Maximum donation amount for the charity. 53 */ 54 struct TALER_Amount max_per_year; 55 56 /** 57 * Public key of the charity. 58 */ 59 struct DONAU_CharityPublicKeyP charity_pub; 60 61 /** 62 * Private key of the charity, created here. 63 */ 64 struct DONAU_CharityPrivateKeyP charity_priv; 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 88 89 /** 90 * Check that the reserve balance and HTTP response code are 91 * both acceptable. 92 * 93 * @param cls closure. 94 * @param gcr HTTP response details 95 */ 96 static void 97 charity_status_cb (void *cls, 98 const struct DONAU_PostCharityResponse *gcr) 99 { 100 struct StatusState *ss = cls; 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 if (ss->expected_response_code == gcr->hr.http_status) 117 ss->charity_id = gcr->details.ok.charity_id; 118 TALER_TESTING_interpreter_next (ss->is); 119 } 120 121 122 /** 123 * Run the command. 124 * 125 * @param cls closure. 126 * @param cmd the command being executed. 127 * @param is the interpreter state. 128 */ 129 static void 130 charity_status_run (void *cls, 131 const struct TALER_TESTING_Command *cmd, 132 struct TALER_TESTING_Interpreter *is) 133 { 134 struct StatusState *ss = cls; 135 136 (void) cmd; 137 ss->is = is; 138 ss->cph = DONAU_charity_post ( 139 TALER_TESTING_interpreter_get_context (is), 140 TALER_TESTING_get_donau_url (is), 141 ss->charity_name, 142 ss->charity_url, 143 &ss->max_per_year, 144 &ss->charity_pub, 145 ss->bearer, 146 &charity_status_cb, 147 ss); 148 } 149 150 151 /** 152 * Cleanup the state from a "reserve status" CMD, and possibly 153 * cancel a pending operation thereof. 154 * 155 * @param cls closure. 156 * @param cmd the command which is being cleaned up. 157 */ 158 static void 159 cleanup (void *cls, 160 const struct TALER_TESTING_Command *cmd) 161 { 162 struct StatusState *ss = cls; 163 164 if (NULL != ss->cph) 165 { 166 // log incomplete command 167 TALER_TESTING_command_incomplete (ss->is, 168 cmd->label); 169 DONAU_charity_post_cancel (ss->cph); 170 ss->cph = NULL; 171 } 172 GNUNET_free (ss); 173 } 174 175 176 /** 177 * Offer internal data from a "deposit" CMD, to other commands. 178 * 179 * @param cls closure. 180 * @param[out] ret result. 181 * @param trait name of the trait. 182 * @param index index number of the object to offer. 183 * @return #GNUNET_OK on success. 184 */ 185 static enum GNUNET_GenericReturnValue 186 charity_post_traits (void *cls, 187 const void **ret, 188 const char *trait, 189 unsigned int index) 190 { 191 struct StatusState *ss = cls; 192 struct TALER_TESTING_Trait traits[] = { 193 TALER_TESTING_make_trait_charity_priv (&ss->charity_priv), 194 TALER_TESTING_make_trait_charity_pub (&ss->charity_pub), 195 TALER_TESTING_make_trait_charity_id (&ss->charity_id), 196 TALER_TESTING_trait_end () 197 }; 198 199 return TALER_TESTING_get_trait (traits, 200 ret, 201 trait, 202 index); 203 } 204 205 206 struct TALER_TESTING_Command 207 TALER_TESTING_cmd_charity_post (const char *label, 208 const char *name, 209 const char *url, 210 const char *max_per_year, 211 const struct DONAU_BearerToken *bearer, 212 unsigned int expected_response_code) 213 { 214 struct StatusState *ss; 215 216 ss = GNUNET_new (struct StatusState); 217 GNUNET_CRYPTO_eddsa_key_create (&ss->charity_priv.eddsa_priv); 218 GNUNET_CRYPTO_eddsa_key_get_public (&ss->charity_priv.eddsa_priv, 219 &ss->charity_pub.eddsa_pub); 220 ss->charity_name = name; 221 ss->charity_url = url; 222 if (GNUNET_OK != 223 TALER_string_to_amount (max_per_year, 224 &ss->max_per_year)) 225 { 226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 227 "Failed to parse amount `%s' at %s\n", 228 max_per_year, 229 label); 230 GNUNET_assert (0); 231 } 232 ss->expected_response_code = expected_response_code; 233 ss->bearer = bearer; 234 { 235 struct TALER_TESTING_Command cmd = { 236 .cls = ss, 237 .label = label, 238 .run = &charity_status_run, 239 .cleanup = &cleanup, 240 .traits = &charity_post_traits 241 }; 242 243 return cmd; 244 } 245 }