# HG changeset patch # User David Demelier # Date 1476877392 -7200 # Node ID a76ccf092570437f89bc47fde288fe0d7359850e # Parent c970b293d8cf73f6f2dc041c8cb1e5f5a20d8285 Tests: add test for server-cmode, #559 diff -r c970b293d8cf -r a76ccf092570 libirccd/irccd/cmd-server-cmode.cpp --- a/libirccd/irccd/cmd-server-cmode.cpp Sun Oct 09 12:09:17 2016 +0200 +++ b/libirccd/irccd/cmd-server-cmode.cpp Wed Oct 19 13:43:12 2016 +0200 @@ -20,44 +20,25 @@ #include "irccd.hpp" #include "server.hpp" #include "service-server.hpp" +#include "transport.hpp" +#include "util.hpp" namespace irccd { namespace command { ServerChannelModeCommand::ServerChannelModeCommand() - : Command("server-cmode", "Server", "Change a channel mode") + : Command("server-cmode") { } -std::vector ServerChannelModeCommand::args() const -{ - return { - { "server", true }, - { "channel", true }, - { "mode", true } - }; -} - -std::vector ServerChannelModeCommand::properties() const +void ServerChannelModeCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - return { - { "server", { nlohmann::json::value_t::string }}, - { "channel", { nlohmann::json::value_t::string }}, - { "mode", { nlohmann::json::value_t::string }} - }; -} - -nlohmann::json ServerChannelModeCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - irccd.servers().require(request["server"].get())->cmode( - request["channel"].get(), - request["mode"].get() + irccd.servers().require(util::json::requireIdentifier(args, "server"))->cmode( + util::json::requireString(args, "channel"), + util::json::requireString(args, "mode") ); - - return nlohmann::json::object(); + client.success("server-cmode"); } } // !command diff -r c970b293d8cf -r a76ccf092570 libirccd/irccd/cmd-server-cmode.hpp --- a/libirccd/irccd/cmd-server-cmode.hpp Sun Oct 09 12:09:17 2016 +0200 +++ b/libirccd/irccd/cmd-server-cmode.hpp Wed Oct 19 13:43:12 2016 +0200 @@ -41,19 +41,9 @@ IRCCD_EXPORT ServerChannelModeCommand(); /** - * \copydoc Command::args - */ - IRCCD_EXPORT std::vector args() const override; - - /** - * \copydoc Command::properties - */ - IRCCD_EXPORT std::vector properties() 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 diff -r c970b293d8cf -r a76ccf092570 tests/CMakeLists.txt --- a/tests/CMakeLists.txt Sun Oct 09 12:09:17 2016 +0200 +++ b/tests/CMakeLists.txt Wed Oct 19 13:43:12 2016 +0200 @@ -20,6 +20,7 @@ project(tests) if (WITH_TESTS) + add_subdirectory(cmd-server-cmode) add_subdirectory(cmd-server-message) # # Misc diff -r c970b293d8cf -r a76ccf092570 tests/cmd-server-cmode/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-cmode/CMakeLists.txt Wed Oct 19 13:43:12 2016 +0200 @@ -0,0 +1,24 @@ +# +# CMakeLists.txt -- CMake build system for irccd +# +# Copyright (c) 2013-2016 David Demelier +# +# 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-cmode + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +) + diff -r c970b293d8cf -r a76ccf092570 tests/cmd-server-cmode/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-cmode/main.cpp Wed Oct 19 13:43:12 2016 +0200 @@ -0,0 +1,76 @@ +/* + * main.cpp -- test server-cmode remote command + * + * Copyright (c) 2013-2016 David Demelier + * + * 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 +#include +#include + +using namespace irccd; +using namespace irccd::command; + +namespace { + +std::string channel; +std::string mode; + +} // !namespace + +class ServerChannelModeTest : public ServerTester { +public: + void cmode(std::string channel, std::string mode) + { + ::channel = channel; + ::mode = mode; + } +}; + +class ServerChannelModeCommandTest : public CommandTester { +public: + ServerChannelModeCommandTest() + : CommandTester(std::make_unique(), + std::make_unique()) + { + m_irccdctl.client().request({ + { "command", "server-cmode" }, + { "server", "test" }, + { "channel", "#staff" }, + { "mode", "+c" } + }); + } +}; + +TEST_F(ServerChannelModeCommandTest, basic) +{ + try { + poll([&] () { + return !channel.empty() && !mode.empty(); + }); + + ASSERT_EQ("#staff", channel); + ASSERT_EQ("+c", mode); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}