Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2021 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_api_cmd_kyc_get.c
22 : * @brief command to test kyc_get request
23 : * @author Christian Grothoff
24 : */
25 :
26 : #include "platform.h"
27 : #include <taler/taler_exchange_service.h>
28 : #include <taler/taler_testing_lib.h>
29 : #include "taler_merchant_service.h"
30 : #include "taler_merchant_testing_lib.h"
31 :
32 :
33 : /**
34 : * State for a "/kyc" GET CMD.
35 : */
36 : struct KycGetState
37 : {
38 : /**
39 : * Operation handle for a GET /private/kyc GET request.
40 : */
41 : struct TALER_MERCHANT_KycGetHandle *kgh;
42 :
43 : /**
44 : * Base URL of the merchant serving the request.
45 : */
46 : const char *merchant_url;
47 :
48 : /**
49 : * Instance to query, NULL if part of @e merchant_url
50 : */
51 : const char *instance_id;
52 :
53 : /**
54 : * Reference to command providing wire hash, NULL to
55 : * query all accounts.
56 : */
57 : const char *h_wire_ref;
58 :
59 : /**
60 : * URL of exchange to query.
61 : */
62 : const char *exchange_url;
63 :
64 : /**
65 : * Set to the payto hash of the first account
66 : * for which we failed to pass the KYC check.
67 : */
68 : struct TALER_PaytoHashP h_payto;
69 :
70 : /**
71 : * Expected HTTP response code.
72 : */
73 : unsigned int expected_http_status;
74 :
75 : /**
76 : * Interpreter state.
77 : */
78 : struct TALER_TESTING_Interpreter *is;
79 :
80 : };
81 :
82 :
83 : /**
84 : * Free the state of a "/kyc" GET CMD, and
85 : * possibly cancel a pending "kyc" GET operation.
86 : *
87 : * @param cls closure with the `struct KycGetState`
88 : * @param cmd command currently being freed.
89 : */
90 : static void
91 0 : kyc_get_cleanup (void *cls,
92 : const struct TALER_TESTING_Command *cmd)
93 : {
94 0 : struct KycGetState *cs = cls;
95 :
96 0 : if (NULL != cs->kgh)
97 : {
98 0 : TALER_LOG_WARNING ("/kyc GET operation did not complete\n");
99 0 : TALER_MERCHANT_kyc_get_cancel (cs->kgh);
100 : }
101 0 : GNUNET_free (cs);
102 0 : }
103 :
104 :
105 : /**
106 : * Process "GET /public/kyc_get" (lookup) response.
107 : *
108 : * @param cls closure
109 : * @param kr response we got
110 : */
111 : static void
112 0 : kyc_get_cb (void *cls,
113 : const struct TALER_MERCHANT_KycResponse *kr)
114 : {
115 0 : struct KycGetState *cs = cls;
116 :
117 0 : cs->kgh = NULL;
118 0 : if (kr->hr.http_status != cs->expected_http_status)
119 : {
120 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121 : "Expected status %u, got %u\n",
122 : cs->expected_http_status,
123 : kr->hr.http_status);
124 0 : TALER_TESTING_FAIL (cs->is);
125 : }
126 0 : switch (kr->hr.http_status)
127 : {
128 0 : case MHD_HTTP_ACCEPTED:
129 0 : if (0 != kr->details.kyc_status.pending_kycs_length)
130 : {
131 : const char *url;
132 : const char *tok;
133 : const char *end;
134 : char *dec;
135 : const char *eq;
136 : size_t toklen;
137 :
138 0 : url = kr->details.kyc_status.pending_kycs[0].kyc_url;
139 0 : tok = strstr (url, "&redirect_uri=");
140 0 : if (NULL == tok)
141 0 : TALER_TESTING_FAIL (cs->is);
142 0 : tok += strlen ("&redirect_uri=");
143 0 : end = strchr (tok, '&');
144 0 : if (NULL == end)
145 0 : toklen = strlen (tok);
146 : else
147 0 : toklen = end - tok;
148 0 : (void) GNUNET_STRINGS_urldecode (tok,
149 : toklen,
150 : &dec);
151 0 : eq = strrchr (dec, '/');
152 0 : if (NULL == eq)
153 : {
154 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 : "Received unexpected KYC URL `%s' (%s)\n",
156 : url,
157 : dec);
158 0 : GNUNET_free (dec);
159 0 : TALER_TESTING_FAIL (cs->is);
160 : }
161 0 : eq++;
162 0 : if (GNUNET_OK !=
163 0 : GNUNET_STRINGS_string_to_data (eq,
164 : strlen (eq),
165 0 : &cs->h_payto,
166 : sizeof (cs->h_payto)))
167 : {
168 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 : "Received unexpected KYC URL `%s' (%s)\n",
170 : url,
171 : dec);
172 0 : GNUNET_free (dec);
173 0 : TALER_TESTING_FAIL (cs->is);
174 : }
175 0 : GNUNET_free (dec);
176 : }
177 0 : break;
178 : }
179 0 : TALER_TESTING_interpreter_next (cs->is);
180 : }
181 :
182 :
183 : /**
184 : * Run the "kyc_get" CMD.
185 : *
186 : * @param cls closure.
187 : * @param cmd command being currently run.
188 : * @param is interpreter state.
189 : */
190 : static void
191 0 : kyc_get_run (void *cls,
192 : const struct TALER_TESTING_Command *cmd,
193 : struct TALER_TESTING_Interpreter *is)
194 : {
195 0 : struct KycGetState *cs = cls;
196 0 : const struct TALER_MerchantWireHashP *h_wire = NULL;
197 :
198 0 : cs->is = is;
199 0 : if (NULL != cs->h_wire_ref)
200 : {
201 : const struct TALER_TESTING_Command *wire_cmd;
202 :
203 0 : if (NULL ==
204 : (wire_cmd =
205 0 : TALER_TESTING_interpreter_lookup_command (cs->is,
206 : cs->h_wire_ref)))
207 : {
208 0 : GNUNET_break (0);
209 0 : TALER_TESTING_FAIL (cs->is);
210 : }
211 : /* Note: at the time of writing, no command offers an h_wire trait,
212 : so for now this code is dead and 'h_wire_ref' must always be NULL... */
213 0 : if (GNUNET_OK !=
214 0 : TALER_TESTING_get_trait_h_wire (wire_cmd,
215 : &h_wire))
216 : {
217 0 : GNUNET_break (0);
218 0 : TALER_TESTING_FAIL (cs->is);
219 : }
220 : }
221 0 : if (NULL == cs->instance_id)
222 0 : cs->kgh = TALER_MERCHANT_kyc_get (is->ctx,
223 : cs->merchant_url,
224 : h_wire,
225 : cs->exchange_url,
226 0 : GNUNET_TIME_UNIT_ZERO,
227 : &kyc_get_cb,
228 : cs);
229 : else
230 0 : cs->kgh = TALER_MERCHANT_management_kyc_get (is->ctx,
231 : cs->merchant_url,
232 : cs->instance_id,
233 : h_wire,
234 : cs->exchange_url,
235 0 : GNUNET_TIME_UNIT_ZERO,
236 : &kyc_get_cb,
237 : cs);
238 :
239 0 : GNUNET_assert (NULL != cs->kgh);
240 : }
241 :
242 :
243 : /**
244 : * Offer internal data from "KYC" GET CMD.
245 : *
246 : * @param cls closure.
247 : * @param[out] ret result (could be anything).
248 : * @param trait name of the trait.
249 : * @param index index number of the object to offer.
250 : * @return #GNUNET_OK on success.
251 : */
252 : static enum GNUNET_GenericReturnValue
253 0 : kyc_get_traits (void *cls,
254 : const void **ret,
255 : const char *trait,
256 : unsigned int index)
257 : {
258 0 : struct KycGetState *cs = cls;
259 : struct TALER_TESTING_Trait traits[] = {
260 0 : TALER_TESTING_make_trait_h_payto (
261 0 : &cs->h_payto),
262 0 : TALER_TESTING_trait_end ()
263 : };
264 :
265 0 : return TALER_TESTING_get_trait (traits,
266 : ret,
267 : trait,
268 : index);
269 : }
270 :
271 :
272 : struct TALER_TESTING_Command
273 0 : TALER_TESTING_cmd_merchant_kyc_get (const char *label,
274 : const char *merchant_url,
275 : const char *instance_id,
276 : const char *h_wire_ref,
277 : const char *exchange_url,
278 : unsigned int expected_http_status)
279 : {
280 : struct KycGetState *cs;
281 :
282 0 : cs = GNUNET_new (struct KycGetState);
283 0 : cs->merchant_url = merchant_url;
284 0 : cs->instance_id = instance_id;
285 0 : cs->h_wire_ref = h_wire_ref;
286 0 : cs->exchange_url = exchange_url;
287 0 : cs->expected_http_status = expected_http_status;
288 : {
289 0 : struct TALER_TESTING_Command cmd = {
290 : .cls = cs,
291 : .label = label,
292 : .run = &kyc_get_run,
293 : .cleanup = &kyc_get_cleanup,
294 : .traits = &kyc_get_traits
295 : };
296 :
297 0 : return cmd;
298 : }
299 : }
300 :
301 :
302 : /* end of testing_api_cmd_kyc_get.c */
|