merchant

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

testing_api_cmd_get_instance.c (9528B)


      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_get_instance.c
     21  * @brief command to test GET /instance/$ID
     22  * @author Christian Grothoff
     23  */
     24 #include "taler/platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler/taler_merchant_service.h"
     28 #include "taler/taler_merchant_testing_lib.h"
     29 #include <taler/taler-merchant/get-management-instances-INSTANCE.h>
     30 
     31 
     32 /**
     33  * State of a "GET instance" CMD.
     34  */
     35 struct GetInstanceState
     36 {
     37 
     38   /**
     39    * Handle for a "GET instance" request.
     40    */
     41   struct TALER_MERCHANT_GetManagementInstanceHandle *igh;
     42 
     43   /**
     44    * The interpreter state.
     45    */
     46   struct TALER_TESTING_Interpreter *is;
     47 
     48   /**
     49    * Public key of the merchant instance.
     50    */
     51   union TALER_AccountPublicKeyP account_pub;
     52 
     53   /**
     54    * Base URL of the merchant serving the request.
     55    */
     56   const char *merchant_url;
     57 
     58   /**
     59    * ID of the instance to run GET for.
     60    */
     61   const char *instance_id;
     62 
     63   /**
     64    * Reference for a POST or PATCH /instances CMD (optional).
     65    */
     66   const char *instance_reference;
     67 
     68   /**
     69    * Expected HTTP response code.
     70    */
     71   unsigned int http_status;
     72 
     73 };
     74 
     75 
     76 /**
     77  * Callback for a /get/instance/$ID operation.
     78  *
     79  * @param cls closure for this function
     80  * @param igr response
     81  */
     82 static void
     83 get_instance_cb (void *cls,
     84                  const struct TALER_MERCHANT_GetManagementInstanceResponse *igr)
     85 {
     86   struct GetInstanceState *gis = cls;
     87   const struct TALER_TESTING_Command *instance_cmd;
     88 
     89   instance_cmd = TALER_TESTING_interpreter_lookup_command (
     90     gis->is,
     91     gis->instance_reference);
     92 
     93   gis->igh = NULL;
     94   if (gis->http_status != igr->hr.http_status)
     95   {
     96     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     97                 "Unexpected response code %u (%d) to command %s\n",
     98                 igr->hr.http_status,
     99                 (int) igr->hr.ec,
    100                 TALER_TESTING_interpreter_get_current_label (gis->is));
    101     TALER_TESTING_interpreter_fail (gis->is);
    102     return;
    103   }
    104   switch (igr->hr.http_status)
    105   {
    106   case MHD_HTTP_OK:
    107     {
    108       const struct TALER_MERCHANT_GetManagementInstanceDetails *details =
    109         &igr->details.ok.details;
    110 
    111       gis->account_pub.merchant_pub
    112         = details->merchant_pub;
    113       {
    114         const char *name;
    115 
    116         if (GNUNET_OK !=
    117             TALER_TESTING_get_trait_instance_name (instance_cmd,
    118                                                    &name))
    119           TALER_TESTING_interpreter_fail (gis->is);
    120         if (0 != strcmp (details->name,
    121                          name))
    122         {
    123           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    124                       "Instance name does not match: Got `%s', wanted `%s'\n",
    125                       details->name,
    126                       name);
    127           TALER_TESTING_interpreter_fail (gis->is);
    128           return;
    129         }
    130       }
    131       {
    132         const json_t *address;
    133 
    134         if (GNUNET_OK !=
    135             TALER_TESTING_get_trait_address (instance_cmd,
    136                                              &address))
    137           TALER_TESTING_interpreter_fail (gis->is);
    138         if (1 != json_equal (details->address,
    139                              address))
    140         {
    141           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    142                       "Instance address does not match\n");
    143           TALER_TESTING_interpreter_fail (gis->is);
    144           return;
    145         }
    146       }
    147       {
    148         const json_t *jurisdiction;
    149 
    150         if (GNUNET_OK !=
    151             TALER_TESTING_get_trait_jurisdiction (instance_cmd,
    152                                                   &jurisdiction))
    153           TALER_TESTING_interpreter_fail (gis->is);
    154         if (1 != json_equal (details->jurisdiction,
    155                              jurisdiction))
    156         {
    157           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    158                       "Instance jurisdiction does not match\n");
    159           TALER_TESTING_interpreter_fail (gis->is);
    160           return;
    161         }
    162       }
    163       {
    164         const bool *use_stefan;
    165 
    166         if (GNUNET_OK !=
    167             TALER_TESTING_get_trait_use_stefan (instance_cmd,
    168                                                 &use_stefan))
    169           TALER_TESTING_interpreter_fail (gis->is);
    170         if (*use_stefan != details->use_stefan)
    171         {
    172           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    173                       "Instance use_stefan value does not match\n");
    174           TALER_TESTING_interpreter_fail (gis->is);
    175           return;
    176         }
    177       }
    178       {
    179         const struct GNUNET_TIME_Relative *default_wire_transfer_delay;
    180 
    181         if (GNUNET_OK !=
    182             TALER_TESTING_get_trait_wire_delay (instance_cmd,
    183                                                 &default_wire_transfer_delay))
    184           TALER_TESTING_interpreter_fail (gis->is);
    185         if (details->default_wire_transfer_delay.rel_value_us !=
    186             default_wire_transfer_delay->rel_value_us)
    187         {
    188           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    189                       "Instance default wire transfer delay does not match\n");
    190           TALER_TESTING_interpreter_fail (gis->is);
    191           return;
    192         }
    193       }
    194       {
    195         const struct GNUNET_TIME_Relative *default_pay_delay;
    196 
    197         if (GNUNET_OK !=
    198             TALER_TESTING_get_trait_pay_delay (instance_cmd,
    199                                                &default_pay_delay))
    200           TALER_TESTING_interpreter_fail (gis->is);
    201         if (details->default_pay_delay.rel_value_us !=
    202             default_pay_delay->rel_value_us)
    203         {
    204           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    205                       "Instance default pay delay does not match\n");
    206           TALER_TESTING_interpreter_fail (gis->is);
    207           return;
    208         }
    209       }
    210     }
    211     break;
    212   case MHD_HTTP_UNAUTHORIZED:
    213     break;
    214   case MHD_HTTP_NOT_FOUND:
    215     break;
    216   default:
    217     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    218                 "Unhandled HTTP status %u for GET instance ID.\n",
    219                 igr->hr.http_status);
    220   }
    221   TALER_TESTING_interpreter_next (gis->is);
    222 }
    223 
    224 
    225 /**
    226  * Run the "GET instance" CMD.
    227  *
    228  *
    229  * @param cls closure.
    230  * @param cmd command being run now.
    231  * @param is interpreter state.
    232  */
    233 static void
    234 get_instance_run (void *cls,
    235                   const struct TALER_TESTING_Command *cmd,
    236                   struct TALER_TESTING_Interpreter *is)
    237 {
    238   struct GetInstanceState *gis = cls;
    239 
    240   gis->is = is;
    241   gis->igh = TALER_MERCHANT_get_management_instance_create (
    242     TALER_TESTING_interpreter_get_context (is),
    243     gis->merchant_url,
    244     gis->instance_id);
    245   {
    246     enum TALER_ErrorCode ec;
    247 
    248     ec = TALER_MERCHANT_get_management_instance_start (
    249       gis->igh,
    250       &get_instance_cb,
    251       gis);
    252     GNUNET_assert (TALER_EC_NONE == ec);
    253   }
    254 }
    255 
    256 
    257 /**
    258  * Free the state of a "GET instance" CMD, and possibly
    259  * cancel a pending operation thereof.
    260  *
    261  * @param cls closure.
    262  * @param cmd command being run.
    263  */
    264 static void
    265 get_instance_cleanup (void *cls,
    266                       const struct TALER_TESTING_Command *cmd)
    267 {
    268   struct GetInstanceState *gis = cls;
    269 
    270   if (NULL != gis->igh)
    271   {
    272     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    273                 "GET /instances/$ID operation did not complete\n");
    274     TALER_MERCHANT_get_management_instance_cancel (gis->igh);
    275   }
    276   GNUNET_free (gis);
    277 }
    278 
    279 
    280 /**
    281  * Offers information from the GET /instance/$ID CMD state to other
    282  * commands.
    283  *
    284  * @param cls closure
    285  * @param[out] ret result (could be anything)
    286  * @param trait name of the trait
    287  * @param index index number of the object to extract.
    288  * @return #GNUNET_OK on success
    289  */
    290 static enum GNUNET_GenericReturnValue
    291 get_instance_traits (void *cls,
    292                      const void **ret,
    293                      const char *trait,
    294                      unsigned int index)
    295 {
    296   struct GetInstanceState *pps = cls;
    297   struct TALER_TESTING_Trait traits[] = {
    298     TALER_TESTING_make_trait_merchant_base_url (
    299       pps->merchant_url),
    300     TALER_TESTING_make_trait_merchant_pub (
    301       &pps->account_pub.merchant_pub),
    302     TALER_TESTING_make_trait_account_pub (
    303       &pps->account_pub),
    304     TALER_TESTING_trait_end (),
    305   };
    306 
    307   return TALER_TESTING_get_trait (traits,
    308                                   ret,
    309                                   trait,
    310                                   index);
    311 }
    312 
    313 
    314 struct TALER_TESTING_Command
    315 TALER_TESTING_cmd_merchant_get_instance (
    316   const char *label,
    317   const char *merchant_url,
    318   const char *instance_id,
    319   unsigned int http_status,
    320   const char *instance_reference)
    321 {
    322   struct GetInstanceState *gis;
    323 
    324   gis = GNUNET_new (struct GetInstanceState);
    325   gis->merchant_url = merchant_url;
    326   gis->instance_id = instance_id;
    327   gis->http_status = http_status;
    328   gis->instance_reference = instance_reference;
    329   {
    330     struct TALER_TESTING_Command cmd = {
    331       .cls = gis,
    332       .label = label,
    333       .run = &get_instance_run,
    334       .cleanup = &get_instance_cleanup,
    335       .traits = &get_instance_traits
    336     };
    337 
    338     return cmd;
    339   }
    340 }
    341 
    342 
    343 /* end of testing_api_cmd_get_instance.c */