merchant

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

testing_api_cmd_get_otp_devices.c (6417B)


      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 src/testing/testing_api_cmd_get_otp_devices.c
     21  * @brief command to test GET /otp-devices
     22  * @author Christian Grothoff
     23  */
     24 #include "platform.h"
     25 struct GetOtpDevicesState;
     26 #define TALER_MERCHANT_GET_PRIVATE_OTP_DEVICES_RESULT_CLOSURE struct GetOtpDevicesState
     27 #include <taler/taler_exchange_service.h>
     28 #include <taler/taler_testing_lib.h>
     29 #include "taler/taler_merchant_service.h"
     30 #include "taler/taler_merchant_testing_lib.h"
     31 #include <taler/merchant/get-private-otp-devices.h>
     32 
     33 
     34 /**
     35  * State of a "GET /otp-devices" CMD.
     36  */
     37 struct GetOtpDevicesState
     38 {
     39 
     40   /**
     41    * Handle for a "GET /otp-devices" request.
     42    */
     43   struct TALER_MERCHANT_GetPrivateOtpDevicesHandle *igh;
     44 
     45   /**
     46    * The interpreter state.
     47    */
     48   struct TALER_TESTING_Interpreter *is;
     49 
     50   /**
     51    * Base URL of the merchant serving the request.
     52    */
     53   const char *merchant_url;
     54 
     55   /**
     56    * Expected HTTP response code.
     57    */
     58   unsigned int http_status;
     59 
     60   /**
     61    * The list of otp_device references.
     62    */
     63   const char **otp_devices;
     64 
     65   /**
     66    * Length of @e otp_devices.
     67    */
     68   unsigned int otp_devices_length;
     69 
     70 };
     71 
     72 
     73 /**
     74  * Callback for a GET /otp-devices operation.
     75  *
     76  * @param cls closure for this function
     77  * @param tgr response details
     78  */
     79 static void
     80 get_otp_devices_cb (struct GetOtpDevicesState *gis,
     81                     const struct TALER_MERCHANT_GetPrivateOtpDevicesResponse *
     82                     tgr)
     83 {
     84 
     85   gis->igh = NULL;
     86   if (gis->http_status != tgr->hr.http_status)
     87   {
     88     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     89                 "Unexpected response code %u (%d) to command %s\n",
     90                 tgr->hr.http_status,
     91                 (int) tgr->hr.ec,
     92                 TALER_TESTING_interpreter_get_current_label (gis->is));
     93     TALER_TESTING_interpreter_fail (gis->is);
     94     return;
     95   }
     96   switch (tgr->hr.http_status)
     97   {
     98   case MHD_HTTP_OK:
     99     if (tgr->details.ok.otp_devices_length != gis->otp_devices_length)
    100     {
    101       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    102                   "Length of otp_devices found does not match\n");
    103       TALER_TESTING_interpreter_fail (gis->is);
    104       return;
    105     }
    106     for (unsigned int i = 0; i < gis->otp_devices_length; ++i)
    107     {
    108       const struct TALER_TESTING_Command *otp_device_cmd;
    109 
    110       otp_device_cmd = TALER_TESTING_interpreter_lookup_command (
    111         gis->is,
    112         gis->otp_devices[i]);
    113 
    114       {
    115         const char *otp_device_id;
    116 
    117         if (GNUNET_OK !=
    118             TALER_TESTING_get_trait_otp_id (otp_device_cmd,
    119                                             &otp_device_id))
    120         {
    121           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    122                       "Could not fetch otp_device id\n");
    123           TALER_TESTING_interpreter_fail (gis->is);
    124           return;
    125         }
    126         if (0 != strcmp (tgr->details.ok.otp_devices[i].otp_device_id,
    127                          otp_device_id))
    128         {
    129           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    130                       "OtpDevice id does not match\n");
    131           TALER_TESTING_interpreter_fail (gis->is);
    132           return;
    133         }
    134       }
    135     }
    136     break;
    137   case MHD_HTTP_UNAUTHORIZED:
    138     break;
    139   case MHD_HTTP_NOT_FOUND:
    140     /* instance does not exist */
    141     break;
    142   default:
    143     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    144                 "Unhandled HTTP status %u (%d).\n",
    145                 tgr->hr.http_status,
    146                 tgr->hr.ec);
    147     break;
    148   }
    149   TALER_TESTING_interpreter_next (gis->is);
    150 }
    151 
    152 
    153 /**
    154  * Run the "GET /otp-devices" CMD.
    155  *
    156  *
    157  * @param cls closure.
    158  * @param cmd command being run now.
    159  * @param is interpreter state.
    160  */
    161 static void
    162 get_otp_devices_run (void *cls,
    163                      const struct TALER_TESTING_Command *cmd,
    164                      struct TALER_TESTING_Interpreter *is)
    165 {
    166   struct GetOtpDevicesState *gis = cls;
    167 
    168   gis->is = is;
    169   gis->igh = TALER_MERCHANT_get_private_otp_devices_create (
    170     TALER_TESTING_interpreter_get_context (is),
    171     gis->merchant_url);
    172   {
    173     enum TALER_ErrorCode ec;
    174 
    175     ec = TALER_MERCHANT_get_private_otp_devices_start (
    176       gis->igh,
    177       &get_otp_devices_cb,
    178       gis);
    179     GNUNET_assert (TALER_EC_NONE == ec);
    180   }
    181 }
    182 
    183 
    184 /**
    185  * Free the state of a "GET otp_device" CMD, and possibly
    186  * cancel a pending operation thereof.
    187  *
    188  * @param cls closure.
    189  * @param cmd command being run.
    190  */
    191 static void
    192 get_otp_devices_cleanup (void *cls,
    193                          const struct TALER_TESTING_Command *cmd)
    194 {
    195   struct GetOtpDevicesState *gis = cls;
    196 
    197   if (NULL != gis->igh)
    198   {
    199     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    200                 "GET /otp-devices operation did not complete\n");
    201     TALER_MERCHANT_get_private_otp_devices_cancel (gis->igh);
    202   }
    203   GNUNET_array_grow (gis->otp_devices,
    204                      gis->otp_devices_length,
    205                      0);
    206   GNUNET_free (gis);
    207 }
    208 
    209 
    210 struct TALER_TESTING_Command
    211 TALER_TESTING_cmd_merchant_get_otp_devices (const char *label,
    212                                             const char *merchant_url,
    213                                             unsigned int http_status,
    214                                             ...)
    215 {
    216   struct GetOtpDevicesState *gis;
    217 
    218   gis = GNUNET_new (struct GetOtpDevicesState);
    219   gis->merchant_url = merchant_url;
    220   gis->http_status = http_status;
    221   {
    222     const char *clabel;
    223     va_list ap;
    224 
    225     va_start (ap, http_status);
    226     while (NULL != (clabel = va_arg (ap, const char *)))
    227     {
    228       GNUNET_array_append (gis->otp_devices,
    229                            gis->otp_devices_length,
    230                            clabel);
    231     }
    232     va_end (ap);
    233   }
    234   {
    235     struct TALER_TESTING_Command cmd = {
    236       .cls = gis,
    237       .label = label,
    238       .run = &get_otp_devices_run,
    239       .cleanup = &get_otp_devices_cleanup
    240     };
    241 
    242     return cmd;
    243   }
    244 }
    245 
    246 
    247 /* end of testing_api_cmd_get_otp_devices.c */