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_webhook.c
21 : * @brief command to test GET /webhooks/$ID
22 : * @author Priscilla HUANG
23 : */
24 : #include "platform.h"
25 : struct GetWebhookState;
26 : #define TALER_MERCHANT_GET_PRIVATE_WEBHOOK_RESULT_CLOSURE struct GetWebhookState
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-webhooks-WEBHOOK_ID.h>
32 :
33 :
34 : /**
35 : * State of a "GET webhook" CMD.
36 : */
37 : struct GetWebhookState
38 : {
39 :
40 : /**
41 : * Handle for a "GET webhook" request.
42 : */
43 : struct TALER_MERCHANT_GetPrivateWebhookHandle *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 webhook to run GET for.
57 : */
58 : const char *webhook_id;
59 :
60 : /**
61 : * Reference for a POST or PATCH /webhooks CMD (optional).
62 : */
63 : const char *webhook_reference;
64 :
65 : /**
66 : * Expected HTTP response code.
67 : */
68 : unsigned int http_status;
69 :
70 : };
71 :
72 :
73 : /**
74 : * Callback for a /get/webhooks/$ID operation.
75 : *
76 : * @param cls closure for this function
77 : * @param wgr response details
78 : */
79 : static void
80 6 : get_webhook_cb (struct GetWebhookState *gis,
81 : const struct TALER_MERCHANT_GetPrivateWebhookResponse *wgr)
82 : {
83 : const struct TALER_TESTING_Command *webhook_cmd;
84 :
85 6 : gis->igh = NULL;
86 6 : if (gis->http_status != wgr->hr.http_status)
87 : {
88 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89 : "Unexpected response code %u (%d) to command %s\n",
90 : wgr->hr.http_status,
91 : (int) wgr->hr.ec,
92 : TALER_TESTING_interpreter_get_current_label (gis->is));
93 0 : TALER_TESTING_interpreter_fail (gis->is);
94 0 : return;
95 : }
96 6 : switch (wgr->hr.http_status)
97 : {
98 4 : case MHD_HTTP_OK:
99 : {
100 : const char *expected_event_type;
101 :
102 4 : webhook_cmd = TALER_TESTING_interpreter_lookup_command (
103 : gis->is,
104 : gis->webhook_reference);
105 4 : if (GNUNET_OK !=
106 4 : TALER_TESTING_get_trait_event_type (webhook_cmd,
107 : &expected_event_type))
108 : {
109 0 : TALER_TESTING_interpreter_fail (gis->is);
110 0 : return;
111 : }
112 4 : if (0 != strcmp (wgr->details.ok.event_type,
113 : expected_event_type))
114 : {
115 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 : "Event type does not match\n");
117 0 : TALER_TESTING_interpreter_fail (gis->is);
118 0 : return;
119 : }
120 : }
121 : {
122 : const char *expected_url;
123 :
124 4 : if (GNUNET_OK !=
125 4 : TALER_TESTING_get_trait_url (webhook_cmd,
126 : &expected_url))
127 : {
128 0 : TALER_TESTING_interpreter_fail (gis->is);
129 0 : return;
130 : }
131 4 : if (0 != strcmp (wgr->details.ok.url,
132 : expected_url))
133 : {
134 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135 : "URL does not match\n");
136 0 : TALER_TESTING_interpreter_fail (gis->is);
137 0 : return;
138 : }
139 : }
140 : {
141 : const char *expected_http_method;
142 :
143 4 : if (GNUNET_OK !=
144 4 : TALER_TESTING_get_trait_http_method (webhook_cmd,
145 : &expected_http_method))
146 : {
147 0 : TALER_TESTING_interpreter_fail (gis->is);
148 0 : return;
149 : }
150 4 : if (0 != strcmp (wgr->details.ok.http_method,
151 : expected_http_method))
152 : {
153 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 : "http_method does not match\n");
155 0 : TALER_TESTING_interpreter_fail (gis->is);
156 0 : return;
157 : }
158 : }
159 : {
160 : const char *expected_header_template;
161 :
162 4 : if (GNUNET_OK !=
163 4 : TALER_TESTING_get_trait_header_template (webhook_cmd,
164 : &expected_header_template))
165 : {
166 0 : TALER_TESTING_interpreter_fail (gis->is);
167 0 : return;
168 : }
169 4 : if ( ( (NULL == wgr->details.ok.header_template) && (NULL !=
170 : expected_header_template))
171 4 : ||
172 4 : ( (NULL != wgr->details.ok.header_template) && (NULL ==
173 : expected_header_template))
174 4 : ||
175 4 : ( (NULL != wgr->details.ok.header_template) &&
176 4 : (0 != strcmp (wgr->details.ok.header_template,
177 : expected_header_template)) ) )
178 : {
179 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180 : "header template does not match\n");
181 0 : TALER_TESTING_interpreter_fail (gis->is);
182 0 : return;
183 : }
184 : }
185 : {
186 : const char *expected_body_template;
187 :
188 4 : if (GNUNET_OK !=
189 4 : TALER_TESTING_get_trait_body_template (webhook_cmd,
190 : &expected_body_template))
191 : {
192 0 : TALER_TESTING_interpreter_fail (gis->is);
193 0 : return;
194 : }
195 4 : if ( ( (NULL == wgr->details.ok.body_template) && (NULL !=
196 : expected_body_template)
197 4 : ) ||
198 4 : ( (NULL != wgr->details.ok.body_template) && (NULL ==
199 : expected_body_template)
200 4 : ) ||
201 4 : ( (NULL != wgr->details.ok.body_template) &&
202 4 : (0 != strcmp (wgr->details.ok.body_template,
203 : expected_body_template)) ) )
204 : {
205 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 : "body template does not match\n");
207 0 : TALER_TESTING_interpreter_fail (gis->is);
208 0 : return;
209 : }
210 : }
211 4 : break;
212 0 : case MHD_HTTP_UNAUTHORIZED:
213 0 : break;
214 2 : case MHD_HTTP_NOT_FOUND:
215 2 : break;
216 0 : default:
217 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
218 : "Unhandled HTTP status.\n");
219 : }
220 6 : TALER_TESTING_interpreter_next (gis->is);
221 : }
222 :
223 :
224 : /**
225 : * Run the "GET webhook" CMD.
226 : *
227 : *
228 : * @param cls closure.
229 : * @param cmd command being run now.
230 : * @param is interpreter state.
231 : */
232 : static void
233 6 : get_webhook_run (void *cls,
234 : const struct TALER_TESTING_Command *cmd,
235 : struct TALER_TESTING_Interpreter *is)
236 : {
237 6 : struct GetWebhookState *gis = cls;
238 :
239 6 : gis->is = is;
240 6 : gis->igh = TALER_MERCHANT_get_private_webhook_create (
241 : TALER_TESTING_interpreter_get_context (is),
242 : gis->merchant_url,
243 : gis->webhook_id);
244 : {
245 : enum TALER_ErrorCode ec;
246 :
247 6 : ec = TALER_MERCHANT_get_private_webhook_start (
248 : gis->igh,
249 : &get_webhook_cb,
250 : gis);
251 6 : GNUNET_assert (TALER_EC_NONE == ec);
252 : }
253 6 : }
254 :
255 :
256 : /**
257 : * Free the state of a "GET webhook" CMD, and possibly
258 : * cancel a pending operation thereof.
259 : *
260 : * @param cls closure.
261 : * @param cmd command being run.
262 : */
263 : static void
264 6 : get_webhook_cleanup (void *cls,
265 : const struct TALER_TESTING_Command *cmd)
266 : {
267 6 : struct GetWebhookState *gis = cls;
268 :
269 6 : if (NULL != gis->igh)
270 : {
271 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
272 : "GET /webhooks/$ID operation did not complete\n");
273 0 : TALER_MERCHANT_get_private_webhook_cancel (gis->igh);
274 : }
275 6 : GNUNET_free (gis);
276 6 : }
277 :
278 :
279 : struct TALER_TESTING_Command
280 6 : TALER_TESTING_cmd_merchant_get_webhook (const char *label,
281 : const char *merchant_url,
282 : const char *webhook_id,
283 : unsigned int http_status,
284 : const char *webhook_reference)
285 : {
286 : struct GetWebhookState *gis;
287 :
288 6 : gis = GNUNET_new (struct GetWebhookState);
289 6 : gis->merchant_url = merchant_url;
290 6 : gis->webhook_id = webhook_id;
291 6 : gis->http_status = http_status;
292 6 : gis->webhook_reference = webhook_reference;
293 : {
294 6 : struct TALER_TESTING_Command cmd = {
295 : .cls = gis,
296 : .label = label,
297 : .run = &get_webhook_run,
298 : .cleanup = &get_webhook_cleanup
299 : };
300 :
301 6 : return cmd;
302 : }
303 : }
304 :
305 :
306 : /* end of testing_api_cmd_get_webhook.c */
|