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_kyc_check_get.c (7350B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2021-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_kyc_check_get.c
     22  * @brief Implement the testing CMDs for the /kyc_check/ GET operations.
     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 "track transaction" CMD.
     30  */
     31 struct KycCheckGetState;
     32 
     33 #define TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE \
     34         struct KycCheckGetState
     35 #include "taler/exchange/get-kyc-check-H_NORMALIZED_PAYTO.h"
     36 #include "taler/taler_testing_lib.h"
     37 
     38 /**
     39  * State for a "track transaction" CMD.
     40  */
     41 struct KycCheckGetState
     42 {
     43 
     44   /**
     45    * Set to the KYC URL *if* the exchange replied with
     46    * a request for KYC (#MHD_HTTP_ACCEPTED or #MHD_HTTP_OK).
     47    */
     48   struct TALER_AccountAccessTokenP access_token;
     49 
     50   /**
     51    * Handle to the "track transaction" pending operation.
     52    */
     53   struct TALER_EXCHANGE_GetKycCheckHandle *kwh;
     54 
     55   /**
     56    * Interpreter state.
     57    */
     58   struct TALER_TESTING_Interpreter *is;
     59 
     60   /**
     61    * Command to get a reserve private key from.
     62    */
     63   const char *payment_target_reference;
     64 
     65   /**
     66    * Command to get an account private key from.
     67    */
     68   const char *account_reference;
     69 
     70   /**
     71    * Expected HTTP response code.
     72    */
     73   unsigned int expected_response_code;
     74 
     75   /**
     76    * What are we waiting for when long-polling?
     77    */
     78   enum TALER_EXCHANGE_KycLongPollTarget lpt;
     79 
     80 };
     81 
     82 
     83 /**
     84  * Handle response to the command.
     85  *
     86  * @param kcg our state
     87  * @param ks GET KYC status response details
     88  */
     89 static void
     90 check_kyc_cb (
     91   TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE *kcg,
     92   const struct TALER_EXCHANGE_GetKycCheckResponse *ks)
     93 {
     94   struct TALER_TESTING_Interpreter *is = kcg->is;
     95 
     96   kcg->kwh = NULL;
     97   if (kcg->expected_response_code != ks->hr.http_status)
     98   {
     99     TALER_TESTING_unexpected_status (is,
    100                                      ks->hr.http_status,
    101                                      kcg->expected_response_code);
    102     return;
    103   }
    104   switch (ks->hr.http_status)
    105   {
    106   case MHD_HTTP_OK:
    107     kcg->access_token = ks->details.ok.access_token;
    108     break;
    109   case MHD_HTTP_ACCEPTED:
    110     kcg->access_token = ks->details.accepted.access_token;
    111     break;
    112   case MHD_HTTP_NO_CONTENT:
    113     break;
    114   default:
    115     GNUNET_break (0);
    116     break;
    117   }
    118   TALER_TESTING_interpreter_next (kcg->is);
    119 }
    120 
    121 
    122 /**
    123  * Run the command.
    124  *
    125  * @param cls closure.
    126  * @param cmd the command to execute.
    127  * @param is the interpreter state.
    128  */
    129 static void
    130 check_kyc_run (void *cls,
    131                const struct TALER_TESTING_Command *cmd,
    132                struct TALER_TESTING_Interpreter *is)
    133 {
    134   struct KycCheckGetState *kcg = cls;
    135   const struct TALER_TESTING_Command *res_cmd;
    136   const struct TALER_TESTING_Command *acc_cmd;
    137   const struct TALER_NormalizedPaytoHashP *h_payto;
    138   const union TALER_AccountPrivateKeyP *account_priv;
    139 
    140   (void) cmd;
    141   kcg->is = is;
    142   res_cmd = TALER_TESTING_interpreter_lookup_command (
    143     kcg->is,
    144     kcg->payment_target_reference);
    145   if (NULL == res_cmd)
    146   {
    147     GNUNET_break (0);
    148     TALER_TESTING_interpreter_fail (kcg->is);
    149     return;
    150   }
    151   acc_cmd = TALER_TESTING_interpreter_lookup_command (
    152     kcg->is,
    153     kcg->account_reference);
    154   if (NULL == acc_cmd)
    155   {
    156     GNUNET_break (0);
    157     TALER_TESTING_interpreter_fail (kcg->is);
    158     return;
    159   }
    160   if (GNUNET_OK !=
    161       TALER_TESTING_get_trait_h_normalized_payto (
    162         res_cmd,
    163         &h_payto))
    164   {
    165     GNUNET_break (0);
    166     TALER_TESTING_interpreter_fail (kcg->is);
    167     return;
    168   }
    169   if (GNUNET_OK !=
    170       TALER_TESTING_get_trait_account_priv (acc_cmd,
    171                                             &account_priv))
    172   {
    173     GNUNET_break (0);
    174     TALER_TESTING_interpreter_fail (kcg->is);
    175     return;
    176   }
    177   if (0 == h_payto)
    178   {
    179     GNUNET_break (0);
    180     TALER_TESTING_interpreter_fail (kcg->is);
    181     return;
    182   }
    183   kcg->kwh = TALER_EXCHANGE_get_kyc_check_create (
    184     TALER_TESTING_interpreter_get_context (is),
    185     TALER_TESTING_get_exchange_url (is),
    186     h_payto,
    187     account_priv);
    188   GNUNET_assert (NULL != kcg->kwh);
    189   if (TALER_EXCHANGE_KLPT_NONE != kcg->lpt)
    190     TALER_EXCHANGE_get_kyc_check_set_options (
    191       kcg->kwh,
    192       TALER_EXCHANGE_get_kyc_check_option_lpt (kcg->lpt),
    193       TALER_EXCHANGE_get_kyc_check_option_timeout (GNUNET_TIME_UNIT_MINUTES));
    194   {
    195     enum TALER_ErrorCode ec;
    196 
    197     ec = TALER_EXCHANGE_get_kyc_check_start (kcg->kwh,
    198                                              &check_kyc_cb,
    199                                              kcg);
    200     if (TALER_EC_NONE != ec)
    201     {
    202       GNUNET_break (0);
    203       kcg->kwh = NULL;
    204       TALER_TESTING_interpreter_fail (kcg->is);
    205       return;
    206     }
    207   }
    208 }
    209 
    210 
    211 /**
    212  * Cleanup the state from a "track transaction" CMD, and possibly
    213  * cancel a operation thereof.
    214  *
    215  * @param cls closure.
    216  * @param cmd the command which is being cleaned up.
    217  */
    218 static void
    219 check_kyc_cleanup (void *cls,
    220                    const struct TALER_TESTING_Command *cmd)
    221 {
    222   struct KycCheckGetState *kcg = cls;
    223 
    224   if (NULL != kcg->kwh)
    225   {
    226     TALER_TESTING_command_incomplete (kcg->is,
    227                                       cmd->label);
    228     TALER_EXCHANGE_get_kyc_check_cancel (kcg->kwh);
    229     kcg->kwh = NULL;
    230   }
    231   GNUNET_free (kcg);
    232 }
    233 
    234 
    235 /**
    236  * Offer internal data from a "check KYC" CMD.
    237  *
    238  * @param cls closure.
    239  * @param[out] ret result (could be anything).
    240  * @param trait name of the trait.
    241  * @param index index number of the object to offer.
    242  * @return #GNUNET_OK on success.
    243  */
    244 static enum GNUNET_GenericReturnValue
    245 check_kyc_traits (void *cls,
    246                   const void **ret,
    247                   const char *trait,
    248                   unsigned int index)
    249 {
    250   struct KycCheckGetState *kcg = cls;
    251   struct TALER_TESTING_Trait traits[] = {
    252     TALER_TESTING_make_trait_account_access_token (&kcg->access_token),
    253     TALER_TESTING_trait_end ()
    254   };
    255 
    256   return TALER_TESTING_get_trait (traits,
    257                                   ret,
    258                                   trait,
    259                                   index);
    260 }
    261 
    262 
    263 struct TALER_TESTING_Command
    264 TALER_TESTING_cmd_check_kyc_get (
    265   const char *label,
    266   const char *payment_target_reference,
    267   const char *account_reference,
    268   enum TALER_EXCHANGE_KycLongPollTarget lpt,
    269   unsigned int expected_response_code)
    270 {
    271   struct KycCheckGetState *kcg;
    272 
    273   kcg = GNUNET_new (struct KycCheckGetState);
    274   kcg->payment_target_reference = payment_target_reference;
    275   kcg->account_reference = account_reference;
    276   kcg->expected_response_code = expected_response_code;
    277   kcg->lpt = lpt;
    278   {
    279     struct TALER_TESTING_Command cmd = {
    280       .cls = kcg,
    281       .label = label,
    282       .run = &check_kyc_run,
    283       .cleanup = &check_kyc_cleanup,
    284       .traits = &check_kyc_traits
    285     };
    286 
    287     return cmd;
    288   }
    289 }
    290 
    291 
    292 /* end of testing_api_cmd_kyc_check_get.c */