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