comparison tests/src/libirccd/command-rule-edit/main.cpp @ 651:1081e45b8628

Tests: use command_test::request helper, closes #784 @1h
author David Demelier <markand@malikania.fr>
date Tue, 27 Mar 2018 20:12:02 +0200
parents 1fa9e5222e87
children e8c4ba5ed1c6
comparison
equal deleted inserted replaced
650:27896e9bcd9e 651:1081e45b8628
50 50
51 BOOST_FIXTURE_TEST_SUITE(rule_edit_test_suite, rule_edit_test) 51 BOOST_FIXTURE_TEST_SUITE(rule_edit_test_suite, rule_edit_test)
52 52
53 BOOST_AUTO_TEST_CASE(add_server) 53 BOOST_AUTO_TEST_CASE(add_server)
54 { 54 {
55 nlohmann::json result; 55 request({
56
57 ctl_->send({
58 { "command", "rule-edit" }, 56 { "command", "rule-edit" },
59 { "add-servers", { "new-s3" } }, 57 { "add-servers", { "new-s3" } },
60 { "index", 0 } 58 { "index", 0 }
61 }); 59 });
62 ctl_->recv([&] (auto, auto msg) { 60
63 result = msg; 61 const auto result = request({
64 }); 62 { "command", "rule-info" },
65 63 { "index", 0 }
66 wait_for([&] () { 64 });
67 return result.is_object(); 65
68 }); 66 BOOST_TEST(result.first.is_object());
69 67 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
70 BOOST_TEST(result.is_object()); 68 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
71 69 BOOST_TEST(json_util::contains(result.first["servers"], "new-s3"));
72 result = nullptr; 70 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
73 ctl_->send({ 71 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
74 { "command", "rule-info" }, 72 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
75 { "index", 0 } 73 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
76 }); 74 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
77 ctl_->recv([&] (auto, auto msg) { 75 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
78 result = msg; 76 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
79 });
80
81 wait_for([&] () {
82 return result.is_object();
83 });
84
85 BOOST_TEST(result.is_object());
86 BOOST_TEST(json_util::contains(result["servers"], "s1"));
87 BOOST_TEST(json_util::contains(result["servers"], "s2"));
88 BOOST_TEST(json_util::contains(result["servers"], "new-s3"));
89 BOOST_TEST(json_util::contains(result["channels"], "c1"));
90 BOOST_TEST(json_util::contains(result["channels"], "c2"));
91 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
92 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
93 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
94 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
95 BOOST_TEST(result["action"].get<std::string>() == "drop");
96 } 77 }
97 78
98 BOOST_AUTO_TEST_CASE(add_channel) 79 BOOST_AUTO_TEST_CASE(add_channel)
99 { 80 {
100 nlohmann::json result; 81 request({
101
102 ctl_->send({
103 { "command", "rule-edit" }, 82 { "command", "rule-edit" },
104 { "add-channels", { "new-c3" } }, 83 { "add-channels", { "new-c3" } },
105 { "index", 0 } 84 { "index", 0 }
106 }); 85 });
107 ctl_->recv([&] (auto, auto msg) { 86
108 result = msg; 87 const auto result = request({
109 }); 88 { "command", "rule-info" },
110 89 { "index", 0 }
111 wait_for([&] () { 90 });
112 return result.is_object(); 91
113 }); 92 BOOST_TEST(result.first.is_object());
114 93 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
115 BOOST_TEST(result.is_object()); 94 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
116 95 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
117 result = nullptr; 96 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
118 ctl_->send({ 97 BOOST_TEST(json_util::contains(result.first["channels"], "new-c3"));
119 { "command", "rule-info" }, 98 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
120 { "index", 0 } 99 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
121 }); 100 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
122 ctl_->recv([&] (auto, auto msg) { 101 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
123 result = msg; 102 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
124 });
125
126 wait_for([&] () {
127 return result.is_object();
128 });
129
130 BOOST_TEST(result.is_object());
131 BOOST_TEST(json_util::contains(result["servers"], "s1"));
132 BOOST_TEST(json_util::contains(result["servers"], "s2"));
133 BOOST_TEST(json_util::contains(result["channels"], "c1"));
134 BOOST_TEST(json_util::contains(result["channels"], "c2"));
135 BOOST_TEST(json_util::contains(result["channels"], "new-c3"));
136 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
137 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
138 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
139 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
140 BOOST_TEST(result["action"].get<std::string>() == "drop");
141 } 103 }
142 104
143 BOOST_AUTO_TEST_CASE(add_plugin) 105 BOOST_AUTO_TEST_CASE(add_plugin)
144 { 106 {
145 nlohmann::json result; 107 request({
146
147 ctl_->send({
148 { "command", "rule-edit" }, 108 { "command", "rule-edit" },
149 { "add-plugins", { "new-p3" } }, 109 { "add-plugins", { "new-p3" } },
150 { "index", 0 } 110 { "index", 0 }
151 }); 111 });
152 ctl_->recv([&] (auto, auto msg) { 112
153 result = msg; 113 const auto result = request({
154 }); 114 { "command", "rule-info" },
155 115 { "index", 0 }
156 wait_for([&] () { 116 });
157 return result.is_object(); 117
158 }); 118 BOOST_TEST(result.first.is_object());
159 119 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
160 BOOST_TEST(result.is_object()); 120 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
161 121 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
162 result = nullptr; 122 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
163 ctl_->send({ 123 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
164 { "command", "rule-info" }, 124 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
165 { "index", 0 } 125 BOOST_TEST(json_util::contains(result.first["plugins"], "new-p3"));
166 }); 126 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
167 ctl_->recv([&] (auto, auto msg) { 127 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
168 result = msg; 128 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
169 });
170
171 wait_for([&] () {
172 return result.is_object();
173 });
174
175 BOOST_TEST(result.is_object());
176 BOOST_TEST(json_util::contains(result["servers"], "s1"));
177 BOOST_TEST(json_util::contains(result["servers"], "s2"));
178 BOOST_TEST(json_util::contains(result["channels"], "c1"));
179 BOOST_TEST(json_util::contains(result["channels"], "c2"));
180 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
181 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
182 BOOST_TEST(json_util::contains(result["plugins"], "new-p3"));
183 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
184 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
185 BOOST_TEST(result["action"].get<std::string>() == "drop");
186 } 129 }
187 130
188 BOOST_AUTO_TEST_CASE(add_event) 131 BOOST_AUTO_TEST_CASE(add_event)
189 { 132 {
190 nlohmann::json result; 133 request({
191
192 ctl_->send({
193 { "command", "rule-edit" }, 134 { "command", "rule-edit" },
194 { "add-events", { "onQuery" } }, 135 { "add-events", { "onQuery" } },
195 { "index", 0 } 136 { "index", 0 }
196 }); 137 });
197 ctl_->recv([&] (auto, auto msg) { 138
198 result = msg; 139 const auto result = request({
199 }); 140 { "command", "rule-info" },
200 141 { "index", 0 }
201 wait_for([&] () { 142 });
202 return result.is_object(); 143
203 }); 144 BOOST_TEST(result.first.is_object());
204 145 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
205 BOOST_TEST(result.is_object()); 146 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
206 147 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
207 result = nullptr; 148 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
208 ctl_->send({ 149 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
209 { "command", "rule-info" }, 150 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
210 { "index", 0 } 151 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
211 }); 152 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
212 ctl_->recv([&] (auto, auto msg) { 153 BOOST_TEST(json_util::contains(result.first["events"], "onQuery"));
213 result = msg; 154 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
214 });
215
216 wait_for([&] () {
217 return result.is_object();
218 });
219
220 BOOST_TEST(result.is_object());
221 BOOST_TEST(json_util::contains(result["servers"], "s1"));
222 BOOST_TEST(json_util::contains(result["servers"], "s2"));
223 BOOST_TEST(json_util::contains(result["channels"], "c1"));
224 BOOST_TEST(json_util::contains(result["channels"], "c2"));
225 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
226 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
227 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
228 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
229 BOOST_TEST(json_util::contains(result["events"], "onQuery"));
230 BOOST_TEST(result["action"].get<std::string>() == "drop");
231 } 155 }
232 156
233 BOOST_AUTO_TEST_CASE(add_event_and_server) 157 BOOST_AUTO_TEST_CASE(add_event_and_server)
234 { 158 {
235 nlohmann::json result; 159 request({
236
237 ctl_->send({
238 { "command", "rule-edit" }, 160 { "command", "rule-edit" },
239 { "add-servers", { "new-s3" } }, 161 { "add-servers", { "new-s3" } },
240 { "add-events", { "onQuery" } }, 162 { "add-events", { "onQuery" } },
241 { "index", 0 } 163 { "index", 0 }
242 }); 164 });
243 ctl_->recv([&] (auto, auto msg) { 165
244 result = msg; 166 const auto result = request({
245 }); 167 { "command", "rule-info" },
246 168 { "index", 0 }
247 wait_for([&] () { 169 });
248 return result.is_object(); 170
249 }); 171 BOOST_TEST(result.first.is_object());
250 172 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
251 BOOST_TEST(result.is_object()); 173 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
252 174 BOOST_TEST(json_util::contains(result.first["servers"], "new-s3"));
253 result = nullptr; 175 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
254 ctl_->send({ 176 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
255 { "command", "rule-info" }, 177 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
256 { "index", 0 } 178 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
257 }); 179 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
258 ctl_->recv([&] (auto, auto msg) { 180 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
259 result = msg; 181 BOOST_TEST(json_util::contains(result.first["events"], "onQuery"));
260 }); 182 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
261
262 wait_for([&] () {
263 return result.is_object();
264 });
265
266 BOOST_TEST(result.is_object());
267 BOOST_TEST(json_util::contains(result["servers"], "s1"));
268 BOOST_TEST(json_util::contains(result["servers"], "s2"));
269 BOOST_TEST(json_util::contains(result["servers"], "new-s3"));
270 BOOST_TEST(json_util::contains(result["channels"], "c1"));
271 BOOST_TEST(json_util::contains(result["channels"], "c2"));
272 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
273 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
274 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
275 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
276 BOOST_TEST(json_util::contains(result["events"], "onQuery"));
277 BOOST_TEST(result["action"].get<std::string>() == "drop");
278 } 183 }
279 184
280 BOOST_AUTO_TEST_CASE(change_action) 185 BOOST_AUTO_TEST_CASE(change_action)
281 { 186 {
282 nlohmann::json result; 187 request({
283
284 ctl_->send({
285 { "command", "rule-edit" }, 188 { "command", "rule-edit" },
286 { "action", "accept" }, 189 { "action", "accept" },
287 { "index", 0 } 190 { "index", 0 }
288 }); 191 });
289 ctl_->recv([&] (auto, auto msg) { 192
290 result = msg; 193 const auto result = request({
291 }); 194 { "command", "rule-info" },
292 195 { "index", 0 }
293 wait_for([&] () { 196 });
294 return result.is_object(); 197
295 }); 198 BOOST_TEST(result.first.is_object());
296 199 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
297 BOOST_TEST(result.is_object()); 200 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
298 201 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
299 result = nullptr; 202 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
300 ctl_->send({ 203 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
301 { "command", "rule-info" }, 204 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
302 { "index", 0 } 205 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
303 }); 206 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
304 ctl_->recv([&] (auto, auto msg) { 207 BOOST_TEST(result.first["action"].get<std::string>() == "accept");
305 result = msg;
306 });
307
308 wait_for([&] () {
309 return result.is_object();
310 });
311
312 BOOST_TEST(result.is_object());
313 BOOST_TEST(json_util::contains(result["servers"], "s1"));
314 BOOST_TEST(json_util::contains(result["servers"], "s2"));
315 BOOST_TEST(json_util::contains(result["channels"], "c1"));
316 BOOST_TEST(json_util::contains(result["channels"], "c2"));
317 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
318 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
319 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
320 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
321 BOOST_TEST(result["action"].get<std::string>() == "accept");
322 } 208 }
323 209
324 BOOST_AUTO_TEST_CASE(remove_server) 210 BOOST_AUTO_TEST_CASE(remove_server)
325 { 211 {
326 nlohmann::json result; 212 request({
327
328 ctl_->send({
329 { "command", "rule-edit" }, 213 { "command", "rule-edit" },
330 { "remove-servers", { "s2" } }, 214 { "remove-servers", { "s2" } },
331 { "index", 0 } 215 { "index", 0 }
332 }); 216 });
333 ctl_->recv([&] (auto, auto msg) { 217
334 result = msg; 218 const auto result = request({
335 }); 219 { "command", "rule-info" },
336 220 { "index", 0 }
337 wait_for([&] () { 221 });
338 return result.is_object(); 222
339 }); 223 BOOST_TEST(result.first.is_object());
340 224 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
341 BOOST_TEST(result.is_object()); 225 BOOST_TEST(!json_util::contains(result.first["servers"], "s2"));
342 226 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
343 result = nullptr; 227 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
344 ctl_->send({ 228 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
345 { "command", "rule-info" }, 229 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
346 { "index", 0 } 230 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
347 }); 231 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
348 ctl_->recv([&] (auto, auto msg) { 232 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
349 result = msg;
350 });
351
352 wait_for([&] () {
353 return result.is_object();
354 });
355
356 BOOST_TEST(result.is_object());
357 BOOST_TEST(json_util::contains(result["servers"], "s1"));
358 BOOST_TEST(!json_util::contains(result["servers"], "s2"));
359 BOOST_TEST(json_util::contains(result["channels"], "c1"));
360 BOOST_TEST(json_util::contains(result["channels"], "c2"));
361 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
362 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
363 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
364 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
365 BOOST_TEST(result["action"].get<std::string>() == "drop");
366 } 233 }
367 234
368 BOOST_AUTO_TEST_CASE(remove_channel) 235 BOOST_AUTO_TEST_CASE(remove_channel)
369 { 236 {
370 nlohmann::json result; 237 request({
371
372 ctl_->send({
373 { "command", "rule-edit" }, 238 { "command", "rule-edit" },
374 { "remove-channels", { "c2" } }, 239 { "remove-channels", { "c2" } },
375 { "index", 0 } 240 { "index", 0 }
376 }); 241 });
377 ctl_->recv([&] (auto, auto msg) { 242
378 result = msg; 243 const auto result = request({
379 }); 244 { "command", "rule-info" },
380 245 { "index", 0 }
381 wait_for([&] () { 246 });
382 return result.is_object(); 247
383 }); 248 BOOST_TEST(result.first.is_object());
384 249 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
385 BOOST_TEST(result.is_object()); 250 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
386 251 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
387 result = nullptr; 252 BOOST_TEST(!json_util::contains(result.first["channels"], "c2"));
388 ctl_->send({ 253 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
389 { "command", "rule-info" }, 254 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
390 { "index", 0 } 255 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
391 }); 256 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
392 ctl_->recv([&] (auto, auto msg) { 257 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
393 result = msg;
394 });
395
396 wait_for([&] () {
397 return result.is_object();
398 });
399
400 BOOST_TEST(result.is_object());
401 BOOST_TEST(json_util::contains(result["servers"], "s1"));
402 BOOST_TEST(json_util::contains(result["servers"], "s2"));
403 BOOST_TEST(json_util::contains(result["channels"], "c1"));
404 BOOST_TEST(!json_util::contains(result["channels"], "c2"));
405 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
406 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
407 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
408 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
409 BOOST_TEST(result["action"].get<std::string>() == "drop");
410 } 258 }
411 259
412 BOOST_AUTO_TEST_CASE(remove_plugin) 260 BOOST_AUTO_TEST_CASE(remove_plugin)
413 { 261 {
414 nlohmann::json result; 262 request({
415
416 ctl_->send({
417 { "command", "rule-edit" }, 263 { "command", "rule-edit" },
418 { "remove-plugins", { "p2" } }, 264 { "remove-plugins", { "p2" } },
419 { "index", 0 } 265 { "index", 0 }
420 }); 266 });
421 ctl_->recv([&] (auto, auto msg) { 267
422 result = msg; 268 const auto result = request({
423 }); 269 { "command", "rule-info" },
424 270 { "index", 0 }
425 wait_for([&] () { 271 });
426 return result.is_object(); 272
427 }); 273 BOOST_TEST(result.first.is_object());
428 274 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
429 BOOST_TEST(result.is_object()); 275 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
430 276 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
431 result = nullptr; 277 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
432 ctl_->send({ 278 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
433 { "command", "rule-info" }, 279 BOOST_TEST(!json_util::contains(result.first["plugins"], "p2"));
434 { "index", 0 } 280 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
435 }); 281 BOOST_TEST(json_util::contains(result.first["events"], "onCommand"));
436 ctl_->recv([&] (auto, auto msg) { 282 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
437 result = msg;
438 });
439
440 wait_for([&] () {
441 return result.is_object();
442 });
443
444 BOOST_TEST(result.is_object());
445 BOOST_TEST(json_util::contains(result["servers"], "s1"));
446 BOOST_TEST(json_util::contains(result["servers"], "s2"));
447 BOOST_TEST(json_util::contains(result["channels"], "c1"));
448 BOOST_TEST(json_util::contains(result["channels"], "c2"));
449 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
450 BOOST_TEST(!json_util::contains(result["plugins"], "p2"));
451 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
452 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
453 BOOST_TEST(result["action"].get<std::string>() == "drop");
454 } 283 }
455 284
456 BOOST_AUTO_TEST_CASE(remove_event) 285 BOOST_AUTO_TEST_CASE(remove_event)
457 { 286 {
458 nlohmann::json result; 287 request({
459
460 ctl_->send({
461 { "command", "rule-edit" }, 288 { "command", "rule-edit" },
462 { "remove-events", { "onCommand" } }, 289 { "remove-events", { "onCommand" } },
463 { "index", 0 } 290 { "index", 0 }
464 }); 291 });
465 ctl_->recv([&] (auto, auto msg) { 292
466 result = msg; 293 const auto result = request({
467 }); 294 { "command", "rule-info" },
468 295 { "index", 0 }
469 wait_for([&] () { 296 });
470 return result.is_object(); 297
471 }); 298 BOOST_TEST(result.first.is_object());
472 299 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
473 BOOST_TEST(result.is_object()); 300 BOOST_TEST(json_util::contains(result.first["servers"], "s2"));
474 301 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
475 result = nullptr; 302 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
476 ctl_->send({ 303 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
477 { "command", "rule-info" }, 304 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
478 { "index", 0 } 305 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
479 }); 306 BOOST_TEST(!json_util::contains(result.first["events"], "onCommand"));
480 ctl_->recv([&] (auto, auto msg) { 307 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
481 result = msg;
482 });
483
484 wait_for([&] () {
485 return result.is_object();
486 });
487
488 BOOST_TEST(result.is_object());
489 BOOST_TEST(json_util::contains(result["servers"], "s1"));
490 BOOST_TEST(json_util::contains(result["servers"], "s2"));
491 BOOST_TEST(json_util::contains(result["channels"], "c1"));
492 BOOST_TEST(json_util::contains(result["channels"], "c2"));
493 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
494 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
495 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
496 BOOST_TEST(!json_util::contains(result["events"], "onCommand"));
497 BOOST_TEST(result["action"].get<std::string>() == "drop");
498 } 308 }
499 309
500 BOOST_AUTO_TEST_CASE(remove_event_and_server) 310 BOOST_AUTO_TEST_CASE(remove_event_and_server)
501 { 311 {
502 nlohmann::json result; 312 request({
503
504 ctl_->send({
505 { "command", "rule-edit" }, 313 { "command", "rule-edit" },
506 { "remove-servers", { "s2" } }, 314 { "remove-servers", { "s2" } },
507 { "remove-events", { "onCommand" } }, 315 { "remove-events", { "onCommand" } },
508 { "index", 0 } 316 { "index", 0 }
509 }); 317 });
510 ctl_->recv([&] (auto, auto msg) { 318
511 result = msg; 319 const auto result = request({
512 }); 320 { "command", "rule-info" },
513 321 { "index", 0 }
514 wait_for([&] () { 322 });
515 return result.is_object(); 323
516 }); 324 BOOST_TEST(result.first.is_object());
517 325 BOOST_TEST(json_util::contains(result.first["servers"], "s1"));
518 BOOST_TEST(result.is_object()); 326 BOOST_TEST(!json_util::contains(result.first["servers"], "s2"));
519 327 BOOST_TEST(json_util::contains(result.first["channels"], "c1"));
520 result = nullptr; 328 BOOST_TEST(json_util::contains(result.first["channels"], "c2"));
521 ctl_->send({ 329 BOOST_TEST(json_util::contains(result.first["plugins"], "p1"));
522 { "command", "rule-info" }, 330 BOOST_TEST(json_util::contains(result.first["plugins"], "p2"));
523 { "index", 0 } 331 BOOST_TEST(json_util::contains(result.first["events"], "onMessage"));
524 }); 332 BOOST_TEST(!json_util::contains(result.first["events"], "onCommand"));
525 ctl_->recv([&] (auto, auto msg) { 333 BOOST_TEST(result.first["action"].get<std::string>() == "drop");
526 result = msg;
527 });
528
529 wait_for([&] () {
530 return result.is_object();
531 });
532
533 BOOST_TEST(result.is_object());
534 BOOST_TEST(json_util::contains(result["servers"], "s1"));
535 BOOST_TEST(!json_util::contains(result["servers"], "s2"));
536 BOOST_TEST(json_util::contains(result["channels"], "c1"));
537 BOOST_TEST(json_util::contains(result["channels"], "c2"));
538 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
539 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
540 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
541 BOOST_TEST(!json_util::contains(result["events"], "onCommand"));
542 BOOST_TEST(result["action"].get<std::string>() == "drop");
543 } 334 }
544 335
545 BOOST_AUTO_TEST_SUITE(errors) 336 BOOST_AUTO_TEST_SUITE(errors)
546 337
547 BOOST_AUTO_TEST_CASE(invalid_index_1) 338 BOOST_AUTO_TEST_CASE(invalid_index_1)
548 { 339 {
549 boost::system::error_code result; 340 const auto result = request({
550 nlohmann::json message;
551
552 ctl_->send({
553 { "command", "rule-edit" }, 341 { "command", "rule-edit" },
554 { "index", -100 }, 342 { "index", -100 },
555 { "action", "drop" } 343 { "action", "drop" }
556 }); 344 });
557 ctl_->recv([&] (auto rresult, auto rmessage) { 345
558 result = rresult; 346 BOOST_TEST(result.second == rule_error::invalid_index);
559 message = rmessage; 347 BOOST_TEST(result.first["error"].template get<int>() == rule_error::invalid_index);
560 }); 348 BOOST_TEST(result.first["errorCategory"].template get<std::string>() == "rule");
561
562 wait_for([&] {
563 return result;
564 });
565
566 BOOST_TEST(result == rule_error::invalid_index);
567 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
568 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
569 } 349 }
570 350
571 BOOST_AUTO_TEST_CASE(invalid_index_2) 351 BOOST_AUTO_TEST_CASE(invalid_index_2)
572 { 352 {
573 boost::system::error_code result; 353 const auto result = request({
574 nlohmann::json message;
575
576 ctl_->send({
577 { "command", "rule-edit" }, 354 { "command", "rule-edit" },
578 { "index", 100 }, 355 { "index", 100 },
579 { "action", "drop" } 356 { "action", "drop" }
580 }); 357 });
581 ctl_->recv([&] (auto rresult, auto rmessage) { 358
582 result = rresult; 359 BOOST_TEST(result.second == rule_error::invalid_index);
583 message = rmessage; 360 BOOST_TEST(result.first["error"].template get<int>() == rule_error::invalid_index);
584 }); 361 BOOST_TEST(result.first["errorCategory"].template get<std::string>() == "rule");
585
586 wait_for([&] {
587 return result;
588 });
589
590 BOOST_TEST(result == rule_error::invalid_index);
591 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
592 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
593 } 362 }
594 363
595 BOOST_AUTO_TEST_CASE(invalid_index_3) 364 BOOST_AUTO_TEST_CASE(invalid_index_3)
596 { 365 {
597 boost::system::error_code result; 366 const auto result = request({
598 nlohmann::json message;
599
600 ctl_->send({
601 { "command", "rule-edit" }, 367 { "command", "rule-edit" },
602 { "index", "notaint" }, 368 { "index", "notaint" },
603 { "action", "drop" } 369 { "action", "drop" }
604 }); 370 });
605 ctl_->recv([&] (auto rresult, auto rmessage) { 371
606 result = rresult; 372 BOOST_TEST(result.second == rule_error::invalid_index);
607 message = rmessage; 373 BOOST_TEST(result.first["error"].template get<int>() == rule_error::invalid_index);
608 }); 374 BOOST_TEST(result.first["errorCategory"].template get<std::string>() == "rule");
609
610 wait_for([&] {
611 return result;
612 });
613
614 BOOST_TEST(result == rule_error::invalid_index);
615 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
616 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
617 } 375 }
618 376
619 BOOST_AUTO_TEST_CASE(invalid_action) 377 BOOST_AUTO_TEST_CASE(invalid_action)
620 { 378 {
621 boost::system::error_code result; 379 const auto result = request({
622 nlohmann::json message;
623
624 ctl_->send({
625 { "command", "rule-edit" }, 380 { "command", "rule-edit" },
626 { "index", 0 }, 381 { "index", 0 },
627 { "action", "unknown" } 382 { "action", "unknown" }
628 }); 383 });
629 ctl_->recv([&] (auto rresult, auto rmessage) { 384
630 result = rresult; 385 BOOST_TEST(result.second == rule_error::invalid_action);
631 message = rmessage; 386 BOOST_TEST(result.first["error"].template get<int>() == rule_error::invalid_action);
632 }); 387 BOOST_TEST(result.first["errorCategory"].template get<std::string>() == "rule");
633
634 wait_for([&] {
635 return result;
636 });
637
638 BOOST_TEST(result == rule_error::invalid_action);
639 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_action);
640 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
641 } 388 }
642 389
643 BOOST_AUTO_TEST_SUITE_END() 390 BOOST_AUTO_TEST_SUITE_END()
644 391
645 BOOST_AUTO_TEST_SUITE_END() 392 BOOST_AUTO_TEST_SUITE_END()