merchant

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

testing_api_cmd_patch_instance.c (7607B)


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