LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_unit.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.9 % 66 60
Test Date: 2025-12-10 19:49:57 Functions: 100.0 % 5 5

            Line data    Source code
       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_patch_unit.c
      21              :  * @brief command to test PATCH /private/units/$ID
      22              :  * @author Bohdan Potuzhnyi
      23              :  */
      24              : #include "platform.h"
      25              : #include <taler/taler_testing_lib.h>
      26              : #include "taler_merchant_service.h"
      27              : #include "taler_merchant_testing_lib.h"
      28              : 
      29              : 
      30              : /**
      31              :  * State for a PATCH /private/units command.
      32              :  */
      33              : struct PatchUnitState
      34              : {
      35              :   /**
      36              :    * In-flight request handle.
      37              :    */
      38              :   struct TALER_MERCHANT_UnitPatchHandle *uph;
      39              : 
      40              :   /**
      41              :    * Interpreter context.
      42              :    */
      43              :   struct TALER_TESTING_Interpreter *is;
      44              : 
      45              :   /**
      46              :    * Merchant backend base URL.
      47              :    */
      48              :   const char *merchant_url;
      49              : 
      50              :   /**
      51              :    * Unit identifier.
      52              :    */
      53              :   const char *unit_id;
      54              : 
      55              :   /**
      56              :    * Optional new long label.
      57              :    */
      58              :   const char *unit_name_long;
      59              : 
      60              :   /**
      61              :    * Optional new short label.
      62              :    */
      63              :   const char *unit_name_short;
      64              : 
      65              :   /**
      66              :    * Optional long label translations.
      67              :    */
      68              :   json_t *unit_name_long_i18n;
      69              : 
      70              :   /**
      71              :    * Optional short label translations.
      72              :    */
      73              :   json_t *unit_name_short_i18n;
      74              : 
      75              :   /**
      76              :    * Whether a new fractional flag was provided.
      77              :    */
      78              :   bool have_unit_allow_fraction;
      79              : 
      80              :   /**
      81              :    * New fractional flag value.
      82              :    */
      83              :   bool unit_allow_fraction;
      84              : 
      85              :   /**
      86              :    * Whether a new precision level was provided.
      87              :    */
      88              :   bool have_unit_precision_level;
      89              : 
      90              :   /**
      91              :    * New precision level.
      92              :    */
      93              :   uint32_t unit_precision_level;
      94              : 
      95              :   /**
      96              :    * Whether a new active flag was provided.
      97              :    */
      98              :   bool have_unit_active;
      99              : 
     100              :   /**
     101              :    * New active flag value.
     102              :    */
     103              :   bool unit_active;
     104              : 
     105              :   /**
     106              :    * Expected HTTP status.
     107              :    */
     108              :   unsigned int http_status;
     109              : };
     110              : 
     111              : 
     112              : /**
     113              :  * Completion callback for PATCH /private/units.
     114              :  */
     115              : static void
     116            4 : patch_unit_cb (void *cls,
     117              :                const struct TALER_MERCHANT_HttpResponse *hr)
     118              : {
     119            4 :   struct PatchUnitState *pus = cls;
     120              : 
     121            4 :   pus->uph = NULL;
     122            4 :   if (pus->http_status != hr->http_status)
     123              :   {
     124            0 :     TALER_TESTING_unexpected_status_with_body (pus->is,
     125              :                                                hr->http_status,
     126              :                                                pus->http_status,
     127              :                                                hr->reply);
     128            0 :     return;
     129              :   }
     130            4 :   TALER_TESTING_interpreter_next (pus->is);
     131              : }
     132              : 
     133              : 
     134              : /**
     135              :  * Issue the PATCH request.
     136              :  */
     137              : static void
     138            4 : patch_unit_run (void *cls,
     139              :                 const struct TALER_TESTING_Command *cmd,
     140              :                 struct TALER_TESTING_Interpreter *is)
     141              : {
     142            4 :   struct PatchUnitState *pus = cls;
     143            8 :   const bool *allow_ptr = pus->have_unit_allow_fraction
     144              :                           ? &pus->unit_allow_fraction
     145            4 :                           : NULL;
     146            8 :   const uint32_t *precision_ptr = pus->have_unit_precision_level
     147              :                                   ? &pus->unit_precision_level
     148            4 :                                   : NULL;
     149            8 :   const bool *active_ptr = pus->have_unit_active
     150              :                            ? &pus->unit_active
     151            4 :                            : NULL;
     152              : 
     153            4 :   pus->is = is;
     154            4 :   pus->uph = TALER_MERCHANT_unit_patch (
     155              :     TALER_TESTING_interpreter_get_context (is),
     156              :     pus->merchant_url,
     157              :     pus->unit_id,
     158              :     pus->unit_name_long,
     159              :     pus->unit_name_short,
     160            4 :     pus->unit_name_long_i18n,
     161            4 :     pus->unit_name_short_i18n,
     162              :     allow_ptr,
     163              :     precision_ptr,
     164              :     active_ptr,
     165              :     &patch_unit_cb,
     166              :     pus);
     167            4 :   if (NULL == pus->uph)
     168              :   {
     169            0 :     GNUNET_break (0);
     170            0 :     TALER_TESTING_interpreter_fail (is);
     171              :   }
     172            4 : }
     173              : 
     174              : 
     175              : /**
     176              :  * Provide traits to other commands.
     177              :  */
     178              : static enum GNUNET_GenericReturnValue
     179           32 : patch_unit_traits (void *cls,
     180              :                    const void **ret,
     181              :                    const char *trait,
     182              :                    unsigned int index)
     183              : {
     184           32 :   struct PatchUnitState *pus = cls;
     185              :   struct TALER_TESTING_Trait traits[] = {
     186           32 :     TALER_TESTING_make_trait_unit_id (pus->unit_id),
     187           32 :     TALER_TESTING_make_trait_unit_name_long (pus->unit_name_long),
     188           32 :     TALER_TESTING_make_trait_unit_name_short (pus->unit_name_short),
     189           32 :     TALER_TESTING_make_trait_unit_name_long_i18n (pus->unit_name_long_i18n),
     190           32 :     TALER_TESTING_make_trait_unit_name_short_i18n (pus->unit_name_short_i18n),
     191           32 :     TALER_TESTING_make_trait_unit_allow_fraction (
     192           32 :       pus->have_unit_allow_fraction
     193              :       ? &pus->unit_allow_fraction
     194              :       : NULL),
     195           32 :     TALER_TESTING_make_trait_unit_precision_level (
     196           32 :       pus->have_unit_precision_level
     197              :       ? &pus->unit_precision_level
     198              :       : NULL),
     199           32 :     TALER_TESTING_make_trait_unit_active (
     200           32 :       pus->have_unit_active
     201              :       ? &pus->unit_active
     202              :       : NULL),
     203           32 :     TALER_TESTING_trait_end ()
     204              :   };
     205              : 
     206           32 :   return TALER_TESTING_get_trait (traits,
     207              :                                   ret,
     208              :                                   trait,
     209              :                                   index);
     210              : }
     211              : 
     212              : 
     213              : /**
     214              :  * Cleanup.
     215              :  */
     216              : static void
     217            4 : patch_unit_cleanup (void *cls,
     218              :                     const struct TALER_TESTING_Command *cmd)
     219              : {
     220            4 :   struct PatchUnitState *pus = cls;
     221              : 
     222            4 :   if (NULL != pus->uph)
     223              :   {
     224            0 :     TALER_MERCHANT_unit_patch_cancel (pus->uph);
     225            0 :     pus->uph = NULL;
     226              :   }
     227            4 :   if (NULL != pus->unit_name_long_i18n)
     228            2 :     json_decref (pus->unit_name_long_i18n);
     229            4 :   if (NULL != pus->unit_name_short_i18n)
     230            2 :     json_decref (pus->unit_name_short_i18n);
     231            4 :   GNUNET_free (pus);
     232            4 : }
     233              : 
     234              : 
     235              : struct TALER_TESTING_Command
     236            4 : TALER_TESTING_cmd_merchant_patch_unit (const char *label,
     237              :                                        const char *merchant_url,
     238              :                                        const char *unit_id,
     239              :                                        const char *unit_name_long,
     240              :                                        const char *unit_name_short,
     241              :                                        json_t *unit_name_long_i18n,
     242              :                                        json_t *unit_name_short_i18n,
     243              :                                        const bool unit_allow_fraction,
     244              :                                        const uint32_t unit_precision_level,
     245              :                                        const bool unit_active,
     246              :                                        unsigned int http_status)
     247              : {
     248              :   struct PatchUnitState *pus;
     249              : 
     250            4 :   pus = GNUNET_new (struct PatchUnitState);
     251            4 :   pus->merchant_url = merchant_url;
     252            4 :   pus->unit_id = unit_id;
     253            4 :   pus->unit_name_long = unit_name_long;
     254            4 :   pus->unit_name_short = unit_name_short;
     255            4 :   pus->unit_name_long_i18n = unit_name_long_i18n;
     256            4 :   pus->unit_name_short_i18n = unit_name_short_i18n;
     257            4 :   pus->unit_allow_fraction = unit_allow_fraction;
     258            4 :   pus->have_unit_allow_fraction = true;
     259            4 :   pus->unit_precision_level = unit_precision_level;
     260            4 :   pus->have_unit_precision_level = true;
     261            4 :   pus->unit_active = unit_active;
     262            4 :   pus->have_unit_active = true;
     263            4 :   pus->http_status = http_status;
     264              :   {
     265            4 :     struct TALER_TESTING_Command cmd = {
     266              :       .cls = pus,
     267              :       .label = label,
     268              :       .run = &patch_unit_run,
     269              :       .cleanup = &patch_unit_cleanup,
     270              :       .traits = &patch_unit_traits
     271              :     };
     272              : 
     273            4 :     return cmd;
     274              :   }
     275              : }
     276              : 
     277              : 
     278              : /* end of testing_api_cmd_patch_unit.c */
        

Generated by: LCOV version 2.0-1