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/testing_api_cmd_kyc_check_get.c
22 : * @brief Implement the testing CMDs for the /kyc_check/ GET operations.
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include "taler_json_lib.h"
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_testing_lib.h"
29 :
30 : /**
31 : * State for a "track transaction" CMD.
32 : */
33 : struct KycCheckGetState
34 : {
35 :
36 : /**
37 : * Command to get a reserve private key from.
38 : */
39 : const char *payment_target_reference;
40 :
41 : /**
42 : * Expected HTTP response code.
43 : */
44 : unsigned int expected_response_code;
45 :
46 : /**
47 : * Set to the KYC URL *if* the exchange replied with
48 : * a request for KYC (#MHD_HTTP_ACCEPTED).
49 : */
50 : char *kyc_url;
51 :
52 : /**
53 : * Handle to the "track transaction" pending operation.
54 : */
55 : struct TALER_EXCHANGE_KycCheckHandle *kwh;
56 :
57 : /**
58 : * Interpreter state.
59 : */
60 : struct TALER_TESTING_Interpreter *is;
61 : };
62 :
63 :
64 : /**
65 : * Handle response to the command.
66 : *
67 : * @param cls closure.
68 : * @param ks GET KYC status response details
69 : */
70 : static void
71 0 : check_kyc_cb (void *cls,
72 : const struct TALER_EXCHANGE_KycStatus *ks)
73 : {
74 0 : struct KycCheckGetState *kcg = cls;
75 0 : struct TALER_TESTING_Interpreter *is = kcg->is;
76 0 : struct TALER_TESTING_Command *cmd = &is->commands[is->ip];
77 :
78 0 : kcg->kwh = NULL;
79 0 : if (kcg->expected_response_code != ks->http_status)
80 : {
81 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
82 : "Unexpected response code %u/%d to command %s in %s:%u\n",
83 : ks->http_status,
84 : (int) ks->ec,
85 : cmd->label,
86 : __FILE__,
87 : __LINE__);
88 0 : TALER_TESTING_interpreter_fail (is);
89 0 : return;
90 : }
91 0 : switch (ks->http_status)
92 : {
93 0 : case MHD_HTTP_OK:
94 0 : break;
95 0 : case MHD_HTTP_ACCEPTED:
96 0 : kcg->kyc_url = GNUNET_strdup (ks->details.accepted.kyc_url);
97 0 : break;
98 0 : case MHD_HTTP_NO_CONTENT:
99 0 : break;
100 0 : default:
101 0 : GNUNET_break (0);
102 0 : break;
103 : }
104 0 : TALER_TESTING_interpreter_next (kcg->is);
105 : }
106 :
107 :
108 : /**
109 : * Run the command.
110 : *
111 : * @param cls closure.
112 : * @param cmd the command to execute.
113 : * @param is the interpreter state.
114 : */
115 : static void
116 0 : check_kyc_run (void *cls,
117 : const struct TALER_TESTING_Command *cmd,
118 : struct TALER_TESTING_Interpreter *is)
119 : {
120 0 : struct KycCheckGetState *kcg = cls;
121 : const struct TALER_TESTING_Command *res_cmd;
122 : const uint64_t *requirement_row;
123 : const struct TALER_PaytoHashP *h_payto;
124 :
125 : (void) cmd;
126 0 : kcg->is = is;
127 0 : res_cmd = TALER_TESTING_interpreter_lookup_command (kcg->is,
128 : kcg->
129 : payment_target_reference);
130 0 : if (NULL == res_cmd)
131 : {
132 0 : GNUNET_break (0);
133 0 : TALER_TESTING_interpreter_fail (kcg->is);
134 0 : return;
135 : }
136 0 : if (GNUNET_OK !=
137 0 : TALER_TESTING_get_trait_legi_requirement_row (res_cmd,
138 : &requirement_row))
139 : {
140 0 : GNUNET_break (0);
141 0 : TALER_TESTING_interpreter_fail (kcg->is);
142 0 : return;
143 : }
144 0 : if (GNUNET_OK !=
145 0 : TALER_TESTING_get_trait_h_payto (res_cmd,
146 : &h_payto))
147 : {
148 0 : GNUNET_break (0);
149 0 : TALER_TESTING_interpreter_fail (kcg->is);
150 0 : return;
151 : }
152 0 : if (0 == *requirement_row)
153 : {
154 0 : GNUNET_break (0);
155 0 : TALER_TESTING_interpreter_fail (kcg->is);
156 0 : return;
157 : }
158 0 : kcg->kwh = TALER_EXCHANGE_kyc_check (is->exchange,
159 : *requirement_row,
160 : h_payto,
161 : TALER_KYCLOGIC_KYC_UT_INDIVIDUAL,
162 : GNUNET_TIME_UNIT_SECONDS,
163 : &check_kyc_cb,
164 : kcg);
165 0 : GNUNET_assert (NULL != kcg->kwh);
166 : }
167 :
168 :
169 : /**
170 : * Cleanup the state from a "track transaction" CMD, and possibly
171 : * cancel a operation thereof.
172 : *
173 : * @param cls closure.
174 : * @param cmd the command which is being cleaned up.
175 : */
176 : static void
177 0 : check_kyc_cleanup (void *cls,
178 : const struct TALER_TESTING_Command *cmd)
179 : {
180 0 : struct KycCheckGetState *kcg = cls;
181 :
182 0 : if (NULL != kcg->kwh)
183 : {
184 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
185 : "Command %u (%s) did not complete\n",
186 : kcg->is->ip,
187 : cmd->label);
188 0 : TALER_EXCHANGE_kyc_check_cancel (kcg->kwh);
189 0 : kcg->kwh = NULL;
190 : }
191 0 : GNUNET_free (kcg->kyc_url);
192 0 : GNUNET_free (kcg);
193 0 : }
194 :
195 :
196 : /**
197 : * Offer internal data from a "check KYC" CMD.
198 : *
199 : * @param cls closure.
200 : * @param[out] ret result (could be anything).
201 : * @param trait name of the trait.
202 : * @param index index number of the object to offer.
203 : * @return #GNUNET_OK on success.
204 : */
205 : static enum GNUNET_GenericReturnValue
206 0 : check_kyc_traits (void *cls,
207 : const void **ret,
208 : const char *trait,
209 : unsigned int index)
210 : {
211 0 : struct KycCheckGetState *kcg = cls;
212 : struct TALER_TESTING_Trait traits[] = {
213 0 : TALER_TESTING_make_trait_kyc_url (
214 0 : (const char **) &kcg->kyc_url),
215 0 : TALER_TESTING_trait_end ()
216 : };
217 :
218 0 : return TALER_TESTING_get_trait (traits,
219 : ret,
220 : trait,
221 : index);
222 : }
223 :
224 :
225 : struct TALER_TESTING_Command
226 0 : TALER_TESTING_cmd_check_kyc_get (const char *label,
227 : const char *payment_target_reference,
228 : unsigned int expected_response_code)
229 : {
230 : struct KycCheckGetState *kcg;
231 :
232 0 : kcg = GNUNET_new (struct KycCheckGetState);
233 0 : kcg->payment_target_reference = payment_target_reference;
234 0 : kcg->expected_response_code = expected_response_code;
235 : {
236 0 : struct TALER_TESTING_Command cmd = {
237 : .cls = kcg,
238 : .label = label,
239 : .run = &check_kyc_run,
240 : .cleanup = &check_kyc_cleanup,
241 : .traits = &check_kyc_traits
242 : };
243 :
244 0 : return cmd;
245 : }
246 : }
247 :
248 :
249 : /* end of testing_api_cmd_kyc_check_get.c */
|