Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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 src/testing/testing_api_cmd_delete_product.c
21 : * @brief command to test DELETE /product/$ID
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : struct DeleteProductState;
26 : #define TALER_MERCHANT_DELETE_PRIVATE_PRODUCT_RESULT_CLOSURE struct DeleteProductState
27 : #include <taler/taler_exchange_service.h>
28 : #include <taler/taler_testing_lib.h>
29 : #include "taler/taler_merchant_service.h"
30 : #include "taler/taler_merchant_testing_lib.h"
31 : #include <taler/merchant/delete-private-products-PRODUCT_ID.h>
32 :
33 :
34 : /**
35 : * State of a "DELETE /products/$ID" CMD.
36 : */
37 : struct DeleteProductState
38 : {
39 :
40 : /**
41 : * Handle for a "DELETE product" request.
42 : */
43 : struct TALER_MERCHANT_DeletePrivateProductHandle *pdh;
44 :
45 : /**
46 : * The interpreter state.
47 : */
48 : struct TALER_TESTING_Interpreter *is;
49 :
50 : /**
51 : * Base URL of the merchant serving the request.
52 : */
53 : const char *merchant_url;
54 :
55 : /**
56 : * ID of the product to run DELETE for.
57 : */
58 : const char *product_id;
59 :
60 : /**
61 : * Expected HTTP response code.
62 : */
63 : unsigned int http_status;
64 :
65 : };
66 :
67 :
68 : /**
69 : * Callback for a /delete/products/$ID operation.
70 : *
71 : * @param cls closure for this function
72 : * @param dpr response being processed
73 : */
74 : static void
75 10 : delete_product_cb (struct DeleteProductState *dis,
76 : const struct TALER_MERCHANT_DeletePrivateProductResponse *dpr
77 : )
78 : {
79 :
80 10 : dis->pdh = NULL;
81 10 : if (dis->http_status != dpr->hr.http_status)
82 : {
83 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
84 : "Unexpected response code %u (%d) to command %s\n",
85 : dpr->hr.http_status,
86 : (int) dpr->hr.ec,
87 : TALER_TESTING_interpreter_get_current_label (dis->is));
88 0 : TALER_TESTING_interpreter_fail (dis->is);
89 0 : return;
90 : }
91 10 : switch (dpr->hr.http_status)
92 : {
93 6 : case MHD_HTTP_NO_CONTENT:
94 6 : break;
95 0 : case MHD_HTTP_UNAUTHORIZED:
96 0 : break;
97 2 : case MHD_HTTP_NOT_FOUND:
98 2 : break;
99 2 : case MHD_HTTP_CONFLICT:
100 2 : break;
101 0 : default:
102 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
103 : "Unhandled HTTP status %u for DELETE product.\n",
104 : dpr->hr.http_status);
105 : }
106 10 : TALER_TESTING_interpreter_next (dis->is);
107 : }
108 :
109 :
110 : /**
111 : * Run the "DELETE product" CMD.
112 : *
113 : *
114 : * @param cls closure.
115 : * @param cmd command being run now.
116 : * @param is interpreter state.
117 : */
118 : static void
119 10 : delete_product_run (void *cls,
120 : const struct TALER_TESTING_Command *cmd,
121 : struct TALER_TESTING_Interpreter *is)
122 : {
123 10 : struct DeleteProductState *dis = cls;
124 :
125 10 : dis->is = is;
126 10 : dis->pdh = TALER_MERCHANT_delete_private_product_create (
127 : TALER_TESTING_interpreter_get_context (is),
128 : dis->merchant_url,
129 : dis->product_id);
130 : {
131 : enum TALER_ErrorCode ec;
132 :
133 10 : ec = TALER_MERCHANT_delete_private_product_start (dis->pdh,
134 : &delete_product_cb,
135 : dis);
136 10 : GNUNET_assert (TALER_EC_NONE == ec);
137 : }
138 10 : }
139 :
140 :
141 : /**
142 : * Free the state of a "DELETE product" CMD, and possibly
143 : * cancel a pending operation thereof.
144 : *
145 : * @param cls closure.
146 : * @param cmd command being run.
147 : */
148 : static void
149 10 : delete_product_cleanup (void *cls,
150 : const struct TALER_TESTING_Command *cmd)
151 : {
152 10 : struct DeleteProductState *dis = cls;
153 :
154 10 : if (NULL != dis->pdh)
155 : {
156 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157 : "DELETE /products/$ID operation did not complete\n");
158 0 : TALER_MERCHANT_delete_private_product_cancel (dis->pdh);
159 : }
160 10 : GNUNET_free (dis);
161 10 : }
162 :
163 :
164 : struct TALER_TESTING_Command
165 10 : TALER_TESTING_cmd_merchant_delete_product (const char *label,
166 : const char *merchant_url,
167 : const char *product_id,
168 : unsigned int http_status)
169 : {
170 : struct DeleteProductState *dis;
171 :
172 10 : dis = GNUNET_new (struct DeleteProductState);
173 10 : dis->merchant_url = merchant_url;
174 10 : dis->product_id = product_id;
175 10 : dis->http_status = http_status;
176 : {
177 10 : struct TALER_TESTING_Command cmd = {
178 : .cls = dis,
179 : .label = label,
180 : .run = &delete_product_run,
181 : .cleanup = &delete_product_cleanup
182 : };
183 :
184 10 : return cmd;
185 : }
186 : }
187 :
188 :
189 : /* end of testing_api_cmd_delete_product.c */
|