merchant

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

testing_api_cmd_get_instance.c (9848B)


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