merchant

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

testing_api_cmd_patch_template.c (6013B)


      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_patch_template.c
     21  * @brief command to test PATCH /template
     22  * @author Priscilla HUANG
     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 "PATCH /template" CMD.
     33  */
     34 struct PatchTemplateState
     35 {
     36 
     37   /**
     38    * Handle for a "GET template" request.
     39    */
     40   struct TALER_MERCHANT_TemplatePatchHandle *iph;
     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 template to run GET for.
     54    */
     55   const char *template_id;
     56 
     57   /**
     58    * description of the template
     59    */
     60   const char *template_description;
     61 
     62   /**
     63    * OTP device ID
     64    */
     65   char *otp_id;
     66 
     67   /**
     68    * Contract of the company
     69    */
     70   json_t *template_contract;
     71 
     72   /**
     73    * Expected HTTP response code.
     74    */
     75   unsigned int http_status;
     76 
     77 };
     78 
     79 
     80 /**
     81  * Callback for a PATCH /templates/$ID operation.
     82  *
     83  * @param cls closure for this function
     84  * @param hr response being processed
     85  */
     86 static void
     87 patch_template_cb (void *cls,
     88                    const struct TALER_MERCHANT_HttpResponse *hr)
     89 {
     90   struct PatchTemplateState *pis = cls;
     91 
     92   pis->iph = NULL;
     93   if (pis->http_status != hr->http_status)
     94   {
     95     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     96                 "Unexpected response code %u (%d) to command %s\n",
     97                 hr->http_status,
     98                 (int) hr->ec,
     99                 TALER_TESTING_interpreter_get_current_label (pis->is));
    100     TALER_TESTING_interpreter_fail (pis->is);
    101     return;
    102   }
    103   switch (hr->http_status)
    104   {
    105   case MHD_HTTP_NO_CONTENT:
    106     break;
    107   case MHD_HTTP_UNAUTHORIZED:
    108     break;
    109   case MHD_HTTP_FORBIDDEN:
    110     break;
    111   case MHD_HTTP_NOT_FOUND:
    112     break;
    113   case MHD_HTTP_CONFLICT:
    114     break;
    115   default:
    116     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    117                 "Unhandled HTTP status %u for PATCH /templates/ID.\n",
    118                 hr->http_status);
    119   }
    120   TALER_TESTING_interpreter_next (pis->is);
    121 }
    122 
    123 
    124 /**
    125  * Run the "PATCH /templates/$ID" CMD.
    126  *
    127  *
    128  * @param cls closure.
    129  * @param cmd command being run now.
    130  * @param is interpreter state.
    131  */
    132 static void
    133 patch_template_run (void *cls,
    134                     const struct TALER_TESTING_Command *cmd,
    135                     struct TALER_TESTING_Interpreter *is)
    136 {
    137   struct PatchTemplateState *pis = cls;
    138 
    139   pis->is = is;
    140   pis->iph = TALER_MERCHANT_template_patch (
    141     TALER_TESTING_interpreter_get_context (is),
    142     pis->merchant_url,
    143     pis->template_id,
    144     pis->template_description,
    145     pis->otp_id,
    146     pis->template_contract,
    147     &patch_template_cb,
    148     pis);
    149   GNUNET_assert (NULL != pis->iph);
    150 }
    151 
    152 
    153 /**
    154  * Offers information from the PATCH /templates CMD state to other
    155  * commands.
    156  *
    157  * @param cls closure
    158  * @param[out] ret result (could be anything)
    159  * @param trait name of the trait
    160  * @param index index number of the object to extract.
    161  * @return #GNUNET_OK on success
    162  */
    163 static enum GNUNET_GenericReturnValue
    164 patch_template_traits (void *cls,
    165                        const void **ret,
    166                        const char *trait,
    167                        unsigned int index)
    168 {
    169   struct PatchTemplateState *pts = cls;
    170   struct TALER_TESTING_Trait traits[] = {
    171     TALER_TESTING_make_trait_template_description (pts->template_description),
    172     TALER_TESTING_make_trait_otp_id (pts->otp_id),
    173     TALER_TESTING_make_trait_template_contract (pts->template_contract),
    174     TALER_TESTING_make_trait_template_id (pts->template_id),
    175     TALER_TESTING_trait_end (),
    176   };
    177 
    178   return TALER_TESTING_get_trait (traits,
    179                                   ret,
    180                                   trait,
    181                                   index);
    182 }
    183 
    184 
    185 /**
    186  * Free the state of a "GET template" CMD, and possibly
    187  * cancel a pending operation thereof.
    188  *
    189  * @param cls closure.
    190  * @param cmd command being run.
    191  */
    192 static void
    193 patch_template_cleanup (void *cls,
    194                         const struct TALER_TESTING_Command *cmd)
    195 {
    196   struct PatchTemplateState *pis = cls;
    197 
    198   if (NULL != pis->iph)
    199   {
    200     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    201                 "PATCH /templates/$ID operation did not complete\n");
    202     TALER_MERCHANT_template_patch_cancel (pis->iph);
    203   }
    204   GNUNET_free (pis->otp_id);
    205   json_decref (pis->template_contract);
    206   GNUNET_free (pis);
    207 }
    208 
    209 
    210 struct TALER_TESTING_Command
    211 TALER_TESTING_cmd_merchant_patch_template (
    212   const char *label,
    213   const char *merchant_url,
    214   const char *template_id,
    215   const char *template_description,
    216   const char *otp_id,
    217   json_t *template_contract,
    218   unsigned int http_status)
    219 {
    220   struct PatchTemplateState *pis;
    221 
    222   pis = GNUNET_new (struct PatchTemplateState);
    223   pis->merchant_url = merchant_url;
    224   pis->template_id = template_id;
    225   pis->http_status = http_status;
    226   pis->template_description = template_description;
    227   pis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id);
    228   pis->template_contract = template_contract; /* ownership taken */
    229   {
    230     struct TALER_TESTING_Command cmd = {
    231       .cls = pis,
    232       .label = label,
    233       .run = &patch_template_run,
    234       .cleanup = &patch_template_cleanup,
    235       .traits = &patch_template_traits
    236     };
    237 
    238     return cmd;
    239   }
    240 }
    241 
    242 
    243 /* end of testing_api_cmd_patch_template.c */