merchant

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

testing_api_cmd_get_template.c (6636B)


      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 testing_api_cmd_get_template.c
     21  * @brief command to test GET /templates/$ID
     22  * @author Priscilla HUANG
     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 template" CMD.
     33  */
     34 struct GetTemplateState
     35 {
     36 
     37   /**
     38    * Handle for a "GET template" request.
     39    */
     40   struct TALER_MERCHANT_TemplateGetHandle *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    * ID of the template to run GET for.
     54    */
     55   const char *template_id;
     56 
     57   /**
     58    * Reference for a POST or PATCH /templates CMD (optional).
     59    */
     60   const char *template_reference;
     61 
     62   /**
     63    * Expected HTTP response code.
     64    */
     65   unsigned int http_status;
     66 
     67 };
     68 
     69 
     70 /**
     71  * Callback for a /get/templates/$ID operation.
     72  *
     73  * @param cls closure for this function
     74  * @param tgr HTTP response details
     75  */
     76 static void
     77 get_template_cb (void *cls,
     78                  const struct TALER_MERCHANT_TemplateGetResponse *tgr)
     79 {
     80   struct GetTemplateState *gis = cls;
     81   const struct TALER_TESTING_Command *template_cmd;
     82 
     83   gis->igh = NULL;
     84   if (gis->http_status != tgr->hr.http_status)
     85   {
     86     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     87                 "Unexpected response code %u (%d) to command %s\n",
     88                 tgr->hr.http_status,
     89                 (int) tgr->hr.ec,
     90                 TALER_TESTING_interpreter_get_current_label (gis->is));
     91     TALER_TESTING_interpreter_fail (gis->is);
     92     return;
     93   }
     94   switch (tgr->hr.http_status)
     95   {
     96   case MHD_HTTP_OK:
     97     {
     98       const char *expected_description;
     99 
    100       template_cmd = TALER_TESTING_interpreter_lookup_command (
    101         gis->is,
    102         gis->template_reference);
    103       if (GNUNET_OK !=
    104           TALER_TESTING_get_trait_template_description (template_cmd,
    105                                                         &expected_description))
    106         TALER_TESTING_interpreter_fail (gis->is);
    107       if (0 != strcmp (tgr->details.ok.template_description,
    108                        expected_description))
    109       {
    110         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    111                     "Template description does not match\n");
    112         TALER_TESTING_interpreter_fail (gis->is);
    113         return;
    114       }
    115     }
    116     {
    117       const char *expected_otp_id;
    118 
    119       if (GNUNET_OK !=
    120           TALER_TESTING_get_trait_otp_id (template_cmd,
    121                                           &expected_otp_id))
    122         TALER_TESTING_interpreter_fail (gis->is);
    123       if ( ( (NULL == tgr->details.ok.otp_id) && (NULL != expected_otp_id)) ||
    124            ( (NULL != tgr->details.ok.otp_id) && (NULL == expected_otp_id)) ||
    125            ( (NULL != tgr->details.ok.otp_id) &&
    126              (0 != strcmp (tgr->details.ok.otp_id,
    127                            expected_otp_id)) ) )
    128       {
    129         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    130                     "Template pos_key `%s' does not match `%s'\n",
    131                     tgr->details.ok.otp_id,
    132                     expected_otp_id);
    133         TALER_TESTING_interpreter_fail (gis->is);
    134         return;
    135       }
    136     }
    137     {
    138       const json_t *expected_template_contract;
    139 
    140       if (GNUNET_OK !=
    141           TALER_TESTING_get_trait_template_contract (template_cmd,
    142                                                      &expected_template_contract))
    143         TALER_TESTING_interpreter_fail (gis->is);
    144       if (1 != json_equal (tgr->details.ok.template_contract,
    145                            expected_template_contract))
    146       {
    147         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    148                     "Template contract does not match\n");
    149         TALER_TESTING_interpreter_fail (gis->is);
    150         return;
    151       }
    152     }
    153     break;
    154   case MHD_HTTP_UNAUTHORIZED:
    155     break;
    156   case MHD_HTTP_NOT_FOUND:
    157     break;
    158   default:
    159     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    160                 "Unhandled HTTP status.\n");
    161   }
    162   TALER_TESTING_interpreter_next (gis->is);
    163 }
    164 
    165 
    166 /**
    167  * Run the "GET template" CMD.
    168  *
    169  *
    170  * @param cls closure.
    171  * @param cmd command being run now.
    172  * @param is interpreter state.
    173  */
    174 static void
    175 get_template_run (void *cls,
    176                   const struct TALER_TESTING_Command *cmd,
    177                   struct TALER_TESTING_Interpreter *is)
    178 {
    179   struct GetTemplateState *gis = cls;
    180 
    181   gis->is = is;
    182   gis->igh = TALER_MERCHANT_template_get (
    183     TALER_TESTING_interpreter_get_context (is),
    184     gis->merchant_url,
    185     gis->template_id,
    186     &get_template_cb,
    187     gis);
    188   GNUNET_assert (NULL != gis->igh);
    189 }
    190 
    191 
    192 /**
    193  * Free the state of a "GET template" CMD, and possibly
    194  * cancel a pending operation thereof.
    195  *
    196  * @param cls closure.
    197  * @param cmd command being run.
    198  */
    199 static void
    200 get_template_cleanup (void *cls,
    201                       const struct TALER_TESTING_Command *cmd)
    202 {
    203   struct GetTemplateState *gis = cls;
    204 
    205   if (NULL != gis->igh)
    206   {
    207     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    208                 "GET /templates/$ID operation did not complete\n");
    209     TALER_MERCHANT_template_get_cancel (gis->igh);
    210   }
    211   GNUNET_free (gis);
    212 }
    213 
    214 
    215 struct TALER_TESTING_Command
    216 TALER_TESTING_cmd_merchant_get_template (const char *label,
    217                                          const char *merchant_url,
    218                                          const char *template_id,
    219                                          unsigned int http_status,
    220                                          const char *template_reference)
    221 {
    222   struct GetTemplateState *gis;
    223 
    224   gis = GNUNET_new (struct GetTemplateState);
    225   gis->merchant_url = merchant_url;
    226   gis->template_id = template_id;
    227   gis->http_status = http_status;
    228   gis->template_reference = template_reference;
    229   {
    230     struct TALER_TESTING_Command cmd = {
    231       .cls = gis,
    232       .label = label,
    233       .run = &get_template_run,
    234       .cleanup = &get_template_cleanup
    235     };
    236 
    237     return cmd;
    238   }
    239 }
    240 
    241 
    242 /* end of testing_api_cmd_get_template.c */