LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_kyc_start.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 38 57 66.7 %
Date: 2025-06-05 21:03:14 Functions: 5 5 100.0 %

          Line data    Source code
       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_start.c
      22             :  * @brief Implement the testing CMDs for a POST /kyc-start operation.
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler_json_lib.h"
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_testing_lib.h"
      29             : 
      30             : /**
      31             :  * State for a POST /kyc-start CMD.
      32             :  */
      33             : struct PostKycStartState
      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 start.
      43             :    */
      44             :   unsigned int requirement_index;
      45             : 
      46             :   /**
      47             :    * Expected HTTP response code.
      48             :    */
      49             :   unsigned int expected_response_code;
      50             : 
      51             :   /**
      52             :    * Redirect URL returned by the request on success.
      53             :    */
      54             :   char *redirect_url;
      55             : 
      56             :   /**
      57             :    * Handle to the KYC start pending operation.
      58             :    */
      59             :   struct TALER_EXCHANGE_KycStartHandle *kwh;
      60             : 
      61             :   /**
      62             :    * Interpreter state.
      63             :    */
      64             :   struct TALER_TESTING_Interpreter *is;
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Handle response to the command.
      70             :  *
      71             :  * @param cls closure.
      72             :  * @param ks GET KYC status response details
      73             :  */
      74             : static void
      75          10 : post_kyc_start_cb (
      76             :   void *cls,
      77             :   const struct TALER_EXCHANGE_KycStartResponse *ks)
      78             : {
      79          10 :   struct PostKycStartState *kcg = cls;
      80          10 :   struct TALER_TESTING_Interpreter *is = kcg->is;
      81             : 
      82          10 :   kcg->kwh = NULL;
      83          10 :   if (kcg->expected_response_code != ks->hr.http_status)
      84             :   {
      85           0 :     TALER_TESTING_unexpected_status (is,
      86             :                                      ks->hr.http_status,
      87             :                                      kcg->expected_response_code);
      88           0 :     return;
      89             :   }
      90          10 :   switch (ks->hr.http_status)
      91             :   {
      92          10 :   case MHD_HTTP_OK:
      93             :     kcg->redirect_url
      94          10 :       = GNUNET_strdup (ks->details.ok.redirect_url);
      95          10 :     break;
      96           0 :   case MHD_HTTP_NO_CONTENT:
      97           0 :     break;
      98           0 :   default:
      99           0 :     GNUNET_break (0);
     100           0 :     break;
     101             :   }
     102          10 :   TALER_TESTING_interpreter_next (kcg->is);
     103             : }
     104             : 
     105             : 
     106             : /**
     107             :  * Run the command.
     108             :  *
     109             :  * @param cls closure.
     110             :  * @param cmd the command to execute.
     111             :  * @param is the interpreter state.
     112             :  */
     113             : static void
     114          10 : post_kyc_start_run (void *cls,
     115             :                     const struct TALER_TESTING_Command *cmd,
     116             :                     struct TALER_TESTING_Interpreter *is)
     117             : {
     118          10 :   struct PostKycStartState *kcg = cls;
     119             :   const struct TALER_TESTING_Command *res_cmd;
     120             :   const char *id;
     121             : 
     122             :   (void) cmd;
     123          10 :   kcg->is = is;
     124          10 :   res_cmd = TALER_TESTING_interpreter_lookup_command (
     125             :     kcg->is,
     126             :     kcg->kyc_info_reference);
     127          10 :   if (NULL == res_cmd)
     128             :   {
     129           0 :     GNUNET_break (0);
     130           0 :     TALER_TESTING_interpreter_fail (kcg->is);
     131           0 :     return;
     132             :   }
     133          10 :   if (GNUNET_OK !=
     134          10 :       TALER_TESTING_get_trait_kyc_id (
     135             :         res_cmd,
     136             :         kcg->requirement_index,
     137             :         &id))
     138             :   {
     139           0 :     GNUNET_break (0);
     140           0 :     TALER_TESTING_interpreter_fail (kcg->is);
     141           0 :     return;
     142             :   }
     143          10 :   if (NULL == id)
     144             :   {
     145           0 :     GNUNET_break (0);
     146           0 :     TALER_TESTING_interpreter_fail (kcg->is);
     147           0 :     return;
     148             :   }
     149          10 :   kcg->kwh = TALER_EXCHANGE_kyc_start (
     150             :     TALER_TESTING_interpreter_get_context (is),
     151             :     TALER_TESTING_get_exchange_url (is),
     152             :     id,
     153             :     &post_kyc_start_cb,
     154             :     kcg);
     155          10 :   GNUNET_assert (NULL != kcg->kwh);
     156             : }
     157             : 
     158             : 
     159             : /**
     160             :  * Cleanup the state from a "track transaction" CMD, and possibly
     161             :  * cancel a operation thereof.
     162             :  *
     163             :  * @param cls closure.
     164             :  * @param cmd the command which is being cleaned up.
     165             :  */
     166             : static void
     167          10 : post_kyc_start_cleanup (void *cls,
     168             :                         const struct TALER_TESTING_Command *cmd)
     169             : {
     170          10 :   struct PostKycStartState *kcg = cls;
     171             : 
     172          10 :   if (NULL != kcg->kwh)
     173             :   {
     174           0 :     TALER_TESTING_command_incomplete (kcg->is,
     175             :                                       cmd->label);
     176           0 :     TALER_EXCHANGE_kyc_start_cancel (kcg->kwh);
     177           0 :     kcg->kwh = NULL;
     178             :   }
     179          10 :   GNUNET_free (kcg->redirect_url);
     180          10 :   GNUNET_free (kcg);
     181          10 : }
     182             : 
     183             : 
     184             : /**
     185             :  * Offer internal data from a "check KYC" CMD.
     186             :  *
     187             :  * @param cls closure.
     188             :  * @param[out] ret result (could be anything).
     189             :  * @param trait name of the trait.
     190             :  * @param index index number of the object to offer.
     191             :  * @return #GNUNET_OK on success.
     192             :  */
     193             : static enum GNUNET_GenericReturnValue
     194          21 : post_kyc_start_traits (void *cls,
     195             :                        const void **ret,
     196             :                        const char *trait,
     197             :                        unsigned int index)
     198             : {
     199          21 :   struct PostKycStartState *kcg = cls;
     200             :   struct TALER_TESTING_Trait traits[] = {
     201          21 :     TALER_TESTING_make_trait_kyc_url (kcg->redirect_url),
     202          21 :     TALER_TESTING_trait_end ()
     203             :   };
     204             : 
     205          21 :   return TALER_TESTING_get_trait (traits,
     206             :                                   ret,
     207             :                                   trait,
     208             :                                   index);
     209             : }
     210             : 
     211             : 
     212             : struct TALER_TESTING_Command
     213          10 : TALER_TESTING_cmd_post_kyc_start (
     214             :   const char *label,
     215             :   const char *kyc_info_reference,
     216             :   unsigned int requirement_index,
     217             :   unsigned int expected_response_code)
     218             : {
     219             :   struct PostKycStartState *kcg;
     220             : 
     221          10 :   kcg = GNUNET_new (struct PostKycStartState);
     222          10 :   kcg->kyc_info_reference = kyc_info_reference;
     223          10 :   kcg->requirement_index = requirement_index;
     224          10 :   kcg->expected_response_code = expected_response_code;
     225             :   {
     226          10 :     struct TALER_TESTING_Command cmd = {
     227             :       .cls = kcg,
     228             :       .label = label,
     229             :       .run = &post_kyc_start_run,
     230             :       .cleanup = &post_kyc_start_cleanup,
     231             :       .traits = &post_kyc_start_traits
     232             :     };
     233             : 
     234          10 :     return cmd;
     235             :   }
     236             : }
     237             : 
     238             : 
     239             : /* end of testing_api_cmd_post_kyc_start.c */

Generated by: LCOV version 1.16