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_template.c
21 : * @brief command to test PATCH /template
22 : * @author Priscilla HUANG
23 : */
24 : #include "platform.h"
25 : struct PatchTemplateState;
26 : #define TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE struct PatchTemplateState
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-templates-TEMPLATE_ID.h>
32 :
33 :
34 : /**
35 : * State of a "PATCH /template" CMD.
36 : */
37 : struct PatchTemplateState
38 : {
39 :
40 : /**
41 : * Handle for a "GET template" request.
42 : */
43 : struct TALER_MERCHANT_PatchPrivateTemplateHandle *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 template to run GET for.
57 : */
58 : const char *template_id;
59 :
60 : /**
61 : * description of the template
62 : */
63 : const char *template_description;
64 :
65 : /**
66 : * OTP device ID
67 : */
68 : char *otp_id;
69 :
70 : /**
71 : * Contract of the company
72 : */
73 : json_t *template_contract;
74 :
75 : /**
76 : * Expected HTTP response code.
77 : */
78 : unsigned int http_status;
79 :
80 : };
81 :
82 :
83 : /**
84 : * Callback for a PATCH /templates/$ID operation.
85 : *
86 : * @param cls closure for this function
87 : * @param result response being processed
88 : */
89 : static void
90 10 : patch_template_cb (struct PatchTemplateState *pis,
91 : const struct TALER_MERCHANT_PatchPrivateTemplateResponse *
92 : result)
93 : {
94 10 : const struct TALER_MERCHANT_HttpResponse *hr = &result->hr;
95 :
96 10 : pis->iph = NULL;
97 10 : if (pis->http_status != hr->http_status)
98 : {
99 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100 : "Unexpected response code %u (%d) to command %s\n",
101 : hr->http_status,
102 : (int) hr->ec,
103 : TALER_TESTING_interpreter_get_current_label (pis->is));
104 0 : TALER_TESTING_interpreter_fail (pis->is);
105 0 : return;
106 : }
107 10 : switch (hr->http_status)
108 : {
109 4 : case MHD_HTTP_NO_CONTENT:
110 4 : break;
111 2 : case MHD_HTTP_BAD_REQUEST:
112 2 : break;
113 0 : case MHD_HTTP_UNAUTHORIZED:
114 0 : break;
115 0 : case MHD_HTTP_FORBIDDEN:
116 0 : break;
117 4 : case MHD_HTTP_NOT_FOUND:
118 4 : break;
119 0 : case MHD_HTTP_CONFLICT:
120 0 : break;
121 0 : default:
122 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
123 : "Unhandled HTTP status %u for PATCH /templates/ID.\n",
124 : hr->http_status);
125 : }
126 10 : TALER_TESTING_interpreter_next (pis->is);
127 : }
128 :
129 :
130 : /**
131 : * Run the "PATCH /templates/$ID" CMD.
132 : *
133 : *
134 : * @param cls closure.
135 : * @param cmd command being run now.
136 : * @param is interpreter state.
137 : */
138 : static void
139 10 : patch_template_run (void *cls,
140 : const struct TALER_TESTING_Command *cmd,
141 : struct TALER_TESTING_Interpreter *is)
142 : {
143 10 : struct PatchTemplateState *pis = cls;
144 :
145 10 : pis->is = is;
146 10 : pis->iph = TALER_MERCHANT_patch_private_template_create (
147 : TALER_TESTING_interpreter_get_context (is),
148 : pis->merchant_url,
149 : pis->template_id,
150 : pis->template_description,
151 10 : pis->otp_id,
152 : pis->template_contract);
153 10 : GNUNET_assert (NULL != pis->iph);
154 : {
155 : enum TALER_ErrorCode ec;
156 :
157 10 : ec = TALER_MERCHANT_patch_private_template_start (
158 : pis->iph,
159 : &patch_template_cb,
160 : pis);
161 10 : GNUNET_assert (TALER_EC_NONE == ec);
162 : }
163 10 : }
164 :
165 :
166 : /**
167 : * Offers information from the PATCH /templates CMD state to other
168 : * commands.
169 : *
170 : * @param cls closure
171 : * @param[out] ret result (could be anything)
172 : * @param trait name of the trait
173 : * @param index index number of the object to extract.
174 : * @return #GNUNET_OK on success
175 : */
176 : static enum GNUNET_GenericReturnValue
177 18 : patch_template_traits (void *cls,
178 : const void **ret,
179 : const char *trait,
180 : unsigned int index)
181 : {
182 18 : struct PatchTemplateState *pts = cls;
183 : struct TALER_TESTING_Trait traits[] = {
184 18 : TALER_TESTING_make_trait_template_description (pts->template_description),
185 18 : TALER_TESTING_make_trait_otp_id (pts->otp_id),
186 18 : TALER_TESTING_make_trait_template_contract (pts->template_contract),
187 18 : TALER_TESTING_make_trait_template_id (pts->template_id),
188 18 : TALER_TESTING_trait_end (),
189 : };
190 :
191 18 : return TALER_TESTING_get_trait (traits,
192 : ret,
193 : trait,
194 : index);
195 : }
196 :
197 :
198 : /**
199 : * Free the state of a "GET template" CMD, and possibly
200 : * cancel a pending operation thereof.
201 : *
202 : * @param cls closure.
203 : * @param cmd command being run.
204 : */
205 : static void
206 10 : patch_template_cleanup (void *cls,
207 : const struct TALER_TESTING_Command *cmd)
208 : {
209 10 : struct PatchTemplateState *pis = cls;
210 :
211 10 : if (NULL != pis->iph)
212 : {
213 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
214 : "PATCH /templates/$ID operation did not complete\n");
215 0 : TALER_MERCHANT_patch_private_template_cancel (pis->iph);
216 : }
217 10 : GNUNET_free (pis->otp_id);
218 10 : json_decref (pis->template_contract);
219 10 : GNUNET_free (pis);
220 10 : }
221 :
222 :
223 : struct TALER_TESTING_Command
224 10 : TALER_TESTING_cmd_merchant_patch_template (
225 : const char *label,
226 : const char *merchant_url,
227 : const char *template_id,
228 : const char *template_description,
229 : const char *otp_id,
230 : json_t *template_contract,
231 : unsigned int http_status)
232 : {
233 : struct PatchTemplateState *pis;
234 :
235 10 : pis = GNUNET_new (struct PatchTemplateState);
236 10 : pis->merchant_url = merchant_url;
237 10 : pis->template_id = template_id;
238 10 : pis->http_status = http_status;
239 10 : pis->template_description = template_description;
240 10 : pis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id);
241 10 : pis->template_contract = template_contract; /* ownership taken */
242 : {
243 10 : struct TALER_TESTING_Command cmd = {
244 : .cls = pis,
245 : .label = label,
246 : .run = &patch_template_run,
247 : .cleanup = &patch_template_cleanup,
248 : .traits = &patch_template_traits
249 : };
250 :
251 10 : return cmd;
252 : }
253 : }
254 :
255 :
256 : /* end of testing_api_cmd_patch_template.c */
|