view tests/src/libirccd-daemon/command-server-message/main.cpp @ 830:4f7e46e593fe

tests: use command directly, closes #991 @4h
author David Demelier <markand@malikania.fr>
date Mon, 11 Feb 2019 20:30:00 +0100
parents 06cc2f95f479
children 5e25439fe98d
line wrap: on
line source

/*
 * main.cpp -- test server-message remote command
 *
 * Copyright (c) 2013-2019 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.
 */

#define BOOST_TEST_MODULE "server-message"
#include <boost/test/unit_test.hpp>

#include <irccd/test/command_fixture.hpp>

namespace irccd {

namespace {

BOOST_FIXTURE_TEST_SUITE(server_message_fixture_suite, test::command_fixture)

BOOST_AUTO_TEST_CASE(basic)
{
	const auto json = request({
		{ "command",    "server-message"        },
		{ "server",     "test"                  },
		{ "target",     "#staff"                },
		{ "message",    "plop!"                 }
	});

	const auto cmd = server_->find("message").back();

	BOOST_TEST(json.size() == 1U);
	BOOST_TEST(json["command"].get<std::string>() == "server-message");
	BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "plop!");
	BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#staff");
}

BOOST_AUTO_TEST_SUITE(errors)

BOOST_AUTO_TEST_CASE(invalid_identifier_1)
{
	const auto json = request({
		{ "command",    "server-message"        },
		{ "server",     123456                  },
		{ "target",     "#music"                },
		{ "message",    "plop!"                 }
	});

	BOOST_TEST(json.size() == 4U);
	BOOST_TEST(json["command"].get<std::string>() == "server-message");
	BOOST_TEST(json["error"].get<int>() == daemon::server_error::invalid_identifier);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_identifier_2)
{
	const auto json = request({
		{ "command",    "server-message"        },
		{ "server",     ""                      },
		{ "target",     "#music"                },
		{ "message",    "plop!"                 }
	});

	BOOST_TEST(json.size() == 4U);
	BOOST_TEST(json["command"].get<std::string>() == "server-message");
	BOOST_TEST(json["error"].get<int>() == daemon::server_error::invalid_identifier);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_channel_1)
{
	const auto json = request({
		{ "command",    "server-message"        },
		{ "server",     "test"                  },
		{ "target",     ""                      },
		{ "message",    "plop!"                 }
	});

	BOOST_TEST(json.size() == 4U);
	BOOST_TEST(json["command"].get<std::string>() == "server-message");
	BOOST_TEST(json["error"].get<int>() == daemon::server_error::invalid_channel);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_channel_2)
{
	const auto json = request({
		{ "command",    "server-message"        },
		{ "server",     "test"                  },
		{ "target",     123456                  },
		{ "message",    "plop!"                 }
	});

	BOOST_TEST(json.size() == 4U);
	BOOST_TEST(json["command"].get<std::string>() == "server-message");
	BOOST_TEST(json["error"].get<int>() == daemon::server_error::invalid_channel);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(not_found)
{
	const auto json = request({
		{ "command",    "server-message"        },
		{ "server",     "unknown"               },
		{ "target",     "#music"                },
		{ "message",    "plop!"                 }
	});

	BOOST_TEST(json.size() == 4U);
	BOOST_TEST(json["command"].get<std::string>() == "server-message");
	BOOST_TEST(json["error"].get<int>() == daemon::server_error::not_found);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

} // !namespace

} // !irccd