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