donau

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

testing_api_cmd_charities_get.c (3793B)


      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_charities_get.c
     21  * @brief Implement the GET /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 #include "donau_testing_lib.h"
     29 
     30 
     31 /**
     32  * State for a "status" CMD.
     33  */
     34 struct StatusState
     35 {
     36   /**
     37    * Handle to the "charities status" operation.
     38    */
     39   struct DONAU_CharitiesGetHandle *cgh;
     40 
     41   /**
     42    * The bearer token for authorization.
     43    */
     44   const struct DONAU_BearerToken *bearer;
     45 
     46   /**
     47    * Expected HTTP response code.
     48    */
     49   unsigned int expected_response_code;
     50 
     51   /**
     52    * Interpreter state.
     53    */
     54   struct TALER_TESTING_Interpreter *is;
     55 
     56 };
     57 
     58 
     59 /**
     60  * Check that the reserve balance and HTTP response code are
     61  * both acceptable.
     62  *
     63  * @param cls closure.
     64  * @param gcr HTTP response details
     65  */
     66 static void
     67 charities_status_cb (void *cls,
     68                      const struct DONAU_GetCharitiesResponse *gcr)
     69 {
     70   struct StatusState *ss = cls;
     71 
     72   ss->cgh = NULL;
     73   if (ss->expected_response_code != gcr->hr.http_status)
     74   {
     75     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     76                 "Unexpected HTTP response code: %d in %s:%u\n",
     77                 gcr->hr.http_status,
     78                 __FILE__,
     79                 __LINE__);
     80     json_dumpf (gcr->hr.reply,
     81                 stderr,
     82                 0);
     83     TALER_TESTING_interpreter_fail (ss->is);
     84     return;
     85   }
     86   TALER_TESTING_interpreter_next (ss->is);
     87 }
     88 
     89 
     90 /**
     91  * Run the command.
     92  *
     93  * @param cls closure.
     94  * @param cmd the command being executed.
     95  * @param is the interpreter state.
     96  */
     97 static void
     98 status_run (void *cls,
     99             const struct TALER_TESTING_Command *cmd,
    100             struct TALER_TESTING_Interpreter *is)
    101 {
    102   struct StatusState *ss = cls;
    103 
    104   (void) cmd;
    105   ss->is = is;
    106   ss->cgh = DONAU_charities_get (
    107     TALER_TESTING_interpreter_get_context (is),
    108     TALER_TESTING_get_donau_url (is),
    109     ss->bearer,
    110     &charities_status_cb,
    111     ss);
    112 }
    113 
    114 
    115 /**
    116  * Cleanup the state from a "reserve status" CMD, and possibly
    117  * cancel a pending operation thereof.
    118  *
    119  * @param cls closure.
    120  * @param cmd the command which is being cleaned up.
    121  */
    122 static void
    123 status_cleanup (void *cls,
    124                 const struct TALER_TESTING_Command *cmd)
    125 {
    126   struct StatusState *ss = cls;
    127 
    128   if (NULL != ss->cgh)
    129   {
    130     // log incomplete command
    131     TALER_TESTING_command_incomplete (ss->is,
    132                                       cmd->label);
    133     DONAU_charities_get_cancel (ss->cgh);
    134     ss->cgh = NULL;
    135   }
    136   GNUNET_free (ss);
    137 }
    138 
    139 
    140 struct TALER_TESTING_Command
    141 TALER_TESTING_cmd_charities_get (const char *label,
    142                                  const struct DONAU_BearerToken *bearer,
    143                                  unsigned int expected_response_code)
    144 {
    145   struct StatusState *ss;
    146 
    147   ss = GNUNET_new (struct StatusState);
    148   ss->expected_response_code = expected_response_code;
    149   ss->bearer = bearer;
    150   {
    151     struct TALER_TESTING_Command cmd = {
    152       .cls = ss,
    153       .label = label,
    154       .run = &status_run,
    155       .cleanup = &status_cleanup
    156     };
    157 
    158     return cmd;
    159   }
    160 }