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