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.
 
 
 
 

91 lines
3.7 KiB

#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <TorrentContext.hpp>
#include <torrent.hpp>
SCENARIO("We are running a GET /session/torrents/id 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 we recive a reqest") {
THEN("the server should reply with service unavailable") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::service_unavailable(response);
}
}
auto torrent = TestTorrent();
torrent_session.valid = true;
GIVEN("we have the server is working properly and we recive a request") {
WHEN("the request path is a proper id") {
request->path = "/session/torrents/" + torrent_id;
GIVEN("we have the torrent") {
torrent_session.theTorrentExists();
THEN("the response should be a json representation of the torrent") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::ok(response, {torrent_json});
}
WHEN("The torrent isn't valid") {
torrent_session.torrents_.back().valid = false;
THEN("the server should reply with bad request") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}
}
}
SCENARIO("We are running a PATCH /session/torrents/id 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 we recive a reqest") {
THEN("the server should reply with service unavailable") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::service_unavailable(response);
}
}
torrent_session.valid = true;
GIVEN("we have the server is working properly and we recive a request") {
WHEN("the request path is a proper id") {
request->path = "/session/torrents/" + torrent_id;
GIVEN("the request is valid and we have the torrent") {
request->content << nlohmann::json({
{"paused", "true"},
{"up_limit", "109"},
{"down_limit", 100},
});
torrent_session.theTorrentExists();
WHEN("The torrent isn't valid") {
torrent_session.torrents_.back().valid = false;
THEN("the server should reply with bad request") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
THEN("the server should reply with accepted") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::accepted(response);
REQUIRE(torrent_session.get_torrents().back().status().paused);
REQUIRE(torrent_session.get_torrents().back().download_limit() == 100);
REQUIRE(torrent_session.get_torrents().back().upload_limit() == 109);
}
}
THEN("the request is valid json, but doesn't have any of the required fields") {
request->content << "{}";
THEN("the server should reply with bad request") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
request->content << "Not valid";
THEN("the server should reply with bad request") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}