merchant

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

testing_api_cmd_post_fountains.c (6705B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 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 /**
     21  * @file src/testing/testing_api_cmd_post_fountains.c
     22  * @brief command to run POST /private/fountains (DD 98)
     23  * @author Bohdan Potuzhnyi
     24  */
     25 #include "platform.h"
     26 struct PostFountainsState;
     27 #define TALER_MERCHANT_POST_PRIVATE_FOUNTAINS_RESULT_CLOSURE \
     28   struct PostFountainsState
     29 #include <gnunet/gnunet_time_lib.h>
     30 #include <taler/taler_exchange_service.h>
     31 #include <taler/taler_testing_lib.h>
     32 #include "taler/taler_merchant_service.h"
     33 #include "taler/taler_merchant_testing_lib.h"
     34 #include <taler/merchant/post-private-fountains.h>
     35 
     36 
     37 /**
     38  * State of a "POST /private/fountains" CMD.
     39  */
     40 struct PostFountainsState
     41 {
     42 
     43   /**
     44    * Expected status code.
     45    */
     46   unsigned int http_status;
     47 
     48   /**
     49    * Handle for a "POST /private/fountains" request.
     50    */
     51   struct TALER_MERCHANT_PostPrivateFountainsHandle *handle;
     52 
     53   /**
     54    * The interpreter state.
     55    */
     56   struct TALER_TESTING_Interpreter *is;
     57 
     58   /**
     59    * Base URL of the merchant serving the request.
     60    */
     61   const char *merchant_url;
     62 
     63   /**
     64    * Description of the fountain.
     65    */
     66   const char *description;
     67 
     68   /**
     69    * Poll frequency of the fountain.
     70    */
     71   struct GNUNET_TIME_Relative poll_freq;
     72 
     73   /**
     74    * Grants of the fountain.
     75    */
     76   const struct TALER_MERCHANT_FountainGrant *grants;
     77 
     78   /**
     79    * Length of the @e grants array.
     80    */
     81   unsigned int num_grants;
     82 
     83   /**
     84    * Identifier of the created fountain.
     85    */
     86   char *fountain_id;
     87 
     88   /**
     89    * Bearer credential of the created fountain.
     90    */
     91   char *fountain_secret;
     92 };
     93 
     94 
     95 /**
     96  * Callback for a POST /private/fountains operation.
     97  *
     98  * @param cls closure for this function
     99  * @param pfr response being processed
    100  */
    101 static void
    102 post_fountains_cb (struct PostFountainsState *state,
    103                    const struct
    104                    TALER_MERCHANT_PostPrivateFountainsResponse *pfr)
    105 {
    106 
    107   state->handle = NULL;
    108   if (state->http_status != pfr->hr.http_status)
    109   {
    110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    111                 "Unexpected response code %u (%d) to command %s\n",
    112                 pfr->hr.http_status,
    113                 (int) pfr->hr.ec,
    114                 TALER_TESTING_interpreter_get_current_label (state->is));
    115     TALER_TESTING_interpreter_fail (state->is);
    116     return;
    117   }
    118   switch (pfr->hr.http_status)
    119   {
    120   case MHD_HTTP_OK:
    121     state->fountain_id = GNUNET_strdup (pfr->details.ok.fountain_id);
    122     state->fountain_secret = GNUNET_strdup (pfr->details.ok.fountain_secret);
    123     break;
    124   case MHD_HTTP_BAD_REQUEST:
    125   case MHD_HTTP_UNAUTHORIZED:
    126   case MHD_HTTP_FORBIDDEN:
    127   case MHD_HTTP_NOT_FOUND:
    128     break;
    129   default:
    130     GNUNET_break (0);
    131     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    132                 "Unhandled HTTP status %u for POST /private/fountains.\n",
    133                 pfr->hr.http_status);
    134   }
    135   TALER_TESTING_interpreter_next (state->is);
    136 }
    137 
    138 
    139 /**
    140  * Run the "POST /private/fountains" CMD.
    141  *
    142  * @param cls closure.
    143  * @param cmd command being run now.
    144  * @param is interpreter state.
    145  */
    146 static void
    147 post_fountains_run (void *cls,
    148                     const struct TALER_TESTING_Command *cmd,
    149                     struct TALER_TESTING_Interpreter *is)
    150 {
    151   struct PostFountainsState *state = cls;
    152 
    153   state->is = is;
    154   state->handle = TALER_MERCHANT_post_private_fountains_create (
    155     TALER_TESTING_interpreter_get_context (is),
    156     state->merchant_url,
    157     state->description,
    158     state->poll_freq,
    159     state->num_grants,
    160     state->grants);
    161   {
    162     enum TALER_ErrorCode ec;
    163 
    164     ec = TALER_MERCHANT_post_private_fountains_start (
    165       state->handle,
    166       &post_fountains_cb,
    167       state);
    168     GNUNET_assert (TALER_EC_NONE == ec);
    169   }
    170 }
    171 
    172 
    173 /**
    174  * Offers information from the "POST /private/fountains" CMD state to
    175  * other commands.
    176  *
    177  * @param cls closure
    178  * @param[out] ret result (could be anything)
    179  * @param trait name of the trait
    180  * @param index index number of the object to extract.
    181  * @return #GNUNET_OK on success
    182  */
    183 static enum GNUNET_GenericReturnValue
    184 post_fountains_traits (void *cls,
    185                        const void **ret,
    186                        const char *trait,
    187                        unsigned int index)
    188 {
    189   struct PostFountainsState *state = cls;
    190   struct TALER_TESTING_Trait traits[] = {
    191     TALER_TESTING_make_trait_fountain_id (state->fountain_id),
    192     TALER_TESTING_make_trait_fountain_secret (state->fountain_secret),
    193     TALER_TESTING_trait_end ()
    194   };
    195 
    196   return TALER_TESTING_get_trait (traits,
    197                                   ret,
    198                                   trait,
    199                                   index);
    200 }
    201 
    202 
    203 /**
    204  * Free the state of a "POST /private/fountains" CMD, and possibly
    205  * cancel a pending operation thereof.
    206  *
    207  * @param cls closure.
    208  * @param cmd command being run.
    209  */
    210 static void
    211 post_fountains_cleanup (void *cls,
    212                         const struct TALER_TESTING_Command *cmd)
    213 {
    214   struct PostFountainsState *state = cls;
    215 
    216   if (NULL != state->handle)
    217   {
    218     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    219                 "POST /private/fountains operation did not complete\n");
    220     TALER_MERCHANT_post_private_fountains_cancel (state->handle);
    221   }
    222   GNUNET_free (state->fountain_id);
    223   GNUNET_free (state->fountain_secret);
    224   GNUNET_free (state);
    225 }
    226 
    227 
    228 struct TALER_TESTING_Command
    229 TALER_TESTING_cmd_merchant_post_fountains (
    230   const char *label,
    231   const char *merchant_url,
    232   unsigned int http_status,
    233   const char *description,
    234   struct GNUNET_TIME_Relative poll_freq,
    235   unsigned int num_grants,
    236   const struct TALER_MERCHANT_FountainGrant *grants)
    237 {
    238   struct PostFountainsState *state;
    239 
    240   state = GNUNET_new (struct PostFountainsState);
    241   state->merchant_url = merchant_url;
    242   state->http_status = http_status;
    243   state->description = description;
    244   state->poll_freq = poll_freq;
    245   state->num_grants = num_grants;
    246   state->grants = grants;
    247   {
    248     struct TALER_TESTING_Command cmd = {
    249       .cls = state,
    250       .label = label,
    251       .run = &post_fountains_run,
    252       .cleanup = &post_fountains_cleanup,
    253       .traits = &post_fountains_traits
    254     };
    255 
    256     return cmd;
    257   }
    258 }
    259 
    260 
    261 /* end of testing_api_cmd_post_fountains.c */