testing_api_cmd_submit_receipts.c (5924B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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_submit_receipts.c 21 * @brief Implement the POST /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_DonorReceiptsToStatementHandle *bsrh; 40 41 /** 42 * Expected HTTP response code. 43 */ 44 unsigned int expected_response_code; 45 46 /** 47 * Interpreter state. 48 */ 49 struct TALER_TESTING_Interpreter *is; 50 51 /** 52 * Issue receipts reference. 53 */ 54 const char *issue_receipt_reference; 55 56 /** 57 * corresponding year 58 */ 59 unsigned long long year; 60 61 /** 62 * number of donation receipts. 63 */ 64 size_t num_receipts; 65 66 /** 67 * array of donation receipts 68 */ 69 const struct DONAU_DonationReceipt *receipts; 70 71 /** 72 * donau keys 73 */ 74 struct DONAU_Keys *keys; 75 76 /** 77 * Hashed and salted tax id of the donor. 78 */ 79 const struct DONAU_HashDonorTaxId *h_donor_tax_id; 80 81 }; 82 83 84 /** 85 * Check that the reserve balance and HTTP response code are 86 * both acceptable. 87 * 88 * @param cls closure. 89 * @param rtsresp HTTP response details 90 */ 91 static void 92 submit_receipts_status_cb (void *cls, 93 const struct 94 DONAU_DonorReceiptsToStatementResult *rtsresp) 95 { 96 struct StatusState *ss = cls; 97 98 ss->bsrh = NULL; 99 if (ss->expected_response_code != rtsresp->hr.http_status) 100 { 101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 102 "Unexpected HTTP response code: %d in %s:%u\n", 103 rtsresp->hr.http_status, 104 __FILE__, 105 __LINE__); 106 json_dumpf (rtsresp->hr.reply, 107 stderr, 108 0); 109 TALER_TESTING_interpreter_fail (ss->is); 110 return; 111 } 112 113 TALER_TESTING_interpreter_next (ss->is); 114 } 115 116 117 /** 118 * Run the command. 119 * 120 * @param cls closure. 121 * @param cmd the command being executed. 122 * @param is the interpreter state. 123 */ 124 static void 125 status_run (void *cls, 126 const struct TALER_TESTING_Command *cmd, 127 struct TALER_TESTING_Interpreter *is) 128 { 129 struct StatusState *ss = cls; 130 131 (void) cmd; 132 ss->is = is; 133 /* Get donau keys from trait */ 134 { 135 const struct TALER_TESTING_Command *keys_cmd; 136 struct DONAU_Keys *keys; 137 138 keys_cmd = TALER_TESTING_interpreter_lookup_command (is, 139 "get-donau"); 140 141 if (GNUNET_OK != 142 TALER_TESTING_get_trait_donau_keys (keys_cmd, &keys)) 143 { 144 GNUNET_break (0); 145 TALER_TESTING_interpreter_fail (is); 146 return; 147 } 148 ss->keys = keys; 149 } 150 151 /* Get donau keys from trait */ 152 { 153 const struct TALER_TESTING_Command *receipts_cmd; 154 const struct DONAU_DonationReceipt **receipts; 155 const struct DONAU_HashDonorTaxId *h_donor_tax_id; 156 const size_t *num_receipts; 157 158 receipts_cmd = TALER_TESTING_interpreter_lookup_command (is, 159 ss-> 160 issue_receipt_reference); 161 162 if (GNUNET_OK != 163 TALER_TESTING_get_trait_donation_receipts (receipts_cmd, &receipts) || 164 GNUNET_OK != 165 TALER_TESTING_get_trait_salted_tax_id_hash (receipts_cmd, 166 &h_donor_tax_id) || 167 GNUNET_OK != 168 TALER_TESTING_get_trait_number_receipts (receipts_cmd, &num_receipts) ) 169 { 170 GNUNET_break (0); 171 TALER_TESTING_interpreter_fail (is); 172 return; 173 } 174 ss->num_receipts = *num_receipts; 175 ss->receipts = *receipts; 176 ss->h_donor_tax_id = h_donor_tax_id; 177 } 178 179 ss->bsrh = DONAU_donor_receipts_to_statement ( 180 TALER_TESTING_interpreter_get_context (is), 181 TALER_TESTING_get_donau_url (is), 182 ss->num_receipts, 183 ss->receipts, 184 ss->year, 185 ss->h_donor_tax_id, 186 &submit_receipts_status_cb, 187 ss); 188 189 } 190 191 192 /** 193 * Cleanup the state from a "issue receipt status" CMD, and possibly 194 * cancel a pending operation thereof. 195 * 196 * @param cls closure. 197 * @param cmd the command which is being cleaned up. 198 */ 199 static void 200 cleanup (void *cls, 201 const struct TALER_TESTING_Command *cmd) 202 { 203 struct StatusState *ss = cls; 204 205 if (NULL != ss->bsrh) 206 { 207 // log incomplete command 208 TALER_TESTING_command_incomplete (ss->is, 209 cmd->label); 210 DONAU_donor_receipts_to_statement_cancel (ss->bsrh); 211 ss->bsrh = NULL; 212 } 213 GNUNET_free (ss); 214 } 215 216 217 struct TALER_TESTING_Command 218 TALER_TESTING_cmd_submit_receipts (const char *label, 219 const char *issue_receipt_reference, 220 const uint64_t year, 221 unsigned int expected_response_code) 222 { 223 struct StatusState *ss; 224 225 ss = GNUNET_new (struct StatusState); 226 227 ss->year = year; 228 ss->expected_response_code = expected_response_code; 229 ss->issue_receipt_reference = issue_receipt_reference; 230 231 { 232 struct TALER_TESTING_Command cmd = { 233 .cls = ss, 234 .label = label, 235 .run = &status_run, 236 .cleanup = &cleanup 237 }; 238 239 return cmd; 240 241 } 242 }