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.
109 lines
4.2 KiB
109 lines
4.2 KiB
#include <Catch/fakeit.hpp> |
|
#include <ConfigContext.hpp> |
|
#include <ServerContext.hpp> |
|
#include <SessionContext.hpp> |
|
#include <torrents.hpp> |
|
|
|
using namespace std; |
|
|
|
const nlohmann::json torrent = { |
|
{"hash", "12a"}, |
|
{"paused", false}, |
|
{"seeding", false}, |
|
{"state", 0}, |
|
{"priority", 0}, |
|
{"name", "Arch"}}; |
|
|
|
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") { |
|
AND_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") { |
|
tr::session::torrents::get(torrent_session, response, request); |
|
CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}}); |
|
} |
|
} |
|
GIVEN("we have at least one torrent") { |
|
auto t_torrent = TestTorrent(); |
|
torrent_session.get_torrents().emplace_back(t_torrent); |
|
THEN("the server should reply with an array of torrents") { |
|
tr::session::torrents::get(torrent_session, response, request); |
|
CommonResponse::ok(response, {{"torrents", {torrent}}}); |
|
}; |
|
} |
|
} |
|
} |
|
} |
|
|
|
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") { |
|
request->content << nlohmann::json::object({{"magnet_uri", "magnet:?xt=urn:btih:c0b0a90089710812fe8c37385a4cc2978eabf3e8&dn=Taylor Swift&tr=http://tracker.sout.no"}}); |
|
THEN("the server should reply with created") { |
|
tr::session::torrents::post(settings, torrent_session, response, request); |
|
CommonResponse::created(response, request, "/session/torrents/hash"); |
|
} |
|
} |
|
|
|
|
|
GIVEN("the torrent doesn't exist") { |
|
// tr::session::torrents::post(settings, torrent_session, response, request); |
|
// tr::session::torrents::get(torrent_session, response, request); |
|
// CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}}); |
|
// } |
|
// } |
|
// GIVEN("we have at least one torrent") { |
|
// auto t_torrent = TestTorrent(); |
|
// torrent_session.get_torrents().emplace_back(t_torrent); |
|
// THEN("the server should reply with an array of torrents") { |
|
// tr::session::torrents::get(torrent_session, response, request); |
|
// CommonResponse::ok(response, {{"torrents", {torrent}}}); |
|
// }; |
|
} |
|
GIVEN("the torrent exists from before") { |
|
} |
|
} |
|
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); |
|
} |
|
} |
|
} |
|
} |
|
}
|
|
|