testing_api_cmd_instance_auth.c (6127B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2021 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_instance_auth.c 21 * @brief command to test /private/auth POSTing 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 struct AuthInstanceState; 26 #define TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_AUTH_RESULT_CLOSURE struct AuthInstanceState 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/post-management-instances-INSTANCE-auth.h> 32 33 34 /** 35 * State of a "POST /instances/$ID/private/auth" CMD. 36 */ 37 struct AuthInstanceState 38 { 39 40 /** 41 * Handle for a "POST auth" request. 42 */ 43 struct TALER_MERCHANT_PostManagementInstancesAuthHandle *iaph; 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 instance to run GET for. 57 */ 58 const char *instance_id; 59 60 /** 61 * Desired token. Can be NULL 62 */ 63 const char *auth_token; 64 65 /** 66 * Expected HTTP response code. 67 */ 68 unsigned int http_status; 69 70 }; 71 72 73 /** 74 * Callback for a POST /instances/$ID/private/auth operation. 75 * 76 * @param cls closure for this function 77 * @param iar response being processed 78 */ 79 static void 80 auth_instance_cb (struct AuthInstanceState *ais, 81 const struct 82 TALER_MERCHANT_PostManagementInstancesAuthResponse *iar) 83 { 84 85 ais->iaph = NULL; 86 if (ais->http_status != iar->hr.http_status) 87 { 88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 89 "Unexpected response code %u (%d) to command %s\n", 90 iar->hr.http_status, 91 (int) iar->hr.ec, 92 TALER_TESTING_interpreter_get_current_label (ais->is)); 93 TALER_TESTING_interpreter_fail (ais->is); 94 return; 95 } 96 switch (iar->hr.http_status) 97 { 98 case MHD_HTTP_NO_CONTENT: 99 break; 100 case MHD_HTTP_BAD_REQUEST: 101 /* likely invalid auth_token value, we do not check client-side */ 102 break; 103 case MHD_HTTP_FORBIDDEN: 104 break; 105 default: 106 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 107 "Unhandled HTTP status %u (%d) returned from /private/auth operation.\n", 108 iar->hr.http_status, 109 iar->hr.ec); 110 } 111 TALER_TESTING_interpreter_next (ais->is); 112 } 113 114 115 /** 116 * Run the "AUTH /instances/$ID" CMD. 117 * 118 * 119 * @param cls closure. 120 * @param cmd command being run now. 121 * @param is interpreter state. 122 */ 123 static void 124 auth_instance_run (void *cls, 125 const struct TALER_TESTING_Command *cmd, 126 struct TALER_TESTING_Interpreter *is) 127 { 128 struct AuthInstanceState *ais = cls; 129 130 ais->is = is; 131 ais->iaph = TALER_MERCHANT_post_management_instances_auth_create ( 132 TALER_TESTING_interpreter_get_context (is), 133 ais->merchant_url, 134 ais->instance_id); 135 TALER_MERCHANT_post_management_instances_auth_set_options ( 136 ais->iaph, 137 TALER_MERCHANT_post_management_instances_auth_option_password (ais->auth_token)); 138 { 139 enum TALER_ErrorCode ec; 140 141 ec = TALER_MERCHANT_post_management_instances_auth_start ( 142 ais->iaph, 143 &auth_instance_cb, 144 ais); 145 GNUNET_assert (TALER_EC_NONE == ec); 146 } 147 } 148 149 150 /** 151 * Free the state of a "POST instance auth" CMD, and possibly 152 * cancel a pending operation thereof. 153 * 154 * @param cls closure. 155 * @param cmd command being run. 156 */ 157 static void 158 auth_instance_cleanup (void *cls, 159 const struct TALER_TESTING_Command *cmd) 160 { 161 struct AuthInstanceState *ais = cls; 162 163 if (NULL != ais->iaph) 164 { 165 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 166 "POST /instance/$ID/auth operation did not complete\n"); 167 TALER_MERCHANT_post_management_instances_auth_cancel (ais->iaph); 168 } 169 GNUNET_free (ais); 170 } 171 172 173 /** 174 * Offer internal data to other commands. 175 * 176 * @param cls closure 177 * @param[out] ret result (could be anything) 178 * @param trait name of the trait 179 * @param index index number of the object to extract. 180 * @return #GNUNET_OK on success 181 */ 182 static enum GNUNET_GenericReturnValue 183 auth_instance_traits (void *cls, 184 const void **ret, 185 const char *trait, 186 unsigned int index) 187 { 188 struct AuthInstanceState *ais = cls; 189 struct TALER_TESTING_Trait traits[] = { 190 TALER_TESTING_make_trait_auth_token (ais->auth_token), 191 TALER_TESTING_trait_end () 192 }; 193 194 return TALER_TESTING_get_trait (traits, 195 ret, 196 trait, 197 index); 198 } 199 200 201 struct TALER_TESTING_Command 202 TALER_TESTING_cmd_merchant_post_instance_auth (const char *label, 203 const char *merchant_url, 204 const char *instance_id, 205 const char *auth_token, 206 unsigned int http_status) 207 { 208 struct AuthInstanceState *ais; 209 210 ais = GNUNET_new (struct AuthInstanceState); 211 ais->merchant_url = merchant_url; 212 ais->instance_id = instance_id; 213 ais->auth_token = auth_token; 214 ais->http_status = http_status; 215 216 { 217 struct TALER_TESTING_Command cmd = { 218 .cls = ais, 219 .label = label, 220 .run = &auth_instance_run, 221 .cleanup = &auth_instance_cleanup, 222 .traits = &auth_instance_traits 223 }; 224 225 return cmd; 226 } 227 } 228 229 230 /* end of testing_api_cmd_auth_instance.c */