LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 75 92 81.5 %
Date: 2021-08-30 06:54:17 Functions: 6 6 100.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_post_instances.c
      21             :  * @brief command to test POST /instances
      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 "POST /instances" CMD.
      33             :  */
      34             : struct PostInstancesState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "POST instance" request.
      39             :    */
      40             :   struct TALER_MERCHANT_InstancesPostHandle *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 POST 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             :    * Authentication token to require for this instance.
      84             :    */
      85             :   const char *auth_token;
      86             : 
      87             :   /**
      88             :    * Wire fee to use.
      89             :    */
      90             :   struct TALER_Amount default_max_wire_fee;
      91             : 
      92             :   /**
      93             :    * Amortization to use.
      94             :    */
      95             :   uint32_t default_wire_fee_amortization;
      96             : 
      97             :   /**
      98             :    * Deposit fee ceiling to use.
      99             :    */
     100             :   struct TALER_Amount default_max_deposit_fee;
     101             : 
     102             :   /**
     103             :    * Wire transfer delay to use.
     104             :    */
     105             :   struct GNUNET_TIME_Relative default_wire_transfer_delay;
     106             : 
     107             :   /**
     108             :    * Order validity default duration to use.
     109             :    */
     110             :   struct GNUNET_TIME_Relative default_pay_delay;
     111             : 
     112             :   /**
     113             :    * Expected HTTP response code.
     114             :    */
     115             :   unsigned int http_status;
     116             : 
     117             : };
     118             : 
     119             : 
     120             : /**
     121             :  * Callback for a POST /instances operation.
     122             :  *
     123             :  * @param cls closure for this function
     124             :  * @param hr response being processed
     125             :  */
     126             : static void
     127          11 : post_instances_cb (void *cls,
     128             :                    const struct TALER_MERCHANT_HttpResponse *hr)
     129             : {
     130          11 :   struct PostInstancesState *pis = cls;
     131             : 
     132          11 :   pis->iph = NULL;
     133          11 :   if (pis->http_status != hr->http_status)
     134             :   {
     135           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     136             :                 "Unexpected response code %u (%d) to command %s\n",
     137             :                 hr->http_status,
     138             :                 (int) hr->ec,
     139             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     140           0 :     TALER_TESTING_interpreter_fail (pis->is);
     141           0 :     return;
     142             :   }
     143          11 :   switch (hr->http_status)
     144             :   {
     145           9 :   case MHD_HTTP_NO_CONTENT:
     146           9 :     break;
     147           1 :   case MHD_HTTP_BAD_REQUEST:
     148           1 :     break;
     149           0 :   case MHD_HTTP_UNAUTHORIZED:
     150           0 :     break;
     151           0 :   case MHD_HTTP_FORBIDDEN:
     152           0 :     break;
     153           0 :   case MHD_HTTP_NOT_FOUND:
     154           0 :     break;
     155           1 :   case MHD_HTTP_CONFLICT:
     156           1 :     break;
     157           0 :   default:
     158           0 :     GNUNET_break (0);
     159           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     160             :                 "Unhandled HTTP status %u for POST instances.\n",
     161             :                 hr->http_status);
     162             :   }
     163          11 :   TALER_TESTING_interpreter_next (pis->is);
     164             : }
     165             : 
     166             : 
     167             : /**
     168             :  * Run the "POST /instances" CMD.
     169             :  *
     170             :  *
     171             :  * @param cls closure.
     172             :  * @param cmd command being run now.
     173             :  * @param is interpreter state.
     174             :  */
     175             : static void
     176          11 : post_instances_run (void *cls,
     177             :                     const struct TALER_TESTING_Command *cmd,
     178             :                     struct TALER_TESTING_Interpreter *is)
     179             : {
     180          11 :   struct PostInstancesState *pis = cls;
     181             : 
     182          11 :   pis->is = is;
     183          22 :   pis->iph = TALER_MERCHANT_instances_post (is->ctx,
     184             :                                             pis->merchant_url,
     185             :                                             pis->instance_id,
     186             :                                             pis->payto_uris_length,
     187             :                                             pis->payto_uris,
     188             :                                             pis->name,
     189          11 :                                             pis->address,
     190          11 :                                             pis->jurisdiction,
     191          11 :                                             &pis->default_max_wire_fee,
     192             :                                             pis->default_wire_fee_amortization,
     193          11 :                                             &pis->default_max_deposit_fee,
     194             :                                             pis->default_wire_transfer_delay,
     195             :                                             pis->default_pay_delay,
     196             :                                             pis->auth_token,
     197             :                                             &post_instances_cb,
     198             :                                             pis);
     199          11 :   if (NULL == pis->iph)
     200             :   {
     201           0 :     GNUNET_break (0);
     202           0 :     TALER_TESTING_interpreter_fail (pis->is);
     203           0 :     return;
     204             :   }
     205             : }
     206             : 
     207             : 
     208             : /**
     209             :  * Offers information from the POST /instances CMD state to other
     210             :  * commands.
     211             :  *
     212             :  * @param cls closure
     213             :  * @param[out] ret result (could be anything)
     214             :  * @param trait name of the trait
     215             :  * @param index index number of the object to extract.
     216             :  * @return #GNUNET_OK on success
     217             :  */
     218             : static int
     219          49 : post_instances_traits (void *cls,
     220             :                        const void **ret,
     221             :                        const char *trait,
     222             :                        unsigned int index)
     223          49 : {
     224          49 :   struct PostInstancesState *pis = cls;
     225             :   #define NUM_TRAITS (pis->payto_uris_length) + 11
     226          49 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
     227             :   traits[0] =
     228          49 :     TALER_TESTING_make_trait_string (0, pis->name);
     229             :   traits[1] =
     230          49 :     TALER_TESTING_make_trait_string (1, pis->instance_id);
     231             :   traits[2] =
     232          49 :     TALER_TESTING_make_trait_json (0, pis->address);
     233             :   traits[3] =
     234          49 :     TALER_TESTING_make_trait_json (1, pis->jurisdiction);
     235             :   traits[4] =
     236          49 :     TALER_TESTING_make_trait_amount_obj (0, &pis->default_max_wire_fee);
     237             :   traits[5] =
     238          49 :     TALER_TESTING_make_trait_uint32 (0, &pis->default_wire_fee_amortization);
     239             :   traits[6] =
     240          49 :     TALER_TESTING_make_trait_amount_obj (1, &pis->default_max_deposit_fee);
     241             :   traits[7] =
     242          49 :     TALER_TESTING_make_trait_relative_time (0,
     243          49 :                                             &pis->default_wire_transfer_delay);
     244             :   traits[8] =
     245          49 :     TALER_TESTING_make_trait_relative_time (1, &pis->default_pay_delay);
     246             :   traits[9] =
     247          49 :     TALER_TESTING_make_trait_uint32 (1, &pis->payto_uris_length);
     248          49 :   traits[NUM_TRAITS - 1] =
     249          49 :     TALER_TESTING_trait_end ();
     250          94 :   for (unsigned int i = 0; i < pis->payto_uris_length; ++i)
     251             :   {
     252          45 :     traits[10 + i] =
     253          45 :       TALER_TESTING_make_trait_string (2 + i, pis->payto_uris[i]);
     254             :   }
     255             : 
     256          49 :   return TALER_TESTING_get_trait (traits,
     257             :                                   ret,
     258             :                                   trait,
     259             :                                   index);
     260             : }
     261             : 
     262             : 
     263             : /**
     264             :  * Free the state of a "POST /instances" CMD, and possibly
     265             :  * cancel a pending operation thereof.
     266             :  *
     267             :  * @param cls closure.
     268             :  * @param cmd command being run.
     269             :  */
     270             : static void
     271          11 : post_instances_cleanup (void *cls,
     272             :                         const struct TALER_TESTING_Command *cmd)
     273             : {
     274          11 :   struct PostInstancesState *pis = cls;
     275             : 
     276          11 :   if (NULL != pis->iph)
     277             :   {
     278           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     279             :                 "POST /instances operation did not complete\n");
     280           0 :     TALER_MERCHANT_instances_post_cancel (pis->iph);
     281             :   }
     282          11 :   json_decref (pis->address);
     283          11 :   json_decref (pis->jurisdiction);
     284          11 :   GNUNET_free (pis->payto_uris);
     285          11 :   GNUNET_free (pis);
     286          11 : }
     287             : 
     288             : 
     289             : struct TALER_TESTING_Command
     290          11 : TALER_TESTING_cmd_merchant_post_instances2 (
     291             :   const char *label,
     292             :   const char *merchant_url,
     293             :   const char *instance_id,
     294             :   unsigned int payto_uris_length,
     295             :   const char *payto_uris[],
     296             :   const char *name,
     297             :   json_t *address,
     298             :   json_t *jurisdiction,
     299             :   const char *default_max_wire_fee,
     300             :   uint32_t default_wire_fee_amortization,
     301             :   const char *default_max_deposit_fee,
     302             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     303             :   struct GNUNET_TIME_Relative default_pay_delay,
     304             :   const char *auth_token,
     305             :   unsigned int http_status)
     306             : {
     307             :   struct PostInstancesState *pis;
     308             : 
     309          11 :   pis = GNUNET_new (struct PostInstancesState);
     310          11 :   pis->merchant_url = merchant_url;
     311          11 :   pis->instance_id = instance_id;
     312          11 :   pis->http_status = http_status;
     313          11 :   pis->payto_uris_length = payto_uris_length;
     314          11 :   pis->payto_uris = GNUNET_new_array (payto_uris_length,
     315             :                                       const char *);
     316          11 :   memcpy (pis->payto_uris,
     317             :           payto_uris,
     318             :           sizeof (const char *) * payto_uris_length);
     319          11 :   pis->name = name;
     320          11 :   pis->address = address; /* ownership transfer! */
     321          11 :   pis->jurisdiction = jurisdiction; /* ownership transfer! */
     322          11 :   GNUNET_assert (GNUNET_OK ==
     323             :                  TALER_string_to_amount (default_max_wire_fee,
     324             :                                          &pis->default_max_wire_fee));
     325          11 :   pis->default_wire_fee_amortization = default_wire_fee_amortization;
     326          11 :   GNUNET_assert (GNUNET_OK ==
     327             :                  TALER_string_to_amount (default_max_deposit_fee,
     328             :                                          &pis->default_max_deposit_fee));
     329          11 :   pis->default_wire_transfer_delay = default_wire_transfer_delay;
     330          11 :   pis->default_pay_delay = default_pay_delay;
     331          11 :   pis->auth_token = auth_token;
     332             :   {
     333          11 :     struct TALER_TESTING_Command cmd = {
     334             :       .cls = pis,
     335             :       .label = label,
     336             :       .run = &post_instances_run,
     337             :       .cleanup = &post_instances_cleanup,
     338             :       .traits = &post_instances_traits
     339             :     };
     340             : 
     341          11 :     return cmd;
     342             :   }
     343             : }
     344             : 
     345             : 
     346             : struct TALER_TESTING_Command
     347          10 : TALER_TESTING_cmd_merchant_post_instances (const char *label,
     348             :                                            const char *merchant_url,
     349             :                                            const char *instance_id,
     350             :                                            const char *payto_uri,
     351             :                                            const char *currency,
     352             :                                            unsigned int http_status)
     353             : {
     354          10 :   const char *payto_uris[] = {
     355             :     payto_uri
     356             :   };
     357             :   struct TALER_Amount default_max_fee;
     358             :   const char *default_max_fee_s;
     359             : 
     360          10 :   GNUNET_assert (GNUNET_OK ==
     361             :                  TALER_amount_set_zero (currency,
     362             :                                         &default_max_fee));
     363          10 :   default_max_fee.value = 1;
     364          10 :   default_max_fee_s = TALER_amount2s (&default_max_fee);
     365             : 
     366          10 :   return TALER_TESTING_cmd_merchant_post_instances2 (
     367             :     label,
     368             :     merchant_url,
     369             :     instance_id,
     370             :     1,
     371             :     payto_uris,
     372             :     instance_id,
     373             :     json_pack ("{s:s}", "city", "shopcity"),
     374             :     json_pack ("{s:s}", "city", "lawyercity"),
     375             :     default_max_fee_s,
     376             :     10,
     377             :     default_max_fee_s,
     378             :     GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */
     379             :     GNUNET_TIME_UNIT_MINUTES,
     380             :     NULL,
     381             :     http_status);
     382             : }
     383             : 
     384             : 
     385             : /* end of testing_api_cmd_post_instance.c */

Generated by: LCOV version 1.14