testing_api_cmd_kyc_get.c (8245B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2021 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_kyc_get.c 21 * @brief command to test kyc_get request 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 struct KycGetState; 26 #define TALER_MERCHANT_GET_PRIVATE_KYC_RESULT_CLOSURE struct KycGetState 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/get-private-kyc.h> 32 33 34 /** 35 * State for a "/kyc" GET CMD. 36 */ 37 struct KycGetState 38 { 39 /** 40 * Operation handle for a GET /private/kyc GET request. 41 */ 42 struct TALER_MERCHANT_GetPrivateKycHandle *kgh; 43 44 /** 45 * Base URL of the merchant serving the request. 46 */ 47 const char *merchant_url; 48 49 /** 50 * Instance to query, NULL if part of @e merchant_url 51 */ 52 const char *instance_id; 53 54 /** 55 * Reference to command providing wire hash, NULL to 56 * query all accounts. 57 */ 58 const char *h_wire_ref; 59 60 /** 61 * URL of exchange to query. 62 */ 63 const char *exchange_url; 64 65 /** 66 * Set to the payto hash of the first account 67 * for which we failed to pass the KYC check. 68 */ 69 struct TALER_NormalizedPaytoHashP h_payto; 70 71 /** 72 * Access token the user needs to start a KYC process. 73 */ 74 struct TALER_AccountAccessTokenP access_token; 75 76 /** 77 * Expected HTTP response code. 78 */ 79 unsigned int expected_http_status; 80 81 /** 82 * Target for long-polling. 83 */ 84 enum TALER_EXCHANGE_KycLongPollTarget lpt; 85 86 /** 87 * Expected KYC state. 88 */ 89 bool expected_kyc_state; 90 91 /** 92 * Expected KYC state. 93 */ 94 bool have_access_token; 95 96 /** 97 * Interpreter state. 98 */ 99 struct TALER_TESTING_Interpreter *is; 100 101 }; 102 103 104 /** 105 * Free the state of a "/kyc" GET CMD, and 106 * possibly cancel a pending "kyc" GET operation. 107 * 108 * @param cls closure with the `struct KycGetState` 109 * @param cmd command currently being freed. 110 */ 111 static void 112 kyc_get_cleanup (void *cls, 113 const struct TALER_TESTING_Command *cmd) 114 { 115 struct KycGetState *cs = cls; 116 117 if (NULL != cs->kgh) 118 { 119 TALER_LOG_WARNING ("/kyc GET operation did not complete\n"); 120 TALER_MERCHANT_get_private_kyc_cancel (cs->kgh); 121 } 122 GNUNET_free (cs); 123 } 124 125 126 /** 127 * Process "GET /public/kyc_get" (lookup) response. 128 * 129 * @param cls closure 130 * @param kr response we got 131 */ 132 static void 133 kyc_get_cb (struct KycGetState *cs, 134 const struct TALER_MERCHANT_GetPrivateKycResponse *kr) 135 { 136 137 cs->kgh = NULL; 138 if (kr->hr.http_status != cs->expected_http_status) 139 { 140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 141 "Expected status %u, got %u\n", 142 cs->expected_http_status, 143 kr->hr.http_status); 144 TALER_TESTING_FAIL (cs->is); 145 } 146 switch (kr->hr.http_status) 147 { 148 case MHD_HTTP_OK: 149 if (! cs->expected_kyc_state) 150 { 151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 152 "Expected KYC state %u, got %u\n", 153 cs->expected_kyc_state, 154 kr->details.ok.kycs_length); 155 TALER_TESTING_FAIL (cs->is); 156 } 157 for (unsigned int i = 0; i<kr->details.ok.kycs_length; i++) 158 { 159 struct TALER_FullPayto payto_uri; 160 161 payto_uri = kr->details.ok.kycs[i].payto_uri; 162 if (NULL == payto_uri.full_payto) 163 { 164 continue; 165 } 166 TALER_full_payto_normalize_and_hash (payto_uri, 167 &cs->h_payto); 168 if (! kr->details.ok.kycs[i].no_access_token) 169 { 170 cs->access_token 171 = kr->details.ok.kycs[i].access_token; 172 cs->have_access_token = true; 173 } 174 break; 175 } 176 break; 177 } 178 TALER_TESTING_interpreter_next (cs->is); 179 } 180 181 182 /** 183 * Run the "kyc_get" CMD. 184 * 185 * @param cls closure. 186 * @param cmd command being currently run. 187 * @param is interpreter state. 188 */ 189 static void 190 kyc_get_run (void *cls, 191 const struct TALER_TESTING_Command *cmd, 192 struct TALER_TESTING_Interpreter *is) 193 { 194 struct KycGetState *cs = cls; 195 const struct TALER_MerchantWireHashP *h_wire = NULL; 196 197 cs->is = is; 198 if (NULL != cs->h_wire_ref) 199 { 200 const struct TALER_TESTING_Command *wire_cmd; 201 202 if (NULL == 203 (wire_cmd = 204 TALER_TESTING_interpreter_lookup_command (cs->is, 205 cs->h_wire_ref))) 206 { 207 TALER_TESTING_FAIL (cs->is); 208 } 209 /* Note: at the time of writing, no command offers an h_wire trait, 210 so for now this code is dead and 'h_wire_ref' must always be NULL... */ 211 if (GNUNET_OK != 212 TALER_TESTING_get_trait_h_wire (wire_cmd, 213 &h_wire)) 214 { 215 TALER_TESTING_FAIL (cs->is); 216 } 217 } 218 cs->kgh = TALER_MERCHANT_get_private_kyc_create ( 219 TALER_TESTING_interpreter_get_context (is), 220 cs->merchant_url); 221 GNUNET_assert (NULL != cs->kgh); 222 if (NULL != cs->instance_id) 223 TALER_MERCHANT_get_private_kyc_set_options ( 224 cs->kgh, 225 TALER_MERCHANT_get_private_kyc_option_instance_id ( 226 cs->instance_id)); 227 if (NULL != h_wire) 228 TALER_MERCHANT_get_private_kyc_set_options ( 229 cs->kgh, 230 TALER_MERCHANT_get_private_kyc_option_h_wire (h_wire)); 231 if (NULL != cs->exchange_url) 232 TALER_MERCHANT_get_private_kyc_set_options ( 233 cs->kgh, 234 TALER_MERCHANT_get_private_kyc_option_exchange_url ( 235 cs->exchange_url)); 236 if (TALER_EXCHANGE_KLPT_NONE != cs->lpt) 237 { 238 TALER_MERCHANT_get_private_kyc_set_options ( 239 cs->kgh, 240 TALER_MERCHANT_get_private_kyc_option_lpt (cs->lpt), 241 TALER_MERCHANT_get_private_kyc_option_timeout ( 242 GNUNET_TIME_UNIT_MINUTES)); 243 } 244 { 245 enum TALER_ErrorCode ec; 246 247 ec = TALER_MERCHANT_get_private_kyc_start (cs->kgh, 248 &kyc_get_cb, 249 cs); 250 GNUNET_assert (TALER_EC_NONE == ec); 251 } 252 } 253 254 255 /** 256 * Offer internal data from "KYC" GET CMD. 257 * 258 * @param cls closure. 259 * @param[out] ret result (could be anything). 260 * @param trait name of the trait. 261 * @param index index number of the object to offer. 262 * @return #GNUNET_OK on success. 263 */ 264 static enum GNUNET_GenericReturnValue 265 kyc_get_traits (void *cls, 266 const void **ret, 267 const char *trait, 268 unsigned int index) 269 { 270 struct KycGetState *cs = cls; 271 struct TALER_TESTING_Trait traits[] = { 272 /* Must be first, skipped if we have no token! */ 273 TALER_TESTING_make_trait_account_access_token ( 274 &cs->access_token), 275 TALER_TESTING_make_trait_h_normalized_payto ( 276 &cs->h_payto), 277 TALER_TESTING_trait_end () 278 }; 279 280 return TALER_TESTING_get_trait ( 281 &traits[cs->have_access_token 282 ? 0 283 : 1], 284 ret, 285 trait, 286 index); 287 } 288 289 290 struct TALER_TESTING_Command 291 TALER_TESTING_cmd_merchant_kyc_get ( 292 const char *label, 293 const char *merchant_url, 294 const char *instance_id, 295 const char *h_wire_ref, 296 const char *exchange_url, 297 enum TALER_EXCHANGE_KycLongPollTarget lpt, 298 unsigned int expected_http_status, 299 bool expected_kyc_state) 300 { 301 struct KycGetState *cs; 302 303 cs = GNUNET_new (struct KycGetState); 304 cs->merchant_url = merchant_url; 305 cs->instance_id = instance_id; 306 cs->h_wire_ref = h_wire_ref; 307 cs->exchange_url = exchange_url; 308 cs->lpt = lpt; 309 cs->expected_http_status = expected_http_status; 310 cs->expected_kyc_state = expected_kyc_state; 311 { 312 struct TALER_TESTING_Command cmd = { 313 .cls = cs, 314 .label = label, 315 .run = &kyc_get_run, 316 .cleanup = &kyc_get_cleanup, 317 .traits = &kyc_get_traits 318 }; 319 320 return cmd; 321 } 322 } 323 324 325 /* end of testing_api_cmd_kyc_get.c */