Mercurial > irccd
comparison tests/cmd-rule-add/main.cpp @ 452:2170aa0e38aa
Irccdctl: implement rule-add
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 20 Jul 2017 22:56:34 +0200 |
parents | |
children | 7e273b7f4f92 |
comparison
equal
deleted
inserted
replaced
451:1fdedd2977d2 | 452:2170aa0e38aa |
---|---|
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 #include <command.hpp> | |
20 #include <command-tester.hpp> | |
21 #include <service.hpp> | |
22 | |
23 using namespace irccd; | |
24 using namespace irccd::command; | |
25 | |
26 class RuleAddCommandTest : public CommandTester { | |
27 protected: | |
28 nlohmann::json m_result; | |
29 | |
30 /* | |
31 * Rule sets are unordered so use this function to search a string in | |
32 * the JSON array. | |
33 */ | |
34 inline bool contains(const nlohmann::json &array, const std::string &str) | |
35 { | |
36 for (const auto &v : array) | |
37 if (v.is_string() && v == str) | |
38 return true; | |
39 | |
40 return false; | |
41 } | |
42 | |
43 public: | |
44 RuleAddCommandTest() | |
45 : CommandTester(std::make_unique<RuleAddCommand>()) | |
46 { | |
47 m_irccd.commands().add(std::make_unique<RuleListCommand>()); | |
48 m_irccdctl.client().onMessage.connect([&] (auto result) { | |
49 m_result = result; | |
50 }); | |
51 } | |
52 }; | |
53 | |
54 TEST_F(RuleAddCommandTest, basic) | |
55 { | |
56 m_irccdctl.client().request({ | |
57 { "command", "rule-add" }, | |
58 { "servers", { "s1", "s2" } }, | |
59 { "channels", { "c1", "c2" } }, | |
60 { "plugins", { "p1", "p2" } }, | |
61 { "events", { "onMessage" } }, | |
62 { "action", "accept" }, | |
63 { "index", 0 } | |
64 }); | |
65 | |
66 try { | |
67 poll([&] () { | |
68 return m_result.is_object(); | |
69 }); | |
70 | |
71 ASSERT_TRUE(m_result.is_object()); | |
72 ASSERT_TRUE(m_result["status"].get<bool>()); | |
73 | |
74 m_result = nullptr; | |
75 m_irccdctl.client().request({{ "command", "rule-list" }}); | |
76 | |
77 poll([&] () { | |
78 return m_result.is_object(); | |
79 }); | |
80 | |
81 ASSERT_TRUE(m_result.is_object()); | |
82 ASSERT_TRUE(m_result["status"].get<bool>()); | |
83 | |
84 auto servers = m_result["list"][0]["servers"]; | |
85 auto channels = m_result["list"][0]["channels"]; | |
86 auto plugins = m_result["list"][0]["plugins"]; | |
87 auto events = m_result["list"][0]["events"]; | |
88 | |
89 ASSERT_TRUE(contains(servers, "s1")); | |
90 ASSERT_TRUE(contains(servers, "s2")); | |
91 ASSERT_TRUE(contains(channels, "c1")); | |
92 ASSERT_TRUE(contains(channels, "c2")); | |
93 ASSERT_TRUE(contains(plugins, "p1")); | |
94 ASSERT_TRUE(contains(plugins, "p2")); | |
95 ASSERT_TRUE(contains(events, "onMessage")); | |
96 ASSERT_EQ("accept", m_result["list"][0]["action"]); | |
97 } catch (const std::exception &ex) { | |
98 FAIL() << ex.what(); | |
99 } | |
100 } | |
101 | |
102 TEST_F(RuleAddCommandTest, append) | |
103 { | |
104 try { | |
105 m_irccdctl.client().request({ | |
106 { "command", "rule-add" }, | |
107 { "servers", { "s1" } }, | |
108 { "channels", { "c1" } }, | |
109 { "plugins", { "p1" } }, | |
110 { "events", { "onMessage" } }, | |
111 { "action", "accept" }, | |
112 { "index", 0 } | |
113 }); | |
114 | |
115 poll([&] () { | |
116 return m_result.is_object(); | |
117 }); | |
118 | |
119 ASSERT_TRUE(m_result.is_object()); | |
120 ASSERT_TRUE(m_result["status"].get<bool>()); | |
121 | |
122 m_result = nullptr; | |
123 m_irccdctl.client().request({ | |
124 { "command", "rule-add" }, | |
125 { "servers", { "s2" } }, | |
126 { "channels", { "c2" } }, | |
127 { "plugins", { "p2" } }, | |
128 { "events", { "onMessage" } }, | |
129 { "action", "drop" }, | |
130 { "index", 1 } | |
131 }); | |
132 | |
133 poll([&] () { | |
134 return m_result.is_object(); | |
135 }); | |
136 | |
137 ASSERT_TRUE(m_result.is_object()); | |
138 ASSERT_TRUE(m_result["status"].get<bool>()); | |
139 | |
140 m_result = nullptr; | |
141 m_irccdctl.client().request({{ "command", "rule-list" }}); | |
142 | |
143 poll([&] () { | |
144 return m_result.is_object(); | |
145 }); | |
146 | |
147 ASSERT_TRUE(m_result.is_object()); | |
148 ASSERT_TRUE(m_result["status"].get<bool>()); | |
149 ASSERT_EQ(2U, m_result["list"].size()); | |
150 | |
151 // Rule 0. | |
152 { | |
153 auto servers = m_result["list"][0]["servers"]; | |
154 auto channels = m_result["list"][0]["channels"]; | |
155 auto plugins = m_result["list"][0]["plugins"]; | |
156 auto events = m_result["list"][0]["events"]; | |
157 | |
158 ASSERT_TRUE(contains(servers, "s1")); | |
159 ASSERT_TRUE(contains(channels, "c1")); | |
160 ASSERT_TRUE(contains(plugins, "p1")); | |
161 ASSERT_TRUE(contains(events, "onMessage")); | |
162 ASSERT_EQ("accept", m_result["list"][0]["action"]); | |
163 } | |
164 | |
165 // Rule 1. | |
166 { | |
167 auto servers = m_result["list"][1]["servers"]; | |
168 auto channels = m_result["list"][1]["channels"]; | |
169 auto plugins = m_result["list"][1]["plugins"]; | |
170 auto events = m_result["list"][1]["events"]; | |
171 | |
172 ASSERT_TRUE(contains(servers, "s2")); | |
173 ASSERT_TRUE(contains(channels, "c2")); | |
174 ASSERT_TRUE(contains(plugins, "p2")); | |
175 ASSERT_TRUE(contains(events, "onMessage")); | |
176 ASSERT_EQ("drop", m_result["list"][1]["action"]); | |
177 } | |
178 } catch (const std::exception &ex) { | |
179 FAIL() << ex.what(); | |
180 } | |
181 } | |
182 | |
183 int main(int argc, char **argv) | |
184 { | |
185 testing::InitGoogleTest(&argc, argv); | |
186 | |
187 return RUN_ALL_TESTS(); | |
188 } |