testing_api_cmd_kyc_check_get.c (7384B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2021-2026 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 /** 21 * @file testing/testing_api_cmd_kyc_check_get.c 22 * @brief Implement the testing CMDs for the /kyc_check/ GET operations. 23 * @author Christian Grothoff 24 */ 25 #include "taler/platform.h" 26 #include "taler/taler_json_lib.h" 27 #include <gnunet/gnunet_curl_lib.h> 28 29 /** 30 * State for a "track transaction" CMD. 31 */ 32 struct KycCheckGetState; 33 34 #define TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE \ 35 struct KycCheckGetState 36 #include "taler/taler-exchange/get-kyc-check-H_NORMALIZED_PAYTO.h" 37 #include "taler/taler_testing_lib.h" 38 39 /** 40 * State for a "track transaction" CMD. 41 */ 42 struct KycCheckGetState 43 { 44 45 /** 46 * Set to the KYC URL *if* the exchange replied with 47 * a request for KYC (#MHD_HTTP_ACCEPTED or #MHD_HTTP_OK). 48 */ 49 struct TALER_AccountAccessTokenP access_token; 50 51 /** 52 * Handle to the "track transaction" pending operation. 53 */ 54 struct TALER_EXCHANGE_GetKycCheckHandle *kwh; 55 56 /** 57 * Interpreter state. 58 */ 59 struct TALER_TESTING_Interpreter *is; 60 61 /** 62 * Command to get a reserve private key from. 63 */ 64 const char *payment_target_reference; 65 66 /** 67 * Command to get an account private key from. 68 */ 69 const char *account_reference; 70 71 /** 72 * Expected HTTP response code. 73 */ 74 unsigned int expected_response_code; 75 76 /** 77 * What are we waiting for when long-polling? 78 */ 79 enum TALER_EXCHANGE_KycLongPollTarget lpt; 80 81 }; 82 83 84 /** 85 * Handle response to the command. 86 * 87 * @param kcg our state 88 * @param ks GET KYC status response details 89 */ 90 static void 91 check_kyc_cb ( 92 TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE *kcg, 93 const struct TALER_EXCHANGE_GetKycCheckResponse *ks) 94 { 95 struct TALER_TESTING_Interpreter *is = kcg->is; 96 97 kcg->kwh = NULL; 98 if (kcg->expected_response_code != ks->hr.http_status) 99 { 100 TALER_TESTING_unexpected_status (is, 101 ks->hr.http_status, 102 kcg->expected_response_code); 103 return; 104 } 105 switch (ks->hr.http_status) 106 { 107 case MHD_HTTP_OK: 108 kcg->access_token = ks->details.ok.access_token; 109 break; 110 case MHD_HTTP_ACCEPTED: 111 kcg->access_token = ks->details.accepted.access_token; 112 break; 113 case MHD_HTTP_NO_CONTENT: 114 break; 115 default: 116 GNUNET_break (0); 117 break; 118 } 119 TALER_TESTING_interpreter_next (kcg->is); 120 } 121 122 123 /** 124 * Run the command. 125 * 126 * @param cls closure. 127 * @param cmd the command to execute. 128 * @param is the interpreter state. 129 */ 130 static void 131 check_kyc_run (void *cls, 132 const struct TALER_TESTING_Command *cmd, 133 struct TALER_TESTING_Interpreter *is) 134 { 135 struct KycCheckGetState *kcg = cls; 136 const struct TALER_TESTING_Command *res_cmd; 137 const struct TALER_TESTING_Command *acc_cmd; 138 const struct TALER_NormalizedPaytoHashP *h_payto; 139 const union TALER_AccountPrivateKeyP *account_priv; 140 141 (void) cmd; 142 kcg->is = is; 143 res_cmd = TALER_TESTING_interpreter_lookup_command ( 144 kcg->is, 145 kcg->payment_target_reference); 146 if (NULL == res_cmd) 147 { 148 GNUNET_break (0); 149 TALER_TESTING_interpreter_fail (kcg->is); 150 return; 151 } 152 acc_cmd = TALER_TESTING_interpreter_lookup_command ( 153 kcg->is, 154 kcg->account_reference); 155 if (NULL == acc_cmd) 156 { 157 GNUNET_break (0); 158 TALER_TESTING_interpreter_fail (kcg->is); 159 return; 160 } 161 if (GNUNET_OK != 162 TALER_TESTING_get_trait_h_normalized_payto ( 163 res_cmd, 164 &h_payto)) 165 { 166 GNUNET_break (0); 167 TALER_TESTING_interpreter_fail (kcg->is); 168 return; 169 } 170 if (GNUNET_OK != 171 TALER_TESTING_get_trait_account_priv (acc_cmd, 172 &account_priv)) 173 { 174 GNUNET_break (0); 175 TALER_TESTING_interpreter_fail (kcg->is); 176 return; 177 } 178 if (0 == h_payto) 179 { 180 GNUNET_break (0); 181 TALER_TESTING_interpreter_fail (kcg->is); 182 return; 183 } 184 kcg->kwh = TALER_EXCHANGE_get_kyc_check_create ( 185 TALER_TESTING_interpreter_get_context (is), 186 TALER_TESTING_get_exchange_url (is), 187 h_payto, 188 account_priv); 189 GNUNET_assert (NULL != kcg->kwh); 190 if (TALER_EXCHANGE_KLPT_NONE != kcg->lpt) 191 TALER_EXCHANGE_get_kyc_check_set_options ( 192 kcg->kwh, 193 TALER_EXCHANGE_get_kyc_check_option_lpt (kcg->lpt), 194 TALER_EXCHANGE_get_kyc_check_option_timeout (GNUNET_TIME_UNIT_MINUTES)); 195 { 196 enum TALER_ErrorCode ec; 197 198 ec = TALER_EXCHANGE_get_kyc_check_start (kcg->kwh, 199 &check_kyc_cb, 200 kcg); 201 if (TALER_EC_NONE != ec) 202 { 203 GNUNET_break (0); 204 kcg->kwh = NULL; 205 TALER_TESTING_interpreter_fail (kcg->is); 206 return; 207 } 208 } 209 } 210 211 212 /** 213 * Cleanup the state from a "track transaction" CMD, and possibly 214 * cancel a operation thereof. 215 * 216 * @param cls closure. 217 * @param cmd the command which is being cleaned up. 218 */ 219 static void 220 check_kyc_cleanup (void *cls, 221 const struct TALER_TESTING_Command *cmd) 222 { 223 struct KycCheckGetState *kcg = cls; 224 225 if (NULL != kcg->kwh) 226 { 227 TALER_TESTING_command_incomplete (kcg->is, 228 cmd->label); 229 TALER_EXCHANGE_get_kyc_check_cancel (kcg->kwh); 230 kcg->kwh = NULL; 231 } 232 GNUNET_free (kcg); 233 } 234 235 236 /** 237 * Offer internal data from a "check KYC" CMD. 238 * 239 * @param cls closure. 240 * @param[out] ret result (could be anything). 241 * @param trait name of the trait. 242 * @param index index number of the object to offer. 243 * @return #GNUNET_OK on success. 244 */ 245 static enum GNUNET_GenericReturnValue 246 check_kyc_traits (void *cls, 247 const void **ret, 248 const char *trait, 249 unsigned int index) 250 { 251 struct KycCheckGetState *kcg = cls; 252 struct TALER_TESTING_Trait traits[] = { 253 TALER_TESTING_make_trait_account_access_token (&kcg->access_token), 254 TALER_TESTING_trait_end () 255 }; 256 257 return TALER_TESTING_get_trait (traits, 258 ret, 259 trait, 260 index); 261 } 262 263 264 struct TALER_TESTING_Command 265 TALER_TESTING_cmd_check_kyc_get ( 266 const char *label, 267 const char *payment_target_reference, 268 const char *account_reference, 269 enum TALER_EXCHANGE_KycLongPollTarget lpt, 270 unsigned int expected_response_code) 271 { 272 struct KycCheckGetState *kcg; 273 274 kcg = GNUNET_new (struct KycCheckGetState); 275 kcg->payment_target_reference = payment_target_reference; 276 kcg->account_reference = account_reference; 277 kcg->expected_response_code = expected_response_code; 278 kcg->lpt = lpt; 279 { 280 struct TALER_TESTING_Command cmd = { 281 .cls = kcg, 282 .label = label, 283 .run = &check_kyc_run, 284 .cleanup = &check_kyc_cleanup, 285 .traits = &check_kyc_traits 286 }; 287 288 return cmd; 289 } 290 } 291 292 293 /* end of testing_api_cmd_kyc_check_get.c */