merchant

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

testing_api_cmd_delete_transfer.c (5051B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2021 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_delete_transfer.c
     21  * @brief command to test DELETE /transfers/$TRANSFER_ID
     22  * @author Jonathan Buchanan
     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 "DELETE /transfer/$TRANSFER_ID" CMD.
     33  */
     34 struct DeleteTransferState
     35 {
     36 
     37   /**
     38    * Handle for a "DELETE transfer" request.
     39    */
     40   struct TALER_MERCHANT_TransferDeleteHandle *tdh;
     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    * Ref to cmd with ID of the transfer to run DELETE for.
     54    */
     55   const char *transfer_ref;
     56 
     57   /**
     58    * Expected HTTP response code.
     59    */
     60   unsigned int http_status;
     61 
     62 };
     63 
     64 
     65 /**
     66  * Callback for a DELETE /transfers/$ID operation.
     67  *
     68  * @param cls closure for this function
     69  * @param hr response being processed
     70  */
     71 static void
     72 delete_transfer_cb (void *cls,
     73                     const struct TALER_MERCHANT_HttpResponse *hr)
     74 {
     75   struct DeleteTransferState *dts = cls;
     76 
     77   dts->tdh = NULL;
     78   if (dts->http_status != hr->http_status)
     79   {
     80     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     81                 "Unexpected response code %u (%d) to command %s\n",
     82                 hr->http_status,
     83                 (int) hr->ec,
     84                 TALER_TESTING_interpreter_get_current_label (dts->is));
     85     TALER_TESTING_interpreter_fail (dts->is);
     86     return;
     87   }
     88   switch (hr->http_status)
     89   {
     90   case MHD_HTTP_NO_CONTENT:
     91     break;
     92   case MHD_HTTP_UNAUTHORIZED:
     93     break;
     94   case MHD_HTTP_NOT_FOUND:
     95     break;
     96   case MHD_HTTP_CONFLICT:
     97     break;
     98   default:
     99     GNUNET_break (0);
    100     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    101                 "Unhandled HTTP status %u for DELETE transfer.\n",
    102                 hr->http_status);
    103   }
    104   TALER_TESTING_interpreter_next (dts->is);
    105 }
    106 
    107 
    108 /**
    109  * Run the "DELETE transfer" CMD.
    110  *
    111  *
    112  * @param cls closure.
    113  * @param cmd command being run now.
    114  * @param is interpreter state.
    115  */
    116 static void
    117 delete_transfer_run (void *cls,
    118                      const struct TALER_TESTING_Command *cmd,
    119                      struct TALER_TESTING_Interpreter *is)
    120 {
    121   struct DeleteTransferState *dts = cls;
    122   const struct TALER_TESTING_Command *ref;
    123   const uint64_t *tid;
    124 
    125   dts->is = is;
    126   ref = TALER_TESTING_interpreter_lookup_command (is,
    127                                                   dts->transfer_ref);
    128   if (NULL == ref)
    129   {
    130     GNUNET_break (0);
    131     TALER_TESTING_interpreter_fail (dts->is);
    132     return;
    133   }
    134   if (GNUNET_OK !=
    135       TALER_TESTING_get_trait_bank_row (ref,
    136                                         &tid))
    137   {
    138     GNUNET_break (0);
    139     TALER_TESTING_interpreter_fail (dts->is);
    140     return;
    141   }
    142   if (0 == tid)
    143   {
    144     GNUNET_break (0);
    145     TALER_TESTING_interpreter_fail (dts->is);
    146     return;
    147   }
    148   dts->tdh = TALER_MERCHANT_transfer_delete (
    149     TALER_TESTING_interpreter_get_context (is),
    150     dts->merchant_url,
    151     *tid,
    152     &delete_transfer_cb,
    153     dts);
    154   GNUNET_assert (NULL != dts->tdh);
    155 }
    156 
    157 
    158 /**
    159  * Free the state of a "DELETE transfer" CMD, and possibly
    160  * cancel a pending operation thereof.
    161  *
    162  * @param cls closure.
    163  * @param cmd command being run.
    164  */
    165 static void
    166 delete_transfer_cleanup (void *cls,
    167                          const struct TALER_TESTING_Command *cmd)
    168 {
    169   struct DeleteTransferState *dts = cls;
    170 
    171   if (NULL != dts->tdh)
    172   {
    173     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    174                 "DELETE /transfers/$TRANSFER_ID operation did not complete\n");
    175     TALER_MERCHANT_transfer_delete_cancel (dts->tdh);
    176   }
    177   GNUNET_free (dts);
    178 }
    179 
    180 
    181 struct TALER_TESTING_Command
    182 TALER_TESTING_cmd_merchant_delete_transfer (const char *label,
    183                                             const char *merchant_url,
    184                                             const char *transfer_ref,
    185                                             unsigned int http_status)
    186 {
    187   struct DeleteTransferState *dts;
    188 
    189   dts = GNUNET_new (struct DeleteTransferState);
    190   dts->merchant_url = merchant_url;
    191   dts->transfer_ref = transfer_ref;
    192   dts->http_status = http_status;
    193   {
    194     struct TALER_TESTING_Command cmd = {
    195       .cls = dts,
    196       .label = label,
    197       .run = &delete_transfer_run,
    198       .cleanup = &delete_transfer_cleanup
    199     };
    200 
    201     return cmd;
    202   }
    203 }