testing_api_cmd_delete_otp_device.c (4913B)
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_otp_device.c 21 * @brief command to test DELETE /otp-devices/$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-otp-devices-DEVICE_ID.h> 30 31 32 /** 33 * State of a "DELETE /otp-devices/$ID" CMD. 34 */ 35 struct DeleteOtpDeviceState 36 { 37 38 /** 39 * Handle for a "DELETE otp_device" request. 40 */ 41 struct TALER_MERCHANT_DeletePrivateOtpDeviceHandle *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 otp_device to run DELETE for. 55 */ 56 const char *otp_device_id; 57 58 /** 59 * Expected HTTP response code. 60 */ 61 unsigned int http_status; 62 63 }; 64 65 66 /** 67 * Callback for a /delete/otp-devices/$ID operation. 68 * 69 * @param cls closure for this function 70 * @param hr response being processed 71 */ 72 static void 73 delete_otp_device_cb (void *cls, 74 const struct 75 TALER_MERCHANT_DeletePrivateOtpDeviceResponse *dor) 76 { 77 struct DeleteOtpDeviceState *dis = cls; 78 79 dis->tdh = NULL; 80 if (dis->http_status != dor->hr.http_status) 81 { 82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 83 "Unexpected response code %u (%d) to command %s\n", 84 dor->hr.http_status, 85 (int) dor->hr.ec, 86 TALER_TESTING_interpreter_get_current_label (dis->is)); 87 TALER_TESTING_interpreter_fail (dis->is); 88 return; 89 } 90 switch (dor->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 otp_device.\n", 103 dor->hr.http_status); 104 } 105 TALER_TESTING_interpreter_next (dis->is); 106 } 107 108 109 /** 110 * Run the "DELETE otp_device" 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_otp_device_run (void *cls, 119 const struct TALER_TESTING_Command *cmd, 120 struct TALER_TESTING_Interpreter *is) 121 { 122 struct DeleteOtpDeviceState *dis = cls; 123 124 dis->is = is; 125 dis->tdh = TALER_MERCHANT_delete_private_otp_device_create ( 126 TALER_TESTING_interpreter_get_context (is), 127 dis->merchant_url, 128 dis->otp_device_id); 129 { 130 enum TALER_ErrorCode ec; 131 132 ec = TALER_MERCHANT_delete_private_otp_device_start (dis->tdh, 133 &delete_otp_device_cb, 134 dis); 135 GNUNET_assert (TALER_EC_NONE == ec); 136 } 137 } 138 139 140 /** 141 * Free the state of a "DELETE otp_device" 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_otp_device_cleanup (void *cls, 149 const struct TALER_TESTING_Command *cmd) 150 { 151 struct DeleteOtpDeviceState *dis = cls; 152 153 if (NULL != dis->tdh) 154 { 155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 156 "DELETE /otp-devices/$ID operation did not complete\n"); 157 TALER_MERCHANT_delete_private_otp_device_cancel (dis->tdh); 158 } 159 GNUNET_free (dis); 160 } 161 162 163 struct TALER_TESTING_Command 164 TALER_TESTING_cmd_merchant_delete_otp_device (const char *label, 165 const char *merchant_url, 166 const char *otp_device_id, 167 unsigned int http_status) 168 { 169 struct DeleteOtpDeviceState *dis; 170 171 dis = GNUNET_new (struct DeleteOtpDeviceState); 172 dis->merchant_url = merchant_url; 173 dis->otp_device_id = otp_device_id; 174 dis->http_status = http_status; 175 { 176 struct TALER_TESTING_Command cmd = { 177 .cls = dis, 178 .label = label, 179 .run = &delete_otp_device_run, 180 .cleanup = &delete_otp_device_cleanup 181 }; 182 183 return cmd; 184 } 185 } 186 187 188 /* end of testing_api_cmd_delete_otp_device.c */