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_templates.c
21 : * @brief command to test GET /templates
22 : * @author Priscilla HUANG
23 : */
24 : #include "platform.h"
25 : struct GetTemplatesState;
26 : #define TALER_MERCHANT_GET_PRIVATE_TEMPLATES_RESULT_CLOSURE struct GetTemplatesState
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.h>
32 :
33 :
34 : /**
35 : * State of a "GET templates" CMD.
36 : */
37 : struct GetTemplatesState
38 : {
39 :
40 : /**
41 : * Handle for a "GET template" request.
42 : */
43 : struct TALER_MERCHANT_GetPrivateTemplatesHandle *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 : * Expected HTTP response code.
57 : */
58 : unsigned int http_status;
59 :
60 : /**
61 : * The list of template references.
62 : */
63 : const char **templates;
64 :
65 : /**
66 : * Length of @e templates.
67 : */
68 : unsigned int templates_length;
69 :
70 : };
71 :
72 :
73 : /**
74 : * Callback for a GET /templates operation.
75 : *
76 : * @param cls closure for this function
77 : * @param tgr response details
78 : */
79 : static void
80 4 : get_templates_cb (struct GetTemplatesState *gis,
81 : const struct TALER_MERCHANT_GetPrivateTemplatesResponse *tgr)
82 : {
83 :
84 4 : gis->igh = NULL;
85 4 : if (gis->http_status != tgr->hr.http_status)
86 : {
87 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
88 : "Unexpected response code %u (%d) to command %s\n",
89 : tgr->hr.http_status,
90 : (int) tgr->hr.ec,
91 : TALER_TESTING_interpreter_get_current_label (gis->is));
92 0 : TALER_TESTING_interpreter_fail (gis->is);
93 0 : return;
94 : }
95 4 : switch (tgr->hr.http_status)
96 : {
97 4 : case MHD_HTTP_OK:
98 4 : if (tgr->details.ok.templates_length != gis->templates_length)
99 : {
100 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
101 : "Length of templates found does not match\n");
102 0 : TALER_TESTING_interpreter_fail (gis->is);
103 0 : return;
104 : }
105 6 : for (unsigned int i = 0; i < gis->templates_length; ++i)
106 : {
107 : const struct TALER_TESTING_Command *template_cmd;
108 :
109 2 : template_cmd = TALER_TESTING_interpreter_lookup_command (
110 : gis->is,
111 2 : gis->templates[i]);
112 :
113 : {
114 : const char *template_id;
115 :
116 2 : if (GNUNET_OK !=
117 2 : TALER_TESTING_get_trait_template_id (template_cmd,
118 : &template_id))
119 : {
120 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121 : "Could not fetch template id\n");
122 0 : TALER_TESTING_interpreter_fail (gis->is);
123 0 : return;
124 : }
125 2 : if (0 != strcmp (tgr->details.ok.templates[i].template_id,
126 : template_id))
127 : {
128 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 : "Template id does not match\n");
130 0 : TALER_TESTING_interpreter_fail (gis->is);
131 0 : return;
132 : }
133 : }
134 : }
135 4 : break;
136 0 : case MHD_HTTP_UNAUTHORIZED:
137 0 : break;
138 0 : case MHD_HTTP_NOT_FOUND:
139 : /* instance does not exist */
140 0 : break;
141 0 : default:
142 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
143 : "Unhandled HTTP status %u (%d).\n",
144 : tgr->hr.http_status,
145 : tgr->hr.ec);
146 0 : break;
147 : }
148 4 : TALER_TESTING_interpreter_next (gis->is);
149 : }
150 :
151 :
152 : /**
153 : * Run the "GET /templates" CMD.
154 : *
155 : *
156 : * @param cls closure.
157 : * @param cmd command being run now.
158 : * @param is interpreter state.
159 : */
160 : static void
161 4 : get_templates_run (void *cls,
162 : const struct TALER_TESTING_Command *cmd,
163 : struct TALER_TESTING_Interpreter *is)
164 : {
165 4 : struct GetTemplatesState *gis = cls;
166 :
167 4 : gis->is = is;
168 4 : gis->igh = TALER_MERCHANT_get_private_templates_create (
169 : TALER_TESTING_interpreter_get_context (is),
170 : gis->merchant_url);
171 : {
172 : enum TALER_ErrorCode ec;
173 :
174 4 : ec = TALER_MERCHANT_get_private_templates_start (gis->igh,
175 : &get_templates_cb,
176 : gis);
177 4 : GNUNET_assert (TALER_EC_NONE == ec);
178 : }
179 4 : }
180 :
181 :
182 : /**
183 : * Free the state of a "GET template" CMD, and possibly
184 : * cancel a pending operation thereof.
185 : *
186 : * @param cls closure.
187 : * @param cmd command being run.
188 : */
189 : static void
190 4 : get_templates_cleanup (void *cls,
191 : const struct TALER_TESTING_Command *cmd)
192 : {
193 4 : struct GetTemplatesState *gis = cls;
194 :
195 4 : if (NULL != gis->igh)
196 : {
197 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
198 : "GET /templates operation did not complete\n");
199 0 : TALER_MERCHANT_get_private_templates_cancel (gis->igh);
200 : }
201 4 : GNUNET_array_grow (gis->templates,
202 : gis->templates_length,
203 : 0);
204 4 : GNUNET_free (gis);
205 4 : }
206 :
207 :
208 : struct TALER_TESTING_Command
209 4 : TALER_TESTING_cmd_merchant_get_templates (const char *label,
210 : const char *merchant_url,
211 : unsigned int http_status,
212 : ...)
213 : {
214 : struct GetTemplatesState *gis;
215 :
216 4 : gis = GNUNET_new (struct GetTemplatesState);
217 4 : gis->merchant_url = merchant_url;
218 4 : gis->http_status = http_status;
219 : {
220 : const char *clabel;
221 : va_list ap;
222 :
223 4 : va_start (ap, http_status);
224 6 : while (NULL != (clabel = va_arg (ap, const char *)))
225 : {
226 2 : GNUNET_array_append (gis->templates,
227 : gis->templates_length,
228 : clabel);
229 : }
230 4 : va_end (ap);
231 : }
232 : {
233 4 : struct TALER_TESTING_Command cmd = {
234 : .cls = gis,
235 : .label = label,
236 : .run = &get_templates_run,
237 : .cleanup = &get_templates_cleanup
238 : };
239 :
240 4 : return cmd;
241 : }
242 : }
243 :
244 :
245 : /* end of testing_api_cmd_get_templates.c */
|