merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_post_otp_devices.c (6834B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022 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  * @file testing_api_cmd_post_otp_devices.c
     21  * @brief command to test POST /otp-devices
     22  * @author Christian Grothoff
     23  */
     24 #include "taler/platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler/taler_merchant_service.h"
     28 #include "taler/taler_merchant_testing_lib.h"
     29 #include <taler/taler-merchant/post-private-otp-devices.h>
     30 
     31 
     32 /**
     33  * State of a "POST /otp-devices" CMD.
     34  */
     35 struct PostOtpDevicesState
     36 {
     37 
     38   /**
     39    * Handle for a "GET otp_device" request.
     40    */
     41   struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *iph;
     42 
     43   /**
     44    * The interpreter state.
     45    */
     46   struct TALER_TESTING_Interpreter *is;
     47 
     48   /**
     49    * Base URL of the merchant serving the request.
     50    */
     51   const char *merchant_url;
     52 
     53   /**
     54    * ID of the otp_device to run POST for.
     55    */
     56   const char *otp_device_id;
     57 
     58   /**
     59    * description of the otp_device
     60    */
     61   const char *otp_device_description;
     62 
     63   /**
     64    * base64-encoded key
     65    */
     66   char *otp_key;
     67 
     68   /**
     69    * Option that add amount of the order
     70    */
     71   enum TALER_MerchantConfirmationAlgorithm otp_alg;
     72 
     73   /**
     74    * Counter at the OTP device.
     75    */
     76   uint64_t otp_ctr;
     77 
     78   /**
     79    * Expected HTTP response code.
     80    */
     81   unsigned int http_status;
     82 
     83 };
     84 
     85 
     86 /**
     87  * Callback for a POST /otp-devices operation.
     88  *
     89  * @param cls closure for this function
     90  * @param hr response being processed
     91  */
     92 static void
     93 post_otp_devices_cb (void *cls,
     94                      const struct TALER_MERCHANT_PostPrivateOtpDevicesResponse *
     95                      odr)
     96 {
     97   struct PostOtpDevicesState *tis = cls;
     98 
     99   tis->iph = NULL;
    100   if (tis->http_status != odr->hr.http_status)
    101   {
    102     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    103                 "Unexpected response code %u (%d) to command %s\n",
    104                 odr->hr.http_status,
    105                 (int) odr->hr.ec,
    106                 TALER_TESTING_interpreter_get_current_label (tis->is));
    107     TALER_TESTING_interpreter_fail (tis->is);
    108     return;
    109   }
    110   switch (odr->hr.http_status)
    111   {
    112   case MHD_HTTP_NO_CONTENT:
    113     break;
    114   case MHD_HTTP_UNAUTHORIZED:
    115     break;
    116   case MHD_HTTP_FORBIDDEN:
    117     break;
    118   case MHD_HTTP_NOT_FOUND:
    119     break;
    120   case MHD_HTTP_CONFLICT:
    121     break;
    122   default:
    123     GNUNET_break (0);
    124     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    125                 "Unhandled HTTP status %u for POST /otp-devices.\n",
    126                 odr->hr.http_status);
    127   }
    128   TALER_TESTING_interpreter_next (tis->is);
    129 }
    130 
    131 
    132 /**
    133  * Run the "POST /otp-devices" CMD.
    134  *
    135  *
    136  * @param cls closure.
    137  * @param cmd command being run now.
    138  * @param is interpreter state.
    139  */
    140 static void
    141 post_otp_devices_run (void *cls,
    142                       const struct TALER_TESTING_Command *cmd,
    143                       struct TALER_TESTING_Interpreter *is)
    144 {
    145   struct PostOtpDevicesState *tis = cls;
    146 
    147   tis->is = is;
    148   tis->iph = TALER_MERCHANT_post_private_otp_devices_create (
    149     TALER_TESTING_interpreter_get_context (is),
    150     tis->merchant_url,
    151     tis->otp_device_id,
    152     tis->otp_device_description,
    153     tis->otp_key,
    154     tis->otp_alg);
    155   GNUNET_assert (GNUNET_OK ==
    156                  TALER_MERCHANT_post_private_otp_devices_set_options (
    157                    tis->iph,
    158                    TALER_MERCHANT_post_private_otp_devices_option_otp_ctr (
    159                      tis->otp_ctr)));
    160   {
    161     enum TALER_ErrorCode ec;
    162 
    163     ec = TALER_MERCHANT_post_private_otp_devices_start (
    164       tis->iph,
    165       &post_otp_devices_cb,
    166       tis);
    167     if (TALER_EC_NONE != ec)
    168     {
    169       GNUNET_break (0);
    170       TALER_TESTING_interpreter_fail (tis->is);
    171       return;
    172     }
    173   }
    174 }
    175 
    176 
    177 /**
    178  * Offers information from the POST /otp-devices CMD state to other
    179  * commands.
    180  *
    181  * @param cls closure
    182  * @param[out] ret result (could be anything)
    183  * @param trait name of the trait
    184  * @param index index number of the object to extract.
    185  * @return #GNUNET_OK on success
    186  */
    187 static enum GNUNET_GenericReturnValue
    188 post_otp_devices_traits (void *cls,
    189                          const void **ret,
    190                          const char *trait,
    191                          unsigned int index)
    192 {
    193   struct PostOtpDevicesState *pts = cls;
    194   struct TALER_TESTING_Trait traits[] = {
    195     TALER_TESTING_make_trait_otp_device_description (pts->otp_device_description
    196                                                      ),
    197     TALER_TESTING_make_trait_otp_key (pts->otp_key),
    198     TALER_TESTING_make_trait_otp_alg (&pts->otp_alg),
    199     TALER_TESTING_make_trait_otp_id (pts->otp_device_id),
    200     TALER_TESTING_trait_end (),
    201   };
    202 
    203   return TALER_TESTING_get_trait (traits,
    204                                   ret,
    205                                   trait,
    206                                   index);
    207 }
    208 
    209 
    210 /**
    211  * Free the state of a "POST otp_device" CMD, and possibly
    212  * cancel a pending operation thereof.
    213  *
    214  * @param cls closure.
    215  * @param cmd command being run.
    216  */
    217 static void
    218 post_otp_devices_cleanup (void *cls,
    219                           const struct TALER_TESTING_Command *cmd)
    220 {
    221   struct PostOtpDevicesState *tis = cls;
    222 
    223   if (NULL != tis->iph)
    224   {
    225     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    226                 "POST /otp-devices operation did not complete\n");
    227     TALER_MERCHANT_post_private_otp_devices_cancel (tis->iph);
    228   }
    229   GNUNET_free (tis->otp_key);
    230   GNUNET_free (tis);
    231 }
    232 
    233 
    234 struct TALER_TESTING_Command
    235 TALER_TESTING_cmd_merchant_post_otp_devices (
    236   const char *label,
    237   const char *merchant_url,
    238   const char *otp_device_id,
    239   const char *otp_device_description,
    240   const char *otp_key,
    241   const enum TALER_MerchantConfirmationAlgorithm otp_alg,
    242   uint64_t otp_ctr,
    243   unsigned int http_status)
    244 {
    245   struct PostOtpDevicesState *tis;
    246 
    247   tis = GNUNET_new (struct PostOtpDevicesState);
    248   tis->merchant_url = merchant_url;
    249   tis->otp_device_id = otp_device_id;
    250   tis->http_status = http_status;
    251   tis->otp_device_description = otp_device_description;
    252   tis->otp_key = GNUNET_strdup (otp_key);
    253   tis->otp_alg = otp_alg;
    254   tis->otp_ctr = otp_ctr;
    255   {
    256     struct TALER_TESTING_Command cmd = {
    257       .cls = tis,
    258       .label = label,
    259       .run = &post_otp_devices_run,
    260       .cleanup = &post_otp_devices_cleanup,
    261       .traits = &post_otp_devices_traits
    262     };
    263 
    264     return cmd;
    265   }
    266 }
    267 
    268 
    269 /* end of testing_api_cmd_post_otp_devices.c */