merchant

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

testing_api_cmd_get_template.c (7019B)


      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         TALER_TESTING_interpreter_fail (gis->is);
    109       if (0 != strcmp (tgr->details.ok.template_description,
    110                        expected_description))
    111       {
    112         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    113                     "Template description does not match\n");
    114         TALER_TESTING_interpreter_fail (gis->is);
    115         return;
    116       }
    117     }
    118     {
    119       const char *expected_otp_id;
    120 
    121       if (GNUNET_OK !=
    122           TALER_TESTING_get_trait_otp_id (template_cmd,
    123                                           &expected_otp_id))
    124         TALER_TESTING_interpreter_fail (gis->is);
    125       if ( ( (NULL == tgr->details.ok.otp_id) && (NULL != expected_otp_id)) ||
    126            ( (NULL != tgr->details.ok.otp_id) && (NULL == expected_otp_id)) ||
    127            ( (NULL != tgr->details.ok.otp_id) &&
    128              (0 != strcmp (tgr->details.ok.otp_id,
    129                            expected_otp_id)) ) )
    130       {
    131         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    132                     "Template pos_key `%s' does not match `%s'\n",
    133                     tgr->details.ok.otp_id,
    134                     expected_otp_id);
    135         TALER_TESTING_interpreter_fail (gis->is);
    136         return;
    137       }
    138     }
    139     {
    140       const json_t *expected_template_contract;
    141 
    142       if (GNUNET_OK !=
    143           TALER_TESTING_get_trait_template_contract (template_cmd,
    144                                                      &expected_template_contract
    145                                                      ))
    146         TALER_TESTING_interpreter_fail (gis->is);
    147       if (1 != json_equal (tgr->details.ok.template_contract,
    148                            expected_template_contract))
    149       {
    150         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    151                     "Template contract does not match\n");
    152         TALER_TESTING_interpreter_fail (gis->is);
    153         return;
    154       }
    155     }
    156     break;
    157   case MHD_HTTP_UNAUTHORIZED:
    158     break;
    159   case MHD_HTTP_NOT_FOUND:
    160     break;
    161   default:
    162     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    163                 "Unhandled HTTP status.\n");
    164   }
    165   TALER_TESTING_interpreter_next (gis->is);
    166 }
    167 
    168 
    169 /**
    170  * Run the "GET template" CMD.
    171  *
    172  *
    173  * @param cls closure.
    174  * @param cmd command being run now.
    175  * @param is interpreter state.
    176  */
    177 static void
    178 get_template_run (void *cls,
    179                   const struct TALER_TESTING_Command *cmd,
    180                   struct TALER_TESTING_Interpreter *is)
    181 {
    182   struct GetTemplateState *gis = cls;
    183 
    184   gis->is = is;
    185   gis->igh = TALER_MERCHANT_get_private_template_create (
    186     TALER_TESTING_interpreter_get_context (is),
    187     gis->merchant_url,
    188     gis->template_id);
    189   {
    190     enum TALER_ErrorCode ec;
    191 
    192     ec = TALER_MERCHANT_get_private_template_start (
    193       gis->igh,
    194       &get_template_cb,
    195       gis);
    196     GNUNET_assert (TALER_EC_NONE == ec);
    197   }
    198 }
    199 
    200 
    201 /**
    202  * Free the state of a "GET template" CMD, and possibly
    203  * cancel a pending operation thereof.
    204  *
    205  * @param cls closure.
    206  * @param cmd command being run.
    207  */
    208 static void
    209 get_template_cleanup (void *cls,
    210                       const struct TALER_TESTING_Command *cmd)
    211 {
    212   struct GetTemplateState *gis = cls;
    213 
    214   if (NULL != gis->igh)
    215   {
    216     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    217                 "GET /templates/$ID operation did not complete\n");
    218     TALER_MERCHANT_get_private_template_cancel (gis->igh);
    219   }
    220   GNUNET_free (gis);
    221 }
    222 
    223 
    224 struct TALER_TESTING_Command
    225 TALER_TESTING_cmd_merchant_get_template (const char *label,
    226                                          const char *merchant_url,
    227                                          const char *template_id,
    228                                          unsigned int http_status,
    229                                          const char *template_reference)
    230 {
    231   struct GetTemplateState *gis;
    232 
    233   gis = GNUNET_new (struct GetTemplateState);
    234   gis->merchant_url = merchant_url;
    235   gis->template_id = template_id;
    236   gis->http_status = http_status;
    237   gis->template_reference = template_reference;
    238   {
    239     struct TALER_TESTING_Command cmd = {
    240       .cls = gis,
    241       .label = label,
    242       .run = &get_template_run,
    243       .cleanup = &get_template_cleanup
    244     };
    245 
    246     return cmd;
    247   }
    248 }
    249 
    250 
    251 /* end of testing_api_cmd_get_template.c */