merchant

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

testing_api_cmd_post_units.c (7160B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 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_post_units.c
     21  * @brief command to test POST /private/units
     22  * @author Bohdan Potuzhnyi
     23  */
     24 #include "taler/platform.h"
     25 #include <taler/taler_testing_lib.h>
     26 #include "taler/taler_merchant_service.h"
     27 #include "taler/taler_merchant_testing_lib.h"
     28 #include <taler/taler-merchant/post-private-units.h>
     29 
     30 
     31 /**
     32  * State for a POST /private/units command.
     33  */
     34 struct PostUnitState
     35 {
     36   /**
     37    * In-flight request handle.
     38    */
     39   struct TALER_MERCHANT_PostPrivateUnitsHandle *uph;
     40 
     41   /**
     42    * Interpreter context.
     43    */
     44   struct TALER_TESTING_Interpreter *is;
     45 
     46   /**
     47    * Merchant backend base URL.
     48    */
     49   const char *merchant_url;
     50 
     51   /**
     52    * Unit identifier.
     53    */
     54   const char *unit_id;
     55 
     56   /**
     57    * Long label.
     58    */
     59   const char *unit_name_long;
     60 
     61   /**
     62    * Short label.
     63    */
     64   const char *unit_name_short;
     65 
     66   /**
     67    * Optional translations (reference counted).
     68    */
     69   json_t *unit_name_long_i18n;
     70 
     71   /**
     72    * Optional translations (reference counted).
     73    */
     74   json_t *unit_name_short_i18n;
     75 
     76   /**
     77    * Whether fractional quantities are allowed.
     78    */
     79   bool unit_allow_fraction;
     80 
     81   /**
     82    * Precision level for fractional quantities.
     83    */
     84   uint32_t unit_precision_level;
     85 
     86   /**
     87    * Whether the unit should be active.
     88    */
     89   bool unit_active;
     90 
     91   /**
     92    * Expected HTTP status.
     93    */
     94   unsigned int http_status;
     95 };
     96 
     97 
     98 /**
     99  * Completion callback for POST /private/units.
    100  */
    101 static void
    102 post_unit_cb (void *cls,
    103               const struct TALER_MERCHANT_PostPrivateUnitsResponse *pur)
    104 {
    105   struct PostUnitState *pus = cls;
    106 
    107   pus->uph = NULL;
    108   if (pus->http_status != pur->hr.http_status)
    109   {
    110     TALER_TESTING_unexpected_status_with_body (pus->is,
    111                                                pur->hr.http_status,
    112                                                pus->http_status,
    113                                                pur->hr.reply);
    114     return;
    115   }
    116   TALER_TESTING_interpreter_next (pus->is);
    117 }
    118 
    119 
    120 /**
    121  * Issue the POST /private/units request.
    122  */
    123 static void
    124 post_unit_run (void *cls,
    125                const struct TALER_TESTING_Command *cmd,
    126                struct TALER_TESTING_Interpreter *is)
    127 {
    128   struct PostUnitState *pus = cls;
    129 
    130   pus->is = is;
    131   pus->uph = TALER_MERCHANT_post_private_units_create (
    132     TALER_TESTING_interpreter_get_context (is),
    133     pus->merchant_url,
    134     pus->unit_id,
    135     pus->unit_name_long,
    136     pus->unit_name_short);
    137   TALER_MERCHANT_post_private_units_set_options (
    138     pus->uph,
    139     TALER_MERCHANT_post_private_units_option_unit_allow_fraction (
    140       pus->unit_allow_fraction),
    141     TALER_MERCHANT_post_private_units_option_unit_precision_level (
    142       pus->unit_precision_level),
    143     TALER_MERCHANT_post_private_units_option_unit_active (
    144       pus->unit_active));
    145   if (NULL != pus->unit_name_long_i18n)
    146     TALER_MERCHANT_post_private_units_set_options (
    147       pus->uph,
    148       TALER_MERCHANT_post_private_units_option_unit_name_long_i18n (
    149         pus->unit_name_long_i18n));
    150   if (NULL != pus->unit_name_short_i18n)
    151     TALER_MERCHANT_post_private_units_set_options (
    152       pus->uph,
    153       TALER_MERCHANT_post_private_units_option_unit_name_short_i18n (
    154         pus->unit_name_short_i18n));
    155   {
    156     enum TALER_ErrorCode ec;
    157 
    158     ec = TALER_MERCHANT_post_private_units_start (
    159       pus->uph,
    160       &post_unit_cb,
    161       pus);
    162     if (TALER_EC_NONE != ec)
    163     {
    164       GNUNET_break (0);
    165       TALER_TESTING_interpreter_fail (is);
    166     }
    167   }
    168 }
    169 
    170 
    171 /**
    172  * Provide traits for other commands.
    173  */
    174 static enum GNUNET_GenericReturnValue
    175 post_unit_traits (void *cls,
    176                   const void **ret,
    177                   const char *trait,
    178                   unsigned int index)
    179 {
    180   struct PostUnitState *pus = cls;
    181   struct TALER_TESTING_Trait traits[] = {
    182     TALER_TESTING_make_trait_unit_id (pus->unit_id),
    183     TALER_TESTING_make_trait_unit_name_long (pus->unit_name_long),
    184     TALER_TESTING_make_trait_unit_name_short (pus->unit_name_short),
    185     TALER_TESTING_make_trait_unit_allow_fraction (&pus->unit_allow_fraction),
    186     TALER_TESTING_make_trait_unit_precision_level (&pus->unit_precision_level),
    187     TALER_TESTING_make_trait_unit_active (&pus->unit_active),
    188     TALER_TESTING_make_trait_unit_name_long_i18n (pus->unit_name_long_i18n),
    189     TALER_TESTING_make_trait_unit_name_short_i18n (pus->unit_name_short_i18n),
    190     TALER_TESTING_trait_end ()
    191   };
    192 
    193   return TALER_TESTING_get_trait (traits,
    194                                   ret,
    195                                   trait,
    196                                   index);
    197 }
    198 
    199 
    200 /**
    201  * Cleanup / cancel pending request.
    202  */
    203 static void
    204 post_unit_cleanup (void *cls,
    205                    const struct TALER_TESTING_Command *cmd)
    206 {
    207   struct PostUnitState *pus = cls;
    208 
    209   if (NULL != pus->uph)
    210   {
    211     TALER_MERCHANT_post_private_units_cancel (pus->uph);
    212     pus->uph = NULL;
    213   }
    214   if (NULL != pus->unit_name_long_i18n)
    215     json_decref (pus->unit_name_long_i18n);
    216   if (NULL != pus->unit_name_short_i18n)
    217     json_decref (pus->unit_name_short_i18n);
    218   GNUNET_free (pus);
    219 }
    220 
    221 
    222 struct TALER_TESTING_Command
    223 TALER_TESTING_cmd_merchant_post_units (const char *label,
    224                                        const char *merchant_url,
    225                                        const char *unit_id,
    226                                        const char *unit_name_long,
    227                                        const char *unit_name_short,
    228                                        bool unit_allow_fraction,
    229                                        uint32_t unit_precision_level,
    230                                        bool unit_active,
    231                                        json_t *unit_name_long_i18n,
    232                                        json_t *unit_name_short_i18n,
    233                                        unsigned int http_status)
    234 {
    235   struct PostUnitState *pus;
    236 
    237   pus = GNUNET_new (struct PostUnitState);
    238   pus->merchant_url = merchant_url;
    239   pus->unit_id = unit_id;
    240   pus->unit_name_long = unit_name_long;
    241   pus->unit_name_short = unit_name_short;
    242   pus->unit_allow_fraction = unit_allow_fraction;
    243   pus->unit_precision_level = unit_precision_level;
    244   pus->unit_active = unit_active;
    245   pus->unit_name_long_i18n = unit_name_long_i18n;
    246   pus->unit_name_short_i18n = unit_name_short_i18n;
    247   pus->http_status = http_status;
    248   {
    249     struct TALER_TESTING_Command cmd = {
    250       .cls = pus,
    251       .label = label,
    252       .run = &post_unit_run,
    253       .cleanup = &post_unit_cleanup,
    254       .traits = &post_unit_traits
    255     };
    256 
    257     return cmd;
    258   }
    259 }
    260 
    261 
    262 /* end of testing_api_cmd_post_units.c */