testing_api_cmd_get_kyc_info.c (5857B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 /** 21 * @file testing/testing_api_cmd_get_kyc_info.c 22 * @brief Implement the testing CMDs for the GET /kyc_info operation. 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 #include "taler/taler_testing_lib.h" 29 30 /** 31 * State for a GET kyc-info CMD. 32 */ 33 struct GetKycInfoState 34 { 35 36 /** 37 * Command to get the account access token from. 38 */ 39 const char *kyc_check_reference; 40 41 /** 42 * Expected HTTP response code. 43 */ 44 unsigned int expected_response_code; 45 46 /** 47 * Handle to the GET /kyc-info pending operation. 48 */ 49 struct TALER_EXCHANGE_KycInfoHandle *kwh; 50 51 /** 52 * Interpreter state. 53 */ 54 struct TALER_TESTING_Interpreter *is; 55 56 /** 57 * Array of IDs for possible KYC processes we could 58 * start according to the response. 59 */ 60 char **ids; 61 62 /** 63 * Length of the @e ids array. 64 */ 65 unsigned int num_ids; 66 }; 67 68 69 /** 70 * Handle response to the command. 71 * 72 * @param cls closure. 73 * @param kpci GET KYC status response details 74 */ 75 static void 76 kyc_info_cb ( 77 void *cls, 78 const struct TALER_EXCHANGE_KycProcessClientInformation *kpci) 79 { 80 struct GetKycInfoState *kcg = cls; 81 struct TALER_TESTING_Interpreter *is = kcg->is; 82 83 kcg->kwh = NULL; 84 if (kcg->expected_response_code != kpci->hr.http_status) 85 { 86 TALER_TESTING_unexpected_status ( 87 is, 88 kpci->hr.http_status, 89 kcg->expected_response_code); 90 return; 91 } 92 switch (kpci->hr.http_status) 93 { 94 case MHD_HTTP_OK: 95 kcg->num_ids = kpci->details.ok.requirements_length; 96 kcg->ids = GNUNET_new_array (kcg->num_ids, 97 char *); 98 for (unsigned int i = 0; i<kcg->num_ids; i++) 99 kcg->ids[i] = GNUNET_strdup ( 100 kpci->details.ok.requirements[i].id); 101 break; 102 case MHD_HTTP_NO_CONTENT: 103 break; 104 default: 105 GNUNET_break (0); 106 break; 107 } 108 TALER_TESTING_interpreter_next (kcg->is); 109 } 110 111 112 /** 113 * Run the command. 114 * 115 * @param cls closure. 116 * @param cmd the command to execute. 117 * @param is the interpreter state. 118 */ 119 static void 120 get_kyc_info_run (void *cls, 121 const struct TALER_TESTING_Command *cmd, 122 struct TALER_TESTING_Interpreter *is) 123 { 124 struct GetKycInfoState *kcg = cls; 125 const struct TALER_TESTING_Command *res_cmd; 126 const struct TALER_AccountAccessTokenP *token; 127 128 (void) cmd; 129 kcg->is = is; 130 res_cmd = TALER_TESTING_interpreter_lookup_command ( 131 kcg->is, 132 kcg->kyc_check_reference); 133 if (NULL == res_cmd) 134 { 135 GNUNET_break (0); 136 TALER_TESTING_interpreter_fail (kcg->is); 137 return; 138 } 139 if (GNUNET_OK != 140 TALER_TESTING_get_trait_account_access_token ( 141 res_cmd, 142 &token)) 143 { 144 GNUNET_break (0); 145 TALER_TESTING_interpreter_fail (kcg->is); 146 return; 147 } 148 kcg->kwh = TALER_EXCHANGE_kyc_info ( 149 TALER_TESTING_interpreter_get_context (is), 150 TALER_TESTING_get_exchange_url (is), 151 token, 152 NULL /* etag */, 153 GNUNET_TIME_UNIT_ZERO, 154 &kyc_info_cb, 155 kcg); 156 GNUNET_assert (NULL != kcg->kwh); 157 } 158 159 160 /** 161 * Cleanup the state from a "track transaction" CMD, and possibly 162 * cancel a operation thereof. 163 * 164 * @param cls closure. 165 * @param cmd the command which is being cleaned up. 166 */ 167 static void 168 get_kyc_info_cleanup ( 169 void *cls, 170 const struct TALER_TESTING_Command *cmd) 171 { 172 struct GetKycInfoState *kcg = cls; 173 174 if (NULL != kcg->kwh) 175 { 176 TALER_TESTING_command_incomplete (kcg->is, 177 cmd->label); 178 TALER_EXCHANGE_kyc_info_cancel (kcg->kwh); 179 kcg->kwh = NULL; 180 } 181 for (unsigned int i = 0; i<kcg->num_ids; i++) 182 GNUNET_free (kcg->ids[i]); 183 GNUNET_free (kcg->ids); 184 GNUNET_free (kcg); 185 } 186 187 188 /** 189 * Offer internal data from a "check KYC" CMD. 190 * 191 * @param cls closure. 192 * @param[out] ret result (could be anything). 193 * @param trait name of the trait. 194 * @param index index number of the object to offer. 195 * @return #GNUNET_OK on success. 196 */ 197 static enum GNUNET_GenericReturnValue 198 get_kyc_info_traits (void *cls, 199 const void **ret, 200 const char *trait, 201 unsigned int index) 202 { 203 struct GetKycInfoState *kcg = cls; 204 struct TALER_TESTING_Trait traits[] = { 205 TALER_TESTING_make_trait_kyc_id (index, 206 kcg->ids[index]), 207 TALER_TESTING_trait_end () 208 }; 209 210 if (index >= kcg->num_ids) 211 return GNUNET_NO; 212 return TALER_TESTING_get_trait (traits, 213 ret, 214 trait, 215 index); 216 } 217 218 219 struct TALER_TESTING_Command 220 TALER_TESTING_cmd_get_kyc_info ( 221 const char *label, 222 const char *kyc_check_reference, 223 unsigned int expected_response_code) 224 { 225 struct GetKycInfoState *kcg; 226 227 kcg = GNUNET_new (struct GetKycInfoState); 228 kcg->kyc_check_reference = kyc_check_reference; 229 kcg->expected_response_code = expected_response_code; 230 { 231 struct TALER_TESTING_Command cmd = { 232 .cls = kcg, 233 .label = label, 234 .run = &get_kyc_info_run, 235 .cleanup = &get_kyc_info_cleanup, 236 .traits = &get_kyc_info_traits 237 }; 238 239 return cmd; 240 } 241 } 242 243 244 /* end of testing_api_cmd_get_kyc_info.c */