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