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 (6360B)


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