testing_api_cmd_post_units.c (7241B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 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 src/testing/testing_api_cmd_post_units.c 21 * @brief command to test POST /private/units 22 * @author Bohdan Potuzhnyi 23 */ 24 #include "platform.h" 25 struct PostUnitState; 26 #define TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE struct PostUnitState 27 #include <taler/taler_testing_lib.h> 28 #include "taler/taler_merchant_service.h" 29 #include "taler/taler_merchant_testing_lib.h" 30 #include <taler/merchant/post-private-units.h> 31 32 33 /** 34 * State for a POST /private/units command. 35 */ 36 struct PostUnitState 37 { 38 /** 39 * In-flight request handle. 40 */ 41 struct TALER_MERCHANT_PostPrivateUnitsHandle *uph; 42 43 /** 44 * Interpreter context. 45 */ 46 struct TALER_TESTING_Interpreter *is; 47 48 /** 49 * Merchant backend base URL. 50 */ 51 const char *merchant_url; 52 53 /** 54 * Unit identifier. 55 */ 56 const char *unit_id; 57 58 /** 59 * Long label. 60 */ 61 const char *unit_name_long; 62 63 /** 64 * Short label. 65 */ 66 const char *unit_name_short; 67 68 /** 69 * Optional translations (reference counted). 70 */ 71 json_t *unit_name_long_i18n; 72 73 /** 74 * Optional translations (reference counted). 75 */ 76 json_t *unit_name_short_i18n; 77 78 /** 79 * Whether fractional quantities are allowed. 80 */ 81 bool unit_allow_fraction; 82 83 /** 84 * Precision level for fractional quantities. 85 */ 86 uint32_t unit_precision_level; 87 88 /** 89 * Whether the unit should be active. 90 */ 91 bool unit_active; 92 93 /** 94 * Expected HTTP status. 95 */ 96 unsigned int http_status; 97 }; 98 99 100 /** 101 * Completion callback for POST /private/units. 102 */ 103 static void 104 post_unit_cb (struct PostUnitState *pus, 105 const struct TALER_MERCHANT_PostPrivateUnitsResponse *pur) 106 { 107 108 pus->uph = NULL; 109 if (pus->http_status != pur->hr.http_status) 110 { 111 TALER_TESTING_unexpected_status_with_body (pus->is, 112 pur->hr.http_status, 113 pus->http_status, 114 pur->hr.reply); 115 return; 116 } 117 TALER_TESTING_interpreter_next (pus->is); 118 } 119 120 121 /** 122 * Issue the POST /private/units request. 123 */ 124 static void 125 post_unit_run (void *cls, 126 const struct TALER_TESTING_Command *cmd, 127 struct TALER_TESTING_Interpreter *is) 128 { 129 struct PostUnitState *pus = cls; 130 131 pus->is = is; 132 pus->uph = TALER_MERCHANT_post_private_units_create ( 133 TALER_TESTING_interpreter_get_context (is), 134 pus->merchant_url, 135 pus->unit_id, 136 pus->unit_name_long, 137 pus->unit_name_short); 138 TALER_MERCHANT_post_private_units_set_options ( 139 pus->uph, 140 TALER_MERCHANT_post_private_units_option_unit_allow_fraction ( 141 pus->unit_allow_fraction), 142 TALER_MERCHANT_post_private_units_option_unit_precision_level ( 143 pus->unit_precision_level), 144 TALER_MERCHANT_post_private_units_option_unit_active ( 145 pus->unit_active)); 146 if (NULL != pus->unit_name_long_i18n) 147 TALER_MERCHANT_post_private_units_set_options ( 148 pus->uph, 149 TALER_MERCHANT_post_private_units_option_unit_name_long_i18n ( 150 pus->unit_name_long_i18n)); 151 if (NULL != pus->unit_name_short_i18n) 152 TALER_MERCHANT_post_private_units_set_options ( 153 pus->uph, 154 TALER_MERCHANT_post_private_units_option_unit_name_short_i18n ( 155 pus->unit_name_short_i18n)); 156 { 157 enum TALER_ErrorCode ec; 158 159 ec = TALER_MERCHANT_post_private_units_start ( 160 pus->uph, 161 &post_unit_cb, 162 pus); 163 if (TALER_EC_NONE != ec) 164 { 165 GNUNET_break (0); 166 TALER_TESTING_interpreter_fail (is); 167 } 168 } 169 } 170 171 172 /** 173 * Provide traits for other commands. 174 */ 175 static enum GNUNET_GenericReturnValue 176 post_unit_traits (void *cls, 177 const void **ret, 178 const char *trait, 179 unsigned int index) 180 { 181 struct PostUnitState *pus = cls; 182 struct TALER_TESTING_Trait traits[] = { 183 TALER_TESTING_make_trait_unit_id (pus->unit_id), 184 TALER_TESTING_make_trait_unit_name_long (pus->unit_name_long), 185 TALER_TESTING_make_trait_unit_name_short (pus->unit_name_short), 186 TALER_TESTING_make_trait_unit_allow_fraction (&pus->unit_allow_fraction), 187 TALER_TESTING_make_trait_unit_precision_level (&pus->unit_precision_level), 188 TALER_TESTING_make_trait_unit_active (&pus->unit_active), 189 TALER_TESTING_make_trait_unit_name_long_i18n (pus->unit_name_long_i18n), 190 TALER_TESTING_make_trait_unit_name_short_i18n (pus->unit_name_short_i18n), 191 TALER_TESTING_trait_end () 192 }; 193 194 return TALER_TESTING_get_trait (traits, 195 ret, 196 trait, 197 index); 198 } 199 200 201 /** 202 * Cleanup / cancel pending request. 203 */ 204 static void 205 post_unit_cleanup (void *cls, 206 const struct TALER_TESTING_Command *cmd) 207 { 208 struct PostUnitState *pus = cls; 209 210 if (NULL != pus->uph) 211 { 212 TALER_MERCHANT_post_private_units_cancel (pus->uph); 213 pus->uph = NULL; 214 } 215 if (NULL != pus->unit_name_long_i18n) 216 json_decref (pus->unit_name_long_i18n); 217 if (NULL != pus->unit_name_short_i18n) 218 json_decref (pus->unit_name_short_i18n); 219 GNUNET_free (pus); 220 } 221 222 223 struct TALER_TESTING_Command 224 TALER_TESTING_cmd_merchant_post_units (const char *label, 225 const char *merchant_url, 226 const char *unit_id, 227 const char *unit_name_long, 228 const char *unit_name_short, 229 bool unit_allow_fraction, 230 uint32_t unit_precision_level, 231 bool unit_active, 232 json_t *unit_name_long_i18n, 233 json_t *unit_name_short_i18n, 234 unsigned int http_status) 235 { 236 struct PostUnitState *pus; 237 238 pus = GNUNET_new (struct PostUnitState); 239 pus->merchant_url = merchant_url; 240 pus->unit_id = unit_id; 241 pus->unit_name_long = unit_name_long; 242 pus->unit_name_short = unit_name_short; 243 pus->unit_allow_fraction = unit_allow_fraction; 244 pus->unit_precision_level = unit_precision_level; 245 pus->unit_active = unit_active; 246 pus->unit_name_long_i18n = unit_name_long_i18n; 247 pus->unit_name_short_i18n = unit_name_short_i18n; 248 pus->http_status = http_status; 249 { 250 struct TALER_TESTING_Command cmd = { 251 .cls = pus, 252 .label = label, 253 .run = &post_unit_run, 254 .cleanup = &post_unit_cleanup, 255 .traits = &post_unit_traits 256 }; 257 258 return cmd; 259 } 260 } 261 262 263 /* end of testing_api_cmd_post_units.c */