LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 81 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 6 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 lib/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             :    * 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 POST /instances operation.
     117             :  *
     118             :  * @param cls closure for this function
     119             :  */
     120             : static void
     121           0 : post_instances_cb (void *cls,
     122             :                    const struct TALER_MERCHANT_HttpResponse *hr)
     123             : {
     124           0 :   struct PostInstancesState *pis = cls;
     125             : 
     126           0 :   pis->iph = NULL;
     127           0 :   if (pis->http_status != hr->http_status)
     128             :   {
     129           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                 "Unexpected response code %u (%d) to command %s\n",
     131             :                 hr->http_status,
     132             :                 (int) hr->ec,
     133             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     134           0 :     TALER_TESTING_interpreter_fail (pis->is);
     135           0 :     return;
     136             :   }
     137           0 :   switch (hr->http_status)
     138             :   {
     139           0 :   case MHD_HTTP_NO_CONTENT:
     140           0 :     break;
     141           0 :   case MHD_HTTP_CONFLICT:
     142           0 :     break;
     143             :   // FIXME: add other legitimate states here...
     144           0 :   default:
     145           0 :     GNUNET_break (0);
     146           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     147             :                 "Unhandled HTTP status.\n");
     148             :   }
     149           0 :   TALER_TESTING_interpreter_next (pis->is);
     150             : }
     151             : 
     152             : 
     153             : /**
     154             :  * Run the "POST /instances" CMD.
     155             :  *
     156             :  *
     157             :  * @param cls closure.
     158             :  * @param cmd command being run now.
     159             :  * @param is interpreter state.
     160             :  */
     161             : static void
     162           0 : post_instances_run (void *cls,
     163             :                     const struct TALER_TESTING_Command *cmd,
     164             :                     struct TALER_TESTING_Interpreter *is)
     165             : {
     166           0 :   struct PostInstancesState *pis = cls;
     167             : 
     168           0 :   pis->is = is;
     169           0 :   pis->iph = TALER_MERCHANT_instances_post (is->ctx,
     170             :                                             pis->merchant_url,
     171             :                                             pis->instance_id,
     172             :                                             pis->payto_uris_length,
     173             :                                             pis->payto_uris,
     174             :                                             pis->name,
     175           0 :                                             pis->address,
     176           0 :                                             pis->jurisdiction,
     177           0 :                                             &pis->default_max_wire_fee,
     178             :                                             pis->default_wire_fee_amortization,
     179           0 :                                             &pis->default_max_deposit_fee,
     180             :                                             pis->default_wire_transfer_delay,
     181             :                                             pis->default_pay_delay,
     182             :                                             &post_instances_cb,
     183             :                                             pis);
     184           0 :   GNUNET_assert (NULL != pis->iph);
     185           0 : }
     186             : 
     187             : 
     188             : /**
     189             :  * Offers information from the POST /instances CMD state to other
     190             :  * commands.
     191             :  *
     192             :  * @param cls closure
     193             :  * @param ret[out] result (could be anything)
     194             :  * @param trait name of the trait
     195             :  * @param index index number of the object to extract.
     196             :  * @return #GNUNET_OK on success
     197             :  */
     198             : static int
     199           0 : post_instances_traits (void *cls,
     200             :                        const void **ret,
     201             :                        const char *trait,
     202             :                        unsigned int index)
     203           0 : {
     204           0 :   struct PostInstancesState *pis = cls;
     205             :   #define NUM_TRAITS (pis->payto_uris_length) + 11
     206           0 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
     207             :   traits[0] =
     208           0 :     TALER_TESTING_make_trait_string (0, pis->name);
     209             :   traits[1] =
     210           0 :     TALER_TESTING_make_trait_string (1, pis->instance_id);
     211             :   traits[2] =
     212           0 :     TALER_TESTING_make_trait_json (0, pis->address);
     213             :   traits[3] =
     214           0 :     TALER_TESTING_make_trait_json (1, pis->jurisdiction);
     215             :   traits[4] =
     216           0 :     TALER_TESTING_make_trait_amount_obj (0, &pis->default_max_wire_fee);
     217             :   traits[5] =
     218           0 :     TALER_TESTING_make_trait_uint32 (0, &pis->default_wire_fee_amortization);
     219             :   traits[6] =
     220           0 :     TALER_TESTING_make_trait_amount_obj (1, &pis->default_max_deposit_fee);
     221             :   traits[7] =
     222           0 :     TALER_TESTING_make_trait_relative_time (0,
     223           0 :                                             &pis->default_wire_transfer_delay);
     224             :   traits[8] =
     225           0 :     TALER_TESTING_make_trait_relative_time (1, &pis->default_pay_delay);
     226             :   traits[9] =
     227           0 :     TALER_TESTING_make_trait_uint32 (1, &pis->payto_uris_length);
     228           0 :   traits[NUM_TRAITS - 1] =
     229           0 :     TALER_TESTING_trait_end ();
     230           0 :   for (unsigned int i = 0; i < pis->payto_uris_length; ++i)
     231             :   {
     232           0 :     traits[10 + i] =
     233           0 :       TALER_TESTING_make_trait_string (2 + i, pis->payto_uris[i]);
     234             :   }
     235             : 
     236           0 :   return TALER_TESTING_get_trait (traits,
     237             :                                   ret,
     238             :                                   trait,
     239             :                                   index);
     240             : }
     241             : 
     242             : 
     243             : /**
     244             :  * Free the state of a "POST /instances" CMD, and possibly
     245             :  * cancel a pending operation thereof.
     246             :  *
     247             :  * @param cls closure.
     248             :  * @param cmd command being run.
     249             :  */
     250             : static void
     251           0 : post_instances_cleanup (void *cls,
     252             :                         const struct TALER_TESTING_Command *cmd)
     253             : {
     254           0 :   struct PostInstancesState *pis = cls;
     255             : 
     256           0 :   if (NULL != pis->iph)
     257             :   {
     258           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     259             :                 "POST /instances operation did not complete\n");
     260           0 :     TALER_MERCHANT_instances_post_cancel (pis->iph);
     261             :   }
     262           0 :   json_decref (pis->address);
     263           0 :   json_decref (pis->jurisdiction);
     264           0 :   GNUNET_free (pis->payto_uris);
     265           0 :   GNUNET_free (pis);
     266           0 : }
     267             : 
     268             : 
     269             : /**
     270             :  * Define a "POST /instances" CMD.
     271             :  *
     272             :  * @param label command label.
     273             :  * @param merchant_url base URL of the merchant serving the
     274             :  *        POST /instances request.
     275             :  * @param instance_id the ID of the instance to query
     276             :  * @param payto_uris_length length of the @a accounts array
     277             :  * @param payto_uris URIs of the bank accounts of the merchant instance
     278             :  * @param name name of the merchant instance
     279             :  * @param address physical address of the merchant instance
     280             :  * @param jurisdiction jurisdiction of the merchant instance
     281             :  * @param default_max_wire_fee default maximum wire fee merchant is willing to fully pay
     282             :  * @param default_wire_fee_amortization default amortization factor for excess wire fees
     283             :  * @param default_max_deposit_fee default maximum deposit fee merchant is willing to pay
     284             :  * @param default_wire_transfer_delay default wire transfer delay merchant will ask for
     285             :  * @param default_pay_delay default validity period for offers merchant makes
     286             :  * @param http_status expected HTTP response code.
     287             :  * @return the command.
     288             :  */
     289             : struct TALER_TESTING_Command
     290           0 : 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             :   unsigned int http_status)
     305             : {
     306             :   struct PostInstancesState *pis;
     307             : 
     308           0 :   pis = GNUNET_new (struct PostInstancesState);
     309           0 :   pis->merchant_url = merchant_url;
     310           0 :   pis->instance_id = instance_id;
     311           0 :   pis->http_status = http_status;
     312           0 :   pis->payto_uris_length = payto_uris_length;
     313           0 :   pis->payto_uris = GNUNET_new_array (payto_uris_length,
     314             :                                       const char *);
     315           0 :   memcpy (pis->payto_uris,
     316             :           payto_uris,
     317             :           sizeof (const char *) * payto_uris_length);
     318           0 :   pis->name = name;
     319           0 :   pis->address = address; /* ownership transfer! */
     320           0 :   pis->jurisdiction = jurisdiction; /* ownership transfer! */
     321           0 :   GNUNET_assert (GNUNET_OK ==
     322             :                  TALER_string_to_amount (default_max_wire_fee,
     323             :                                          &pis->default_max_wire_fee));
     324           0 :   pis->default_wire_fee_amortization = default_wire_fee_amortization;
     325           0 :   GNUNET_assert (GNUNET_OK ==
     326             :                  TALER_string_to_amount (default_max_deposit_fee,
     327             :                                          &pis->default_max_deposit_fee));
     328           0 :   pis->default_wire_transfer_delay = default_wire_transfer_delay;
     329           0 :   pis->default_pay_delay = default_pay_delay;
     330             :   {
     331           0 :     struct TALER_TESTING_Command cmd = {
     332             :       .cls = pis,
     333             :       .label = label,
     334             :       .run = &post_instances_run,
     335             :       .cleanup = &post_instances_cleanup,
     336             :       .traits = &post_instances_traits
     337             :     };
     338             : 
     339           0 :     return cmd;
     340             :   }
     341             : }
     342             : 
     343             : 
     344             : /**
     345             :  * Define a "POST /instances" CMD, simple version
     346             :  *
     347             :  * @param label command label.
     348             :  * @param merchant_url base URL of the merchant serving the
     349             :  *        POST /instances request.
     350             :  * @param instance_id the ID of the instance to create
     351             :  * @param payto_uri payment URI to use
     352             :  * @param currency currency to use for default fees
     353             :  * @param http_status expected HTTP response code.
     354             :  * @return the command.
     355             :  */
     356             : struct TALER_TESTING_Command
     357           0 : TALER_TESTING_cmd_merchant_post_instances (const char *label,
     358             :                                            const char *merchant_url,
     359             :                                            const char *instance_id,
     360             :                                            const char *payto_uri,
     361             :                                            const char *currency,
     362             :                                            unsigned int http_status)
     363             : {
     364           0 :   const char *payto_uris[] = {
     365             :     payto_uri
     366             :   };
     367             :   struct TALER_Amount default_max_fee;
     368             :   const char *default_max_fee_s;
     369             : 
     370           0 :   GNUNET_assert (GNUNET_OK ==
     371             :                  TALER_amount_get_zero (currency,
     372             :                                         &default_max_fee));
     373           0 :   default_max_fee.value = 1;
     374           0 :   default_max_fee_s = TALER_amount2s (&default_max_fee);
     375             : 
     376           0 :   return TALER_TESTING_cmd_merchant_post_instances2 (
     377             :     label,
     378             :     merchant_url,
     379             :     instance_id,
     380             :     1,
     381             :     payto_uris,
     382             :     instance_id,
     383             :     json_pack ("{s:s}", "city", "shopcity"),
     384             :     json_pack ("{s:s}", "city", "lawyercity"),
     385             :     default_max_fee_s,
     386             :     10,
     387             :     default_max_fee_s,
     388             :     GNUNET_TIME_UNIT_ZERO, /* no wire transfer delay */
     389             :     GNUNET_TIME_UNIT_MINUTES,
     390             :     http_status);
     391             : }
     392             : 
     393             : 
     394             : /* end of testing_api_cmd_post_instance.c */

Generated by: LCOV version 1.14