testing_api_cmd_get_otp_device.c (5559B)
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 src/testing/testing_api_cmd_get_otp_device.c 21 * @brief command to test GET /otp-devices/$ID 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 struct GetOtpDeviceState; 26 #define TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE struct GetOtpDeviceState 27 #include <taler/taler_exchange_service.h> 28 #include <taler/taler_testing_lib.h> 29 #include "taler/taler_merchant_service.h" 30 #include "taler/taler_merchant_testing_lib.h" 31 #include <taler/merchant/get-private-otp-devices-DEVICE_ID.h> 32 33 34 /** 35 * State of a "GET OTP device" CMD. 36 */ 37 struct GetOtpDeviceState 38 { 39 40 /** 41 * Handle for a "GET /otp-device/$ID" request. 42 */ 43 struct TALER_MERCHANT_GetPrivateOtpDeviceHandle *igh; 44 45 /** 46 * The interpreter state. 47 */ 48 struct TALER_TESTING_Interpreter *is; 49 50 /** 51 * Base URL of the merchant serving the request. 52 */ 53 const char *merchant_url; 54 55 /** 56 * ID of the otp_device to run GET for. 57 */ 58 const char *otp_device_id; 59 60 /** 61 * Reference for a POST or PATCH /otp-devices CMD (optional). 62 */ 63 const char *otp_device_reference; 64 65 /** 66 * Expected HTTP response code. 67 */ 68 unsigned int http_status; 69 70 }; 71 72 73 /** 74 * Callback for a GET /otp-devices/$ID operation. 75 * 76 * @param cls closure for this function 77 * @param tgr HTTP response details 78 */ 79 static void 80 get_otp_device_cb (struct GetOtpDeviceState *gis, 81 const struct TALER_MERCHANT_GetPrivateOtpDeviceResponse *tgr) 82 { 83 const struct TALER_TESTING_Command *otp_device_cmd; 84 85 gis->igh = NULL; 86 if (gis->http_status != tgr->hr.http_status) 87 { 88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 89 "Unexpected response code %u (%d) to command %s\n", 90 tgr->hr.http_status, 91 (int) tgr->hr.ec, 92 TALER_TESTING_interpreter_get_current_label (gis->is)); 93 TALER_TESTING_interpreter_fail (gis->is); 94 return; 95 } 96 switch (tgr->hr.http_status) 97 { 98 case MHD_HTTP_OK: 99 { 100 const char *expected_description; 101 102 otp_device_cmd = TALER_TESTING_interpreter_lookup_command ( 103 gis->is, 104 gis->otp_device_reference); 105 if (GNUNET_OK != 106 TALER_TESTING_get_trait_otp_device_description (otp_device_cmd, 107 &expected_description) 108 ) 109 { 110 TALER_TESTING_interpreter_fail (gis->is); 111 return; 112 } 113 if (0 != strcmp (tgr->details.ok.otp_device_description, 114 expected_description)) 115 { 116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 117 "OtpDevice description does not match\n"); 118 TALER_TESTING_interpreter_fail (gis->is); 119 return; 120 } 121 } 122 break; 123 case MHD_HTTP_UNAUTHORIZED: 124 break; 125 case MHD_HTTP_NOT_FOUND: 126 break; 127 default: 128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 129 "Unhandled HTTP status.\n"); 130 } 131 TALER_TESTING_interpreter_next (gis->is); 132 } 133 134 135 /** 136 * Run the "GET /otp-device/$ID" CMD. 137 * 138 * 139 * @param cls closure. 140 * @param cmd command being run now. 141 * @param is interpreter state. 142 */ 143 static void 144 get_otp_device_run (void *cls, 145 const struct TALER_TESTING_Command *cmd, 146 struct TALER_TESTING_Interpreter *is) 147 { 148 struct GetOtpDeviceState *gis = cls; 149 150 gis->is = is; 151 gis->igh = TALER_MERCHANT_get_private_otp_device_create ( 152 TALER_TESTING_interpreter_get_context (is), 153 gis->merchant_url, 154 gis->otp_device_id); 155 { 156 enum TALER_ErrorCode ec; 157 158 ec = TALER_MERCHANT_get_private_otp_device_start ( 159 gis->igh, 160 &get_otp_device_cb, 161 gis); 162 GNUNET_assert (TALER_EC_NONE == ec); 163 } 164 } 165 166 167 /** 168 * Free the state of a "GET /otp-device/$ID" CMD, and possibly 169 * cancel a pending operation thereof. 170 * 171 * @param cls closure. 172 * @param cmd command being run. 173 */ 174 static void 175 get_otp_device_cleanup (void *cls, 176 const struct TALER_TESTING_Command *cmd) 177 { 178 struct GetOtpDeviceState *gis = cls; 179 180 if (NULL != gis->igh) 181 { 182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 183 "GET /otp-devices/$ID operation did not complete\n"); 184 TALER_MERCHANT_get_private_otp_device_cancel (gis->igh); 185 } 186 GNUNET_free (gis); 187 } 188 189 190 struct TALER_TESTING_Command 191 TALER_TESTING_cmd_merchant_get_otp_device ( 192 const char *label, 193 const char *merchant_url, 194 const char *otp_device_id, 195 unsigned int http_status, 196 const char *otp_device_reference) 197 { 198 struct GetOtpDeviceState *gis; 199 200 gis = GNUNET_new (struct GetOtpDeviceState); 201 gis->merchant_url = merchant_url; 202 gis->otp_device_id = otp_device_id; 203 gis->http_status = http_status; 204 gis->otp_device_reference = otp_device_reference; 205 { 206 struct TALER_TESTING_Command cmd = { 207 .cls = gis, 208 .label = label, 209 .run = &get_otp_device_run, 210 .cleanup = &get_otp_device_cleanup 211 }; 212 213 return cmd; 214 } 215 } 216 217 218 /* end of testing_api_cmd_get_otp_device.c */