merchant

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

testing_api_cmd_get_template.c (6930B)


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