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