exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_purse_delete.c (5006B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it
      6   under the terms of the GNU General Public License as published by
      7   the Free Software Foundation; either version 3, or (at your
      8   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 GNU
     13   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/testing_api_cmd_purse_delete.c
     21  * @brief command for testing /management/purse/disable.
     22  * @author Christian Grothoff
     23  */
     24 #include "taler/taler_json_lib.h"
     25 #include <gnunet/gnunet_curl_lib.h>
     26 struct PurseDeleteState;
     27 #define TALER_EXCHANGE_DELETE_PURSES_RESULT_CLOSURE \
     28         struct PurseDeleteState
     29 #include "taler/exchange/delete-purses-PURSE_PUB.h"
     30 #include "taler/taler_testing_lib.h"
     31 
     32 
     33 /**
     34  * State for a "purse_delete" CMD.
     35  */
     36 struct PurseDeleteState
     37 {
     38 
     39   /**
     40    * Purse delete handle while operation is running.
     41    */
     42   struct TALER_EXCHANGE_DeletePursesHandle *dph;
     43 
     44   /**
     45    * Our interpreter.
     46    */
     47   struct TALER_TESTING_Interpreter *is;
     48 
     49   /**
     50    * Expected HTTP response code.
     51    */
     52   unsigned int expected_response_code;
     53 
     54   /**
     55    * Command that created the purse we now want to
     56    * delete.
     57    */
     58   const char *purse_cmd;
     59 };
     60 
     61 
     62 /**
     63  * Callback to analyze the DELETE /purses/$PID response, just used to check if
     64  * the response code is acceptable.
     65  *
     66  * @param pds closure.
     67  * @param pdr HTTP response details
     68  */
     69 static void
     70 purse_delete_cb (struct PurseDeleteState *pds,
     71                  const struct TALER_EXCHANGE_DeletePursesResponse *pdr)
     72 {
     73   pds->dph = NULL;
     74   if (pds->expected_response_code != pdr->hr.http_status)
     75   {
     76     TALER_TESTING_unexpected_status (pds->is,
     77                                      pdr->hr.http_status,
     78                                      pds->expected_response_code);
     79     return;
     80   }
     81   TALER_TESTING_interpreter_next (pds->is);
     82 }
     83 
     84 
     85 /**
     86  * Run the command.
     87  *
     88  * @param cls closure.
     89  * @param cmd the command to execute.
     90  * @param is the interpreter state.
     91  */
     92 static void
     93 purse_delete_run (void *cls,
     94                   const struct TALER_TESTING_Command *cmd,
     95                   struct TALER_TESTING_Interpreter *is)
     96 {
     97   struct PurseDeleteState *pds = cls;
     98   const struct TALER_PurseContractPrivateKeyP *purse_priv;
     99   const struct TALER_TESTING_Command *ref;
    100   const char *exchange_url;
    101 
    102   (void) cmd;
    103   exchange_url = TALER_TESTING_get_exchange_url (is);
    104   if (NULL == exchange_url)
    105   {
    106     GNUNET_break (0);
    107     return;
    108   }
    109   ref = TALER_TESTING_interpreter_lookup_command (is,
    110                                                   pds->purse_cmd);
    111   if (NULL == ref)
    112   {
    113     GNUNET_break (0);
    114     TALER_TESTING_interpreter_fail (is);
    115     return;
    116   }
    117   if (GNUNET_OK !=
    118       TALER_TESTING_get_trait_purse_priv (ref,
    119                                           &purse_priv))
    120   {
    121     GNUNET_break (0);
    122     TALER_TESTING_interpreter_fail (is);
    123     return;
    124   }
    125   pds->is = is;
    126   pds->dph = TALER_EXCHANGE_delete_purses_create (
    127     TALER_TESTING_interpreter_get_context (is),
    128     exchange_url,
    129     purse_priv);
    130   if (NULL == pds->dph)
    131   {
    132     GNUNET_break (0);
    133     TALER_TESTING_interpreter_fail (is);
    134     return;
    135   }
    136   {
    137     enum TALER_ErrorCode ec;
    138 
    139     ec = TALER_EXCHANGE_delete_purses_start (
    140       pds->dph,
    141       &purse_delete_cb,
    142       pds);
    143     if (TALER_EC_NONE != ec)
    144     {
    145       GNUNET_break (0);
    146       TALER_EXCHANGE_delete_purses_cancel (pds->dph);
    147       pds->dph = NULL;
    148       TALER_TESTING_interpreter_fail (is);
    149       return;
    150     }
    151   }
    152 }
    153 
    154 
    155 /**
    156  * Free the state of a "purse_delete" CMD, and possibly cancel a
    157  * pending operation thereof.
    158  *
    159  * @param cls closure, must be a `struct PurseDeleteState`.
    160  * @param cmd the command which is being cleaned up.
    161  */
    162 static void
    163 purse_delete_cleanup (void *cls,
    164                       const struct TALER_TESTING_Command *cmd)
    165 {
    166   struct PurseDeleteState *pds = cls;
    167 
    168   if (NULL != pds->dph)
    169   {
    170     TALER_TESTING_command_incomplete (pds->is,
    171                                       cmd->label);
    172     TALER_EXCHANGE_delete_purses_cancel (pds->dph);
    173     pds->dph = NULL;
    174   }
    175   GNUNET_free (pds);
    176 }
    177 
    178 
    179 struct TALER_TESTING_Command
    180 TALER_TESTING_cmd_purse_delete (const char *label,
    181                                 unsigned int expected_http_status,
    182                                 const char *purse_cmd)
    183 {
    184   struct PurseDeleteState *ds;
    185 
    186   ds = GNUNET_new (struct PurseDeleteState);
    187   ds->expected_response_code = expected_http_status;
    188   ds->purse_cmd = purse_cmd;
    189   {
    190     struct TALER_TESTING_Command cmd = {
    191       .cls = ds,
    192       .label = label,
    193       .run = &purse_delete_run,
    194       .cleanup = &purse_delete_cleanup
    195     };
    196 
    197     return cmd;
    198   }
    199 }
    200 
    201 
    202 /* end of testing_api_cmd_purse_delete.c */