merchant

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

testing_api_cmd_post_instances.c (7532B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-2023 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_post_instances.c
     21  * @brief command to test POST /instances
     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 "POST /instances" CMD.
     33  */
     34 struct PostInstancesState
     35 {
     36 
     37   /**
     38    * Handle for a "POST instance" request.
     39    */
     40   struct TALER_MERCHANT_InstancesPostHandle *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 POST 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    * Authentication token to require for this instance.
     74    */
     75   const char *auth_token;
     76 
     77   /**
     78    * Use STEFAN curves?
     79    */
     80   bool use_stefan;
     81 
     82   /**
     83    * Wire transfer delay to use.
     84    */
     85   struct GNUNET_TIME_Relative default_wire_transfer_delay;
     86 
     87   /**
     88    * Order validity default duration to use.
     89    */
     90   struct GNUNET_TIME_Relative default_pay_delay;
     91 
     92   /**
     93    * Expected HTTP response code.
     94    */
     95   unsigned int http_status;
     96 
     97 };
     98 
     99 
    100 /**
    101  * Callback for a POST /instances operation.
    102  *
    103  * @param cls closure for this function
    104  * @param hr response being processed
    105  */
    106 static void
    107 post_instances_cb (
    108   void *cls,
    109   const struct TALER_MERCHANT_HttpResponse *hr)
    110 {
    111   struct PostInstancesState *pis = cls;
    112 
    113   pis->iph = NULL;
    114   if (pis->http_status != hr->http_status)
    115   {
    116     TALER_TESTING_unexpected_status_with_body (
    117       pis->is,
    118       hr->http_status,
    119       pis->http_status,
    120       hr->reply);
    121     TALER_TESTING_interpreter_fail (pis->is);
    122     return;
    123   }
    124   switch (hr->http_status)
    125   {
    126   case MHD_HTTP_NO_CONTENT:
    127     break;
    128   case MHD_HTTP_BAD_REQUEST:
    129     break;
    130   case MHD_HTTP_UNAUTHORIZED:
    131     break;
    132   case MHD_HTTP_FORBIDDEN:
    133     break;
    134   case MHD_HTTP_NOT_FOUND:
    135     break;
    136   case MHD_HTTP_CONFLICT:
    137     break;
    138   default:
    139     GNUNET_break (0);
    140     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    141                 "Unhandled HTTP status %u for POST instances.\n",
    142                 hr->http_status);
    143   }
    144   TALER_TESTING_interpreter_next (pis->is);
    145 }
    146 
    147 
    148 /**
    149  * Run the "POST /instances" CMD.
    150  *
    151  *
    152  * @param cls closure.
    153  * @param cmd command being run now.
    154  * @param is interpreter state.
    155  */
    156 static void
    157 post_instances_run (
    158   void *cls,
    159   const struct TALER_TESTING_Command *cmd,
    160   struct TALER_TESTING_Interpreter *is)
    161 {
    162   struct PostInstancesState *pis = cls;
    163 
    164   pis->is = is;
    165   pis->iph = TALER_MERCHANT_instances_post (
    166     TALER_TESTING_interpreter_get_context (is),
    167     pis->merchant_url,
    168     pis->instance_id,
    169     pis->name,
    170     pis->address,
    171     pis->jurisdiction,
    172     pis->use_stefan,
    173     pis->default_wire_transfer_delay,
    174     pis->default_pay_delay,
    175     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
    176                                    15),
    177     pis->auth_token,
    178     &post_instances_cb,
    179     pis);
    180   if (NULL == pis->iph)
    181   {
    182     GNUNET_break (0);
    183     TALER_TESTING_interpreter_fail (pis->is);
    184     return;
    185   }
    186 }
    187 
    188 
    189 /**
    190  * Offers information from the POST /instances CMD state to other
    191  * commands.
    192  *
    193  * @param cls closure
    194  * @param[out] ret result (could be anything)
    195  * @param trait name of the trait
    196  * @param index index number of the object to extract.
    197  * @return #GNUNET_OK on success
    198  */
    199 static enum GNUNET_GenericReturnValue
    200 post_instances_traits (void *cls,
    201                        const void **ret,
    202                        const char *trait,
    203                        unsigned int index)
    204 {
    205   struct PostInstancesState *pis = cls;
    206   struct TALER_TESTING_Trait traits[] = {
    207     TALER_TESTING_make_trait_instance_name (pis->name),
    208     TALER_TESTING_make_trait_instance_id (pis->instance_id),
    209     TALER_TESTING_make_trait_address (pis->address),
    210     TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction),
    211     TALER_TESTING_make_trait_use_stefan (&pis->use_stefan),
    212     TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay),
    213     TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay),
    214     TALER_TESTING_trait_end ()
    215   };
    216 
    217   return TALER_TESTING_get_trait (traits,
    218                                   ret,
    219                                   trait,
    220                                   index);
    221 }
    222 
    223 
    224 /**
    225  * Free the state of a "POST /instances" CMD, and possibly
    226  * cancel a pending operation thereof.
    227  *
    228  * @param cls closure.
    229  * @param cmd command being run.
    230  */
    231 static void
    232 post_instances_cleanup (void *cls,
    233                         const struct TALER_TESTING_Command *cmd)
    234 {
    235   struct PostInstancesState *pis = cls;
    236 
    237   if (NULL != pis->iph)
    238   {
    239     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    240                 "POST /instances operation did not complete\n");
    241     TALER_MERCHANT_instances_post_cancel (pis->iph);
    242   }
    243   json_decref (pis->address);
    244   json_decref (pis->jurisdiction);
    245   GNUNET_free (pis);
    246 }
    247 
    248 
    249 struct TALER_TESTING_Command
    250 TALER_TESTING_cmd_merchant_post_instances2 (
    251   const char *label,
    252   const char *merchant_url,
    253   const char *instance_id,
    254   const char *name,
    255   json_t *address,
    256   json_t *jurisdiction,
    257   bool use_stefan,
    258   struct GNUNET_TIME_Relative default_wire_transfer_delay,
    259   struct GNUNET_TIME_Relative default_pay_delay,
    260   const char *auth_token,
    261   unsigned int http_status)
    262 {
    263   struct PostInstancesState *pis;
    264 
    265   pis = GNUNET_new (struct PostInstancesState);
    266   pis->merchant_url = merchant_url;
    267   pis->instance_id = instance_id;
    268   pis->http_status = http_status;
    269   pis->name = name;
    270   pis->address = address; /* ownership transfer! */
    271   pis->jurisdiction = jurisdiction; /* ownership transfer! */
    272   pis->use_stefan = use_stefan;
    273   pis->default_wire_transfer_delay = default_wire_transfer_delay;
    274   pis->default_pay_delay = default_pay_delay;
    275   pis->auth_token = auth_token;
    276   {
    277     struct TALER_TESTING_Command cmd = {
    278       .cls = pis,
    279       .label = label,
    280       .run = &post_instances_run,
    281       .cleanup = &post_instances_cleanup,
    282       .traits = &post_instances_traits
    283     };
    284 
    285     return cmd;
    286   }
    287 }
    288 
    289 
    290 struct TALER_TESTING_Command
    291 TALER_TESTING_cmd_merchant_post_instances (
    292   const char *label,
    293   const char *merchant_url,
    294   const char *instance_id,
    295   unsigned int http_status)
    296 {
    297   return TALER_TESTING_cmd_merchant_post_instances2 (
    298     label,
    299     merchant_url,
    300     instance_id,
    301     instance_id,
    302     json_pack ("{s:s}", "city", "shopcity"),
    303     json_pack ("{s:s}", "city", "lawyercity"),
    304     true,
    305     GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */
    306     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
    307                                    2), /* small pay delay */
    308     NULL,
    309     http_status);
    310 }
    311 
    312 
    313 /* end of testing_api_cmd_post_instance.c */