Mercurial > irccd
changeset 308:02b86903157f
Tests: add test for server-kick, #559
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 19 Oct 2016 18:17:44 +0200 |
parents | 2fa974c03282 |
children | 2f72a42ba595 |
files | libirccd/irccd/cmd-server-kick.cpp libirccd/irccd/cmd-server-kick.hpp tests/CMakeLists.txt tests/cmd-server-kick/CMakeLists.txt tests/cmd-server-kick/main.cpp |
diffstat | 5 files changed, 141 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/libirccd/irccd/cmd-server-kick.cpp Wed Oct 19 18:04:57 2016 +0200 +++ b/libirccd/irccd/cmd-server-kick.cpp Wed Oct 19 18:17:44 2016 +0200 @@ -20,60 +20,26 @@ #include "irccd.hpp" #include "server.hpp" #include "service-server.hpp" +#include "transport.hpp" +#include "util.hpp" namespace irccd { namespace command { ServerKickCommand::ServerKickCommand() - : Command("server-kick", "Server", "Kick someone from a channel") + : Command("server-kick") { } -std::vector<Command::Arg> ServerKickCommand::args() const -{ - return { - { "server", true }, - { "target", true }, - { "channel", true }, - { "reason", false } - }; -} - -std::vector<Command::Property> ServerKickCommand::properties() const -{ - return { - { "server", { nlohmann::json::value_t::string }}, - { "target", { nlohmann::json::value_t::string }}, - { "channel", { nlohmann::json::value_t::string }} - }; -} - -nlohmann::json ServerKickCommand::request(Irccdctl &, const CommandRequest &args) const +void ServerKickCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - auto req = nlohmann::json::object({ - { "server", args.arg(0) }, - { "target", args.arg(1) }, - { "channel", args.arg(2) } - }); - - if (args.length() == 4) - req.push_back({"reason", args.arg(3)}); - - return req; -} - -nlohmann::json ServerKickCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - irccd.servers().require(request["server"])->kick( - request["target"], - request["channel"], - request.count("reason") > 0 ? request["reason"] : "" + irccd.servers().require(util::json::requireIdentifier(args, "server"))->kick( + util::json::requireString(args, "target"), + util::json::requireString(args, "channel"), + util::json::getString(args, "reason") ); - - return nlohmann::json::object(); + client.success("server-kick"); } } // !command
--- a/libirccd/irccd/cmd-server-kick.hpp Wed Oct 19 18:04:57 2016 +0200 +++ b/libirccd/irccd/cmd-server-kick.hpp Wed Oct 19 18:17:44 2016 +0200 @@ -41,24 +41,9 @@ IRCCD_EXPORT ServerKickCommand(); /** - * \copydoc Command::args - */ - IRCCD_EXPORT std::vector<Arg> args() const override; - - /** - * \copydoc Command::properties - */ - IRCCD_EXPORT std::vector<Property> properties() const override; - - /** - * \copydoc Command::request - */ - IRCCD_EXPORT nlohmann::json request(Irccdctl &irccdctl, const CommandRequest &args) const override; - - /** * \copydoc Command::exec */ - IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override; + IRCCD_EXPORT void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override; }; } // !command
--- a/tests/CMakeLists.txt Wed Oct 19 18:04:57 2016 +0200 +++ b/tests/CMakeLists.txt Wed Oct 19 18:17:44 2016 +0200 @@ -26,6 +26,7 @@ add_subdirectory(cmd-server-info) add_subdirectory(cmd-server-invite) add_subdirectory(cmd-server-join) + add_subdirectory(cmd-server-kick) add_subdirectory(cmd-server-list) add_subdirectory(cmd-server-me) add_subdirectory(cmd-server-message)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-kick/CMakeLists.txt Wed Oct 19 18:17:44 2016 +0200 @@ -0,0 +1,24 @@ +# +# CMakeLists.txt -- CMake build system for irccd +# +# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +irccd_define_test( + NAME cmd-server-kick + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-kick/main.cpp Wed Oct 19 18:17:44 2016 +0200 @@ -0,0 +1,106 @@ +/* + * main.cpp -- test server-kick remote command + * + * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <cmd-server-kick.hpp> +#include <command-tester.hpp> +#include <server-tester.hpp> + +using namespace irccd; +using namespace irccd::command; + +namespace { + +std::string target; +std::string channel; +std::string reason; + +} // !namespace + +class ServerKickTest : public ServerTester { +public: + void kick(std::string target, std::string channel, std::string reason) override + { + ::target = target; + ::channel = channel; + ::reason = reason; + } +}; + +class ServerKickCommandTest : public CommandTester { +public: + ServerKickCommandTest() + : CommandTester(std::make_unique<ServerKickCommand>(), + std::make_unique<ServerKickTest>()) + { + target.clear(); + channel.clear(); + reason.clear(); + } +}; + +TEST_F(ServerKickCommandTest, basic) +{ + try { + m_irccdctl.client().request({ + { "command", "server-kick" }, + { "server", "test" }, + { "target", "francis" }, + { "channel", "#staff" }, + { "reason", "too noisy" } + }); + + poll([&] () { + return !target.empty() && !channel.empty(); + }); + + ASSERT_EQ("francis", target); + ASSERT_EQ("#staff", channel); + ASSERT_EQ("too noisy", reason); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(ServerKickCommandTest, noreason) +{ + try { + m_irccdctl.client().request({ + { "command", "server-kick" }, + { "server", "test" }, + { "target", "francis" }, + { "channel", "#staff" } + }); + + poll([&] () { + return !target.empty() && !channel.empty(); + }); + + ASSERT_EQ("francis", target); + ASSERT_EQ("#staff", channel); + ASSERT_EQ("", reason); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}