merchant

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

testing_api_cmd_get_otp_device.c (5181B)


      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_get_otp_device.c
     21  * @brief command to test GET /otp-devices/$ID
     22  * @author Christian Grothoff
     23  */
     24 #include "platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler_merchant_service.h"
     28 #include "taler_merchant_testing_lib.h"
     29 
     30 
     31 /**
     32  * State of a "GET OTP device" CMD.
     33  */
     34 struct GetOtpDeviceState
     35 {
     36 
     37   /**
     38    * Handle for a "GET /otp-device/$ID" request.
     39    */
     40   struct TALER_MERCHANT_OtpDeviceGetHandle *igh;
     41 
     42   /**
     43    * The interpreter state.
     44    */
     45   struct TALER_TESTING_Interpreter *is;
     46 
     47   /**
     48    * Base URL of the merchant serving the request.
     49    */
     50   const char *merchant_url;
     51 
     52   /**
     53    * ID of the otp_device to run GET for.
     54    */
     55   const char *otp_device_id;
     56 
     57   /**
     58    * Reference for a POST or PATCH /otp-devices CMD (optional).
     59    */
     60   const char *otp_device_reference;
     61 
     62   /**
     63    * Expected HTTP response code.
     64    */
     65   unsigned int http_status;
     66 
     67 };
     68 
     69 
     70 /**
     71  * Callback for a GET /otp-devices/$ID operation.
     72  *
     73  * @param cls closure for this function
     74  * @param tgr HTTP response details
     75  */
     76 static void
     77 get_otp_device_cb (void *cls,
     78                    const struct TALER_MERCHANT_OtpDeviceGetResponse *tgr)
     79 {
     80   struct GetOtpDeviceState *gis = cls;
     81   const struct TALER_TESTING_Command *otp_device_cmd;
     82 
     83   gis->igh = NULL;
     84   if (gis->http_status != tgr->hr.http_status)
     85   {
     86     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     87                 "Unexpected response code %u (%d) to command %s\n",
     88                 tgr->hr.http_status,
     89                 (int) tgr->hr.ec,
     90                 TALER_TESTING_interpreter_get_current_label (gis->is));
     91     TALER_TESTING_interpreter_fail (gis->is);
     92     return;
     93   }
     94   switch (tgr->hr.http_status)
     95   {
     96   case MHD_HTTP_OK:
     97     {
     98       const char *expected_description;
     99 
    100       otp_device_cmd = TALER_TESTING_interpreter_lookup_command (
    101         gis->is,
    102         gis->otp_device_reference);
    103       if (GNUNET_OK !=
    104           TALER_TESTING_get_trait_otp_device_description (otp_device_cmd,
    105                                                           &expected_description))
    106         TALER_TESTING_interpreter_fail (gis->is);
    107       if (0 != strcmp (tgr->details.ok.otp_device_description,
    108                        expected_description))
    109       {
    110         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    111                     "OtpDevice description does not match\n");
    112         TALER_TESTING_interpreter_fail (gis->is);
    113         return;
    114       }
    115     }
    116     break;
    117   case MHD_HTTP_UNAUTHORIZED:
    118     break;
    119   case MHD_HTTP_NOT_FOUND:
    120     break;
    121   default:
    122     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    123                 "Unhandled HTTP status.\n");
    124   }
    125   TALER_TESTING_interpreter_next (gis->is);
    126 }
    127 
    128 
    129 /**
    130  * Run the "GET /otp-device/$ID" CMD.
    131  *
    132  *
    133  * @param cls closure.
    134  * @param cmd command being run now.
    135  * @param is interpreter state.
    136  */
    137 static void
    138 get_otp_device_run (void *cls,
    139                     const struct TALER_TESTING_Command *cmd,
    140                     struct TALER_TESTING_Interpreter *is)
    141 {
    142   struct GetOtpDeviceState *gis = cls;
    143 
    144   gis->is = is;
    145   gis->igh = TALER_MERCHANT_otp_device_get (
    146     TALER_TESTING_interpreter_get_context (is),
    147     gis->merchant_url,
    148     gis->otp_device_id,
    149     &get_otp_device_cb,
    150     gis);
    151   GNUNET_assert (NULL != gis->igh);
    152 }
    153 
    154 
    155 /**
    156  * Free the state of a "GET /otp-device/$ID" CMD, and possibly
    157  * cancel a pending operation thereof.
    158  *
    159  * @param cls closure.
    160  * @param cmd command being run.
    161  */
    162 static void
    163 get_otp_device_cleanup (void *cls,
    164                         const struct TALER_TESTING_Command *cmd)
    165 {
    166   struct GetOtpDeviceState *gis = cls;
    167 
    168   if (NULL != gis->igh)
    169   {
    170     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    171                 "GET /otp-devices/$ID operation did not complete\n");
    172     TALER_MERCHANT_otp_device_get_cancel (gis->igh);
    173   }
    174   GNUNET_free (gis);
    175 }
    176 
    177 
    178 struct TALER_TESTING_Command
    179 TALER_TESTING_cmd_merchant_get_otp_device (
    180   const char *label,
    181   const char *merchant_url,
    182   const char *otp_device_id,
    183   unsigned int http_status,
    184   const char *otp_device_reference)
    185 {
    186   struct GetOtpDeviceState *gis;
    187 
    188   gis = GNUNET_new (struct GetOtpDeviceState);
    189   gis->merchant_url = merchant_url;
    190   gis->otp_device_id = otp_device_id;
    191   gis->http_status = http_status;
    192   gis->otp_device_reference = otp_device_reference;
    193   {
    194     struct TALER_TESTING_Command cmd = {
    195       .cls = gis,
    196       .label = label,
    197       .run = &get_otp_device_run,
    198       .cleanup = &get_otp_device_cleanup
    199     };
    200 
    201     return cmd;
    202   }
    203 }
    204 
    205 
    206 /* end of testing_api_cmd_get_otp_device.c */