merchant

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

testing_api_cmd_patch_template.c (6527B)


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