testing_api_cmd_forget_order.c (6674B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020 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 /** 21 * @file src/testing/testing_api_cmd_forget_order.c 22 * @brief command to forget fields of an order 23 * @author Jonathan Buchanan 24 */ 25 #include "platform.h" 26 struct OrderForgetState; 27 #define TALER_MERCHANT_PATCH_PRIVATE_ORDERS_FORGET_RESULT_CLOSURE struct OrderForgetState 28 #include <taler/taler_exchange_service.h> 29 #include <taler/taler_testing_lib.h> 30 #include "taler/taler_merchant_service.h" 31 #include "taler/taler_merchant_testing_lib.h" 32 #include <taler/merchant/patch-private-orders-ORDER_ID-forget.h> 33 34 35 /** 36 * State for a "order forget" CMD. 37 */ 38 struct OrderForgetState 39 { 40 /** 41 * The interpreter state. 42 */ 43 struct TALER_TESTING_Interpreter *is; 44 45 /** 46 * URL of the merchant backend. 47 */ 48 const char *merchant_url; 49 50 /** 51 * Expected status code. 52 */ 53 unsigned int http_status; 54 55 /** 56 * PATCH /orders/$ORDER_ID/forget operation handle. 57 */ 58 struct TALER_MERCHANT_PatchPrivateOrdersForgetHandle *ofh; 59 60 /** 61 * Reference to a order operation. 62 */ 63 const char *order_reference; 64 65 /** 66 * Order id to forget for. If NULL, the @a order_reference 67 * will offer this value. 68 */ 69 const char *order_id; 70 71 /** 72 * The list of paths to forget in the contract terms. 73 */ 74 const char **paths; 75 76 /** 77 * The length of @e paths. 78 */ 79 unsigned int paths_length; 80 }; 81 82 83 /** 84 * Free the state of a "order forget" CMD, and possibly 85 * cancel it if it did not complete. 86 * 87 * @param cls closure. 88 * @param cmd command being freed. 89 */ 90 static void 91 order_forget_cleanup (void *cls, 92 const struct TALER_TESTING_Command *cmd) 93 { 94 struct OrderForgetState *ofs = cls; 95 96 if (NULL != ofs->ofh) 97 { 98 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 99 "Command '%s' did not complete\n", 100 cmd->label); 101 TALER_MERCHANT_patch_private_orders_forget_cancel (ofs->ofh); 102 ofs->ofh = NULL; 103 } 104 GNUNET_array_grow (ofs->paths, 105 ofs->paths_length, 106 0); 107 GNUNET_free (ofs); 108 } 109 110 111 /** 112 * Callback for "order forget" operation, to check the 113 * response code is as expected. 114 * 115 * @param cls closure 116 * @param result HTTP response we got 117 */ 118 static void 119 order_forget_cb (struct OrderForgetState *ofs, 120 const struct TALER_MERCHANT_PatchPrivateOrdersForgetResponse * 121 result) 122 { 123 const struct TALER_MERCHANT_HttpResponse *hr = &result->hr; 124 125 ofs->ofh = NULL; 126 if (ofs->http_status != hr->http_status) 127 { 128 TALER_TESTING_unexpected_status_with_body (ofs->is, 129 hr->http_status, 130 ofs->http_status, 131 hr->reply); 132 return; 133 } 134 TALER_TESTING_interpreter_next (ofs->is); 135 } 136 137 138 /** 139 * Run the "order forget" CMD. 140 * 141 * @param cls closure. 142 * @param cmd command currently being run. 143 * @param is interpreter state. 144 */ 145 static void 146 order_forget_run (void *cls, 147 const struct TALER_TESTING_Command *cmd, 148 struct TALER_TESTING_Interpreter *is) 149 { 150 struct OrderForgetState *ofs = cls; 151 const char *order_id; 152 153 ofs->is = is; 154 if (NULL != ofs->order_id) 155 { 156 order_id = ofs->order_id; 157 } 158 else 159 { 160 const struct TALER_TESTING_Command *order_cmd; 161 162 order_cmd 163 = TALER_TESTING_interpreter_lookup_command (is, 164 ofs->order_reference); 165 if (NULL == order_cmd) 166 TALER_TESTING_FAIL (is); 167 if (GNUNET_OK != 168 TALER_TESTING_get_trait_order_id (order_cmd, 169 &order_id)) 170 TALER_TESTING_FAIL (is); 171 } 172 ofs->ofh = TALER_MERCHANT_patch_private_orders_forget_create ( 173 TALER_TESTING_interpreter_get_context (is), 174 ofs->merchant_url, 175 order_id, 176 ofs->paths_length, 177 ofs->paths); 178 GNUNET_assert (NULL != ofs->ofh); 179 { 180 enum TALER_ErrorCode ec; 181 182 ec = TALER_MERCHANT_patch_private_orders_forget_start ( 183 ofs->ofh, 184 &order_forget_cb, 185 ofs); 186 GNUNET_assert (TALER_EC_NONE == ec); 187 } 188 } 189 190 191 /** 192 * Offer internal data to other commands. 193 * 194 * @param cls closure 195 * @param[out] ret result (could be anything) 196 * @param trait name of the trait 197 * @param index index number of the object to extract. 198 * @return #GNUNET_OK on success 199 */ 200 static enum GNUNET_GenericReturnValue 201 order_forget_traits (void *cls, 202 const void **ret, 203 const char *trait, 204 unsigned int index) 205 { 206 struct OrderForgetState *ofs = cls; 207 struct TALER_TESTING_Trait traits[ofs->paths_length + 2]; 208 209 traits[0] = TALER_TESTING_make_trait_paths_length (&ofs->paths_length); 210 for (unsigned int i = 0; i < ofs->paths_length; ++i) 211 traits[i + 1] = TALER_TESTING_make_trait_paths (i, 212 ofs->paths[i]); 213 traits[ofs->paths_length + 1] = TALER_TESTING_trait_end (); 214 215 return TALER_TESTING_get_trait (traits, 216 ret, 217 trait, 218 index); 219 } 220 221 222 struct TALER_TESTING_Command 223 TALER_TESTING_cmd_merchant_forget_order ( 224 const char *label, 225 const char *merchant_url, 226 unsigned int http_status, 227 const char *order_reference, 228 const char *order_id, 229 ...) 230 { 231 struct OrderForgetState *ofs; 232 233 ofs = GNUNET_new (struct OrderForgetState); 234 ofs->http_status = http_status; 235 ofs->order_reference = order_reference; 236 ofs->merchant_url = merchant_url; 237 ofs->order_id = order_id; 238 { 239 const char *path; 240 va_list ap; 241 242 va_start (ap, order_id); 243 while (NULL != (path = va_arg (ap, const char *))) 244 { 245 GNUNET_array_append (ofs->paths, 246 ofs->paths_length, 247 path); 248 } 249 va_end (ap); 250 } 251 { 252 struct TALER_TESTING_Command cmd = { 253 .cls = ofs, 254 .label = label, 255 .run = &order_forget_run, 256 .cleanup = &order_forget_cleanup, 257 .traits = &order_forget_traits 258 }; 259 260 return cmd; 261 } 262 }