merchant

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

testing_api_cmd_get_templates.c (6376B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022 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_templates.c
     21  * @brief command to test GET /templates
     22  * @author Priscilla HUANG
     23  */
     24 #include "platform.h"
     25 struct GetTemplatesState;
     26 #define TALER_MERCHANT_GET_PRIVATE_TEMPLATES_RESULT_CLOSURE struct GetTemplatesState
     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-private-templates.h>
     32 
     33 
     34 /**
     35  * State of a "GET templates" CMD.
     36  */
     37 struct GetTemplatesState
     38 {
     39 
     40   /**
     41    * Handle for a "GET template" request.
     42    */
     43   struct TALER_MERCHANT_GetPrivateTemplatesHandle *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 template references.
     62    */
     63   const char **templates;
     64 
     65   /**
     66    * Length of @e templates.
     67    */
     68   unsigned int templates_length;
     69 
     70 };
     71 
     72 
     73 /**
     74  * Callback for a GET /templates operation.
     75  *
     76  * @param cls closure for this function
     77  * @param tgr response details
     78  */
     79 static void
     80 get_templates_cb (struct GetTemplatesState *gis,
     81                   const struct TALER_MERCHANT_GetPrivateTemplatesResponse *tgr)
     82 {
     83 
     84   gis->igh = NULL;
     85   if (gis->http_status != tgr->hr.http_status)
     86   {
     87     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     88                 "Unexpected response code %u (%d) to command %s\n",
     89                 tgr->hr.http_status,
     90                 (int) tgr->hr.ec,
     91                 TALER_TESTING_interpreter_get_current_label (gis->is));
     92     TALER_TESTING_interpreter_fail (gis->is);
     93     return;
     94   }
     95   switch (tgr->hr.http_status)
     96   {
     97   case MHD_HTTP_OK:
     98     if (tgr->details.ok.templates_length != gis->templates_length)
     99     {
    100       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    101                   "Length of templates found does not match\n");
    102       TALER_TESTING_interpreter_fail (gis->is);
    103       return;
    104     }
    105     for (unsigned int i = 0; i < gis->templates_length; ++i)
    106     {
    107       const struct TALER_TESTING_Command *template_cmd;
    108 
    109       template_cmd = TALER_TESTING_interpreter_lookup_command (
    110         gis->is,
    111         gis->templates[i]);
    112 
    113       {
    114         const char *template_id;
    115 
    116         if (GNUNET_OK !=
    117             TALER_TESTING_get_trait_template_id (template_cmd,
    118                                                  &template_id))
    119         {
    120           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    121                       "Could not fetch template id\n");
    122           TALER_TESTING_interpreter_fail (gis->is);
    123           return;
    124         }
    125         if (0 != strcmp (tgr->details.ok.templates[i].template_id,
    126                          template_id))
    127         {
    128           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    129                       "Template id does not match\n");
    130           TALER_TESTING_interpreter_fail (gis->is);
    131           return;
    132         }
    133       }
    134     }
    135     break;
    136   case MHD_HTTP_UNAUTHORIZED:
    137     break;
    138   case MHD_HTTP_NOT_FOUND:
    139     /* instance does not exist */
    140     break;
    141   default:
    142     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    143                 "Unhandled HTTP status %u (%d).\n",
    144                 tgr->hr.http_status,
    145                 tgr->hr.ec);
    146     break;
    147   }
    148   TALER_TESTING_interpreter_next (gis->is);
    149 }
    150 
    151 
    152 /**
    153  * Run the "GET /templates" CMD.
    154  *
    155  *
    156  * @param cls closure.
    157  * @param cmd command being run now.
    158  * @param is interpreter state.
    159  */
    160 static void
    161 get_templates_run (void *cls,
    162                    const struct TALER_TESTING_Command *cmd,
    163                    struct TALER_TESTING_Interpreter *is)
    164 {
    165   struct GetTemplatesState *gis = cls;
    166 
    167   gis->is = is;
    168   gis->igh = TALER_MERCHANT_get_private_templates_create (
    169     TALER_TESTING_interpreter_get_context (is),
    170     gis->merchant_url);
    171   {
    172     enum TALER_ErrorCode ec;
    173 
    174     ec = TALER_MERCHANT_get_private_templates_start (gis->igh,
    175                                                      &get_templates_cb,
    176                                                      gis);
    177     GNUNET_assert (TALER_EC_NONE == ec);
    178   }
    179 }
    180 
    181 
    182 /**
    183  * Free the state of a "GET template" CMD, and possibly
    184  * cancel a pending operation thereof.
    185  *
    186  * @param cls closure.
    187  * @param cmd command being run.
    188  */
    189 static void
    190 get_templates_cleanup (void *cls,
    191                        const struct TALER_TESTING_Command *cmd)
    192 {
    193   struct GetTemplatesState *gis = cls;
    194 
    195   if (NULL != gis->igh)
    196   {
    197     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    198                 "GET /templates operation did not complete\n");
    199     TALER_MERCHANT_get_private_templates_cancel (gis->igh);
    200   }
    201   GNUNET_array_grow (gis->templates,
    202                      gis->templates_length,
    203                      0);
    204   GNUNET_free (gis);
    205 }
    206 
    207 
    208 struct TALER_TESTING_Command
    209 TALER_TESTING_cmd_merchant_get_templates (const char *label,
    210                                           const char *merchant_url,
    211                                           unsigned int http_status,
    212                                           ...)
    213 {
    214   struct GetTemplatesState *gis;
    215 
    216   gis = GNUNET_new (struct GetTemplatesState);
    217   gis->merchant_url = merchant_url;
    218   gis->http_status = http_status;
    219   {
    220     const char *clabel;
    221     va_list ap;
    222 
    223     va_start (ap, http_status);
    224     while (NULL != (clabel = va_arg (ap, const char *)))
    225     {
    226       GNUNET_array_append (gis->templates,
    227                            gis->templates_length,
    228                            clabel);
    229     }
    230     va_end (ap);
    231   }
    232   {
    233     struct TALER_TESTING_Command cmd = {
    234       .cls = gis,
    235       .label = label,
    236       .run = &get_templates_run,
    237       .cleanup = &get_templates_cleanup
    238     };
    239 
    240     return cmd;
    241   }
    242 }
    243 
    244 
    245 /* end of testing_api_cmd_get_templates.c */