merchant

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

testing_api_cmd_get_template.c (7115B)


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