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