merchant

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

testing_api_cmd_get_instances.c (6765B)


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