merchant

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

testing_api_cmd_patch_template.c (6487B)


      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_UNAUTHORIZED:
    112     break;
    113   case MHD_HTTP_FORBIDDEN:
    114     break;
    115   case MHD_HTTP_NOT_FOUND:
    116     break;
    117   case MHD_HTTP_CONFLICT:
    118     break;
    119   default:
    120     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    121                 "Unhandled HTTP status %u for PATCH /templates/ID.\n",
    122                 hr->http_status);
    123   }
    124   TALER_TESTING_interpreter_next (pis->is);
    125 }
    126 
    127 
    128 /**
    129  * Run the "PATCH /templates/$ID" CMD.
    130  *
    131  *
    132  * @param cls closure.
    133  * @param cmd command being run now.
    134  * @param is interpreter state.
    135  */
    136 static void
    137 patch_template_run (void *cls,
    138                     const struct TALER_TESTING_Command *cmd,
    139                     struct TALER_TESTING_Interpreter *is)
    140 {
    141   struct PatchTemplateState *pis = cls;
    142 
    143   pis->is = is;
    144   pis->iph = TALER_MERCHANT_patch_private_template_create (
    145     TALER_TESTING_interpreter_get_context (is),
    146     pis->merchant_url,
    147     pis->template_id,
    148     pis->template_description,
    149     pis->otp_id,
    150     pis->template_contract);
    151   GNUNET_assert (NULL != pis->iph);
    152   {
    153     enum TALER_ErrorCode ec;
    154 
    155     ec = TALER_MERCHANT_patch_private_template_start (
    156       pis->iph,
    157       &patch_template_cb,
    158       pis);
    159     GNUNET_assert (TALER_EC_NONE == ec);
    160   }
    161 }
    162 
    163 
    164 /**
    165  * Offers information from the PATCH /templates CMD state to other
    166  * commands.
    167  *
    168  * @param cls closure
    169  * @param[out] ret result (could be anything)
    170  * @param trait name of the trait
    171  * @param index index number of the object to extract.
    172  * @return #GNUNET_OK on success
    173  */
    174 static enum GNUNET_GenericReturnValue
    175 patch_template_traits (void *cls,
    176                        const void **ret,
    177                        const char *trait,
    178                        unsigned int index)
    179 {
    180   struct PatchTemplateState *pts = cls;
    181   struct TALER_TESTING_Trait traits[] = {
    182     TALER_TESTING_make_trait_template_description (pts->template_description),
    183     TALER_TESTING_make_trait_otp_id (pts->otp_id),
    184     TALER_TESTING_make_trait_template_contract (pts->template_contract),
    185     TALER_TESTING_make_trait_template_id (pts->template_id),
    186     TALER_TESTING_trait_end (),
    187   };
    188 
    189   return TALER_TESTING_get_trait (traits,
    190                                   ret,
    191                                   trait,
    192                                   index);
    193 }
    194 
    195 
    196 /**
    197  * Free the state of a "GET template" CMD, and possibly
    198  * cancel a pending operation thereof.
    199  *
    200  * @param cls closure.
    201  * @param cmd command being run.
    202  */
    203 static void
    204 patch_template_cleanup (void *cls,
    205                         const struct TALER_TESTING_Command *cmd)
    206 {
    207   struct PatchTemplateState *pis = cls;
    208 
    209   if (NULL != pis->iph)
    210   {
    211     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    212                 "PATCH /templates/$ID operation did not complete\n");
    213     TALER_MERCHANT_patch_private_template_cancel (pis->iph);
    214   }
    215   GNUNET_free (pis->otp_id);
    216   json_decref (pis->template_contract);
    217   GNUNET_free (pis);
    218 }
    219 
    220 
    221 struct TALER_TESTING_Command
    222 TALER_TESTING_cmd_merchant_patch_template (
    223   const char *label,
    224   const char *merchant_url,
    225   const char *template_id,
    226   const char *template_description,
    227   const char *otp_id,
    228   json_t *template_contract,
    229   unsigned int http_status)
    230 {
    231   struct PatchTemplateState *pis;
    232 
    233   pis = GNUNET_new (struct PatchTemplateState);
    234   pis->merchant_url = merchant_url;
    235   pis->template_id = template_id;
    236   pis->http_status = http_status;
    237   pis->template_description = template_description;
    238   pis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id);
    239   pis->template_contract = template_contract; /* ownership taken */
    240   {
    241     struct TALER_TESTING_Command cmd = {
    242       .cls = pis,
    243       .label = label,
    244       .run = &patch_template_run,
    245       .cleanup = &patch_template_cleanup,
    246       .traits = &patch_template_traits
    247     };
    248 
    249     return cmd;
    250   }
    251 }
    252 
    253 
    254 /* end of testing_api_cmd_patch_template.c */