merchant

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

testing_api_cmd_get_otp_device.c (5210B)


      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 "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 
     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           )
    107         TALER_TESTING_interpreter_fail (gis->is);
    108       if (0 != strcmp (tgr->details.ok.otp_device_description,
    109                        expected_description))
    110       {
    111         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    112                     "OtpDevice description does not match\n");
    113         TALER_TESTING_interpreter_fail (gis->is);
    114         return;
    115       }
    116     }
    117     break;
    118   case MHD_HTTP_UNAUTHORIZED:
    119     break;
    120   case MHD_HTTP_NOT_FOUND:
    121     break;
    122   default:
    123     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    124                 "Unhandled HTTP status.\n");
    125   }
    126   TALER_TESTING_interpreter_next (gis->is);
    127 }
    128 
    129 
    130 /**
    131  * Run the "GET /otp-device/$ID" CMD.
    132  *
    133  *
    134  * @param cls closure.
    135  * @param cmd command being run now.
    136  * @param is interpreter state.
    137  */
    138 static void
    139 get_otp_device_run (void *cls,
    140                     const struct TALER_TESTING_Command *cmd,
    141                     struct TALER_TESTING_Interpreter *is)
    142 {
    143   struct GetOtpDeviceState *gis = cls;
    144 
    145   gis->is = is;
    146   gis->igh = TALER_MERCHANT_otp_device_get (
    147     TALER_TESTING_interpreter_get_context (is),
    148     gis->merchant_url,
    149     gis->otp_device_id,
    150     &get_otp_device_cb,
    151     gis);
    152   GNUNET_assert (NULL != gis->igh);
    153 }
    154 
    155 
    156 /**
    157  * Free the state of a "GET /otp-device/$ID" CMD, and possibly
    158  * cancel a pending operation thereof.
    159  *
    160  * @param cls closure.
    161  * @param cmd command being run.
    162  */
    163 static void
    164 get_otp_device_cleanup (void *cls,
    165                         const struct TALER_TESTING_Command *cmd)
    166 {
    167   struct GetOtpDeviceState *gis = cls;
    168 
    169   if (NULL != gis->igh)
    170   {
    171     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    172                 "GET /otp-devices/$ID operation did not complete\n");
    173     TALER_MERCHANT_otp_device_get_cancel (gis->igh);
    174   }
    175   GNUNET_free (gis);
    176 }
    177 
    178 
    179 struct TALER_TESTING_Command
    180 TALER_TESTING_cmd_merchant_get_otp_device (
    181   const char *label,
    182   const char *merchant_url,
    183   const char *otp_device_id,
    184   unsigned int http_status,
    185   const char *otp_device_reference)
    186 {
    187   struct GetOtpDeviceState *gis;
    188 
    189   gis = GNUNET_new (struct GetOtpDeviceState);
    190   gis->merchant_url = merchant_url;
    191   gis->otp_device_id = otp_device_id;
    192   gis->http_status = http_status;
    193   gis->otp_device_reference = otp_device_reference;
    194   {
    195     struct TALER_TESTING_Command cmd = {
    196       .cls = gis,
    197       .label = label,
    198       .run = &get_otp_device_run,
    199       .cleanup = &get_otp_device_cleanup
    200     };
    201 
    202     return cmd;
    203   }
    204 }
    205 
    206 
    207 /* end of testing_api_cmd_get_otp_device.c */