comparison tests/cmd-rule-remove/main.cpp @ 557:c729f06c6f27

Tests: convert cmd-rule-*, #593
author David Demelier <markand@malikania.fr>
date Sat, 25 Nov 2017 14:34:20 +0100
parents 7e273b7f4f92
children 067240931226
comparison
equal deleted inserted replaced
556:c9b703f923d0 557:c729f06c6f27
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 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 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <command.hpp> 19 #define BOOST_TEST_MODULE "rule-remove"
20 #include <command-tester.hpp> 20 #include <boost/test/unit_test.hpp>
21 #include <service.hpp>
22 21
23 using namespace irccd; 22 #include <irccd/json_util.hpp>
24 23
25 class RuleRemoveCommandTest : public CommandTester { 24 #include <irccd/command.hpp>
26 protected: 25 #include <irccd/rule_service.hpp>
27 nlohmann::json m_result;
28 26
29 /* 27 #include <command_test.hpp>
30 * Rule sets are unordered so use this function to search a string in 28
31 * the JSON array. 29 namespace irccd {
32 */ 30
33 inline bool contains(const nlohmann::json &array, const std::string &str) 31 namespace {
32
33 class rule_remove_test : public command_test<rule_remove_command> {
34 public:
35 rule_remove_test()
34 { 36 {
35 for (const auto &v : array) 37 daemon_->commands().add(std::make_unique<rule_list_command>());
36 if (v.is_string() && v == str) 38 daemon_->rules().add(rule(
37 return true;
38
39 return false;
40 }
41
42 public:
43 RuleRemoveCommandTest()
44 : CommandTester(std::make_unique<rule_remove_command>())
45 {
46 m_irccd.commands().add(std::make_unique<rule_list_command>());
47 m_irccd.rules().add(rule(
48 { "s1", "s2" }, 39 { "s1", "s2" },
49 { "c1", "c2" }, 40 { "c1", "c2" },
50 { "o1", "o2" }, 41 { "o1", "o2" },
51 { "p1", "p2" }, 42 { "p1", "p2" },
52 { "onMessage", "onCommand" }, 43 { "onMessage", "onCommand" },
53 rule::action_type::drop 44 rule::action_type::drop
54 )); 45 ));
55 m_irccd.rules().add(rule( 46 daemon_->rules().add(rule(
56 { "s1", }, 47 { "s1", },
57 { "c1", }, 48 { "c1", },
58 { "o1", }, 49 { "o1", },
59 { "p1", }, 50 { "p1", },
60 { "onMessage", }, 51 { "onMessage", },
61 rule::action_type::accept 52 rule::action_type::accept
62 )); 53 ));
63 m_irccdctl.client().onMessage.connect([&] (auto result) {
64 m_result = result;
65 });
66 } 54 }
67 }; 55 };
68 56
69 TEST_F(RuleRemoveCommandTest, basic) 57 } // !namespace
58
59 BOOST_FIXTURE_TEST_SUITE(rule_remove_test_suite, rule_remove_test)
60
61 BOOST_AUTO_TEST_CASE(basic)
70 { 62 {
71 try { 63 nlohmann::json result;
72 m_irccdctl.client().request({
73 { "command", "rule-remove" },
74 { "index", 1 }
75 });
76 64
77 poll([&] () { 65 ctl_->send({
78 return m_result.is_object(); 66 { "command", "rule-remove" },
79 }); 67 { "index", 1 }
68 });
69 ctl_->recv([&] (auto, auto msg) {
70 result = msg;
71 });
80 72
81 ASSERT_TRUE(m_result.is_object()); 73 wait_for([&] () {
82 ASSERT_TRUE(m_result["status"].get<bool>()); 74 return result.is_object();
75 });
83 76
84 m_result = nullptr; 77 BOOST_TEST(result.is_object());
85 m_irccdctl.client().request({{ "command", "rule-list" }});
86 78
87 poll([&] () { 79 result = nullptr;
88 return m_result.is_object(); 80 ctl_->send({{ "command", "rule-list" }});
89 }); 81 ctl_->recv([&] (auto, auto msg) {
82 result = msg;
83 });
90 84
91 ASSERT_TRUE(m_result["list"].is_array()); 85 wait_for([&] () {
92 ASSERT_EQ(1U, m_result["list"].size()); 86 return result.is_object();
87 });
93 88
94 auto servers = m_result["list"][0]["servers"]; 89 BOOST_TEST(result["list"].is_array());
95 auto channels = m_result["list"][0]["channels"]; 90 BOOST_TEST(result["list"].size() == 1U);
96 auto plugins = m_result["list"][0]["plugins"];
97 auto events = m_result["list"][0]["events"];
98 91
99 ASSERT_TRUE(contains(servers, "s1")); 92 auto servers = result["list"][0]["servers"];
100 ASSERT_TRUE(contains(servers, "s2")); 93 auto channels = result["list"][0]["channels"];
101 ASSERT_TRUE(contains(channels, "c1")); 94 auto plugins = result["list"][0]["plugins"];
102 ASSERT_TRUE(contains(channels, "c2")); 95 auto events = result["list"][0]["events"];
103 ASSERT_TRUE(contains(plugins, "p1")); 96
104 ASSERT_TRUE(contains(plugins, "p2")); 97 BOOST_TEST(json_util::contains(servers, "s1"));
105 ASSERT_TRUE(contains(events, "onMessage")); 98 BOOST_TEST(json_util::contains(servers, "s2"));
106 ASSERT_TRUE(contains(events, "onCommand")); 99 BOOST_TEST(json_util::contains(channels, "c1"));
107 ASSERT_EQ("drop", m_result["list"][0]["action"]); 100 BOOST_TEST(json_util::contains(channels, "c2"));
108 } catch (const std::exception &ex) { 101 BOOST_TEST(json_util::contains(plugins, "p1"));
109 FAIL() << ex.what(); 102 BOOST_TEST(json_util::contains(plugins, "p2"));
110 } 103 BOOST_TEST(json_util::contains(events, "onMessage"));
104 BOOST_TEST(json_util::contains(events, "onCommand"));
105 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "drop");
111 } 106 }
112 107
113 TEST_F(RuleRemoveCommandTest, empty) 108 BOOST_AUTO_TEST_CASE(empty)
114 { 109 {
115 m_irccd.rules().remove(0); 110 nlohmann::json result;
116 m_irccd.rules().remove(0);
117 111
118 try { 112 daemon_->rules().remove(0);
119 m_irccdctl.client().request({ 113 daemon_->rules().remove(0);
120 { "command", "rule-remove" },
121 { "index", 1 }
122 });
123 114
124 poll([&] () { 115 ctl_->send({
125 return m_result.is_object(); 116 { "command", "rule-remove" },
126 }); 117 { "index", 1 }
118 });
119 ctl_->recv([&] (auto, auto msg) {
120 result = msg;
121 });
127 122
128 ASSERT_TRUE(m_result.is_object()); 123 wait_for([&] () {
129 ASSERT_FALSE(m_result["status"].get<bool>()); 124 return result.is_object();
130 } catch (const std::exception &ex) { 125 });
131 FAIL() << ex.what(); 126
132 } 127 BOOST_TEST(result.is_object());
133 } 128 }
134 129
135 TEST_F(RuleRemoveCommandTest, outOfBounds) 130 BOOST_AUTO_TEST_CASE(out_of_bounds)
136 { 131 {
137 try { 132 nlohmann::json result;
138 m_irccdctl.client().request({
139 { "command", "rule-remove" },
140 { "index", 123 }
141 });
142 133
143 poll([&] () { 134 ctl_->send({
144 return m_result.is_object(); 135 { "command", "rule-remove" },
145 }); 136 { "index", 123 }
137 });
138 ctl_->recv([&] (auto, auto msg) {
139 result = msg;
140 });
146 141
147 ASSERT_TRUE(m_result.is_object()); 142 wait_for([&] () {
148 ASSERT_FALSE(m_result["status"].get<bool>()); 143 return result.is_object();
149 } catch (const std::exception &ex) { 144 });
150 FAIL() << ex.what(); 145
151 } 146 // TODO: error code
147 BOOST_TEST(result.is_object());
148 BOOST_TEST(result.count("error"));
152 } 149 }
153 150
154 int main(int argc, char **argv) 151 BOOST_AUTO_TEST_SUITE_END()
155 {
156 testing::InitGoogleTest(&argc, argv);
157 152
158 return RUN_ALL_TESTS(); 153 } // !irccd
159 }