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