Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2020 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_wallet_get_tip.c
21 : * @brief command to test the tipping.
22 : * @author Marcello Stanisci
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 GET /tips/$TIP_ID CMD.
33 : */
34 : struct WalletTipGetState
35 : {
36 :
37 : /**
38 : * The merchant base URL.
39 : */
40 : const char *merchant_url;
41 :
42 : /**
43 : * Expected HTTP response code for this CMD.
44 : */
45 : unsigned int http_status;
46 :
47 : /**
48 : * Whether to compare amounts or not.
49 : */
50 : bool cmp_amounts;
51 :
52 : /**
53 : * The expected amount remaining.
54 : */
55 : struct TALER_Amount amount_remaining;
56 :
57 : /**
58 : * The handle to the current GET /tips/$TIP_ID request.
59 : */
60 : struct TALER_MERCHANT_TipWalletGetHandle *tgh;
61 :
62 : /**
63 : * The interpreter state.
64 : */
65 : struct TALER_TESTING_Interpreter *is;
66 :
67 : /**
68 : * Reference to a command that created a tip.
69 : */
70 : const char *tip_reference;
71 : };
72 :
73 :
74 : /**
75 : * Callback to process a GET /tips/$TIP_ID request, it mainly
76 : * checks that what the backend returned matches the command's
77 : * expectations.
78 : *
79 : * @param cls closure
80 : * @param hr HTTP response
81 : * @param reserve_expiration when the tip reserve will expire
82 : * @param exchange_url from where to pick up the tip
83 : * @param amount_remaining how much is remaining
84 : */
85 : static void
86 0 : wallet_tip_get_cb (void *cls,
87 : const struct TALER_MERCHANT_HttpResponse *hr,
88 : struct GNUNET_TIME_Timestamp reserve_expiration,
89 : const char *exchange_url,
90 : const struct TALER_Amount *amount_remaining)
91 : {
92 0 : struct WalletTipGetState *gts = cls;
93 : const struct TALER_TESTING_Command *tip_cmd;
94 :
95 0 : tip_cmd = TALER_TESTING_interpreter_lookup_command (
96 : gts->is,
97 : gts->tip_reference);
98 :
99 0 : gts->tgh = NULL;
100 0 : if (gts->http_status != hr->http_status)
101 : {
102 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 : "Unexpected response code %u (%d) to command %s\n",
104 : hr->http_status,
105 : (int) hr->ec,
106 : TALER_TESTING_interpreter_get_current_label (gts->is));
107 0 : TALER_TESTING_interpreter_fail (gts->is);
108 0 : return;
109 : }
110 0 : switch (hr->http_status)
111 : {
112 0 : case MHD_HTTP_OK:
113 0 : if (gts->cmp_amounts)
114 : {
115 0 : if ((GNUNET_OK != TALER_amount_cmp_currency (>s->amount_remaining,
116 0 : amount_remaining)) ||
117 0 : (0 != TALER_amount_cmp (>s->amount_remaining,
118 : amount_remaining)))
119 : {
120 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121 : "Amount remaining on tip does not match\n");
122 0 : TALER_TESTING_interpreter_fail (gts->is);
123 0 : return;
124 : }
125 : }
126 : {
127 : const struct GNUNET_TIME_Timestamp *expiration;
128 :
129 0 : if (GNUNET_OK !=
130 0 : TALER_TESTING_get_trait_timestamp (tip_cmd,
131 : 0,
132 : &expiration))
133 0 : TALER_TESTING_interpreter_fail (gts->is);
134 0 : if (GNUNET_TIME_timestamp_cmp (*expiration,
135 : !=,
136 : reserve_expiration))
137 : {
138 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139 : "Tip expiration does not match\n");
140 0 : TALER_TESTING_interpreter_fail (gts->is);
141 0 : return;
142 : }
143 : }
144 0 : break;
145 0 : default:
146 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147 : "Unhandled HTTP status.\n");
148 : }
149 0 : TALER_TESTING_interpreter_next (gts->is);
150 : }
151 :
152 :
153 : /**
154 : * Run the "GET tip" CMD.
155 : *
156 : * @param cls closure.
157 : * @param cmd command being run now.
158 : * @param is interpreter state.
159 : */
160 : static void
161 0 : wallet_get_tip_run (void *cls,
162 : const struct TALER_TESTING_Command *cmd,
163 : struct TALER_TESTING_Interpreter *is)
164 : {
165 0 : struct WalletTipGetState *tgs = cls;
166 : const struct TALER_TESTING_Command *tip_cmd;
167 : const struct TALER_TipIdentifierP *tip_id;
168 :
169 0 : tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
170 : tgs->tip_reference);
171 :
172 0 : if (GNUNET_OK !=
173 0 : TALER_TESTING_get_trait_tip_id (tip_cmd,
174 : &tip_id))
175 0 : TALER_TESTING_FAIL (is);
176 :
177 0 : tgs->is = is;
178 0 : tgs->tgh = TALER_MERCHANT_wallet_tip_get (is->ctx,
179 : tgs->merchant_url,
180 : tip_id,
181 : &wallet_tip_get_cb,
182 : tgs);
183 : }
184 :
185 :
186 : /**
187 : * Free the state of a "GET tip" CMD, and possibly
188 : * cancel a pending operation thereof.
189 : *
190 : * @param cls closure.
191 : * @param cmd command being run.
192 : */
193 : static void
194 0 : wallet_get_tip_cleanup (void *cls,
195 : const struct TALER_TESTING_Command *cmd)
196 : {
197 0 : struct WalletTipGetState *tgs = cls;
198 :
199 0 : if (NULL != tgs->tgh)
200 : {
201 0 : TALER_LOG_WARNING ("Get tip operation did not complete\n");
202 0 : TALER_MERCHANT_wallet_tip_get_cancel (tgs->tgh);
203 : }
204 0 : GNUNET_free (tgs);
205 0 : }
206 :
207 :
208 : struct TALER_TESTING_Command
209 0 : TALER_TESTING_cmd_wallet_get_tip (const char *label,
210 : const char *merchant_url,
211 : const char *tip_reference,
212 : unsigned int http_status)
213 : {
214 : struct WalletTipGetState *tgs;
215 :
216 0 : tgs = GNUNET_new (struct WalletTipGetState);
217 0 : tgs->merchant_url = merchant_url;
218 0 : tgs->tip_reference = tip_reference;
219 0 : tgs->http_status = http_status;
220 : {
221 0 : struct TALER_TESTING_Command cmd = {
222 : .cls = tgs,
223 : .label = label,
224 : .run = &wallet_get_tip_run,
225 : .cleanup = &wallet_get_tip_cleanup
226 : };
227 :
228 0 : return cmd;
229 : }
230 : }
231 :
232 :
233 : struct TALER_TESTING_Command
234 0 : TALER_TESTING_cmd_wallet_get_tip2 (const char *label,
235 : const char *merchant_url,
236 : const char *tip_reference,
237 : const char *amount_remaining,
238 : unsigned int http_status)
239 : {
240 : struct WalletTipGetState *tgs;
241 :
242 0 : tgs = GNUNET_new (struct WalletTipGetState);
243 0 : tgs->merchant_url = merchant_url;
244 0 : tgs->tip_reference = tip_reference;
245 0 : tgs->cmp_amounts = true;
246 0 : GNUNET_assert (GNUNET_OK == TALER_string_to_amount (amount_remaining,
247 : &tgs->amount_remaining));
248 0 : tgs->http_status = http_status;
249 : {
250 0 : struct TALER_TESTING_Command cmd = {
251 : .cls = tgs,
252 : .label = label,
253 : .run = &wallet_get_tip_run,
254 : .cleanup = &wallet_get_tip_cleanup
255 : };
256 :
257 0 : return cmd;
258 : }
259 : }
260 :
261 :
262 : /* end of testing_api_cmd_wallet_get_tip.c */
|