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