testing_api_cmd_get_template.c (6708B)
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_template.c 21 * @brief command to test GET /templates/$ID 22 * @author Priscilla HUANG 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 30 31 /** 32 * State of a "GET template" CMD. 33 */ 34 struct GetTemplateState 35 { 36 37 /** 38 * Handle for a "GET template" request. 39 */ 40 struct TALER_MERCHANT_TemplateGetHandle *igh; 41 42 /** 43 * The interpreter state. 44 */ 45 struct TALER_TESTING_Interpreter *is; 46 47 /** 48 * Base URL of the merchant serving the request. 49 */ 50 const char *merchant_url; 51 52 /** 53 * ID of the template to run GET for. 54 */ 55 const char *template_id; 56 57 /** 58 * Reference for a POST or PATCH /templates CMD (optional). 59 */ 60 const char *template_reference; 61 62 /** 63 * Expected HTTP response code. 64 */ 65 unsigned int http_status; 66 67 }; 68 69 70 /** 71 * Callback for a /get/templates/$ID operation. 72 * 73 * @param cls closure for this function 74 * @param tgr HTTP response details 75 */ 76 static void 77 get_template_cb (void *cls, 78 const struct TALER_MERCHANT_TemplateGetResponse *tgr) 79 { 80 struct GetTemplateState *gis = cls; 81 const struct TALER_TESTING_Command *template_cmd; 82 83 gis->igh = NULL; 84 if (gis->http_status != tgr->hr.http_status) 85 { 86 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 87 "Unexpected response code %u (%d) to command %s\n", 88 tgr->hr.http_status, 89 (int) tgr->hr.ec, 90 TALER_TESTING_interpreter_get_current_label (gis->is)); 91 TALER_TESTING_interpreter_fail (gis->is); 92 return; 93 } 94 switch (tgr->hr.http_status) 95 { 96 case MHD_HTTP_OK: 97 { 98 const char *expected_description; 99 100 template_cmd = TALER_TESTING_interpreter_lookup_command ( 101 gis->is, 102 gis->template_reference); 103 if (GNUNET_OK != 104 TALER_TESTING_get_trait_template_description (template_cmd, 105 &expected_description)) 106 TALER_TESTING_interpreter_fail (gis->is); 107 if (0 != strcmp (tgr->details.ok.template_description, 108 expected_description)) 109 { 110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 111 "Template description does not match\n"); 112 TALER_TESTING_interpreter_fail (gis->is); 113 return; 114 } 115 } 116 { 117 const char *expected_otp_id; 118 119 if (GNUNET_OK != 120 TALER_TESTING_get_trait_otp_id (template_cmd, 121 &expected_otp_id)) 122 TALER_TESTING_interpreter_fail (gis->is); 123 if ( ( (NULL == tgr->details.ok.otp_id) && (NULL != expected_otp_id)) || 124 ( (NULL != tgr->details.ok.otp_id) && (NULL == expected_otp_id)) || 125 ( (NULL != tgr->details.ok.otp_id) && 126 (0 != strcmp (tgr->details.ok.otp_id, 127 expected_otp_id)) ) ) 128 { 129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 130 "Template pos_key `%s' does not match `%s'\n", 131 tgr->details.ok.otp_id, 132 expected_otp_id); 133 TALER_TESTING_interpreter_fail (gis->is); 134 return; 135 } 136 } 137 { 138 const json_t *expected_template_contract; 139 140 if (GNUNET_OK != 141 TALER_TESTING_get_trait_template_contract (template_cmd, 142 &expected_template_contract 143 )) 144 TALER_TESTING_interpreter_fail (gis->is); 145 if (1 != json_equal (tgr->details.ok.template_contract, 146 expected_template_contract)) 147 { 148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 149 "Template contract does not match\n"); 150 TALER_TESTING_interpreter_fail (gis->is); 151 return; 152 } 153 } 154 break; 155 case MHD_HTTP_UNAUTHORIZED: 156 break; 157 case MHD_HTTP_NOT_FOUND: 158 break; 159 default: 160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 161 "Unhandled HTTP status.\n"); 162 } 163 TALER_TESTING_interpreter_next (gis->is); 164 } 165 166 167 /** 168 * Run the "GET template" CMD. 169 * 170 * 171 * @param cls closure. 172 * @param cmd command being run now. 173 * @param is interpreter state. 174 */ 175 static void 176 get_template_run (void *cls, 177 const struct TALER_TESTING_Command *cmd, 178 struct TALER_TESTING_Interpreter *is) 179 { 180 struct GetTemplateState *gis = cls; 181 182 gis->is = is; 183 gis->igh = TALER_MERCHANT_template_get ( 184 TALER_TESTING_interpreter_get_context (is), 185 gis->merchant_url, 186 gis->template_id, 187 &get_template_cb, 188 gis); 189 GNUNET_assert (NULL != gis->igh); 190 } 191 192 193 /** 194 * Free the state of a "GET template" CMD, and possibly 195 * cancel a pending operation thereof. 196 * 197 * @param cls closure. 198 * @param cmd command being run. 199 */ 200 static void 201 get_template_cleanup (void *cls, 202 const struct TALER_TESTING_Command *cmd) 203 { 204 struct GetTemplateState *gis = cls; 205 206 if (NULL != gis->igh) 207 { 208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 209 "GET /templates/$ID operation did not complete\n"); 210 TALER_MERCHANT_template_get_cancel (gis->igh); 211 } 212 GNUNET_free (gis); 213 } 214 215 216 struct TALER_TESTING_Command 217 TALER_TESTING_cmd_merchant_get_template (const char *label, 218 const char *merchant_url, 219 const char *template_id, 220 unsigned int http_status, 221 const char *template_reference) 222 { 223 struct GetTemplateState *gis; 224 225 gis = GNUNET_new (struct GetTemplateState); 226 gis->merchant_url = merchant_url; 227 gis->template_id = template_id; 228 gis->http_status = http_status; 229 gis->template_reference = template_reference; 230 { 231 struct TALER_TESTING_Command cmd = { 232 .cls = gis, 233 .label = label, 234 .run = &get_template_run, 235 .cleanup = &get_template_cleanup 236 }; 237 238 return cmd; 239 } 240 } 241 242 243 /* end of testing_api_cmd_get_template.c */