testing_api_cmd_delete_template.c (4785B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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_template.c 21 * @brief command to test DELETE /templates/$ID 22 * @author Priscilla HUANG 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-templates-TEMPLATE_ID.h> 30 31 32 /** 33 * State of a "DELETE /templates/$ID" CMD. 34 */ 35 struct DeleteTemplateState 36 { 37 38 /** 39 * Handle for a "DELETE template" request. 40 */ 41 struct TALER_MERCHANT_DeletePrivateTemplateHandle *tdh; 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 template to run DELETE for. 55 */ 56 const char *template_id; 57 58 /** 59 * Expected HTTP response code. 60 */ 61 unsigned int http_status; 62 63 }; 64 65 66 /** 67 * Callback for a /delete/templates/$ID operation. 68 * 69 * @param cls closure for this function 70 * @param hr response being processed 71 */ 72 static void 73 delete_template_cb (void *cls, 74 const struct TALER_MERCHANT_DeletePrivateTemplateResponse * 75 dtr) 76 { 77 struct DeleteTemplateState *dis = cls; 78 79 dis->tdh = NULL; 80 if (dis->http_status != dtr->hr.http_status) 81 { 82 TALER_TESTING_unexpected_status_with_body (dis->is, 83 dtr->hr.http_status, 84 dis->http_status, 85 dtr->hr.reply); 86 return; 87 } 88 switch (dtr->hr.http_status) 89 { 90 case MHD_HTTP_NO_CONTENT: 91 break; 92 case MHD_HTTP_UNAUTHORIZED: 93 break; 94 case MHD_HTTP_NOT_FOUND: 95 break; 96 case MHD_HTTP_CONFLICT: 97 break; 98 default: 99 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 100 "Unhandled HTTP status %u for DELETE template.\n", 101 dtr->hr.http_status); 102 } 103 TALER_TESTING_interpreter_next (dis->is); 104 } 105 106 107 /** 108 * Run the "DELETE template" CMD. 109 * 110 * 111 * @param cls closure. 112 * @param cmd command being run now. 113 * @param is interpreter state. 114 */ 115 static void 116 delete_template_run (void *cls, 117 const struct TALER_TESTING_Command *cmd, 118 struct TALER_TESTING_Interpreter *is) 119 { 120 struct DeleteTemplateState *dis = cls; 121 122 dis->is = is; 123 dis->tdh = TALER_MERCHANT_delete_private_template_create ( 124 TALER_TESTING_interpreter_get_context (is), 125 dis->merchant_url, 126 dis->template_id); 127 { 128 enum TALER_ErrorCode ec; 129 130 ec = TALER_MERCHANT_delete_private_template_start (dis->tdh, 131 &delete_template_cb, 132 dis); 133 GNUNET_assert (TALER_EC_NONE == ec); 134 } 135 } 136 137 138 /** 139 * Free the state of a "DELETE template" CMD, and possibly 140 * cancel a pending operation thereof. 141 * 142 * @param cls closure. 143 * @param cmd command being run. 144 */ 145 static void 146 delete_template_cleanup (void *cls, 147 const struct TALER_TESTING_Command *cmd) 148 { 149 struct DeleteTemplateState *dis = cls; 150 151 if (NULL != dis->tdh) 152 { 153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 154 "DELETE /templates/$ID operation did not complete\n"); 155 TALER_MERCHANT_delete_private_template_cancel (dis->tdh); 156 } 157 GNUNET_free (dis); 158 } 159 160 161 struct TALER_TESTING_Command 162 TALER_TESTING_cmd_merchant_delete_template (const char *label, 163 const char *merchant_url, 164 const char *template_id, 165 unsigned int http_status) 166 { 167 struct DeleteTemplateState *dis; 168 169 dis = GNUNET_new (struct DeleteTemplateState); 170 dis->merchant_url = merchant_url; 171 dis->template_id = template_id; 172 dis->http_status = http_status; 173 { 174 struct TALER_TESTING_Command cmd = { 175 .cls = dis, 176 .label = label, 177 .run = &delete_template_run, 178 .cleanup = &delete_template_cleanup 179 }; 180 181 return cmd; 182 } 183 } 184 185 186 /* end of testing_api_cmd_delete_template.c */