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_post_webhooks.c
21 : * @brief command to test POST /webhooks
22 : * @author Priscilla HUANG
23 : */
24 : #include "platform.h"
25 : struct PostWebhooksState;
26 : #define TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_RESULT_CLOSURE struct PostWebhooksState
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-private-webhooks.h>
32 :
33 :
34 : /**
35 : * State of a "POST /webhooks" CMD.
36 : */
37 : struct PostWebhooksState
38 : {
39 :
40 : /**
41 : * Handle for a "GET webhook" request.
42 : */
43 : struct TALER_MERCHANT_PostPrivateWebhooksHandle *iph;
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 POST for.
57 : */
58 : const char *webhook_id;
59 :
60 : /**
61 : * event of the webhook
62 : */
63 : const char *event_type;
64 :
65 : /**
66 : * url use by the customer
67 : */
68 : const char *url;
69 :
70 : /**
71 : * http_method use by the merchant
72 : */
73 : const char *http_method;
74 :
75 : /**
76 : * header of the webhook
77 : */
78 : const char *header_template;
79 :
80 : /**
81 : * body of the webhook
82 : */
83 : const char *body_template;
84 :
85 : /**
86 : * Expected HTTP response code.
87 : */
88 : unsigned int http_status;
89 :
90 : };
91 :
92 :
93 : /**
94 : * Callback for a POST /webhooks operation.
95 : *
96 : * @param cls closure for this function
97 : * @param wpr response being processed
98 : */
99 : static void
100 18 : post_webhooks_cb (struct PostWebhooksState *wis,
101 : const struct TALER_MERCHANT_PostPrivateWebhooksResponse *wpr)
102 : {
103 :
104 18 : wis->iph = NULL;
105 18 : if (wis->http_status != wpr->hr.http_status)
106 : {
107 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108 : "Unexpected response code %u (%d) to command %s\n",
109 : wpr->hr.http_status,
110 : (int) wpr->hr.ec,
111 : TALER_TESTING_interpreter_get_current_label (wis->is));
112 0 : TALER_TESTING_interpreter_fail (wis->is);
113 0 : return;
114 : }
115 18 : switch (wpr->hr.http_status)
116 : {
117 16 : case MHD_HTTP_NO_CONTENT:
118 16 : break;
119 0 : case MHD_HTTP_UNAUTHORIZED:
120 0 : break;
121 0 : case MHD_HTTP_FORBIDDEN:
122 0 : break;
123 0 : case MHD_HTTP_NOT_FOUND:
124 0 : break;
125 2 : case MHD_HTTP_CONFLICT:
126 2 : break;
127 0 : default:
128 0 : GNUNET_break (0);
129 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
130 : "Unhandled HTTP status %u for POST /templates.\n",
131 : wpr->hr.http_status);
132 : }
133 18 : TALER_TESTING_interpreter_next (wis->is);
134 : }
135 :
136 :
137 : /**
138 : * Run the "POST /webhooks" CMD.
139 : *
140 : *
141 : * @param cls closure.
142 : * @param cmd command being run now.
143 : * @param is interpreter state.
144 : */
145 : static void
146 18 : post_webhooks_run (void *cls,
147 : const struct TALER_TESTING_Command *cmd,
148 : struct TALER_TESTING_Interpreter *is)
149 : {
150 18 : struct PostWebhooksState *wis = cls;
151 :
152 18 : wis->is = is;
153 18 : wis->iph = TALER_MERCHANT_post_private_webhooks_create (
154 : TALER_TESTING_interpreter_get_context (is),
155 : wis->merchant_url,
156 : wis->webhook_id,
157 : wis->event_type,
158 : wis->url,
159 : wis->http_method);
160 18 : if (NULL != wis->header_template)
161 18 : TALER_MERCHANT_post_private_webhooks_set_options (
162 : wis->iph,
163 : TALER_MERCHANT_post_private_webhooks_option_header_template (
164 : wis->header_template));
165 18 : if (NULL != wis->body_template)
166 18 : TALER_MERCHANT_post_private_webhooks_set_options (
167 : wis->iph,
168 : TALER_MERCHANT_post_private_webhooks_option_body_template (
169 : wis->body_template));
170 : {
171 : enum TALER_ErrorCode ec;
172 :
173 18 : ec = TALER_MERCHANT_post_private_webhooks_start (
174 : wis->iph,
175 : &post_webhooks_cb,
176 : wis);
177 18 : GNUNET_assert (TALER_EC_NONE == ec);
178 : }
179 18 : }
180 :
181 :
182 : /**
183 : * Offers information from the POST /webhooks CMD state to other
184 : * commands.
185 : *
186 : * @param cls closure
187 : * @param[out] ret result (could be anything)
188 : * @param trait name of the trait
189 : * @param index index number of the object to extract.
190 : * @return #GNUNET_OK on success
191 : */
192 : static int
193 12 : post_webhooks_traits (void *cls,
194 : const void **ret,
195 : const char *trait,
196 : unsigned int index)
197 : {
198 12 : struct PostWebhooksState *pws = cls;
199 : struct TALER_TESTING_Trait traits[] = {
200 12 : TALER_TESTING_make_trait_event_type (pws->event_type),
201 12 : TALER_TESTING_make_trait_url (pws->url),
202 12 : TALER_TESTING_make_trait_http_method (pws->http_method),
203 12 : TALER_TESTING_make_trait_header_template (pws->header_template),
204 12 : TALER_TESTING_make_trait_body_template (pws->body_template),
205 12 : TALER_TESTING_make_trait_webhook_id (pws->webhook_id),
206 12 : TALER_TESTING_trait_end (),
207 : };
208 :
209 12 : return TALER_TESTING_get_trait (traits,
210 : ret,
211 : trait,
212 : index);
213 : }
214 :
215 :
216 : /**
217 : * Free the state of a "POST webhook" CMD, and possibly
218 : * cancel a pending operation thereof.
219 : *
220 : * @param cls closure.
221 : * @param cmd command being run.
222 : */
223 : static void
224 18 : post_webhooks_cleanup (void *cls,
225 : const struct TALER_TESTING_Command *cmd)
226 : {
227 18 : struct PostWebhooksState *wis = cls;
228 :
229 18 : if (NULL != wis->iph)
230 : {
231 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 : "POST /webhooks operation did not complete\n");
233 0 : TALER_MERCHANT_post_private_webhooks_cancel (wis->iph);
234 : }
235 18 : GNUNET_free (wis);
236 18 : }
237 :
238 :
239 : struct TALER_TESTING_Command
240 18 : TALER_TESTING_cmd_merchant_post_webhooks2 (
241 : const char *label,
242 : const char *merchant_url,
243 : const char *webhook_id,
244 : const char *event_type,
245 : const char *url,
246 : const char *http_method,
247 : const char *header_template,
248 : const char *body_template,
249 : unsigned int http_status)
250 : {
251 : struct PostWebhooksState *wis;
252 :
253 18 : wis = GNUNET_new (struct PostWebhooksState);
254 18 : wis->merchant_url = merchant_url;
255 18 : wis->webhook_id = webhook_id;
256 18 : wis->http_status = http_status;
257 18 : wis->event_type = event_type;
258 18 : wis->url = url;
259 18 : wis->http_method = http_method;
260 18 : wis->header_template = (NULL==header_template) ? NULL : header_template;
261 18 : wis->body_template = (NULL==body_template) ? NULL : body_template;
262 : {
263 18 : struct TALER_TESTING_Command cmd = {
264 : .cls = wis,
265 : .label = label,
266 : .run = &post_webhooks_run,
267 : .cleanup = &post_webhooks_cleanup,
268 : .traits = &post_webhooks_traits
269 : };
270 :
271 18 : return cmd;
272 : }
273 : }
274 :
275 :
276 : struct TALER_TESTING_Command
277 10 : TALER_TESTING_cmd_merchant_post_webhooks (const char *label,
278 : const char *merchant_url,
279 : const char *webhook_id,
280 : const char *event_type,
281 : unsigned int http_status)
282 : {
283 10 : return TALER_TESTING_cmd_merchant_post_webhooks2 (
284 : label,
285 : merchant_url,
286 : webhook_id,
287 : event_type,
288 : "http://localhost:12345/",
289 : "POST",
290 : "Taler-test-header: EFEHYJS-Bakery",
291 : "5.0 EUR",
292 : http_status);
293 : }
294 :
295 :
296 : /* end of testing_api_cmd_post_webhooks.c */
|