merchant

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

testing_api_cmd_patch_instance.c (6843B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020 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_instance.c
     21  * @brief command to test PATCH /instance
     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 "PATCH /instance" CMD.
     33  */
     34 struct PatchInstanceState
     35 {
     36 
     37   /**
     38    * Handle for a "PATCH /instance/$ID" request.
     39    */
     40   struct TALER_MERCHANT_InstancePatchHandle *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 instance to run PATCH for.
     54    */
     55   const char *instance_id;
     56 
     57   /**
     58    * Name of the instance.
     59    */
     60   const char *name;
     61 
     62   /**
     63    * Address to use.
     64    */
     65   json_t *address;
     66 
     67   /**
     68    * Jurisdiction to use.
     69    */
     70   json_t *jurisdiction;
     71 
     72   /**
     73    * Use STEFAN curve?
     74    */
     75   bool use_stefan;
     76 
     77   /**
     78    * Wire transfer delay to use.
     79    */
     80   struct GNUNET_TIME_Relative default_wire_transfer_delay;
     81 
     82   /**
     83    * Order validity default duration to use.
     84    */
     85   struct GNUNET_TIME_Relative default_pay_delay;
     86 
     87   /**
     88    * Expected HTTP response code.
     89    */
     90   unsigned int http_status;
     91 
     92 };
     93 
     94 
     95 /**
     96  * Callback for a PATCH /instances/$ID operation.
     97  *
     98  * @param cls closure for this function
     99  * @param hr response being processed
    100  */
    101 static void
    102 patch_instance_cb (void *cls,
    103                    const struct TALER_MERCHANT_HttpResponse *hr)
    104 {
    105   struct PatchInstanceState *pis = cls;
    106 
    107   pis->iph = NULL;
    108   if (pis->http_status != hr->http_status)
    109   {
    110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    111                 "Unexpected response code %u (%d) to command %s\n",
    112                 hr->http_status,
    113                 (int) hr->ec,
    114                 TALER_TESTING_interpreter_get_current_label (pis->is));
    115     TALER_TESTING_interpreter_fail (pis->is);
    116     return;
    117   }
    118   switch (hr->http_status)
    119   {
    120   case MHD_HTTP_NO_CONTENT:
    121     break;
    122   case MHD_HTTP_BAD_REQUEST:
    123     /* happens also for currency mismatch */
    124     break;
    125   case MHD_HTTP_UNAUTHORIZED:
    126     break;
    127   case MHD_HTTP_NOT_FOUND:
    128     break;
    129   case MHD_HTTP_CONFLICT:
    130     break;
    131   default:
    132     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    133                 "Unhandled HTTP status %u for PATCH instance.\n",
    134                 hr->http_status);
    135   }
    136   TALER_TESTING_interpreter_next (pis->is);
    137 }
    138 
    139 
    140 /**
    141  * Run the "PATCH /instances/$ID" CMD.
    142  *
    143  *
    144  * @param cls closure.
    145  * @param cmd command being run now.
    146  * @param is interpreter state.
    147  */
    148 static void
    149 patch_instance_run (void *cls,
    150                     const struct TALER_TESTING_Command *cmd,
    151                     struct TALER_TESTING_Interpreter *is)
    152 {
    153   struct PatchInstanceState *pis = cls;
    154 
    155   pis->is = is;
    156   pis->iph = TALER_MERCHANT_instance_patch (
    157     TALER_TESTING_interpreter_get_context (is),
    158     pis->merchant_url,
    159     pis->instance_id,
    160     pis->name,
    161     pis->address,
    162     pis->jurisdiction,
    163     pis->use_stefan,
    164     pis->default_wire_transfer_delay,
    165     pis->default_pay_delay,
    166     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
    167                                    15),
    168     &patch_instance_cb,
    169     pis);
    170   GNUNET_assert (NULL != pis->iph);
    171 }
    172 
    173 
    174 /**
    175  * Offers information from the PATCH /instances CMD state to other
    176  * commands.
    177  *
    178  * @param cls closure
    179  * @param[out] ret result (could be anything)
    180  * @param trait name of the trait
    181  * @param index index number of the object to extract.
    182  * @return #GNUNET_OK on success
    183  */
    184 static enum GNUNET_GenericReturnValue
    185 patch_instance_traits (void *cls,
    186                        const void **ret,
    187                        const char *trait,
    188                        unsigned int index)
    189 {
    190   struct PatchInstanceState *pis = cls;
    191   struct TALER_TESTING_Trait traits[] = {
    192     TALER_TESTING_make_trait_instance_name (pis->name),
    193     TALER_TESTING_make_trait_instance_id (pis->instance_id),
    194     TALER_TESTING_make_trait_address (pis->address),
    195     TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction),
    196     TALER_TESTING_make_trait_use_stefan (&pis->use_stefan),
    197     TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay),
    198     TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay),
    199     TALER_TESTING_trait_end ()
    200   };
    201 
    202   return TALER_TESTING_get_trait (traits,
    203                                   ret,
    204                                   trait,
    205                                   index);
    206 }
    207 
    208 
    209 /**
    210  * Free the state of a "PATCH /instances/$ID" CMD, and possibly
    211  * cancel a pending operation thereof.
    212  *
    213  * @param cls closure.
    214  * @param cmd command being run.
    215  */
    216 static void
    217 patch_instance_cleanup (void *cls,
    218                         const struct TALER_TESTING_Command *cmd)
    219 {
    220   struct PatchInstanceState *pis = cls;
    221 
    222   if (NULL != pis->iph)
    223   {
    224     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    225                 "PATCH /instance/$ID operation did not complete\n");
    226     TALER_MERCHANT_instance_patch_cancel (pis->iph);
    227   }
    228   json_decref (pis->jurisdiction);
    229   json_decref (pis->address);
    230   GNUNET_free (pis);
    231 }
    232 
    233 
    234 struct TALER_TESTING_Command
    235 TALER_TESTING_cmd_merchant_patch_instance (
    236   const char *label,
    237   const char *merchant_url,
    238   const char *instance_id,
    239   const char *name,
    240   json_t *address,
    241   json_t *jurisdiction,
    242   bool use_stefan,
    243   struct GNUNET_TIME_Relative default_wire_transfer_delay,
    244   struct GNUNET_TIME_Relative default_pay_delay,
    245   unsigned int http_status)
    246 {
    247   struct PatchInstanceState *pis;
    248 
    249   pis = GNUNET_new (struct PatchInstanceState);
    250   pis->merchant_url = merchant_url;
    251   pis->instance_id = instance_id;
    252   pis->http_status = http_status;
    253   pis->name = name;
    254   pis->address = address; /* ownership transfer! */
    255   pis->jurisdiction = jurisdiction; /* ownership transfer! */
    256   pis->use_stefan = use_stefan;
    257   pis->default_wire_transfer_delay = default_wire_transfer_delay;
    258   pis->default_pay_delay = default_pay_delay;
    259   {
    260     struct TALER_TESTING_Command cmd = {
    261       .cls = pis,
    262       .label = label,
    263       .run = &patch_instance_run,
    264       .cleanup = &patch_instance_cleanup,
    265       .traits = &patch_instance_traits
    266     };
    267 
    268     return cmd;
    269   }
    270 }
    271 
    272 
    273 /* end of testing_api_cmd_patch_instance.c */