testing_api_cmd_delete_instance.c (5742B)
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_instance.c 21 * @brief command to test DELETE /instance/$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-management-instances-INSTANCE.h> 30 31 32 /** 33 * State of a "DELETE /instances/$ID" CMD. 34 */ 35 struct DeleteInstanceState 36 { 37 38 /** 39 * Handle for a "DELETE instance" request. 40 */ 41 struct TALER_MERCHANT_DeleteManagementInstanceHandle *igh; 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 instance to run DELETE for. 55 */ 56 const char *instance_id; 57 58 /** 59 * Expected HTTP response code. 60 */ 61 unsigned int http_status; 62 63 /** 64 * Use purge, not delete. 65 */ 66 bool purge; 67 68 }; 69 70 71 /** 72 * Callback for a /delete/instances/$ID operation. 73 * 74 * @param cls closure for this function 75 * @param hr response being processed 76 */ 77 static void 78 delete_instance_cb (void *cls, 79 const struct 80 TALER_MERCHANT_DeleteManagementInstanceResponse *dir) 81 { 82 struct DeleteInstanceState *dis = cls; 83 84 dis->igh = NULL; 85 if (dis->http_status != dir->hr.http_status) 86 { 87 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 88 "Unexpected response code %u (%d) to command %s\n", 89 dir->hr.http_status, 90 (int) dir->hr.ec, 91 TALER_TESTING_interpreter_get_current_label (dis->is)); 92 TALER_TESTING_interpreter_fail (dis->is); 93 return; 94 } 95 switch (dir->hr.http_status) 96 { 97 case MHD_HTTP_NO_CONTENT: 98 break; 99 case MHD_HTTP_UNAUTHORIZED: 100 break; 101 case MHD_HTTP_NOT_FOUND: 102 break; 103 default: 104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 105 "Unhandled HTTP status %d for DELETE instance.\n", 106 dir->hr.http_status); 107 } 108 TALER_TESTING_interpreter_next (dis->is); 109 } 110 111 112 /** 113 * Run the "DELETE instance" CMD. 114 * 115 * @param cls closure. 116 * @param cmd command being run now. 117 * @param is interpreter state. 118 */ 119 static void 120 delete_instance_run (void *cls, 121 const struct TALER_TESTING_Command *cmd, 122 struct TALER_TESTING_Interpreter *is) 123 { 124 struct DeleteInstanceState *dis = cls; 125 126 dis->is = is; 127 dis->igh = TALER_MERCHANT_delete_management_instance_create ( 128 TALER_TESTING_interpreter_get_context (is), 129 dis->merchant_url, 130 dis->instance_id); 131 if (dis->purge) 132 TALER_MERCHANT_delete_management_instance_set_options ( 133 dis->igh, 134 { .option = TALER_MERCHANT_DELETE_MANAGEMENT_INSTANCE_OPTION_PURGE }); 135 { 136 enum TALER_ErrorCode ec; 137 138 ec = TALER_MERCHANT_delete_management_instance_start (dis->igh, 139 &delete_instance_cb, 140 dis); 141 GNUNET_assert (TALER_EC_NONE == ec); 142 } 143 } 144 145 146 /** 147 * Free the state of a "DELETE instance" CMD, and possibly 148 * cancel a pending operation thereof. 149 * 150 * @param cls closure. 151 * @param cmd command being run. 152 */ 153 static void 154 delete_instance_cleanup (void *cls, 155 const struct TALER_TESTING_Command *cmd) 156 { 157 struct DeleteInstanceState *dis = cls; 158 159 if (NULL != dis->igh) 160 { 161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 162 "DELETE /instances/$ID operation did not complete\n"); 163 TALER_MERCHANT_delete_management_instance_cancel (dis->igh); 164 } 165 GNUNET_free (dis); 166 } 167 168 169 struct TALER_TESTING_Command 170 TALER_TESTING_cmd_merchant_delete_instance (const char *label, 171 const char *merchant_url, 172 const char *instance_id, 173 unsigned int http_status) 174 { 175 struct DeleteInstanceState *dis; 176 177 dis = GNUNET_new (struct DeleteInstanceState); 178 dis->merchant_url = merchant_url; 179 dis->instance_id = instance_id; 180 dis->http_status = http_status; 181 { 182 struct TALER_TESTING_Command cmd = { 183 .cls = dis, 184 .label = label, 185 .run = &delete_instance_run, 186 .cleanup = &delete_instance_cleanup 187 }; 188 189 return cmd; 190 } 191 } 192 193 194 struct TALER_TESTING_Command 195 TALER_TESTING_cmd_merchant_purge_instance (const char *label, 196 const char *merchant_url, 197 const char *instance_id, 198 unsigned int http_status) 199 { 200 struct DeleteInstanceState *dis; 201 202 dis = GNUNET_new (struct DeleteInstanceState); 203 dis->merchant_url = merchant_url; 204 dis->instance_id = instance_id; 205 dis->http_status = http_status; 206 dis->purge = true; 207 { 208 struct TALER_TESTING_Command cmd = { 209 .cls = dis, 210 .label = label, 211 .run = &delete_instance_run, 212 .cleanup = &delete_instance_cleanup 213 }; 214 215 return cmd; 216 } 217 } 218 219 220 /* end of testing_api_cmd_delete_instance.c */