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