merchant

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

testing_api_cmd_get_instances.c (7046B)


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