testing_api_cmd_delete_product.c (4787B)
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 * @file testing_api_cmd_delete_product.c 21 * @brief command to test DELETE /product/$ID 22 * @author Christian Grothoff 23 */ 24 #include "taler/platform.h" 25 #include <taler/taler_exchange_service.h> 26 #include <taler/taler_testing_lib.h> 27 #include "taler/taler_merchant_service.h" 28 #include "taler/taler_merchant_testing_lib.h" 29 #include <taler/taler-merchant/delete-private-products-PRODUCT_ID.h> 30 31 32 /** 33 * State of a "DELETE /products/$ID" CMD. 34 */ 35 struct DeleteProductState 36 { 37 38 /** 39 * Handle for a "DELETE product" request. 40 */ 41 struct TALER_MERCHANT_DeletePrivateProductHandle *pdh; 42 43 /** 44 * The interpreter state. 45 */ 46 struct TALER_TESTING_Interpreter *is; 47 48 /** 49 * Base URL of the merchant serving the request. 50 */ 51 const char *merchant_url; 52 53 /** 54 * ID of the product to run DELETE for. 55 */ 56 const char *product_id; 57 58 /** 59 * Expected HTTP response code. 60 */ 61 unsigned int http_status; 62 63 }; 64 65 66 /** 67 * Callback for a /delete/products/$ID operation. 68 * 69 * @param cls closure for this function 70 * @param hr response being processed 71 */ 72 static void 73 delete_product_cb (void *cls, 74 const struct TALER_MERCHANT_DeletePrivateProductResponse *dpr 75 ) 76 { 77 struct DeleteProductState *dis = cls; 78 79 dis->pdh = NULL; 80 if (dis->http_status != dpr->hr.http_status) 81 { 82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 83 "Unexpected response code %u (%d) to command %s\n", 84 dpr->hr.http_status, 85 (int) dpr->hr.ec, 86 TALER_TESTING_interpreter_get_current_label (dis->is)); 87 TALER_TESTING_interpreter_fail (dis->is); 88 return; 89 } 90 switch (dpr->hr.http_status) 91 { 92 case MHD_HTTP_NO_CONTENT: 93 break; 94 case MHD_HTTP_UNAUTHORIZED: 95 break; 96 case MHD_HTTP_NOT_FOUND: 97 break; 98 case MHD_HTTP_CONFLICT: 99 break; 100 default: 101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 102 "Unhandled HTTP status %u for DELETE product.\n", 103 dpr->hr.http_status); 104 } 105 TALER_TESTING_interpreter_next (dis->is); 106 } 107 108 109 /** 110 * Run the "DELETE product" CMD. 111 * 112 * 113 * @param cls closure. 114 * @param cmd command being run now. 115 * @param is interpreter state. 116 */ 117 static void 118 delete_product_run (void *cls, 119 const struct TALER_TESTING_Command *cmd, 120 struct TALER_TESTING_Interpreter *is) 121 { 122 struct DeleteProductState *dis = cls; 123 124 dis->is = is; 125 dis->pdh = TALER_MERCHANT_delete_private_product_create ( 126 TALER_TESTING_interpreter_get_context (is), 127 dis->merchant_url, 128 dis->product_id); 129 { 130 enum TALER_ErrorCode ec; 131 132 ec = TALER_MERCHANT_delete_private_product_start (dis->pdh, 133 &delete_product_cb, 134 dis); 135 GNUNET_assert (TALER_EC_NONE == ec); 136 } 137 } 138 139 140 /** 141 * Free the state of a "DELETE product" CMD, and possibly 142 * cancel a pending operation thereof. 143 * 144 * @param cls closure. 145 * @param cmd command being run. 146 */ 147 static void 148 delete_product_cleanup (void *cls, 149 const struct TALER_TESTING_Command *cmd) 150 { 151 struct DeleteProductState *dis = cls; 152 153 if (NULL != dis->pdh) 154 { 155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 156 "DELETE /products/$ID operation did not complete\n"); 157 TALER_MERCHANT_delete_private_product_cancel (dis->pdh); 158 } 159 GNUNET_free (dis); 160 } 161 162 163 struct TALER_TESTING_Command 164 TALER_TESTING_cmd_merchant_delete_product (const char *label, 165 const char *merchant_url, 166 const char *product_id, 167 unsigned int http_status) 168 { 169 struct DeleteProductState *dis; 170 171 dis = GNUNET_new (struct DeleteProductState); 172 dis->merchant_url = merchant_url; 173 dis->product_id = product_id; 174 dis->http_status = http_status; 175 { 176 struct TALER_TESTING_Command cmd = { 177 .cls = dis, 178 .label = label, 179 .run = &delete_product_run, 180 .cleanup = &delete_product_cleanup 181 }; 182 183 return cmd; 184 } 185 } 186 187 188 /* end of testing_api_cmd_delete_product.c */