donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_charity_delete.c (4711B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2024 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/testing_api_cmd_charity_delete.c
     21  * @brief Implement the DELETE /charities test command.
     22  * @author Lukas Matyja
     23  */
     24 #include <donau_config.h>
     25 #include <taler/taler_json_lib.h>
     26 #include <gnunet/gnunet_curl_lib.h>
     27 #include <taler/taler_testing_lib.h>
     28 struct StatusState;
     29 #define DONAU_DELETE_CHARITY_RESULT_CLOSURE struct StatusState
     30 #include "donau_testing_lib.h"
     31 
     32 
     33 /**
     34  * State for a "status" CMD.
     35  */
     36 struct StatusState
     37 {
     38   /**
     39    * Handle to the "charity status" operation.
     40    */
     41   struct DONAU_CharityDeleteHandle *cgh;
     42 
     43   /**
     44    * The bearer token for authorization.
     45    */
     46   const struct DONAU_BearerToken *bearer;
     47 
     48   /**
     49    * The ID of the requested charity.
     50    */
     51   uint64_t charity_id;
     52 
     53   /**
     54    * Expected HTTP response code.
     55    */
     56   unsigned int expected_response_code;
     57 
     58   /**
     59    * Interpreter state.
     60    */
     61   struct TALER_TESTING_Interpreter *is;
     62 
     63   /**
     64    * Reference to charity post command.
     65    */
     66   const char *charity_reference;
     67 
     68 };
     69 
     70 
     71 /**
     72  * Check that the reserve balance and HTTP response code are
     73  * both acceptable.
     74  *
     75  * @param ss closure.
     76  * @param dcr HTTP response details
     77  */
     78 static void
     79 charity_status_cb (struct StatusState *ss,
     80                    const struct DONAU_DeleteCharityResponse *dcr)
     81 {
     82   ss->cgh = NULL;
     83   if (ss->expected_response_code != dcr->hr.http_status)
     84   {
     85     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     86                 "Unexpected HTTP response code: %d in %s:%u\n",
     87                 dcr->hr.http_status,
     88                 __FILE__,
     89                 __LINE__);
     90     json_dumpf (dcr->hr.reply,
     91                 stderr,
     92                 0);
     93     TALER_TESTING_interpreter_fail (ss->is);
     94     return;
     95   }
     96   TALER_TESTING_interpreter_next (ss->is);
     97 }
     98 
     99 
    100 /**
    101  * Run the command.
    102  *
    103  * @param cls closure.
    104  * @param cmd the command being executed.
    105  * @param is the interpreter state.
    106  */
    107 static void
    108 status_run (void *cls,
    109             const struct TALER_TESTING_Command *cmd,
    110             struct TALER_TESTING_Interpreter *is)
    111 {
    112   struct StatusState *ss = cls;
    113 
    114   (void) cmd;
    115   ss->is = is;
    116   /* Get charity id from trait */
    117   {
    118     const struct TALER_TESTING_Command *charity_post_cmd;
    119     const uint64_t *charity_id;
    120 
    121     charity_post_cmd =
    122       TALER_TESTING_interpreter_lookup_command (is,
    123                                                 ss->charity_reference);
    124 
    125     if (GNUNET_OK !=
    126         TALER_TESTING_get_trait_charity_id (charity_post_cmd,
    127                                             &charity_id))
    128     {
    129       GNUNET_break (0);
    130       TALER_TESTING_interpreter_fail (is);
    131       return;
    132     }
    133     ss->charity_id = (uint64_t) *(charity_id);
    134   }
    135 
    136   ss->cgh = DONAU_charity_delete (
    137     TALER_TESTING_interpreter_get_context (is),
    138     TALER_TESTING_get_donau_url (is),
    139     ss->charity_id,
    140     ss->bearer,
    141     &charity_status_cb,
    142     ss);
    143 }
    144 
    145 
    146 /**
    147  * Cleanup the state from a "reserve status" CMD, and possibly
    148  * cancel a pending operation thereof.
    149  *
    150  * @param cls closure.
    151  * @param cmd the command which is being cleaned up.
    152  */
    153 static void
    154 status_cleanup (void *cls,
    155                 const struct TALER_TESTING_Command *cmd)
    156 {
    157   struct StatusState *ss = cls;
    158 
    159   if (NULL != ss->cgh)
    160   {
    161     // log incomplete command
    162     TALER_TESTING_command_incomplete (ss->is,
    163                                       cmd->label);
    164     DONAU_charity_delete_cancel (ss->cgh);
    165     ss->cgh = NULL;
    166   }
    167   GNUNET_free (ss);
    168 }
    169 
    170 
    171 struct TALER_TESTING_Command
    172 TALER_TESTING_cmd_charity_delete (const char *label,
    173                                   const char *charity_reference,
    174                                   const struct DONAU_BearerToken *bearer,
    175                                   unsigned int expected_response_code)
    176 {
    177   struct StatusState *ss;
    178 
    179   ss = GNUNET_new (struct StatusState);
    180   ss->expected_response_code = expected_response_code;
    181   ss->bearer = bearer;
    182   ss->charity_reference = charity_reference;
    183   {
    184     struct TALER_TESTING_Command cmd = {
    185       .cls = ss,
    186       .label = label,
    187       .run = &status_run,
    188       .cleanup = &status_cleanup
    189     };
    190 
    191     return cmd;
    192   }
    193 }