From 5f1bc697ce8b0b7d26225b2642ec17b5adce341e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Tue, 16 May 2017 19:46:06 +0200 Subject: [PATCH] Test pause patch on torrent --- toREST/include/torrent.hpp | 39 ++++++++++++++++++++++-- toREST/include/torrents.hpp | 1 + toREST/tests/include/ConfigContext.hpp | 1 + toREST/tests/include/ServerContext.hpp | 1 + toREST/tests/include/SessionContext.hpp | 6 +++- toREST/tests/include/TorrentContext.hpp | 14 +++++---- toREST/tests/stubs/SessionContext.cpp | 40 +++++++++++++------------ toREST/tests/stubs/TorrentContext.cpp | 12 ++++++-- toREST/tests/test.cpp | 1 + toREST/tests/torrent_test.cpp | 25 ++++++++++++++-- 10 files changed, 109 insertions(+), 31 deletions(-) diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 4a7f5ef..62e695f 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -68,12 +68,47 @@ void patch(torrent_session &session, response resp, request req) { return respond(http::service_unavailable); } - const auto hash = req->path.substr(18, req->path.size() - 18); //TODO hacky - const auto torrent = session.find_torrent(util::sha1::info_hash(hash)); + const auto hash = req->path.substr(18, req->path.size() - 18); + +#ifndef _TR_TESTING_ + auto torrent = session.find_torrent(util::sha1::info_hash(hash)); +#else + auto &torrent = session.find_torrent(util::sha1::info_hash(hash)); +#endif if (!torrent.is_valid()) { return respond(http::bad_request); } + + auto request_object = util::json::parse(req->content); + + if (request_object.is_null()) { + return respond(http::bad_request); + } + + if (!request_object.is_object()) { + return respond(http::bad_request); + } + + if (request_object.find("paused") != request_object.end()) { + bool paused = false; + const auto obj = request_object.at("paused"); + if (obj.is_boolean()) + paused = request_object.at("paused"); + else if (obj.is_string()) { + std::string p = obj; + paused = (p == "true"); + } + if (torrent.status().paused != paused) { + if (paused) + torrent.pause(); + else { + torrent.resume(); + } + } + } + + respond(http::accepted); } } } diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 64ffc76..2163a77 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -1,5 +1,6 @@ #ifndef _TR_TORRENTS_HPP_ #define _TR_TORRENTS_HPP_ + #include #include #include diff --git a/toREST/tests/include/ConfigContext.hpp b/toREST/tests/include/ConfigContext.hpp index ffd9cb3..98735b4 100644 --- a/toREST/tests/include/ConfigContext.hpp +++ b/toREST/tests/include/ConfigContext.hpp @@ -1,3 +1,4 @@ +#define _TR_TESTING_ #ifndef _TR_TEST_CONFIG_CONTEXT_HPP_ #define _TR_TEST_CONFIG_CONTEXT_HPP_ diff --git a/toREST/tests/include/ServerContext.hpp b/toREST/tests/include/ServerContext.hpp index 1afafe6..e6ca2f8 100644 --- a/toREST/tests/include/ServerContext.hpp +++ b/toREST/tests/include/ServerContext.hpp @@ -1,3 +1,4 @@ +#define _TR_TESTING_ #ifndef _TR_TEST_SERVER_CONTEXT_HPP_ #define _TR_TEST_SERVER_CONTEXT_HPP_ diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index 528a0a8..2ec5620 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -26,6 +26,9 @@ public: class TestSession { public: + TestSession() { + invalid_.valid = false; + } bool valid = false; bool paused = false; std::vector torrents_; @@ -38,11 +41,12 @@ public: int listen_port(); bool is_dht_running() const; std::vector &get_torrents(); - TestTorrent find_torrent(const libtorrent::sha1_hash &hash); + TestTorrent &find_torrent(const libtorrent::sha1_hash &hash); void remove_torrent(const TestTorrent &torrent, int options); void apply_settings(TestSessionSettings settings); void async_add_torrent(const libtorrent::add_torrent_params ¶ms); void theTorrentExists(); + TestTorrent invalid_; }; diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 9b79ab7..7b80b61 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -1,3 +1,4 @@ +#define _TR_TESTING_ #ifndef _TR_TEST_TORRENT_CONTEXT_HPP_ #define _TR_TEST_TORRENT_CONTEXT_HPP_ @@ -5,6 +6,9 @@ #include #include +const auto torrent_url = "http://sout.no/torrent.torrent"; +const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8"; + class TorrentStatus { public: bool paused = false; @@ -13,16 +17,17 @@ public: int state = 0; int priority = 0; std::string name = "Arch"; - std::string save_path; + std::string save_path = ""; }; class TestTorrent { public: TestTorrent(const libtorrent::add_torrent_params ¶ms); TestTorrent() {} + operator bool() const { return is_valid(); }; bool is_valid() const; libtorrent::sha1_hash info_hash() const; - TorrentStatus status(int type = 0); + TorrentStatus &status(int type = 0); int query_name = 1; int query_save_path = 2; bool valid = true; @@ -30,13 +35,12 @@ public: int upload_limit() const; int download_limit_ = 0; int download_limit() const; + void pause(int flags = 0); + void resume(); libtorrent::sha1_hash hash_; TorrentStatus status_; }; -const auto torrent_url = "http://sout.no/torrent.torrent"; -const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8"; - const nlohmann::json torrent_json = { {"info_hash", torrent_id}, {"paused", false}, diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index 6c1fb98..93ad9dc 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -1,16 +1,17 @@ +#include #include #include -#include #include void TestSession::theTorrentExists() { torrents_.emplace_back(); + auto &torrent = torrents_.back(); libtorrent::sha1_hash sha1_hash; std::stringstream ss; ss << torrent_id; ss >> sha1_hash; - torrents_.back().hash_ = sha1_hash; - torrents_.back().valid = true; + torrent.hash_ = sha1_hash; + torrent.valid = true; REQUIRE(get_torrents().size() == 1); } @@ -77,27 +78,28 @@ std::vector &TestSession::get_torrents() { return torrents_; } -TestTorrent TestSession::find_torrent(const libtorrent::sha1_hash &hash) { - for (auto &torrent : torrents_) { - if (torrent.info_hash() == hash) { - return torrent; - } +TestTorrent &TestSession::find_torrent(const libtorrent::sha1_hash &hash) { + auto itr = std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent torrent) { + return torrent.info_hash() == hash; + }); + if (itr != torrents_.end()) { + return *itr; } - auto res = TestTorrent(); - res.valid = false; - return res; + return invalid_; } void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) { if (options > -1) { - auto itr = torrents_.end(); - for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { - if (itr->info_hash() == torrent.info_hash()) { - break; - } - } - if (itr != torrents_.end()) - torrents_.erase(itr); + // auto itr = torrents_.end(); + // for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { + // if (itr->info_hash() == torrent.info_hash()) { + // break; + // } + // } + // if (itr != torrents_.end()) + torrents_.erase(std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent a) { + return a.info_hash() == torrent.info_hash(); + })); } } diff --git a/toREST/tests/stubs/TorrentContext.cpp b/toREST/tests/stubs/TorrentContext.cpp index dfd398c..b646cba 100644 --- a/toREST/tests/stubs/TorrentContext.cpp +++ b/toREST/tests/stubs/TorrentContext.cpp @@ -1,6 +1,6 @@ #include -TorrentStatus TestTorrent::status(int type) { +TorrentStatus &TestTorrent::status(int type) { return status_; } @@ -17,7 +17,7 @@ int TestTorrent::download_limit() const { } TestTorrent::TestTorrent(const libtorrent::add_torrent_params ¶ms) { - auto paused = (params.flags & libtorrent::add_torrent_params::flag_paused) == libtorrent::add_torrent_params::flag_paused; + const auto paused = (params.flags & libtorrent::add_torrent_params::flag_paused) == libtorrent::add_torrent_params::flag_paused; status_.paused = paused; status_.save_path = params.save_path; status_.name = params.name; @@ -28,4 +28,12 @@ TestTorrent::TestTorrent(const libtorrent::add_torrent_params ¶ms) { libtorrent::sha1_hash TestTorrent::info_hash() const { return hash_; +} + +void TestTorrent::pause(int flags) { + status_.paused = true; +} + +void TestTorrent::resume() { + status_.paused = false; } \ No newline at end of file diff --git a/toREST/tests/test.cpp b/toREST/tests/test.cpp index bbb9571..e8087c9 100644 --- a/toREST/tests/test.cpp +++ b/toREST/tests/test.cpp @@ -1,2 +1,3 @@ +#define _TR_TESTING_ #define CATCH_CONFIG_MAIN #include \ No newline at end of file diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index ddeb297..12509c3 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -48,12 +48,16 @@ SCENARIO("We are running a PATCH /session/torrents/id resource") { 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") { + 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; @@ -62,6 +66,23 @@ SCENARIO("We are running a PATCH /session/torrents/id resource") { 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); + } + } + 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); } } }