Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 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 testing_api_cmd_post_units.c
21 : * @brief command to test POST /private/units
22 : * @author Bohdan Potuzhnyi
23 : */
24 : #include "platform.h"
25 : #include <taler/taler_testing_lib.h>
26 : #include "taler_merchant_service.h"
27 : #include "taler_merchant_testing_lib.h"
28 :
29 :
30 : /**
31 : * State for a POST /private/units command.
32 : */
33 : struct PostUnitState
34 : {
35 : /**
36 : * In-flight request handle.
37 : */
38 : struct TALER_MERCHANT_UnitsPostHandle *uph;
39 :
40 : /**
41 : * Interpreter context.
42 : */
43 : struct TALER_TESTING_Interpreter *is;
44 :
45 : /**
46 : * Merchant backend base URL.
47 : */
48 : const char *merchant_url;
49 :
50 : /**
51 : * Unit identifier.
52 : */
53 : const char *unit_id;
54 :
55 : /**
56 : * Long label.
57 : */
58 : const char *unit_name_long;
59 :
60 : /**
61 : * Short label.
62 : */
63 : const char *unit_name_short;
64 :
65 : /**
66 : * Optional translations (reference counted).
67 : */
68 : json_t *unit_name_long_i18n;
69 :
70 : /**
71 : * Optional translations (reference counted).
72 : */
73 : json_t *unit_name_short_i18n;
74 :
75 : /**
76 : * Whether fractional quantities are allowed.
77 : */
78 : bool unit_allow_fraction;
79 :
80 : /**
81 : * Precision level for fractional quantities.
82 : */
83 : uint32_t unit_precision_level;
84 :
85 : /**
86 : * Whether the unit should be active.
87 : */
88 : bool unit_active;
89 :
90 : /**
91 : * Expected HTTP status.
92 : */
93 : unsigned int http_status;
94 : };
95 :
96 :
97 : /**
98 : * Completion callback for POST /private/units.
99 : */
100 : static void
101 6 : post_unit_cb (void *cls,
102 : const struct TALER_MERCHANT_HttpResponse *hr)
103 : {
104 6 : struct PostUnitState *pus = cls;
105 :
106 6 : pus->uph = NULL;
107 6 : if (pus->http_status != hr->http_status)
108 : {
109 0 : TALER_TESTING_unexpected_status_with_body (pus->is,
110 : hr->http_status,
111 : pus->http_status,
112 : hr->reply);
113 0 : return;
114 : }
115 6 : TALER_TESTING_interpreter_next (pus->is);
116 : }
117 :
118 :
119 : /**
120 : * Issue the POST /private/units request.
121 : */
122 : static void
123 6 : post_unit_run (void *cls,
124 : const struct TALER_TESTING_Command *cmd,
125 : struct TALER_TESTING_Interpreter *is)
126 : {
127 6 : struct PostUnitState *pus = cls;
128 :
129 6 : pus->is = is;
130 6 : pus->uph = TALER_MERCHANT_units_post (
131 : TALER_TESTING_interpreter_get_context (is),
132 : pus->merchant_url,
133 : pus->unit_id,
134 : pus->unit_name_long,
135 : pus->unit_name_short,
136 6 : pus->unit_allow_fraction,
137 : pus->unit_precision_level,
138 6 : pus->unit_active,
139 6 : pus->unit_name_long_i18n,
140 6 : pus->unit_name_short_i18n,
141 : &post_unit_cb,
142 : pus);
143 6 : if (NULL == pus->uph)
144 : {
145 0 : GNUNET_break (0);
146 0 : TALER_TESTING_interpreter_fail (is);
147 : }
148 6 : }
149 :
150 :
151 : /**
152 : * Provide traits for other commands.
153 : */
154 : static enum GNUNET_GenericReturnValue
155 32 : post_unit_traits (void *cls,
156 : const void **ret,
157 : const char *trait,
158 : unsigned int index)
159 : {
160 32 : struct PostUnitState *pus = cls;
161 : struct TALER_TESTING_Trait traits[] = {
162 32 : TALER_TESTING_make_trait_unit_id (pus->unit_id),
163 32 : TALER_TESTING_make_trait_unit_name_long (pus->unit_name_long),
164 32 : TALER_TESTING_make_trait_unit_name_short (pus->unit_name_short),
165 32 : TALER_TESTING_make_trait_unit_allow_fraction (&pus->unit_allow_fraction),
166 32 : TALER_TESTING_make_trait_unit_precision_level (&pus->unit_precision_level),
167 32 : TALER_TESTING_make_trait_unit_active (&pus->unit_active),
168 32 : TALER_TESTING_make_trait_unit_name_long_i18n (pus->unit_name_long_i18n),
169 32 : TALER_TESTING_make_trait_unit_name_short_i18n (pus->unit_name_short_i18n),
170 32 : TALER_TESTING_trait_end ()
171 : };
172 :
173 32 : return TALER_TESTING_get_trait (traits,
174 : ret,
175 : trait,
176 : index);
177 : }
178 :
179 :
180 : /**
181 : * Cleanup / cancel pending request.
182 : */
183 : static void
184 6 : post_unit_cleanup (void *cls,
185 : const struct TALER_TESTING_Command *cmd)
186 : {
187 6 : struct PostUnitState *pus = cls;
188 :
189 6 : if (NULL != pus->uph)
190 : {
191 0 : TALER_MERCHANT_units_post_cancel (pus->uph);
192 0 : pus->uph = NULL;
193 : }
194 6 : if (NULL != pus->unit_name_long_i18n)
195 4 : json_decref (pus->unit_name_long_i18n);
196 6 : if (NULL != pus->unit_name_short_i18n)
197 4 : json_decref (pus->unit_name_short_i18n);
198 6 : GNUNET_free (pus);
199 6 : }
200 :
201 :
202 : struct TALER_TESTING_Command
203 6 : TALER_TESTING_cmd_merchant_post_units (const char *label,
204 : const char *merchant_url,
205 : const char *unit_id,
206 : const char *unit_name_long,
207 : const char *unit_name_short,
208 : bool unit_allow_fraction,
209 : uint32_t unit_precision_level,
210 : bool unit_active,
211 : json_t *unit_name_long_i18n,
212 : json_t *unit_name_short_i18n,
213 : unsigned int http_status)
214 : {
215 : struct PostUnitState *pus;
216 :
217 6 : pus = GNUNET_new (struct PostUnitState);
218 6 : pus->merchant_url = merchant_url;
219 6 : pus->unit_id = unit_id;
220 6 : pus->unit_name_long = unit_name_long;
221 6 : pus->unit_name_short = unit_name_short;
222 6 : pus->unit_allow_fraction = unit_allow_fraction;
223 6 : pus->unit_precision_level = unit_precision_level;
224 6 : pus->unit_active = unit_active;
225 6 : pus->unit_name_long_i18n = unit_name_long_i18n;
226 6 : pus->unit_name_short_i18n = unit_name_short_i18n;
227 6 : pus->http_status = http_status;
228 : {
229 6 : struct TALER_TESTING_Command cmd = {
230 : .cls = pus,
231 : .label = label,
232 : .run = &post_unit_run,
233 : .cleanup = &post_unit_cleanup,
234 : .traits = &post_unit_traits
235 : };
236 :
237 6 : return cmd;
238 : }
239 : }
240 :
241 :
242 : /* end of testing_api_cmd_post_units.c */
|