Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024, 2026 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 : /**
21 : * @file testing/testing_api_cmd_get_kyc_info.c
22 : * @brief Implement the testing CMDs for the GET /kyc_info operation.
23 : * @author Christian Grothoff
24 : */
25 : #include "taler/taler_json_lib.h"
26 : #include <gnunet/gnunet_curl_lib.h>
27 :
28 : /**
29 : * State for a GET kyc-info CMD.
30 : */
31 : struct GetKycInfoState;
32 :
33 : #define TALER_EXCHANGE_GET_KYC_INFO_RESULT_CLOSURE \
34 : struct GetKycInfoState
35 : #include "taler/exchange/get-kyc-info-ACCESS_TOKEN.h"
36 : #include "taler/taler_testing_lib.h"
37 :
38 : /**
39 : * State for a GET kyc-info CMD.
40 : */
41 : struct GetKycInfoState
42 : {
43 :
44 : /**
45 : * Command to get the account access token from.
46 : */
47 : const char *kyc_check_reference;
48 :
49 : /**
50 : * Expected HTTP response code.
51 : */
52 : unsigned int expected_response_code;
53 :
54 : /**
55 : * Handle to the GET /kyc-info pending operation.
56 : */
57 : struct TALER_EXCHANGE_GetKycInfoHandle *kwh;
58 :
59 : /**
60 : * Interpreter state.
61 : */
62 : struct TALER_TESTING_Interpreter *is;
63 :
64 : /**
65 : * Array of IDs for possible KYC processes we could
66 : * start according to the response.
67 : */
68 : char **ids;
69 :
70 : /**
71 : * Length of the @e ids array.
72 : */
73 : unsigned int num_ids;
74 : };
75 :
76 :
77 : /**
78 : * Handle response to the command.
79 : *
80 : * @param kcg our state
81 : * @param kpci GET KYC status response details
82 : */
83 : static void
84 11 : kyc_info_cb (
85 : TALER_EXCHANGE_GET_KYC_INFO_RESULT_CLOSURE *kcg,
86 : const struct TALER_EXCHANGE_GetKycInfoResponse *kpci)
87 : {
88 11 : struct TALER_TESTING_Interpreter *is = kcg->is;
89 :
90 11 : kcg->kwh = NULL;
91 11 : if (kcg->expected_response_code != kpci->hr.http_status)
92 : {
93 0 : TALER_TESTING_unexpected_status (
94 : is,
95 : kpci->hr.http_status,
96 : kcg->expected_response_code);
97 0 : return;
98 : }
99 11 : switch (kpci->hr.http_status)
100 : {
101 11 : case MHD_HTTP_OK:
102 11 : kcg->num_ids = kpci->details.ok.requirements_length;
103 11 : kcg->ids = GNUNET_new_array (kcg->num_ids,
104 : char *);
105 22 : for (unsigned int i = 0; i<kcg->num_ids; i++)
106 11 : kcg->ids[i] = GNUNET_strdup (
107 : kpci->details.ok.requirements[i].id);
108 11 : break;
109 0 : case MHD_HTTP_NO_CONTENT:
110 0 : break;
111 0 : default:
112 0 : GNUNET_break (0);
113 0 : break;
114 : }
115 11 : TALER_TESTING_interpreter_next (kcg->is);
116 : }
117 :
118 :
119 : /**
120 : * Run the command.
121 : *
122 : * @param cls closure.
123 : * @param cmd the command to execute.
124 : * @param is the interpreter state.
125 : */
126 : static void
127 11 : get_kyc_info_run (void *cls,
128 : const struct TALER_TESTING_Command *cmd,
129 : struct TALER_TESTING_Interpreter *is)
130 : {
131 11 : struct GetKycInfoState *kcg = cls;
132 : const struct TALER_TESTING_Command *res_cmd;
133 : const struct TALER_AccountAccessTokenP *token;
134 :
135 : (void) cmd;
136 11 : kcg->is = is;
137 11 : res_cmd = TALER_TESTING_interpreter_lookup_command (
138 : kcg->is,
139 : kcg->kyc_check_reference);
140 11 : if (NULL == res_cmd)
141 : {
142 0 : GNUNET_break (0);
143 0 : TALER_TESTING_interpreter_fail (kcg->is);
144 0 : return;
145 : }
146 11 : if (GNUNET_OK !=
147 11 : TALER_TESTING_get_trait_account_access_token (
148 : res_cmd,
149 : &token))
150 : {
151 0 : GNUNET_break (0);
152 0 : TALER_TESTING_interpreter_fail (kcg->is);
153 0 : return;
154 : }
155 11 : kcg->kwh = TALER_EXCHANGE_get_kyc_info_create (
156 : TALER_TESTING_interpreter_get_context (is),
157 : TALER_TESTING_get_exchange_url (is),
158 : token);
159 11 : GNUNET_assert (NULL != kcg->kwh);
160 : {
161 : enum TALER_ErrorCode ec;
162 :
163 11 : ec = TALER_EXCHANGE_get_kyc_info_start (kcg->kwh,
164 : &kyc_info_cb,
165 : kcg);
166 11 : if (TALER_EC_NONE != ec)
167 : {
168 0 : GNUNET_break (0);
169 0 : kcg->kwh = NULL;
170 0 : TALER_TESTING_interpreter_fail (kcg->is);
171 0 : return;
172 : }
173 : }
174 : }
175 :
176 :
177 : /**
178 : * Cleanup the state from a "track transaction" CMD, and possibly
179 : * cancel a operation thereof.
180 : *
181 : * @param cls closure.
182 : * @param cmd the command which is being cleaned up.
183 : */
184 : static void
185 11 : get_kyc_info_cleanup (
186 : void *cls,
187 : const struct TALER_TESTING_Command *cmd)
188 : {
189 11 : struct GetKycInfoState *kcg = cls;
190 :
191 11 : if (NULL != kcg->kwh)
192 : {
193 0 : TALER_TESTING_command_incomplete (kcg->is,
194 : cmd->label);
195 0 : TALER_EXCHANGE_get_kyc_info_cancel (kcg->kwh);
196 0 : kcg->kwh = NULL;
197 : }
198 22 : for (unsigned int i = 0; i<kcg->num_ids; i++)
199 11 : GNUNET_free (kcg->ids[i]);
200 11 : GNUNET_free (kcg->ids);
201 11 : GNUNET_free (kcg);
202 11 : }
203 :
204 :
205 : /**
206 : * Offer internal data from a "check KYC" CMD.
207 : *
208 : * @param cls closure.
209 : * @param[out] ret result (could be anything).
210 : * @param trait name of the trait.
211 : * @param index index number of the object to offer.
212 : * @return #GNUNET_OK on success.
213 : */
214 : static enum GNUNET_GenericReturnValue
215 32 : get_kyc_info_traits (void *cls,
216 : const void **ret,
217 : const char *trait,
218 : unsigned int index)
219 : {
220 32 : struct GetKycInfoState *kcg = cls;
221 : struct TALER_TESTING_Trait traits[] = {
222 32 : TALER_TESTING_make_trait_kyc_id (index,
223 32 : kcg->ids[index]),
224 32 : TALER_TESTING_trait_end ()
225 : };
226 :
227 32 : if (index >= kcg->num_ids)
228 0 : return GNUNET_NO;
229 32 : return TALER_TESTING_get_trait (traits,
230 : ret,
231 : trait,
232 : index);
233 : }
234 :
235 :
236 : struct TALER_TESTING_Command
237 11 : TALER_TESTING_cmd_get_kyc_info (
238 : const char *label,
239 : const char *kyc_check_reference,
240 : unsigned int expected_response_code)
241 : {
242 : struct GetKycInfoState *kcg;
243 :
244 11 : kcg = GNUNET_new (struct GetKycInfoState);
245 11 : kcg->kyc_check_reference = kyc_check_reference;
246 11 : kcg->expected_response_code = expected_response_code;
247 : {
248 11 : struct TALER_TESTING_Command cmd = {
249 : .cls = kcg,
250 : .label = label,
251 : .run = &get_kyc_info_run,
252 : .cleanup = &get_kyc_info_cleanup,
253 : .traits = &get_kyc_info_traits
254 : };
255 :
256 11 : return cmd;
257 : }
258 : }
259 :
260 :
261 : /* end of testing_api_cmd_get_kyc_info.c */
|