Mercurial > irccd
comparison tests/src/rule-add-command/main.cpp @ 581:a51b5dd5b761
Tests: put everything in src/
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 04 Dec 2017 14:12:13 +0100 |
parents | tests/rule-add-command/main.cpp@84ea13c850f4 |
children | 029667d16d12 |
comparison
equal
deleted
inserted
replaced
580:2e16c3623531 | 581:a51b5dd5b761 |
---|---|
1 /* | |
2 * main.cpp -- test rule-add remote command | |
3 * | |
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #define BOOST_TEST_MODULE "rule-add" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <irccd/json_util.hpp> | |
23 | |
24 #include <irccd/command.hpp> | |
25 #include <irccd/rule_service.hpp> | |
26 | |
27 #include <command_test.hpp> | |
28 | |
29 namespace irccd { | |
30 | |
31 namespace { | |
32 | |
33 class rule_add_test : public command_test<rule_add_command> { | |
34 public: | |
35 rule_add_test() | |
36 { | |
37 daemon_->commands().add(std::make_unique<rule_list_command>()); | |
38 } | |
39 }; | |
40 | |
41 } // !namespace | |
42 | |
43 BOOST_FIXTURE_TEST_SUITE(rule_add_test_suite, rule_add_test) | |
44 | |
45 BOOST_AUTO_TEST_CASE(basic) | |
46 { | |
47 nlohmann::json result; | |
48 | |
49 ctl_->send({ | |
50 { "command", "rule-add" }, | |
51 { "servers", { "s1", "s2" } }, | |
52 { "channels", { "c1", "c2" } }, | |
53 { "plugins", { "p1", "p2" } }, | |
54 { "events", { "onMessage" } }, | |
55 { "action", "accept" }, | |
56 { "index", 0 } | |
57 }); | |
58 ctl_->recv([&] (auto, auto msg) { | |
59 result = msg; | |
60 }); | |
61 | |
62 wait_for([&] () { | |
63 return result.is_object(); | |
64 }); | |
65 | |
66 BOOST_TEST(result.is_object()); | |
67 | |
68 result = nullptr; | |
69 ctl_->send({{"command", "rule-list"}}); | |
70 ctl_->recv([&] (auto, auto msg) { | |
71 result = msg; | |
72 }); | |
73 | |
74 wait_for([&] () { | |
75 return result.is_object(); | |
76 }); | |
77 | |
78 BOOST_TEST(result.is_object()); | |
79 | |
80 auto servers = result["list"][0]["servers"]; | |
81 auto channels = result["list"][0]["channels"]; | |
82 auto plugins = result["list"][0]["plugins"]; | |
83 auto events = result["list"][0]["events"]; | |
84 | |
85 BOOST_TEST(json_util::contains(servers, "s1")); | |
86 BOOST_TEST(json_util::contains(servers, "s2")); | |
87 BOOST_TEST(json_util::contains(channels, "c1")); | |
88 BOOST_TEST(json_util::contains(channels, "c2")); | |
89 BOOST_TEST(json_util::contains(plugins, "p1")); | |
90 BOOST_TEST(json_util::contains(plugins, "p2")); | |
91 BOOST_TEST(json_util::contains(events, "onMessage")); | |
92 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "accept"); | |
93 } | |
94 | |
95 BOOST_AUTO_TEST_CASE(append) | |
96 { | |
97 nlohmann::json result; | |
98 | |
99 ctl_->send({ | |
100 { "command", "rule-add" }, | |
101 { "servers", { "s1" } }, | |
102 { "channels", { "c1" } }, | |
103 { "plugins", { "p1" } }, | |
104 { "events", { "onMessage" } }, | |
105 { "action", "accept" }, | |
106 { "index", 0 } | |
107 }); | |
108 ctl_->recv([&] (auto, auto msg) { | |
109 result = msg; | |
110 }); | |
111 | |
112 wait_for([&] () { | |
113 return result.is_object(); | |
114 }); | |
115 | |
116 BOOST_TEST(result.is_object()); | |
117 | |
118 result = nullptr; | |
119 ctl_->send({ | |
120 { "command", "rule-add" }, | |
121 { "servers", { "s2" } }, | |
122 { "channels", { "c2" } }, | |
123 { "plugins", { "p2" } }, | |
124 { "events", { "onMessage" } }, | |
125 { "action", "drop" }, | |
126 { "index", 1 } | |
127 }); | |
128 ctl_->recv([&] (auto, auto msg) { | |
129 result = msg; | |
130 }); | |
131 | |
132 wait_for([&] () { | |
133 return result.is_object(); | |
134 }); | |
135 | |
136 BOOST_TEST(result.is_object()); | |
137 | |
138 result = nullptr; | |
139 ctl_->send({{"command", "rule-list"}}); | |
140 ctl_->recv([&] (auto, auto msg) { | |
141 result = msg; | |
142 }); | |
143 | |
144 wait_for([&] () { | |
145 return result.is_object(); | |
146 }); | |
147 | |
148 BOOST_TEST(result.is_object()); | |
149 BOOST_TEST(result["list"].size() == 2U); | |
150 | |
151 // Rule 0. | |
152 { | |
153 auto servers = result["list"][0]["servers"]; | |
154 auto channels = result["list"][0]["channels"]; | |
155 auto plugins = result["list"][0]["plugins"]; | |
156 auto events = result["list"][0]["events"]; | |
157 | |
158 BOOST_TEST(json_util::contains(servers, "s1")); | |
159 BOOST_TEST(json_util::contains(channels, "c1")); | |
160 BOOST_TEST(json_util::contains(plugins, "p1")); | |
161 BOOST_TEST(json_util::contains(events, "onMessage")); | |
162 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "accept"); | |
163 } | |
164 | |
165 // Rule 1. | |
166 { | |
167 auto servers = result["list"][1]["servers"]; | |
168 auto channels = result["list"][1]["channels"]; | |
169 auto plugins = result["list"][1]["plugins"]; | |
170 auto events = result["list"][1]["events"]; | |
171 | |
172 BOOST_TEST(json_util::contains(servers, "s2")); | |
173 BOOST_TEST(json_util::contains(channels, "c2")); | |
174 BOOST_TEST(json_util::contains(plugins, "p2")); | |
175 BOOST_TEST(json_util::contains(events, "onMessage")); | |
176 BOOST_TEST(result["list"][1]["action"].get<std::string>() == "drop"); | |
177 } | |
178 } | |
179 | |
180 BOOST_AUTO_TEST_SUITE(errors) | |
181 | |
182 BOOST_AUTO_TEST_CASE(invalid_action) | |
183 { | |
184 boost::system::error_code result; | |
185 | |
186 ctl_->send({ | |
187 { "command", "rule-add" }, | |
188 { "action", "unknown" } | |
189 }); | |
190 ctl_->recv([&] (auto code, auto) { | |
191 result = code; | |
192 }); | |
193 | |
194 wait_for([&] { | |
195 return result; | |
196 }); | |
197 | |
198 BOOST_ASSERT(result == rule_error::invalid_action); | |
199 } | |
200 | |
201 BOOST_AUTO_TEST_SUITE_END() | |
202 | |
203 BOOST_AUTO_TEST_SUITE_END() | |
204 | |
205 } // !irccd |