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_post_kyc_start.c (6212B)


      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_post_kyc_start.c
     22  * @brief Implement the testing CMDs for a POST /kyc-start 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 POST /kyc-start CMD.
     30  */
     31 struct PostKycStartState;
     32 
     33 #define TALER_EXCHANGE_POST_KYC_START_RESULT_CLOSURE \
     34         struct PostKycStartState
     35 #include "taler/exchange/post-kyc-start-ID.h"
     36 #include "taler/taler_testing_lib.h"
     37 
     38 /**
     39  * State for a POST /kyc-start CMD.
     40  */
     41 struct PostKycStartState
     42 {
     43 
     44   /**
     45    * Command that did a GET on /kyc-info
     46    */
     47   const char *kyc_info_reference;
     48 
     49   /**
     50    * Index of the requirement to start.
     51    */
     52   unsigned int requirement_index;
     53 
     54   /**
     55    * Expected HTTP response code.
     56    */
     57   unsigned int expected_response_code;
     58 
     59   /**
     60    * Redirect URL returned by the request on success.
     61    */
     62   char *redirect_url;
     63 
     64   /**
     65    * Handle to the KYC start pending operation.
     66    */
     67   struct TALER_EXCHANGE_PostKycStartHandle *kwh;
     68 
     69   /**
     70    * Interpreter state.
     71    */
     72   struct TALER_TESTING_Interpreter *is;
     73 };
     74 
     75 
     76 /**
     77  * Handle response to the command.
     78  *
     79  * @param kcg our state
     80  * @param ks GET KYC status response details
     81  */
     82 static void
     83 post_kyc_start_cb (
     84   struct PostKycStartState *kcg,
     85   const struct TALER_EXCHANGE_PostKycStartResponse *ks)
     86 {
     87   struct TALER_TESTING_Interpreter *is = kcg->is;
     88 
     89   kcg->kwh = NULL;
     90   if (kcg->expected_response_code != ks->hr.http_status)
     91   {
     92     TALER_TESTING_unexpected_status (is,
     93                                      ks->hr.http_status,
     94                                      kcg->expected_response_code);
     95     return;
     96   }
     97   switch (ks->hr.http_status)
     98   {
     99   case MHD_HTTP_OK:
    100     kcg->redirect_url
    101       = GNUNET_strdup (ks->details.ok.redirect_url);
    102     break;
    103   case MHD_HTTP_NO_CONTENT:
    104     break;
    105   default:
    106     GNUNET_break (0);
    107     break;
    108   }
    109   TALER_TESTING_interpreter_next (kcg->is);
    110 }
    111 
    112 
    113 /**
    114  * Run the command.
    115  *
    116  * @param cls closure.
    117  * @param cmd the command to execute.
    118  * @param is the interpreter state.
    119  */
    120 static void
    121 post_kyc_start_run (void *cls,
    122                     const struct TALER_TESTING_Command *cmd,
    123                     struct TALER_TESTING_Interpreter *is)
    124 {
    125   struct PostKycStartState *kcg = cls;
    126   const struct TALER_TESTING_Command *res_cmd;
    127   const char *id;
    128 
    129   (void) cmd;
    130   kcg->is = is;
    131   res_cmd = TALER_TESTING_interpreter_lookup_command (
    132     kcg->is,
    133     kcg->kyc_info_reference);
    134   if (NULL == res_cmd)
    135   {
    136     GNUNET_break (0);
    137     TALER_TESTING_interpreter_fail (kcg->is);
    138     return;
    139   }
    140   if (GNUNET_OK !=
    141       TALER_TESTING_get_trait_kyc_id (
    142         res_cmd,
    143         kcg->requirement_index,
    144         &id))
    145   {
    146     GNUNET_break (0);
    147     TALER_TESTING_interpreter_fail (kcg->is);
    148     return;
    149   }
    150   if (NULL == id)
    151   {
    152     GNUNET_break (0);
    153     TALER_TESTING_interpreter_fail (kcg->is);
    154     return;
    155   }
    156   kcg->kwh = TALER_EXCHANGE_post_kyc_start_create (
    157     TALER_TESTING_interpreter_get_context (is),
    158     TALER_TESTING_get_exchange_url (is),
    159     id);
    160   GNUNET_assert (NULL != kcg->kwh);
    161   {
    162     enum TALER_ErrorCode ec;
    163 
    164     ec = TALER_EXCHANGE_post_kyc_start_start (kcg->kwh,
    165                                               &post_kyc_start_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 post_kyc_start_cleanup (void *cls,
    187                         const struct TALER_TESTING_Command *cmd)
    188 {
    189   struct PostKycStartState *kcg = cls;
    190 
    191   if (NULL != kcg->kwh)
    192   {
    193     TALER_TESTING_command_incomplete (kcg->is,
    194                                       cmd->label);
    195     TALER_EXCHANGE_post_kyc_start_cancel (kcg->kwh);
    196     kcg->kwh = NULL;
    197   }
    198   GNUNET_free (kcg->redirect_url);
    199   GNUNET_free (kcg);
    200 }
    201 
    202 
    203 /**
    204  * Offer internal data from a "check KYC" CMD.
    205  *
    206  * @param cls closure.
    207  * @param[out] ret result (could be anything).
    208  * @param trait name of the trait.
    209  * @param index index number of the object to offer.
    210  * @return #GNUNET_OK on success.
    211  */
    212 static enum GNUNET_GenericReturnValue
    213 post_kyc_start_traits (void *cls,
    214                        const void **ret,
    215                        const char *trait,
    216                        unsigned int index)
    217 {
    218   struct PostKycStartState *kcg = cls;
    219   struct TALER_TESTING_Trait traits[] = {
    220     TALER_TESTING_make_trait_kyc_url (kcg->redirect_url),
    221     TALER_TESTING_trait_end ()
    222   };
    223 
    224   return TALER_TESTING_get_trait (traits,
    225                                   ret,
    226                                   trait,
    227                                   index);
    228 }
    229 
    230 
    231 struct TALER_TESTING_Command
    232 TALER_TESTING_cmd_post_kyc_start (
    233   const char *label,
    234   const char *kyc_info_reference,
    235   unsigned int requirement_index,
    236   unsigned int expected_response_code)
    237 {
    238   struct PostKycStartState *kcg;
    239 
    240   kcg = GNUNET_new (struct PostKycStartState);
    241   kcg->kyc_info_reference = kyc_info_reference;
    242   kcg->requirement_index = requirement_index;
    243   kcg->expected_response_code = expected_response_code;
    244   {
    245     struct TALER_TESTING_Command cmd = {
    246       .cls = kcg,
    247       .label = label,
    248       .run = &post_kyc_start_run,
    249       .cleanup = &post_kyc_start_cleanup,
    250       .traits = &post_kyc_start_traits
    251     };
    252 
    253     return cmd;
    254   }
    255 }
    256 
    257 
    258 /* end of testing_api_cmd_post_kyc_start.c */