merchant

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

testing_api_cmd_post_templates.c (7399B)


      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_post_templates.c
     21  * @brief command to test POST /templates
     22  * @author Priscilla HUANG
     23  */
     24 #include "platform.h"
     25 struct PostTemplatesState;
     26 #define TALER_MERCHANT_POST_PRIVATE_TEMPLATES_RESULT_CLOSURE struct PostTemplatesState
     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/post-private-templates.h>
     32 
     33 
     34 /**
     35  * State of a "POST /templates" CMD.
     36  */
     37 struct PostTemplatesState
     38 {
     39 
     40   /**
     41    * Handle for a "GET template" request.
     42    */
     43   struct TALER_MERCHANT_PostPrivateTemplatesHandle *iph;
     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 POST for.
     57    */
     58   const char *template_id;
     59 
     60   /**
     61    * description of the template
     62    */
     63   const char *template_description;
     64 
     65   /**
     66    * OTP device ID.
     67    */
     68   char *otp_id;
     69 
     70   /**
     71    * Contract of the company
     72    */
     73   json_t *template_contract;
     74 
     75   /**
     76    * Expected HTTP response code.
     77    */
     78   unsigned int http_status;
     79 
     80 };
     81 
     82 
     83 /**
     84  * Callback for a POST /templates operation.
     85  *
     86  * @param cls closure for this function
     87  * @param ptr response being processed
     88  */
     89 static void
     90 post_templates_cb (struct PostTemplatesState *tis,
     91                    const struct TALER_MERCHANT_PostPrivateTemplatesResponse *ptr
     92                    )
     93 {
     94 
     95   tis->iph = NULL;
     96   if (tis->http_status != ptr->hr.http_status)
     97   {
     98     TALER_TESTING_unexpected_status_with_body (tis->is,
     99                                                ptr->hr.http_status,
    100                                                tis->http_status,
    101                                                ptr->hr.reply);
    102     return;
    103   }
    104   switch (ptr->hr.http_status)
    105   {
    106   case MHD_HTTP_NO_CONTENT:
    107     break;
    108   case MHD_HTTP_UNAUTHORIZED:
    109     break;
    110   case MHD_HTTP_FORBIDDEN:
    111     break;
    112   case MHD_HTTP_NOT_FOUND:
    113     break;
    114   case MHD_HTTP_CONFLICT:
    115     break;
    116   default:
    117     GNUNET_break (0);
    118     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    119                 "Unhandled HTTP status %u for POST /templates.\n",
    120                 ptr->hr.http_status);
    121   }
    122   TALER_TESTING_interpreter_next (tis->is);
    123 }
    124 
    125 
    126 /**
    127  * Run the "POST /templates" CMD.
    128  *
    129  *
    130  * @param cls closure.
    131  * @param cmd command being run now.
    132  * @param is interpreter state.
    133  */
    134 static void
    135 post_templates_run (void *cls,
    136                     const struct TALER_TESTING_Command *cmd,
    137                     struct TALER_TESTING_Interpreter *is)
    138 {
    139   struct PostTemplatesState *tis = cls;
    140 
    141   tis->is = is;
    142   tis->iph = TALER_MERCHANT_post_private_templates_create (
    143     TALER_TESTING_interpreter_get_context (is),
    144     tis->merchant_url,
    145     tis->template_id,
    146     tis->template_description,
    147     tis->template_contract);
    148   if (NULL != tis->otp_id)
    149     TALER_MERCHANT_post_private_templates_set_options (
    150       tis->iph,
    151       TALER_MERCHANT_post_private_templates_option_otp_id (
    152         tis->otp_id));
    153   {
    154     enum TALER_ErrorCode ec;
    155 
    156     ec = TALER_MERCHANT_post_private_templates_start (
    157       tis->iph,
    158       &post_templates_cb,
    159       tis);
    160     if (TALER_EC_NONE != ec)
    161     {
    162       GNUNET_break (0);
    163       TALER_TESTING_interpreter_fail (tis->is);
    164       return;
    165     }
    166   }
    167 }
    168 
    169 
    170 /**
    171  * Offers information from the POST /templates CMD state to other
    172  * commands.
    173  *
    174  * @param cls closure
    175  * @param[out] ret result (could be anything)
    176  * @param trait name of the trait
    177  * @param index index number of the object to extract.
    178  * @return #GNUNET_OK on success
    179  */
    180 static enum GNUNET_GenericReturnValue
    181 post_templates_traits (void *cls,
    182                        const void **ret,
    183                        const char *trait,
    184                        unsigned int index)
    185 {
    186   struct PostTemplatesState *pts = cls;
    187   struct TALER_TESTING_Trait traits[] = {
    188     TALER_TESTING_make_trait_template_description (pts->template_description),
    189     TALER_TESTING_make_trait_otp_id (pts->otp_id),
    190     TALER_TESTING_make_trait_template_contract (pts->template_contract),
    191     TALER_TESTING_make_trait_template_id (pts->template_id),
    192     TALER_TESTING_trait_end (),
    193   };
    194 
    195   return TALER_TESTING_get_trait (traits,
    196                                   ret,
    197                                   trait,
    198                                   index);
    199 }
    200 
    201 
    202 /**
    203  * Free the state of a "POST template" CMD, and possibly
    204  * cancel a pending operation thereof.
    205  *
    206  * @param cls closure.
    207  * @param cmd command being run.
    208  */
    209 static void
    210 post_templates_cleanup (void *cls,
    211                         const struct TALER_TESTING_Command *cmd)
    212 {
    213   struct PostTemplatesState *tis = cls;
    214 
    215   if (NULL != tis->iph)
    216   {
    217     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    218                 "POST /templates operation did not complete\n");
    219     TALER_MERCHANT_post_private_templates_cancel (tis->iph);
    220   }
    221   GNUNET_free (tis->otp_id);
    222   json_decref (tis->template_contract);
    223   GNUNET_free (tis);
    224 }
    225 
    226 
    227 struct TALER_TESTING_Command
    228 TALER_TESTING_cmd_merchant_post_templates2 (
    229   const char *label,
    230   const char *merchant_url,
    231   const char *template_id,
    232   const char *template_description,
    233   const char *otp_id,
    234   json_t *template_contract,
    235   unsigned int http_status)
    236 {
    237   struct PostTemplatesState *tis;
    238 
    239   GNUNET_assert ((NULL == template_contract) ||
    240                  json_is_object (template_contract));
    241 
    242   tis = GNUNET_new (struct PostTemplatesState);
    243   tis->merchant_url = merchant_url;
    244   tis->template_id = template_id;
    245   tis->http_status = http_status;
    246   tis->template_description = template_description;
    247   tis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id);
    248   tis->template_contract = template_contract;
    249   {
    250     struct TALER_TESTING_Command cmd = {
    251       .cls = tis,
    252       .label = label,
    253       .run = &post_templates_run,
    254       .cleanup = &post_templates_cleanup,
    255       .traits = &post_templates_traits
    256     };
    257 
    258     return cmd;
    259   }
    260 }
    261 
    262 
    263 struct TALER_TESTING_Command
    264 TALER_TESTING_cmd_merchant_post_templates (const char *label,
    265                                            const char *merchant_url,
    266                                            const char *template_id,
    267                                            const char *template_description,
    268                                            unsigned int http_status)
    269 {
    270   return TALER_TESTING_cmd_merchant_post_templates2 (
    271     label,
    272     merchant_url,
    273     template_id,
    274     template_description,
    275     NULL,
    276     GNUNET_JSON_PACK (
    277       GNUNET_JSON_pack_uint64 ("minimum_age", 0),
    278       GNUNET_JSON_pack_time_rel ("pay_duration",
    279                                  GNUNET_TIME_UNIT_MINUTES)),
    280     http_status);
    281 }
    282 
    283 
    284 /* end of testing_api_cmd_post_templates.c */