merchant

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

testing_api_cmd_get_instances.c (7141B)


      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 src/testing/testing_api_cmd_get_instances.c
     21  * @brief command to test GET /instances
     22  * @author Christian Grothoff
     23  */
     24 #include "platform.h"
     25 struct GetInstancesState;
     26 #define TALER_MERCHANT_GET_MANAGEMENT_INSTANCES_RESULT_CLOSURE struct GetInstancesState
     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/get-management-instances.h>
     32 
     33 
     34 /**
     35  * State of a "GET instances" CMD.
     36  */
     37 struct GetInstancesState
     38 {
     39 
     40   /**
     41    * Handle for a "GET instance" request.
     42    */
     43   struct TALER_MERCHANT_GetManagementInstancesHandle *igh;
     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    * Expected HTTP response code.
     57    */
     58   unsigned int http_status;
     59 
     60   /**
     61    * The list of instance references to compare to.
     62    */
     63   const char **instances;
     64 
     65   /**
     66    * The length of @e instances.
     67    */
     68   unsigned int instances_length;
     69 
     70 };
     71 
     72 
     73 /**
     74  * Callback for a GET /instances operation.
     75  *
     76  * @param cls closure for this function
     77  * @param igr response
     78  */
     79 static void
     80 get_instances_cb (struct GetInstancesState *gis,
     81                   const struct TALER_MERCHANT_GetManagementInstancesResponse *
     82                   igr)
     83 {
     84   const struct TALER_MERCHANT_HttpResponse *hr = &igr->hr;
     85 
     86   gis->igh = NULL;
     87   if (gis->http_status != hr->http_status)
     88   {
     89     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     90                 "Unexpected response code %u (%d) to command %s\n",
     91                 hr->http_status,
     92                 (int) hr->ec,
     93                 TALER_TESTING_interpreter_get_current_label (gis->is));
     94     TALER_TESTING_interpreter_fail (gis->is);
     95     return;
     96   }
     97   switch (hr->http_status)
     98   {
     99   case MHD_HTTP_OK:
    100     {
    101       unsigned int iis_length
    102         = igr->details.ok.iis_length;
    103       const struct TALER_MERCHANT_GetManagementInstancesInstanceInfo *iis
    104         = igr->details.ok.iis;
    105 
    106       if (iis_length != gis->instances_length)
    107       {
    108         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    109                     "Length of instances found does not match\n");
    110         TALER_TESTING_interpreter_fail (gis->is);
    111         return;
    112       }
    113       for (unsigned int i = 0; i < iis_length; ++i)
    114       {
    115         const struct TALER_TESTING_Command *instance_cmd;
    116 
    117         instance_cmd = TALER_TESTING_interpreter_lookup_command (
    118           gis->is,
    119           gis->instances[i]);
    120 
    121         {
    122           const char *name;
    123 
    124           if (GNUNET_OK !=
    125               TALER_TESTING_get_trait_instance_name (instance_cmd,
    126                                                      &name))
    127           {
    128             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    129                         "Could not fetch instance name\n");
    130             TALER_TESTING_interpreter_fail (gis->is);
    131             return;
    132           }
    133           if (0 != strcmp (iis[i].name,
    134                            name))
    135           {
    136             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    137                         "Instance name does not match\n");
    138             TALER_TESTING_interpreter_fail (gis->is);
    139             return;
    140           }
    141         }
    142 
    143         {
    144           const char *id;
    145 
    146           if (GNUNET_OK !=
    147               TALER_TESTING_get_trait_instance_id (instance_cmd,
    148                                                    &id))
    149           {
    150             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    151                         "Could not fetch instance id\n");
    152             TALER_TESTING_interpreter_fail (gis->is);
    153             return;
    154           }
    155           if (0 != strcmp (iis[i].id,
    156                            id))
    157           {
    158             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    159                         "Instance id does not match\n");
    160             TALER_TESTING_interpreter_fail (gis->is);
    161             return;
    162           }
    163         }
    164       }
    165 
    166       // FIXME: compare payment_targets
    167       break;
    168     }
    169   default:
    170     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    171                 "Unhandled HTTP status %u for GET /instances.\n",
    172                 hr->http_status);
    173   }
    174   TALER_TESTING_interpreter_next (gis->is);
    175 }
    176 
    177 
    178 /**
    179  * Run the "GET /instances" CMD.
    180  *
    181  *
    182  * @param cls closure.
    183  * @param cmd command being run now.
    184  * @param is interpreter state.
    185  */
    186 static void
    187 get_instances_run (void *cls,
    188                    const struct TALER_TESTING_Command *cmd,
    189                    struct TALER_TESTING_Interpreter *is)
    190 {
    191   struct GetInstancesState *gis = cls;
    192 
    193   gis->is = is;
    194   gis->igh = TALER_MERCHANT_get_management_instances_create (
    195     TALER_TESTING_interpreter_get_context (is),
    196     gis->merchant_url);
    197   {
    198     enum TALER_ErrorCode ec;
    199 
    200     ec = TALER_MERCHANT_get_management_instances_start (
    201       gis->igh,
    202       &get_instances_cb,
    203       gis);
    204     GNUNET_assert (TALER_EC_NONE == ec);
    205   }
    206 }
    207 
    208 
    209 /**
    210  * Free the state of a "GET instance" 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 get_instances_cleanup (void *cls,
    218                        const struct TALER_TESTING_Command *cmd)
    219 {
    220   struct GetInstancesState *gis = cls;
    221 
    222   if (NULL != gis->igh)
    223   {
    224     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    225                 "GET /instances operation did not complete\n");
    226     TALER_MERCHANT_get_management_instances_cancel (gis->igh);
    227   }
    228   GNUNET_array_grow (gis->instances,
    229                      gis->instances_length,
    230                      0);
    231   GNUNET_free (gis);
    232 }
    233 
    234 
    235 struct TALER_TESTING_Command
    236 TALER_TESTING_cmd_merchant_get_instances (const char *label,
    237                                           const char *merchant_url,
    238                                           unsigned int http_status,
    239                                           ...)
    240 {
    241   struct GetInstancesState *gis;
    242 
    243   gis = GNUNET_new (struct GetInstancesState);
    244   gis->merchant_url = merchant_url;
    245   gis->http_status = http_status;
    246   {
    247     const char *clabel;
    248     va_list ap;
    249 
    250     va_start (ap, http_status);
    251     while (NULL != (clabel = va_arg (ap, const char *)))
    252     {
    253       GNUNET_array_append (gis->instances,
    254                            gis->instances_length,
    255                            clabel);
    256     }
    257     va_end (ap);
    258   }
    259   {
    260     struct TALER_TESTING_Command cmd = {
    261       .cls = gis,
    262       .label = label,
    263       .run = &get_instances_run,
    264       .cleanup = &get_instances_cleanup
    265     };
    266 
    267     return cmd;
    268   }
    269 }
    270 
    271 
    272 /* end of testing_api_cmd_get_instances.c */