testing_api_cmd_post_account.c (6894B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 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_api_cmd_post_account.c 21 * @brief command to test POST /account 22 * @author Christian Grothoff 23 */ 24 #include "taler/platform.h" 25 #include <taler/taler_exchange_service.h> 26 #include <taler/taler_testing_lib.h> 27 #include "taler/taler_merchant_service.h" 28 #include "taler/taler_merchant_testing_lib.h" 29 #include <taler/taler-merchant/post-private-accounts.h> 30 31 32 /** 33 * State of a "POST /account" CMD. 34 */ 35 struct PostAccountState 36 { 37 38 /** 39 * Handle for a "GET product" request. 40 */ 41 struct TALER_MERCHANT_PostPrivateAccountsHandle *aph; 42 43 /** 44 * The interpreter state. 45 */ 46 struct TALER_TESTING_Interpreter *is; 47 48 /** 49 * Base URL of the merchant serving the request. 50 */ 51 const char *merchant_url; 52 53 /** 54 * Wire hash of the created account, set on success. 55 */ 56 struct TALER_MerchantWireHashP h_wire; 57 58 /** 59 * RFC 8905 URI for the account to create. 60 */ 61 struct TALER_FullPayto payto_uri; 62 63 /** 64 * Credit facade URL for the account to create. 65 */ 66 char *credit_facade_url; 67 68 /** 69 * Credit facade credentials for the account to create. 70 */ 71 json_t *credit_facade_credentials; 72 73 /** 74 * Expected HTTP response code. 75 */ 76 unsigned int http_status; 77 78 }; 79 80 81 /** 82 * Callback for a POST /account operation. 83 * 84 * @param cls closure for this function 85 * @param apr response being processed 86 */ 87 static void 88 post_account_cb (void *cls, 89 const struct TALER_MERCHANT_PostPrivateAccountsResponse *apr) 90 { 91 struct PostAccountState *pas = cls; 92 93 pas->aph = NULL; 94 if (pas->http_status != apr->hr.http_status) 95 { 96 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 97 "Unexpected response code %u (%d) to command %s\n", 98 apr->hr.http_status, 99 (int) apr->hr.ec, 100 TALER_TESTING_interpreter_get_current_label (pas->is)); 101 TALER_TESTING_interpreter_fail (pas->is); 102 return; 103 } 104 switch (apr->hr.http_status) 105 { 106 case MHD_HTTP_OK: 107 pas->h_wire = apr->details.ok.h_wire; 108 break; 109 case MHD_HTTP_UNAUTHORIZED: 110 break; 111 case MHD_HTTP_FORBIDDEN: 112 break; 113 case MHD_HTTP_NOT_FOUND: 114 break; 115 case MHD_HTTP_CONFLICT: 116 break; 117 default: 118 GNUNET_break (0); 119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 120 "Unhandled HTTP status %u for POST /account.\n", 121 apr->hr.http_status); 122 } 123 TALER_TESTING_interpreter_next (pas->is); 124 } 125 126 127 /** 128 * Run the "POST /account" CMD. 129 * 130 * 131 * @param cls closure. 132 * @param cmd command being run now. 133 * @param is interpreter state. 134 */ 135 static void 136 post_account_run (void *cls, 137 const struct TALER_TESTING_Command *cmd, 138 struct TALER_TESTING_Interpreter *is) 139 { 140 struct PostAccountState *pas = cls; 141 142 pas->is = is; 143 pas->aph = TALER_MERCHANT_post_private_accounts_create ( 144 TALER_TESTING_interpreter_get_context (is), 145 pas->merchant_url, 146 pas->payto_uri); 147 if (NULL != pas->credit_facade_url) 148 TALER_MERCHANT_post_private_accounts_set_options ( 149 pas->aph, 150 TALER_MERCHANT_post_private_accounts_option_credit_facade_url ( 151 pas->credit_facade_url)); 152 if (NULL != pas->credit_facade_credentials) 153 TALER_MERCHANT_post_private_accounts_set_options ( 154 pas->aph, 155 TALER_MERCHANT_post_private_accounts_option_credit_facade_credentials ( 156 pas->credit_facade_credentials)); 157 { 158 enum TALER_ErrorCode ec; 159 160 ec = TALER_MERCHANT_post_private_accounts_start ( 161 pas->aph, 162 &post_account_cb, 163 pas); 164 GNUNET_assert (TALER_EC_NONE == ec); 165 } 166 } 167 168 169 /** 170 * Offers information from the POST /account CMD state to other 171 * commands. 172 * 173 * @param cls closure 174 * @param[out] ret result (could be anything) 175 * @param trait name of the trait 176 * @param index index number of the object to extract. 177 * @return #GNUNET_OK on success 178 */ 179 static enum GNUNET_GenericReturnValue 180 post_account_traits (void *cls, 181 const void **ret, 182 const char *trait, 183 unsigned int index) 184 { 185 struct PostAccountState *pps = cls; 186 struct TALER_TESTING_Trait traits[] = { 187 TALER_TESTING_make_trait_h_wires ( 188 0, 189 &pps->h_wire), 190 TALER_TESTING_make_trait_h_wire ( 191 &pps->h_wire), 192 TALER_TESTING_make_trait_payto_uris ( 193 0, 194 &pps->payto_uri), 195 TALER_TESTING_make_trait_merchant_base_url ( 196 pps->merchant_url), 197 TALER_TESTING_trait_end (), 198 }; 199 200 return TALER_TESTING_get_trait (traits, 201 ret, 202 trait, 203 index); 204 } 205 206 207 /** 208 * Free the state of a "POST product" CMD, and possibly 209 * cancel a pending operation thereof. 210 * 211 * @param cls closure. 212 * @param cmd command being run. 213 */ 214 static void 215 post_account_cleanup (void *cls, 216 const struct TALER_TESTING_Command *cmd) 217 { 218 struct PostAccountState *pas = cls; 219 220 if (NULL != pas->aph) 221 { 222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 223 "POST /account operation did not complete\n"); 224 TALER_MERCHANT_post_private_accounts_cancel (pas->aph); 225 } 226 GNUNET_free (pas->payto_uri.full_payto); 227 GNUNET_free (pas->credit_facade_url); 228 json_decref (pas->credit_facade_credentials); 229 GNUNET_free (pas); 230 } 231 232 233 struct TALER_TESTING_Command 234 TALER_TESTING_cmd_merchant_post_account ( 235 const char *label, 236 const char *merchant_url, 237 struct TALER_FullPayto payto_uri, 238 const char *credit_facade_url, 239 const json_t *credit_facade_credentials, 240 unsigned int http_status) 241 { 242 struct PostAccountState *pas; 243 244 pas = GNUNET_new (struct PostAccountState); 245 pas->merchant_url = merchant_url; 246 pas->payto_uri.full_payto 247 = GNUNET_strdup (payto_uri.full_payto); 248 if (NULL != credit_facade_url) 249 pas->credit_facade_url = GNUNET_strdup (credit_facade_url); 250 if (NULL != credit_facade_credentials) 251 pas->credit_facade_credentials 252 = json_incref ((json_t *) credit_facade_credentials); 253 pas->http_status = http_status; 254 { 255 struct TALER_TESTING_Command cmd = { 256 .cls = pas, 257 .label = label, 258 .run = &post_account_run, 259 .cleanup = &post_account_cleanup, 260 .traits = &post_account_traits 261 }; 262 263 return cmd; 264 } 265 } 266 267 268 /* end of testing_api_cmd_post_account.c */