merchant

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

testing_api_cmd_get_otp_device.c (5527B)


      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_device.c
     21  * @brief command to test GET /otp-devices/$ID
     22  * @author Christian Grothoff
     23  */
     24 #include "platform.h"
     25 struct GetOtpDeviceState;
     26 #define TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE struct GetOtpDeviceState
     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-DEVICE_ID.h>
     32 
     33 
     34 /**
     35  * State of a "GET OTP device" CMD.
     36  */
     37 struct GetOtpDeviceState
     38 {
     39 
     40   /**
     41    * Handle for a "GET /otp-device/$ID" request.
     42    */
     43   struct TALER_MERCHANT_GetPrivateOtpDeviceHandle *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    * ID of the otp_device to run GET for.
     57    */
     58   const char *otp_device_id;
     59 
     60   /**
     61    * Reference for a POST or PATCH /otp-devices CMD (optional).
     62    */
     63   const char *otp_device_reference;
     64 
     65   /**
     66    * Expected HTTP response code.
     67    */
     68   unsigned int http_status;
     69 
     70 };
     71 
     72 
     73 /**
     74  * Callback for a GET /otp-devices/$ID operation.
     75  *
     76  * @param cls closure for this function
     77  * @param tgr HTTP response details
     78  */
     79 static void
     80 get_otp_device_cb (struct GetOtpDeviceState *gis,
     81                    const struct TALER_MERCHANT_GetPrivateOtpDeviceResponse *tgr)
     82 {
     83   const struct TALER_TESTING_Command *otp_device_cmd;
     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     {
    100       const char *expected_description;
    101 
    102       otp_device_cmd = TALER_TESTING_interpreter_lookup_command (
    103         gis->is,
    104         gis->otp_device_reference);
    105       if (GNUNET_OK !=
    106           TALER_TESTING_get_trait_otp_device_description (otp_device_cmd,
    107                                                           &expected_description)
    108           )
    109         TALER_TESTING_interpreter_fail (gis->is);
    110       if (0 != strcmp (tgr->details.ok.otp_device_description,
    111                        expected_description))
    112       {
    113         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    114                     "OtpDevice description does not match\n");
    115         TALER_TESTING_interpreter_fail (gis->is);
    116         return;
    117       }
    118     }
    119     break;
    120   case MHD_HTTP_UNAUTHORIZED:
    121     break;
    122   case MHD_HTTP_NOT_FOUND:
    123     break;
    124   default:
    125     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    126                 "Unhandled HTTP status.\n");
    127   }
    128   TALER_TESTING_interpreter_next (gis->is);
    129 }
    130 
    131 
    132 /**
    133  * Run the "GET /otp-device/$ID" CMD.
    134  *
    135  *
    136  * @param cls closure.
    137  * @param cmd command being run now.
    138  * @param is interpreter state.
    139  */
    140 static void
    141 get_otp_device_run (void *cls,
    142                     const struct TALER_TESTING_Command *cmd,
    143                     struct TALER_TESTING_Interpreter *is)
    144 {
    145   struct GetOtpDeviceState *gis = cls;
    146 
    147   gis->is = is;
    148   gis->igh = TALER_MERCHANT_get_private_otp_device_create (
    149     TALER_TESTING_interpreter_get_context (is),
    150     gis->merchant_url,
    151     gis->otp_device_id);
    152   {
    153     enum TALER_ErrorCode ec;
    154 
    155     ec = TALER_MERCHANT_get_private_otp_device_start (
    156       gis->igh,
    157       &get_otp_device_cb,
    158       gis);
    159     GNUNET_assert (TALER_EC_NONE == ec);
    160   }
    161 }
    162 
    163 
    164 /**
    165  * Free the state of a "GET /otp-device/$ID" CMD, and possibly
    166  * cancel a pending operation thereof.
    167  *
    168  * @param cls closure.
    169  * @param cmd command being run.
    170  */
    171 static void
    172 get_otp_device_cleanup (void *cls,
    173                         const struct TALER_TESTING_Command *cmd)
    174 {
    175   struct GetOtpDeviceState *gis = cls;
    176 
    177   if (NULL != gis->igh)
    178   {
    179     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    180                 "GET /otp-devices/$ID operation did not complete\n");
    181     TALER_MERCHANT_get_private_otp_device_cancel (gis->igh);
    182   }
    183   GNUNET_free (gis);
    184 }
    185 
    186 
    187 struct TALER_TESTING_Command
    188 TALER_TESTING_cmd_merchant_get_otp_device (
    189   const char *label,
    190   const char *merchant_url,
    191   const char *otp_device_id,
    192   unsigned int http_status,
    193   const char *otp_device_reference)
    194 {
    195   struct GetOtpDeviceState *gis;
    196 
    197   gis = GNUNET_new (struct GetOtpDeviceState);
    198   gis->merchant_url = merchant_url;
    199   gis->otp_device_id = otp_device_id;
    200   gis->http_status = http_status;
    201   gis->otp_device_reference = otp_device_reference;
    202   {
    203     struct TALER_TESTING_Command cmd = {
    204       .cls = gis,
    205       .label = label,
    206       .run = &get_otp_device_run,
    207       .cleanup = &get_otp_device_cleanup
    208     };
    209 
    210     return cmd;
    211   }
    212 }
    213 
    214 
    215 /* end of testing_api_cmd_get_otp_device.c */