merchant

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

testing_api_cmd_get_transfers.c (9246B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2023 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_transfers.c
     21  * @brief command to test GET /transfers.
     22  * @author Marcello Stanisci
     23  * @author Christian Grothoff
     24  */
     25 #include "platform.h"
     26 #include <taler/taler_exchange_service.h>
     27 #include <taler/taler_testing_lib.h>
     28 #include "taler_merchant_service.h"
     29 #include "taler_merchant_testing_lib.h"
     30 
     31 
     32 /**
     33  * State of a GET transfers CMD.
     34  */
     35 struct GetTransfersState
     36 {
     37 
     38   /**
     39    * Handle for a "get transfer" request.
     40    */
     41   struct TALER_MERCHANT_GetTransfersHandle *gth;
     42 
     43   /**
     44    * The interpreter state.
     45    */
     46   struct TALER_TESTING_Interpreter *is;
     47 
     48   /**
     49    * Base URL of the merchant serving the request.
     50    */
     51   const char *merchant_url;
     52 
     53   /**
     54    * payto URI of the merchant to filter by.
     55    */
     56   struct TALER_FullPayto payto_uri;
     57 
     58   /**
     59    * Expected HTTP response code.
     60    */
     61   unsigned int http_status;
     62 
     63   /**
     64    * Reference for a "check bank" CMD.  It offers the
     65    * WTID to get.
     66    */
     67   const char *check_bank_reference;
     68 
     69   /**
     70    * Array of POST /transfer command labels we expect to see listed.
     71    */
     72   const char **transfers;
     73 
     74   /**
     75    * Length of @e transfers.
     76    */
     77   unsigned int transfers_length;
     78 
     79 };
     80 
     81 
     82 /**
     83  * Check the result of our GET /transfers request to a merchant
     84  *
     85  * @param cls closure
     86  * @param gtr response details
     87  */
     88 static void
     89 get_transfers_cb (
     90   void *cls,
     91   const struct TALER_MERCHANT_GetTransfersResponse *gtr)
     92 {
     93   struct GetTransfersState *gts = cls;
     94 
     95   gts->gth = NULL;
     96   if (gts->http_status != gtr->hr.http_status)
     97   {
     98     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     99                 "Unexpected response code %u (%d) to command %s\n",
    100                 gtr->hr.http_status,
    101                 (int) gtr->hr.ec,
    102                 TALER_TESTING_interpreter_get_current_label (gts->is));
    103     TALER_TESTING_interpreter_fail (gts->is);
    104     return;
    105   }
    106   switch (gtr->hr.http_status)
    107   {
    108   case MHD_HTTP_OK:
    109     if (gtr->details.ok.transfers_length != gts->transfers_length)
    110     {
    111       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    112                   "Transfers length does not match (got %u/want %u)\n",
    113                   gtr->details.ok.transfers_length,
    114                   gts->transfers_length);
    115       TALER_TESTING_interpreter_fail (gts->is);
    116       return;
    117     }
    118     for (unsigned int i = 0; i < gtr->details.ok.transfers_length; ++i)
    119     {
    120       const struct TALER_MERCHANT_TransferData *transfer
    121         = &gtr->details.ok.transfers[i];
    122       const struct TALER_TESTING_Command *transfer_cmd;
    123 
    124       transfer_cmd = TALER_TESTING_interpreter_lookup_command (
    125         gts->is,
    126         gts->transfers[i]);
    127       if (NULL == transfer_cmd)
    128       {
    129         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    130                     "Command `%s' not found!\n",
    131                     gts->transfers[i]);
    132         TALER_TESTING_interpreter_fail (gts->is);
    133         return;
    134       }
    135       {
    136         const struct TALER_WireTransferIdentifierRawP *wtid;
    137 
    138         if (GNUNET_OK !=
    139             TALER_TESTING_get_trait_wtid (transfer_cmd,
    140                                           &wtid))
    141         {
    142           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    143                       "Could not fetch wire transfer id\n");
    144           TALER_TESTING_interpreter_fail (gts->is);
    145           return;
    146         }
    147         if (0 != GNUNET_memcmp (wtid,
    148                                 &transfer->wtid))
    149         {
    150           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    151                       "Wire transfer id does not match\n");
    152           TALER_TESTING_interpreter_fail (gts->is);
    153           return;
    154         }
    155         TALER_TESTING_cmd_merchant_post_transfer_set_serial (
    156           (struct TALER_TESTING_Command *) transfer_cmd,
    157           transfer->credit_serial);
    158       }
    159       {
    160         const struct TALER_FullPayto *payto_uri;
    161 
    162         if (GNUNET_OK !=
    163             TALER_TESTING_get_trait_credit_payto_uri (transfer_cmd,
    164                                                       &payto_uri))
    165         {
    166           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    167                       "Could not fetch wire transfer payto uri\n");
    168           TALER_TESTING_interpreter_fail (gts->is);
    169           return;
    170         }
    171         if (0 !=
    172             TALER_full_payto_cmp (*payto_uri,
    173                                   transfer->payto_uri))
    174         {
    175           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    176                       "Wire transfer payto uri does not match: %s != %s\n",
    177                       payto_uri->full_payto,
    178                       transfer->payto_uri.full_payto);
    179           TALER_TESTING_interpreter_fail (gts->is);
    180           return;
    181         }
    182       }
    183       {
    184         const struct TALER_Amount *credit_amount;
    185 
    186         if (GNUNET_OK !=
    187             TALER_TESTING_get_trait_amount (transfer_cmd,
    188                                             &credit_amount))
    189         {
    190           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    191                       "Could not fetch wire transfer credit amount\n");
    192           TALER_TESTING_interpreter_fail (gts->is);
    193           return;
    194         }
    195         if ( (GNUNET_OK !=
    196               TALER_amount_cmp_currency (credit_amount,
    197                                          &transfer->credit_amount)) ||
    198              (0 != TALER_amount_cmp (credit_amount,
    199                                      &transfer->credit_amount)))
    200         {
    201           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    202                       "Wire transfer credit amount does not match\n");
    203           TALER_TESTING_interpreter_fail (gts->is);
    204           return;
    205         }
    206       }
    207       {
    208         const char *exchange_url;
    209 
    210         if (GNUNET_OK !=
    211             TALER_TESTING_get_trait_exchange_url (transfer_cmd,
    212                                                   &exchange_url))
    213         {
    214           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    215                       "Could not fetch wire transfer exchange url\n");
    216           TALER_TESTING_interpreter_fail (gts->is);
    217           return;
    218         }
    219         if (0 != strcmp (exchange_url,
    220                          transfer->exchange_url))
    221         {
    222           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    223                       "Wire transfer exchange url does not match\n");
    224           TALER_TESTING_interpreter_fail (gts->is);
    225           return;
    226         }
    227       }
    228     }
    229     break;
    230   default:
    231     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    232                 "Unhandled HTTP status %u.\n",
    233                 gtr->hr.http_status);
    234     break;
    235   }
    236   TALER_TESTING_interpreter_next (gts->is);
    237 }
    238 
    239 
    240 /**
    241  * Run the "get transfer" CMD.
    242  *
    243  * @param cls closure.
    244  * @param cmd command being run now.
    245  * @param is interpreter state.
    246  */
    247 static void
    248 get_transfers_run (void *cls,
    249                    const struct TALER_TESTING_Command *cmd,
    250                    struct TALER_TESTING_Interpreter *is)
    251 {
    252   struct GetTransfersState *gts = cls;
    253 
    254   gts->is = is;
    255   gts->gth = TALER_MERCHANT_transfers_get (
    256     TALER_TESTING_interpreter_get_context (is),
    257     gts->merchant_url,
    258     gts->payto_uri,
    259     GNUNET_TIME_UNIT_FOREVER_TS,
    260     GNUNET_TIME_UNIT_ZERO_TS,
    261     INT64_MAX,
    262     0,
    263     TALER_EXCHANGE_YNA_ALL,
    264     &get_transfers_cb,
    265     gts);
    266   GNUNET_assert (NULL != gts->gth);
    267 }
    268 
    269 
    270 /**
    271  * Free the state of a "get transfer" CMD, and possibly
    272  * cancel a pending operation thereof.
    273  *
    274  * @param cls closure.
    275  * @param cmd command being run.
    276  */
    277 static void
    278 get_transfers_cleanup (void *cls,
    279                        const struct TALER_TESTING_Command *cmd)
    280 {
    281   struct GetTransfersState *gts = cls;
    282 
    283   if (NULL != gts->gth)
    284   {
    285     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    286                 "GET /transfer operation did not complete\n");
    287     TALER_MERCHANT_transfers_get_cancel (gts->gth);
    288   }
    289   GNUNET_array_grow (gts->transfers,
    290                      gts->transfers_length,
    291                      0);
    292   GNUNET_free (gts);
    293 }
    294 
    295 
    296 struct TALER_TESTING_Command
    297 TALER_TESTING_cmd_merchant_get_transfers (
    298   const char *label,
    299   const char *merchant_url,
    300   struct TALER_FullPayto payto_uri,
    301   unsigned int http_code,
    302   ...)
    303 {
    304   struct GetTransfersState *gts;
    305 
    306   gts = GNUNET_new (struct GetTransfersState);
    307   gts->merchant_url = merchant_url;
    308   gts->payto_uri = payto_uri;
    309   gts->http_status = http_code;
    310   {
    311     const char *clabel;
    312     va_list ap;
    313 
    314     va_start (ap, http_code);
    315     while (NULL != (clabel = va_arg (ap, const char *)))
    316     {
    317       GNUNET_array_append (gts->transfers,
    318                            gts->transfers_length,
    319                            clabel);
    320     }
    321     va_end (ap);
    322   }
    323   {
    324     struct TALER_TESTING_Command cmd = {
    325       .cls = gts,
    326       .label = label,
    327       .run = &get_transfers_run,
    328       .cleanup = &get_transfers_cleanup
    329     };
    330 
    331     return cmd;
    332   }
    333 }
    334 
    335 
    336 /* end of testing_api_cmd_get_transfers.c */