LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_kyc_get.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 81 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2021 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_api_cmd_kyc_get.c
      22             :  * @brief command to test kyc_get request
      23             :  * @author Christian Grothoff
      24             :  */
      25             : 
      26             : #include "platform.h"
      27             : #include <taler/taler_exchange_service.h>
      28             : #include <taler/taler_testing_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include "taler_merchant_testing_lib.h"
      31             : 
      32             : 
      33             : /**
      34             :  * State for a "/kyc" GET CMD.
      35             :  */
      36             : struct KycGetState
      37             : {
      38             :   /**
      39             :    * Operation handle for a GET /private/kyc GET request.
      40             :    */
      41             :   struct TALER_MERCHANT_KycGetHandle *kgh;
      42             : 
      43             :   /**
      44             :    * Base URL of the merchant serving the request.
      45             :    */
      46             :   const char *merchant_url;
      47             : 
      48             :   /**
      49             :    * Instance to query, NULL if part of @e merchant_url
      50             :    */
      51             :   const char *instance_id;
      52             : 
      53             :   /**
      54             :    * Reference to command providing wire hash, NULL to
      55             :    * query all accounts.
      56             :    */
      57             :   const char *h_wire_ref;
      58             : 
      59             :   /**
      60             :    * URL of exchange to query.
      61             :    */
      62             :   const char *exchange_url;
      63             : 
      64             :   /**
      65             :    * Set to the payto hash of the first account
      66             :    * for which we failed to pass the KYC check.
      67             :    */
      68             :   struct TALER_PaytoHashP h_payto;
      69             : 
      70             :   /**
      71             :    * Expected HTTP response code.
      72             :    */
      73             :   unsigned int expected_http_status;
      74             : 
      75             :   /**
      76             :    * Interpreter state.
      77             :    */
      78             :   struct TALER_TESTING_Interpreter *is;
      79             : 
      80             : };
      81             : 
      82             : 
      83             : /**
      84             :  * Free the state of a "/kyc" GET CMD, and
      85             :  * possibly cancel a pending "kyc" GET operation.
      86             :  *
      87             :  * @param cls closure with the `struct KycGetState`
      88             :  * @param cmd command currently being freed.
      89             :  */
      90             : static void
      91           0 : kyc_get_cleanup (void *cls,
      92             :                  const struct TALER_TESTING_Command *cmd)
      93             : {
      94           0 :   struct KycGetState *cs = cls;
      95             : 
      96           0 :   if (NULL != cs->kgh)
      97             :   {
      98           0 :     TALER_LOG_WARNING ("/kyc GET operation did not complete\n");
      99           0 :     TALER_MERCHANT_kyc_get_cancel (cs->kgh);
     100             :   }
     101           0 :   GNUNET_free (cs);
     102           0 : }
     103             : 
     104             : 
     105             : /**
     106             :  * Process "GET /public/kyc_get" (lookup) response.
     107             :  *
     108             :  * @param cls closure
     109             :  * @param kr response we got
     110             :  */
     111             : static void
     112           0 : kyc_get_cb (void *cls,
     113             :             const struct TALER_MERCHANT_KycResponse *kr)
     114             : {
     115           0 :   struct KycGetState *cs = cls;
     116             : 
     117           0 :   cs->kgh = NULL;
     118           0 :   if (kr->hr.http_status != cs->expected_http_status)
     119             :   {
     120           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     121             :                 "Expected status %u, got %u\n",
     122             :                 cs->expected_http_status,
     123             :                 kr->hr.http_status);
     124           0 :     TALER_TESTING_FAIL (cs->is);
     125             :   }
     126           0 :   switch (kr->hr.http_status)
     127             :   {
     128           0 :   case MHD_HTTP_ACCEPTED:
     129           0 :     if (0 != kr->details.kyc_status.pending_kycs_length)
     130             :     {
     131             :       const char *url;
     132             :       const char *tok;
     133             :       const char *end;
     134             :       char *dec;
     135             :       const char *eq;
     136             :       const char *nq;
     137             :       size_t toklen;
     138             : 
     139           0 :       url = kr->details.kyc_status.pending_kycs[0].kyc_url;
     140           0 :       tok = strstr (url, "&redirect_uri=");
     141           0 :       if (NULL == tok)
     142           0 :         TALER_TESTING_FAIL (cs->is);
     143           0 :       tok += strlen ("&redirect_uri=");
     144           0 :       end = strchr (tok, '&');
     145           0 :       if (NULL == end)
     146           0 :         toklen = strlen (tok);
     147             :       else
     148           0 :         toklen = end - tok;
     149           0 :       (void) GNUNET_STRINGS_urldecode (tok,
     150             :                                        toklen,
     151             :                                        &dec);
     152           0 :       eq = strstr (dec, "/kyc-proof/");
     153           0 :       if (NULL == eq)
     154             :       {
     155           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     156             :                     "Received unexpected KYC URL `%s' (%s)\n",
     157             :                     url,
     158             :                     dec);
     159           0 :         GNUNET_free (dec);
     160           0 :         TALER_TESTING_FAIL (cs->is);
     161             :       }
     162           0 :       eq += strlen ("/kyc-proof/");
     163           0 :       nq = strchr (eq, '/');
     164           0 :       if (NULL == eq)
     165             :       {
     166           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167             :                     "Received unexpected KYC URL `%s' (%s)\n",
     168             :                     url,
     169             :                     dec);
     170           0 :         GNUNET_free (dec);
     171           0 :         TALER_TESTING_FAIL (cs->is);
     172             :       }
     173           0 :       if (GNUNET_OK !=
     174           0 :           GNUNET_STRINGS_string_to_data (eq,
     175           0 :                                          nq - eq,
     176           0 :                                          &cs->h_payto,
     177             :                                          sizeof (cs->h_payto)))
     178             :       {
     179           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     180             :                     "Received unexpected KYC URL `%s' (%s)\n",
     181             :                     url,
     182             :                     dec);
     183           0 :         GNUNET_free (dec);
     184           0 :         TALER_TESTING_FAIL (cs->is);
     185             :       }
     186           0 :       GNUNET_free (dec);
     187             :     }
     188           0 :     break;
     189             :   }
     190           0 :   TALER_TESTING_interpreter_next (cs->is);
     191             : }
     192             : 
     193             : 
     194             : /**
     195             :  * Run the "kyc_get" CMD.
     196             :  *
     197             :  * @param cls closure.
     198             :  * @param cmd command being currently run.
     199             :  * @param is interpreter state.
     200             :  */
     201             : static void
     202           0 : kyc_get_run (void *cls,
     203             :              const struct TALER_TESTING_Command *cmd,
     204             :              struct TALER_TESTING_Interpreter *is)
     205             : {
     206           0 :   struct KycGetState *cs = cls;
     207           0 :   const struct TALER_MerchantWireHashP *h_wire = NULL;
     208             : 
     209           0 :   cs->is = is;
     210           0 :   if (NULL != cs->h_wire_ref)
     211             :   {
     212             :     const struct TALER_TESTING_Command *wire_cmd;
     213             : 
     214           0 :     if (NULL ==
     215             :         (wire_cmd =
     216           0 :            TALER_TESTING_interpreter_lookup_command (cs->is,
     217             :                                                      cs->h_wire_ref)))
     218             :     {
     219           0 :       GNUNET_break (0);
     220           0 :       TALER_TESTING_FAIL (cs->is);
     221             :     }
     222             :     /* Note: at the time of writing, no command offers an h_wire trait,
     223             :        so for now this code is dead and 'h_wire_ref' must always be NULL... */
     224           0 :     if (GNUNET_OK !=
     225           0 :         TALER_TESTING_get_trait_h_wire (wire_cmd,
     226             :                                         &h_wire))
     227             :     {
     228           0 :       GNUNET_break (0);
     229           0 :       TALER_TESTING_FAIL (cs->is);
     230             :     }
     231             :   }
     232           0 :   if (NULL == cs->instance_id)
     233           0 :     cs->kgh = TALER_MERCHANT_kyc_get (is->ctx,
     234             :                                       cs->merchant_url,
     235             :                                       h_wire,
     236             :                                       cs->exchange_url,
     237           0 :                                       GNUNET_TIME_UNIT_ZERO,
     238             :                                       &kyc_get_cb,
     239             :                                       cs);
     240             :   else
     241           0 :     cs->kgh = TALER_MERCHANT_management_kyc_get (is->ctx,
     242             :                                                  cs->merchant_url,
     243             :                                                  cs->instance_id,
     244             :                                                  h_wire,
     245             :                                                  cs->exchange_url,
     246           0 :                                                  GNUNET_TIME_UNIT_ZERO,
     247             :                                                  &kyc_get_cb,
     248             :                                                  cs);
     249             : 
     250           0 :   GNUNET_assert (NULL != cs->kgh);
     251             : }
     252             : 
     253             : 
     254             : /**
     255             :  * Offer internal data from "KYC" GET CMD.
     256             :  *
     257             :  * @param cls closure.
     258             :  * @param[out] ret result (could be anything).
     259             :  * @param trait name of the trait.
     260             :  * @param index index number of the object to offer.
     261             :  * @return #GNUNET_OK on success.
     262             :  */
     263             : static enum GNUNET_GenericReturnValue
     264           0 : kyc_get_traits (void *cls,
     265             :                 const void **ret,
     266             :                 const char *trait,
     267             :                 unsigned int index)
     268             : {
     269           0 :   struct KycGetState *cs = cls;
     270             :   struct TALER_TESTING_Trait traits[] = {
     271           0 :     TALER_TESTING_make_trait_h_payto (
     272           0 :       &cs->h_payto),
     273           0 :     TALER_TESTING_trait_end ()
     274             :   };
     275             : 
     276           0 :   return TALER_TESTING_get_trait (traits,
     277             :                                   ret,
     278             :                                   trait,
     279             :                                   index);
     280             : }
     281             : 
     282             : 
     283             : struct TALER_TESTING_Command
     284           0 : TALER_TESTING_cmd_merchant_kyc_get (const char *label,
     285             :                                     const char *merchant_url,
     286             :                                     const char *instance_id,
     287             :                                     const char *h_wire_ref,
     288             :                                     const char *exchange_url,
     289             :                                     unsigned int expected_http_status)
     290             : {
     291             :   struct KycGetState *cs;
     292             : 
     293           0 :   cs = GNUNET_new (struct KycGetState);
     294           0 :   cs->merchant_url = merchant_url;
     295           0 :   cs->instance_id = instance_id;
     296           0 :   cs->h_wire_ref = h_wire_ref;
     297           0 :   cs->exchange_url = exchange_url;
     298           0 :   cs->expected_http_status = expected_http_status;
     299             :   {
     300           0 :     struct TALER_TESTING_Command cmd = {
     301             :       .cls = cs,
     302             :       .label = label,
     303             :       .run = &kyc_get_run,
     304             :       .cleanup = &kyc_get_cleanup,
     305             :       .traits = &kyc_get_traits
     306             :     };
     307             : 
     308           0 :     return cmd;
     309             :   }
     310             : }
     311             : 
     312             : 
     313             : /* end of testing_api_cmd_kyc_get.c */

Generated by: LCOV version 1.14