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