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_patch_webhook.c
21 : * @brief command to test PATCH /webhook
22 : * @author Priscilla HUANG
23 : */
24 : #include "platform.h"
25 : struct PatchWebhookState;
26 : #define TALER_MERCHANT_PATCH_PRIVATE_WEBHOOK_RESULT_CLOSURE struct PatchWebhookState
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/patch-private-webhooks-WEBHOOK_ID.h>
32 :
33 :
34 : /**
35 : * State of a "PATCH /webhook" CMD.
36 : */
37 : struct PatchWebhookState
38 : {
39 :
40 : /**
41 : * Handle for a "GET webhook" request.
42 : */
43 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *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 GET 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_template 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 PATCH /webhooks/$ID operation.
95 : *
96 : * @param cls closure for this function
97 : * @param result response being processed
98 : */
99 : static void
100 4 : patch_webhook_cb (struct PatchWebhookState *pis,
101 : const struct TALER_MERCHANT_PatchPrivateWebhookResponse *
102 : result)
103 : {
104 4 : const struct TALER_MERCHANT_HttpResponse *hr = &result->hr;
105 :
106 4 : pis->iph = NULL;
107 4 : if (pis->http_status != hr->http_status)
108 : {
109 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 : "Unexpected response code %u (%d) to command %s\n",
111 : hr->http_status,
112 : (int) hr->ec,
113 : TALER_TESTING_interpreter_get_current_label (pis->is));
114 0 : TALER_TESTING_interpreter_fail (pis->is);
115 0 : return;
116 : }
117 4 : switch (hr->http_status)
118 : {
119 2 : case MHD_HTTP_NO_CONTENT:
120 2 : break;
121 0 : case MHD_HTTP_UNAUTHORIZED:
122 0 : break;
123 0 : case MHD_HTTP_FORBIDDEN:
124 0 : break;
125 2 : case MHD_HTTP_NOT_FOUND:
126 2 : break;
127 0 : case MHD_HTTP_CONFLICT:
128 0 : break;
129 0 : default:
130 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
131 : "Unhandled HTTP status %u for PATCH /webhooks/ID.\n",
132 : hr->http_status);
133 : }
134 4 : TALER_TESTING_interpreter_next (pis->is);
135 : }
136 :
137 :
138 : /**
139 : * Run the "PATCH /webhooks/$ID" CMD.
140 : *
141 : *
142 : * @param cls closure.
143 : * @param cmd command being run now.
144 : * @param is interpreter state.
145 : */
146 : static void
147 4 : patch_webhook_run (void *cls,
148 : const struct TALER_TESTING_Command *cmd,
149 : struct TALER_TESTING_Interpreter *is)
150 : {
151 4 : struct PatchWebhookState *pis = cls;
152 :
153 4 : pis->is = is;
154 4 : pis->iph = TALER_MERCHANT_patch_private_webhook_create (
155 : TALER_TESTING_interpreter_get_context (is),
156 : pis->merchant_url,
157 : pis->webhook_id,
158 : pis->event_type,
159 : pis->url,
160 : pis->http_method);
161 4 : GNUNET_assert (NULL != pis->iph);
162 4 : if (NULL != pis->header_template)
163 4 : TALER_MERCHANT_patch_private_webhook_set_options (
164 : pis->iph,
165 : TALER_MERCHANT_patch_private_webhook_option_header_template (
166 : pis->header_template));
167 4 : if (NULL != pis->body_template)
168 4 : TALER_MERCHANT_patch_private_webhook_set_options (
169 : pis->iph,
170 : TALER_MERCHANT_patch_private_webhook_option_body_template (
171 : pis->body_template));
172 : {
173 : enum TALER_ErrorCode ec;
174 :
175 4 : ec = TALER_MERCHANT_patch_private_webhook_start (
176 : pis->iph,
177 : &patch_webhook_cb,
178 : pis);
179 4 : GNUNET_assert (TALER_EC_NONE == ec);
180 : }
181 4 : }
182 :
183 :
184 : /**
185 : * Offers information from the PATCH /webhooks CMD state to other
186 : * commands.
187 : *
188 : * @param cls closure
189 : * @param[out] ret result (could be anything)
190 : * @param trait name of the trait
191 : * @param index index number of the object to extract.
192 : * @return #GNUNET_OK on success
193 : */
194 : static enum GNUNET_GenericReturnValue
195 10 : patch_webhook_traits (void *cls,
196 : const void **ret,
197 : const char *trait,
198 : unsigned int index)
199 : {
200 10 : struct PatchWebhookState *pws = cls;
201 : struct TALER_TESTING_Trait traits[] = {
202 10 : TALER_TESTING_make_trait_event_type (pws->event_type),
203 10 : TALER_TESTING_make_trait_url (pws->url),
204 10 : TALER_TESTING_make_trait_http_method (pws->http_method),
205 10 : TALER_TESTING_make_trait_header_template (pws->header_template),
206 10 : TALER_TESTING_make_trait_body_template (pws->body_template),
207 10 : TALER_TESTING_make_trait_webhook_id (pws->webhook_id),
208 10 : TALER_TESTING_trait_end (),
209 : };
210 :
211 10 : return TALER_TESTING_get_trait (traits,
212 : ret,
213 : trait,
214 : index);
215 : }
216 :
217 :
218 : /**
219 : * Free the state of a "GET webhook" CMD, and possibly
220 : * cancel a pending operation thereof.
221 : *
222 : * @param cls closure.
223 : * @param cmd command being run.
224 : */
225 : static void
226 4 : patch_webhook_cleanup (void *cls,
227 : const struct TALER_TESTING_Command *cmd)
228 : {
229 4 : struct PatchWebhookState *pis = cls;
230 :
231 4 : if (NULL != pis->iph)
232 : {
233 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234 : "PATCH /webhooks/$ID operation did not complete\n");
235 0 : TALER_MERCHANT_patch_private_webhook_cancel (pis->iph);
236 : }
237 4 : GNUNET_free (pis);
238 4 : }
239 :
240 :
241 : struct TALER_TESTING_Command
242 4 : TALER_TESTING_cmd_merchant_patch_webhook (
243 : const char *label,
244 : const char *merchant_url,
245 : const char *webhook_id,
246 : const char *event_type,
247 : const char *url,
248 : const char *http_method,
249 : const char *header_template,
250 : const char *body_template,
251 : unsigned int http_status)
252 : {
253 : struct PatchWebhookState *pis;
254 :
255 4 : pis = GNUNET_new (struct PatchWebhookState);
256 4 : pis->merchant_url = merchant_url;
257 4 : pis->webhook_id = webhook_id;
258 4 : pis->http_status = http_status;
259 4 : pis->event_type = event_type;
260 4 : pis->url = url;
261 4 : pis->http_method = http_method;
262 4 : pis->header_template = (NULL == header_template) ? NULL : header_template;
263 4 : pis->body_template = (NULL == body_template) ? NULL : body_template;
264 : {
265 4 : struct TALER_TESTING_Command cmd = {
266 : .cls = pis,
267 : .label = label,
268 : .run = &patch_webhook_run,
269 : .cleanup = &patch_webhook_cleanup,
270 : .traits = &patch_webhook_traits
271 : };
272 :
273 4 : return cmd;
274 : }
275 : }
276 :
277 :
278 : /* end of testing_api_cmd_patch_webhook.c */
|