LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 81 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 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_instance.c
      21             :  * @brief command to test PATCH /instance
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include <taler/taler_exchange_service.h>
      26             : #include <taler/taler_testing_lib.h>
      27             : #include "taler_merchant_service.h"
      28             : #include "taler_merchant_testing_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * State of a "PATCH /instance" CMD.
      33             :  */
      34             : struct PatchInstanceState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "PATCH /instance/$ID" request.
      39             :    */
      40             :   struct TALER_MERCHANT_InstancePatchHandle *iph;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * ID of the instance to run PATCH for.
      54             :    */
      55             :   const char *instance_id;
      56             : 
      57             :   /**
      58             :    * Length of the @payto_uris array
      59             :    */
      60             :   unsigned int payto_uris_length;
      61             : 
      62             :   /**
      63             :    * Array of payto URIs.
      64             :    */
      65             :   const char **payto_uris;
      66             : 
      67             :   /**
      68             :    * Name of the instance.
      69             :    */
      70             :   const char *name;
      71             : 
      72             :   /**
      73             :    * Address to use.
      74             :    */
      75             :   json_t *address;
      76             : 
      77             :   /**
      78             :    * Jurisdiction to use.
      79             :    */
      80             :   json_t *jurisdiction;
      81             : 
      82             :   /**
      83             :    * Wire fee to use.
      84             :    */
      85             :   struct TALER_Amount default_max_wire_fee;
      86             : 
      87             :   /**
      88             :    * Amortization to use.
      89             :    */
      90             :   uint32_t default_wire_fee_amortization;
      91             : 
      92             :   /**
      93             :    * Deposit fee ceiling to use.
      94             :    */
      95             :   struct TALER_Amount default_max_deposit_fee;
      96             : 
      97             :   /**
      98             :    * Wire transfer delay to use.
      99             :    */
     100             :   struct GNUNET_TIME_Relative default_wire_transfer_delay;
     101             : 
     102             :   /**
     103             :    * Order validity default duration to use.
     104             :    */
     105             :   struct GNUNET_TIME_Relative default_pay_delay;
     106             : 
     107             :   /**
     108             :    * Expected HTTP response code.
     109             :    */
     110             :   unsigned int http_status;
     111             : 
     112             : };
     113             : 
     114             : 
     115             : /**
     116             :  * Callback for a PATCH /instances/$ID operation.
     117             :  *
     118             :  * @param cls closure for this function
     119             :  * @param hr response being processed
     120             :  */
     121             : static void
     122           0 : patch_instance_cb (void *cls,
     123             :                    const struct TALER_MERCHANT_HttpResponse *hr)
     124             : {
     125           0 :   struct PatchInstanceState *pis = cls;
     126             : 
     127           0 :   pis->iph = NULL;
     128           0 :   if (pis->http_status != hr->http_status)
     129             :   {
     130           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     131             :                 "Unexpected response code %u (%d) to command %s\n",
     132             :                 hr->http_status,
     133             :                 (int) hr->ec,
     134             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     135           0 :     TALER_TESTING_interpreter_fail (pis->is);
     136           0 :     return;
     137             :   }
     138           0 :   switch (hr->http_status)
     139             :   {
     140           0 :   case MHD_HTTP_NO_CONTENT:
     141           0 :     break;
     142           0 :   case MHD_HTTP_BAD_REQUEST:
     143             :     /* happens also for currency mismatch */
     144           0 :     break;
     145           0 :   case MHD_HTTP_UNAUTHORIZED:
     146           0 :     break;
     147           0 :   case MHD_HTTP_NOT_FOUND:
     148           0 :     break;
     149           0 :   case MHD_HTTP_CONFLICT:
     150           0 :     break;
     151           0 :   default:
     152           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     153             :                 "Unhandled HTTP status %u for PATCH instance.\n",
     154             :                 hr->http_status);
     155             :   }
     156           0 :   TALER_TESTING_interpreter_next (pis->is);
     157             : }
     158             : 
     159             : 
     160             : /**
     161             :  * Run the "PATCH /instances/$ID" CMD.
     162             :  *
     163             :  *
     164             :  * @param cls closure.
     165             :  * @param cmd command being run now.
     166             :  * @param is interpreter state.
     167             :  */
     168             : static void
     169           0 : patch_instance_run (void *cls,
     170             :                     const struct TALER_TESTING_Command *cmd,
     171             :                     struct TALER_TESTING_Interpreter *is)
     172             : {
     173           0 :   struct PatchInstanceState *pis = cls;
     174             : 
     175           0 :   pis->is = is;
     176           0 :   pis->iph = TALER_MERCHANT_instance_patch (is->ctx,
     177             :                                             pis->merchant_url,
     178             :                                             pis->instance_id,
     179             :                                             pis->payto_uris_length,
     180             :                                             pis->payto_uris,
     181             :                                             pis->name,
     182           0 :                                             pis->address,
     183           0 :                                             pis->jurisdiction,
     184           0 :                                             &pis->default_max_wire_fee,
     185             :                                             pis->default_wire_fee_amortization,
     186           0 :                                             &pis->default_max_deposit_fee,
     187             :                                             pis->default_wire_transfer_delay,
     188             :                                             pis->default_pay_delay,
     189             :                                             &patch_instance_cb,
     190             :                                             pis);
     191           0 :   GNUNET_assert (NULL != pis->iph);
     192           0 : }
     193             : 
     194             : 
     195             : /**
     196             :  * Offers information from the PATCH /instances CMD state to other
     197             :  * commands.
     198             :  *
     199             :  * @param cls closure
     200             :  * @param[out] ret result (could be anything)
     201             :  * @param trait name of the trait
     202             :  * @param index index number of the object to extract.
     203             :  * @return #GNUNET_OK on success
     204             :  */
     205             : static int
     206           0 : patch_instance_traits (void *cls,
     207             :                        const void **ret,
     208             :                        const char *trait,
     209             :                        unsigned int index)
     210           0 : {
     211           0 :   struct PatchInstanceState *pis = cls;
     212             :   #define NUM_TRAITS (pis->payto_uris_length) + 11
     213           0 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
     214             :   traits[0] =
     215           0 :     TALER_TESTING_make_trait_instance_name (&pis->name);
     216             :   traits[1] =
     217           0 :     TALER_TESTING_make_trait_instance_id (&pis->instance_id);
     218             :   traits[2] =
     219           0 :     TALER_TESTING_make_trait_address (pis->address);
     220             :   traits[3] =
     221           0 :     TALER_TESTING_make_trait_jurisdiction (pis->jurisdiction);
     222             :   traits[4] =
     223           0 :     TALER_TESTING_make_trait_max_wire_fee (&pis->default_max_wire_fee);
     224             :   traits[5] =
     225           0 :     TALER_TESTING_make_trait_wire_fee_amortization (
     226           0 :       &pis->default_wire_fee_amortization);
     227             :   traits[6] =
     228           0 :     TALER_TESTING_make_trait_max_deposit_fee (&pis->default_max_deposit_fee);
     229             :   traits[7] =
     230           0 :     TALER_TESTING_make_trait_wire_delay (&pis->default_wire_transfer_delay);
     231             :   traits[8] =
     232           0 :     TALER_TESTING_make_trait_pay_delay (&pis->default_pay_delay);
     233             :   traits[9] =
     234           0 :     TALER_TESTING_make_trait_payto_length (&pis->payto_uris_length);
     235           0 :   traits[NUM_TRAITS - 1] =
     236           0 :     TALER_TESTING_trait_end ();
     237           0 :   for (unsigned int i = 0; i < pis->payto_uris_length; ++i)
     238             :   {
     239           0 :     traits[10 + i] =
     240           0 :       TALER_TESTING_make_trait_payto_uris (i,
     241           0 :                                            &pis->payto_uris[i]);
     242             :   }
     243             : 
     244           0 :   return TALER_TESTING_get_trait (traits,
     245             :                                   ret,
     246             :                                   trait,
     247             :                                   index);
     248             : }
     249             : 
     250             : 
     251             : /**
     252             :  * Free the state of a "PATCH /instances/$ID" CMD, and possibly
     253             :  * cancel a pending operation thereof.
     254             :  *
     255             :  * @param cls closure.
     256             :  * @param cmd command being run.
     257             :  */
     258             : static void
     259           0 : patch_instance_cleanup (void *cls,
     260             :                         const struct TALER_TESTING_Command *cmd)
     261             : {
     262           0 :   struct PatchInstanceState *pis = cls;
     263             : 
     264           0 :   if (NULL != pis->iph)
     265             :   {
     266           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     267             :                 "PATCH /instance/$ID operation did not complete\n");
     268           0 :     TALER_MERCHANT_instance_patch_cancel (pis->iph);
     269             :   }
     270           0 :   json_decref (pis->address);
     271           0 :   json_decref (pis->jurisdiction);
     272           0 :   GNUNET_free (pis->payto_uris);
     273           0 :   GNUNET_free (pis);
     274           0 : }
     275             : 
     276             : 
     277             : struct TALER_TESTING_Command
     278           0 : TALER_TESTING_cmd_merchant_patch_instance (
     279             :   const char *label,
     280             :   const char *merchant_url,
     281             :   const char *instance_id,
     282             :   unsigned int payto_uris_length,
     283             :   const char *payto_uris[],
     284             :   const char *name,
     285             :   json_t *address,
     286             :   json_t *jurisdiction,
     287             :   const char *default_max_wire_fee,
     288             :   uint32_t default_wire_fee_amortization,
     289             :   const char *default_max_deposit_fee,
     290             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     291             :   struct GNUNET_TIME_Relative default_pay_delay,
     292             :   unsigned int http_status)
     293             : {
     294             :   struct PatchInstanceState *pis;
     295             : 
     296           0 :   pis = GNUNET_new (struct PatchInstanceState);
     297           0 :   pis->merchant_url = merchant_url;
     298           0 :   pis->instance_id = instance_id;
     299           0 :   pis->http_status = http_status;
     300           0 :   pis->payto_uris_length = payto_uris_length;
     301           0 :   pis->payto_uris = GNUNET_new_array (payto_uris_length,
     302             :                                       const char *);
     303           0 :   memcpy (pis->payto_uris,
     304             :           payto_uris,
     305             :           sizeof (const char *) * payto_uris_length);
     306           0 :   pis->name = name;
     307           0 :   pis->address = address; /* ownership transfer! */
     308           0 :   pis->jurisdiction = jurisdiction; /* ownership transfer! */
     309           0 :   GNUNET_assert (GNUNET_OK ==
     310             :                  TALER_string_to_amount (default_max_wire_fee,
     311             :                                          &pis->default_max_wire_fee));
     312           0 :   pis->default_wire_fee_amortization = default_wire_fee_amortization;
     313           0 :   GNUNET_assert (GNUNET_OK ==
     314             :                  TALER_string_to_amount (default_max_deposit_fee,
     315             :                                          &pis->default_max_deposit_fee));
     316           0 :   pis->default_wire_transfer_delay = default_wire_transfer_delay;
     317           0 :   pis->default_pay_delay = default_pay_delay;
     318             :   {
     319           0 :     struct TALER_TESTING_Command cmd = {
     320             :       .cls = pis,
     321             :       .label = label,
     322             :       .run = &patch_instance_run,
     323             :       .cleanup = &patch_instance_cleanup,
     324             :       .traits = &patch_instance_traits
     325             :     };
     326             : 
     327           0 :     return cmd;
     328             :   }
     329             : }
     330             : 
     331             : 
     332             : /* end of testing_api_cmd_patch_instance.c */

Generated by: LCOV version 1.14