You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
11 KiB

#include <Catch/catch.hpp>
#include <ConfigContext.hpp>
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <torrents.hpp>
SCENARIO("We are running a GET /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
WHEN("we recive a request") {
tr::session::torrents::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
WHEN("we recive a valid request") {
GIVEN("we have no torrents") {
THEN("the server should reply with an empty array") {
9 years ago
tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
}
}
9 years ago
GIVEN("we have at least one torrent") {
torrent_session.theTorrentExists();
9 years ago
THEN("the server should reply with an array of torrents") {
tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", {torrent_json}}});
9 years ago
};
}
}
}
}
SCENARIO("We are running a POST /session/torrents resource") {
auto torrent_session = TestSession();
auto settings = Config();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
AND_WHEN("we recive a request") {
tr::session::torrents::post(settings, torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
WHEN("we recive a valid request") {
GIVEN("we use the default download directory") {
AND_WHEN("we fill the magnet uri field") {
THEN("the server should reply with created") {
request->content << nlohmann::json::object({{"magnet_uri", magnet_uri}});
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST");
}
}
// AND_WHEN("we fill the magnet url field"){
// THEN("the server should reply with created") {
// request->content << nlohmann::json::object({{"url", torrent_url}});
// tr::session::torrents::post(settings, torrent_session, response, request);
// CommonResponse::created(response, request, "/session/torrents/" + std::string("0000000000000000000000000000000000000000"));
// REQUIRE(torrent_session.get_torrents().size() == 1);
// REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST");
// }
// }
}
GIVEN("we specify a absolute save path") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "/music"}});
tr::session::torrents::post(settings, torrent_session, response, request);
THEN("the server should reply with bad request") {
CommonResponse::bad_request(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
}
GIVEN("we specify a relative save path") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"}});
THEN("the server should reply created") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == -1);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == -1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
GIVEN("we specify the torrent field to be paused") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"},
{"paused", "true"}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == -1);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == -1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
GIVEN("we specify up an down speed") {
const std::string name = "Dent";
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"},
{"up_speed", "100"},
{"down_speed", "100"},
{"name", name}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].status().name == name);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == 100);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == 100);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
GIVEN("we specify up an down speed") {
const std::string name = "Dent";
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"},
{"up_speed", 90},
{"down_speed", "100"},
{"name", name}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].status().name == name);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == 90);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == 100);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
}
WHEN("we recive an invalid request") {
GIVEN("the request doesn't contain valid json") {
request->content << "Not valid";
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("it is valid json but are missing requred field") {
request->content << nlohmann::json({{"up_speed", "100"}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}
}
SCENARIO("We are running a DELETE /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
AND_WHEN("we recive a request") {
tr::session::torrents::del(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
WHEN("we recive an invalid request") {
GIVEN("the request doesn't contain valid json") {
request->content << "Not valid";
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("it is valid json but are missing requred field") {
request->content << nlohmann::json({{"up_speed", "100"}});
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("required field is wrong type") {
request->content << nlohmann::json({{"info_hash", 1}});
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("fields are correct") {
GIVEN("the torrent exists") {
torrent_session.theTorrentExists();
THEN("the server should reply no content") {
request->content << nlohmann::json({{"info_hash", torrent_id}});
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
GIVEN("we want to remove files") {
THEN("the should reply with no content") {
request->content << nlohmann::json({{"info_hash", torrent_id},
{"remove_files", true}});
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
THEN("we should reply with no content") {
request->content << nlohmann::json({{"info_hash", torrent_id},
{"remove_files", "true"}});
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
}
}
GIVEN("the torrent doesn't exist") {
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}
}
}