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_form.c (7622B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 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_form.c
     22  * @brief Implement the testing CMDs for a POST /kyc-form 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 #include "taler/taler_testing_lib.h"
     29 
     30 /**
     31  * State for a POST /kyc-upload/$ID CMD.
     32  */
     33 struct PostKycFormState
     34 {
     35 
     36   /**
     37    * Command that did a GET on /kyc-info
     38    */
     39   const char *kyc_info_reference;
     40 
     41   /**
     42    * Index of the requirement to form.
     43    */
     44   unsigned int requirement_index;
     45 
     46   /**
     47    * Expected HTTP response code.
     48    */
     49   unsigned int expected_response_code;
     50 
     51   /**
     52    * HTTP header to use.
     53    */
     54   struct curl_slist *form_header;
     55 
     56   /**
     57    * Form data to POST.
     58    */
     59   const char *form_data;
     60 
     61   /**
     62    * Curl handle performing the POST.
     63    */
     64   struct GNUNET_CURL_Job *job;
     65 
     66   /**
     67    * Interpreter state.
     68    */
     69   struct TALER_TESTING_Interpreter *is;
     70 };
     71 
     72 
     73 /**
     74  * Handle response to the command.
     75  *
     76  * @param cls closure.
     77  * @param response_code HTTP response code from server, 0 on hard error
     78  * @param response in JSON, NULL if response was not in JSON format
     79  */
     80 static void
     81 post_kyc_form_cb (
     82   void *cls,
     83   long response_code,
     84   const void *response)
     85 {
     86   struct PostKycFormState *kcg = cls;
     87   struct TALER_TESTING_Interpreter *is = kcg->is;
     88 
     89   (void) response;
     90   kcg->job = NULL;
     91   if (kcg->expected_response_code != response_code)
     92   {
     93     TALER_TESTING_unexpected_status (is,
     94                                      (unsigned int) response_code,
     95                                      kcg->expected_response_code);
     96     return;
     97   }
     98   TALER_TESTING_interpreter_next (kcg->is);
     99 }
    100 
    101 
    102 /**
    103  * Get a curl handle with the right defaults.
    104  *
    105  * @param url URL to query
    106  */
    107 static CURL *
    108 curl_easy_setup (const char *url)
    109 {
    110   CURL *eh;
    111 
    112   eh = curl_easy_init ();
    113   if (NULL == eh)
    114   {
    115     GNUNET_break (0);
    116     return NULL;
    117   }
    118   GNUNET_assert (CURLE_OK ==
    119                  curl_easy_setopt (eh,
    120                                    CURLOPT_URL,
    121                                    url));
    122   /* Enable compression (using whatever curl likes), see
    123      https://curl.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html  */
    124   GNUNET_break (CURLE_OK ==
    125                 curl_easy_setopt (eh,
    126                                   CURLOPT_ACCEPT_ENCODING,
    127                                   ""));
    128   GNUNET_assert (CURLE_OK ==
    129                  curl_easy_setopt (eh,
    130                                    CURLOPT_TCP_FASTOPEN,
    131                                    1L));
    132   return eh;
    133 }
    134 
    135 
    136 /**
    137  * Run the command.
    138  *
    139  * @param cls closure.
    140  * @param cmd the command to execute.
    141  * @param is the interpreter state.
    142  */
    143 static void
    144 post_kyc_form_run (void *cls,
    145                    const struct TALER_TESTING_Command *cmd,
    146                    struct TALER_TESTING_Interpreter *is)
    147 {
    148   struct PostKycFormState *kcg = cls;
    149   const struct TALER_TESTING_Command *res_cmd;
    150   const char *id;
    151   CURL *eh;
    152 
    153   (void) cmd;
    154   kcg->is = is;
    155   res_cmd = TALER_TESTING_interpreter_lookup_command (
    156     kcg->is,
    157     kcg->kyc_info_reference);
    158   if (NULL == res_cmd)
    159   {
    160     GNUNET_break (0);
    161     TALER_TESTING_interpreter_fail (kcg->is);
    162     return;
    163   }
    164   if (GNUNET_OK !=
    165       TALER_TESTING_get_trait_kyc_id (
    166         res_cmd,
    167         kcg->requirement_index,
    168         &id))
    169   {
    170     GNUNET_break (0);
    171     TALER_TESTING_interpreter_fail (kcg->is);
    172     return;
    173   }
    174   if (NULL == id)
    175   {
    176     GNUNET_break (0);
    177     TALER_TESTING_interpreter_fail (kcg->is);
    178     return;
    179   }
    180   {
    181     char *url;
    182 
    183     GNUNET_asprintf (&url,
    184                      "%skyc-upload/%s",
    185                      TALER_TESTING_get_exchange_url (is),
    186                      id);
    187     eh = curl_easy_setup (url);
    188     if (NULL == eh)
    189     {
    190       GNUNET_break (0);
    191       GNUNET_free (url);
    192       TALER_TESTING_interpreter_fail (kcg->is);
    193       return;
    194     }
    195     GNUNET_free (url);
    196   }
    197   GNUNET_assert (
    198     CURLE_OK ==
    199     curl_easy_setopt (eh,
    200                       CURLOPT_POST,
    201                       1L));
    202   GNUNET_assert (
    203     CURLE_OK ==
    204     curl_easy_setopt (eh,
    205                       CURLOPT_POSTFIELDS,
    206                       kcg->form_data));
    207   GNUNET_assert (
    208     CURLE_OK ==
    209     curl_easy_setopt (eh,
    210                       CURLOPT_POSTFIELDSIZE_LARGE,
    211                       (curl_off_t) strlen (kcg->form_data)));
    212   kcg->job = GNUNET_CURL_job_add2 (
    213     TALER_TESTING_interpreter_get_context (is),
    214     eh,
    215     kcg->form_header,
    216     &post_kyc_form_cb,
    217     kcg);
    218   GNUNET_assert (NULL != kcg->job);
    219 }
    220 
    221 
    222 /**
    223  * Cleanup the state from a "track transaction" CMD, and possibly
    224  * cancel a operation thereof.
    225  *
    226  * @param cls closure.
    227  * @param cmd the command which is being cleaned up.
    228  */
    229 static void
    230 post_kyc_form_cleanup (void *cls,
    231                        const struct TALER_TESTING_Command *cmd)
    232 {
    233   struct PostKycFormState *kcg = cls;
    234 
    235   if (NULL != kcg->job)
    236   {
    237     TALER_TESTING_command_incomplete (kcg->is,
    238                                       cmd->label);
    239     GNUNET_CURL_job_cancel (kcg->job);
    240     kcg->job = NULL;
    241   }
    242   curl_slist_free_all (kcg->form_header);
    243   GNUNET_free (kcg);
    244 }
    245 
    246 
    247 /**
    248  * Offer internal data from a "check KYC" CMD.
    249  *
    250  * @param cls closure.
    251  * @param[out] ret result (could be anything).
    252  * @param trait name of the trait.
    253  * @param index index number of the object to offer.
    254  * @return #GNUNET_OK on success.
    255  */
    256 static enum GNUNET_GenericReturnValue
    257 post_kyc_form_traits (void *cls,
    258                       const void **ret,
    259                       const char *trait,
    260                       unsigned int index)
    261 {
    262   struct PostKycFormState *kcg = cls;
    263   struct TALER_TESTING_Trait traits[] = {
    264     TALER_TESTING_trait_end ()
    265   };
    266 
    267   (void) kcg;
    268   return TALER_TESTING_get_trait (traits,
    269                                   ret,
    270                                   trait,
    271                                   index);
    272 }
    273 
    274 
    275 struct TALER_TESTING_Command
    276 TALER_TESTING_cmd_post_kyc_form (
    277   const char *label,
    278   const char *kyc_info_reference,
    279   unsigned int requirement_index,
    280   const char *form_data_content_type,
    281   const char *form_data,
    282   unsigned int expected_response_code)
    283 {
    284   struct PostKycFormState *kcg;
    285 
    286   kcg = GNUNET_new (struct PostKycFormState);
    287   kcg->kyc_info_reference = kyc_info_reference;
    288   kcg->requirement_index = requirement_index;
    289   if (NULL != form_data_content_type)
    290   {
    291     char *hdr;
    292 
    293     GNUNET_asprintf (&hdr,
    294                      "%s: %s",
    295                      MHD_HTTP_HEADER_CONTENT_ENCODING,
    296                      form_data_content_type);
    297     kcg->form_header
    298       = curl_slist_append (NULL,
    299                            hdr);
    300     GNUNET_free (hdr);
    301   }
    302   kcg->form_data = form_data;
    303   kcg->expected_response_code = expected_response_code;
    304   {
    305     struct TALER_TESTING_Command cmd = {
    306       .cls = kcg,
    307       .label = label,
    308       .run = &post_kyc_form_run,
    309       .cleanup = &post_kyc_form_cleanup,
    310       .traits = &post_kyc_form_traits
    311     };
    312 
    313     return cmd;
    314   }
    315 }
    316 
    317 
    318 /* end of testing_api_cmd_post_kyc_form.c */