exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_get_kyc_info.c (6326B)


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