From 7cbb6a52538b2ae446226c4692e3978ef02ab288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 15 May 2017 15:10:02 +0200 Subject: [PATCH] Refactor torrent representation in tests --- toREST/include/torrent.hpp | 9 +++++++ toREST/include/torrents.hpp | 8 ++++-- toREST/tests/include/ServerContext.hpp | 1 + toREST/tests/include/SessionContext.hpp | 13 +++++++++ toREST/tests/include/TorrentContext.hpp | 14 ++++++++++ toREST/tests/session_test.cpp | 11 ++++---- toREST/tests/stubs/ServerContext.cpp | 11 ++++---- toREST/tests/stubs/SessionContext.cpp | 11 ++++++++ toREST/tests/torrent_test.cpp | 28 +++++++++++++------- toREST/tests/torrents_test.cpp | 35 +++---------------------- 10 files changed, 86 insertions(+), 55 deletions(-) diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 8b7c8b3..3f0b73f 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -1,7 +1,9 @@ #ifndef _TR_TORRENT_HPP_ #define _TR_TORRENT_HPP_ +#include #include +#include namespace tr { namespace session { @@ -20,6 +22,13 @@ void get(torrent_session &session, response resp, request req) { if (!session.is_valid()) { return respond(http::service_unavailable); } + + std::string hash = req->path.substr(18, req->path.size() - 18); + + http_response.set_body(nlohmann::json::object( + {{"info_hash", hash}})); + http_response.set_status(http::ok); + *resp << http_response; } template diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index c0da13b..c2e14c6 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -17,15 +17,19 @@ void get(torrent_session &session, response resp, request req) { auto torrents_json = nlohmann::json::array(); for (auto &torrent : torrents) { if (torrent.is_valid()) { - const auto hash = torrent.info_hash().to_string(); + std::stringstream ss; + ss << torrent.info_hash(); + const std::string hash = ss.str(); const auto status = torrent.status( torrent.query_name | torrent.query_save_path); - torrents_json.push_back({{"hash", hash}, + torrents_json.push_back({{"info_hash", hash}, {"paused", status.paused}, {"seeding", status.is_seeding}, {"state", status.state}, {"priority", status.priority}, + {"up_limit", torrent.upload_limit()}, + {"down_limit", torrent.download_limit()}, {"name", status.name}}); } } diff --git a/toREST/tests/include/ServerContext.hpp b/toREST/tests/include/ServerContext.hpp index 4166986..1afafe6 100644 --- a/toREST/tests/include/ServerContext.hpp +++ b/toREST/tests/include/ServerContext.hpp @@ -9,6 +9,7 @@ class TestRequest { public: std::stringstream content; + std::string path; }; class TestResponse : public std::stringstream { diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index bfbb5a8..528a0a8 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -42,6 +42,19 @@ public: 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(); }; + +const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no"; + +const nlohmann::json session_json = { + {"dht_enabled", true}, + {"down_limit", 0}, + {"paused", false}, + {"port", 0}, + {"torrents", 0}, + {"up_limit", 0}}; + + #endif \ No newline at end of file diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 083ebbe..79ea136 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -1,6 +1,7 @@ #ifndef _TR_TEST_TORRENT_CONTEXT_HPP_ #define _TR_TEST_TORRENT_CONTEXT_HPP_ +#include #include #include @@ -33,4 +34,17 @@ public: 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}, + {"seeding", false}, + {"state", 0}, + {"priority", 0}, + {"up_limit", 0}, + {"down_limit", 0}, + {"name", "Arch"}}; + #endif \ No newline at end of file diff --git a/toREST/tests/session_test.cpp b/toREST/tests/session_test.cpp index 09416b4..206ff02 100644 --- a/toREST/tests/session_test.cpp +++ b/toREST/tests/session_test.cpp @@ -3,9 +3,6 @@ #include #include -const nlohmann::json ok_data = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", false}, {"port", 0}, {"torrents", 0}, {"up_limit", 0}}; -const nlohmann::json ok_data_paused = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", true}, {"port", 0}, {"torrents", 0}, {"up_limit", 0}}; - SCENARIO("We are running a GET /session resource") { auto torrent_session = TestSession(); auto response = std::make_shared(); @@ -20,17 +17,19 @@ SCENARIO("We are running a GET /session resource") { } GIVEN("the server is working properly") { torrent_session.valid = true; + auto data = session_json; WHEN("the server is paused") { torrent_session.paused = true; - tr::session::get(torrent_session, response, request); + data.at("paused") = true; THEN("the server should reply with the paused field set to true") { - CommonResponse::ok(response, ok_data_paused); + tr::session::get(torrent_session, response, request); + CommonResponse::ok(response, data); } } WHEN("the server is not paused") { tr::session::get(torrent_session, response, request); THEN("the server should reply with the paused field set to true") { - CommonResponse::ok(response, ok_data); + CommonResponse::ok(response, session_json); } } } diff --git a/toREST/tests/stubs/ServerContext.cpp b/toREST/tests/stubs/ServerContext.cpp index 26b2d9a..988eaae 100644 --- a/toREST/tests/stubs/ServerContext.cpp +++ b/toREST/tests/stubs/ServerContext.cpp @@ -62,9 +62,12 @@ void header_present(std::shared_ptr response, const std::string &h REQUIRE(loc_itr->first == header); } -void is_json_response(std::shared_ptr response) { +void is_json_response(std::shared_ptr response, nlohmann::json data = nullptr) { const auto content = response->content(); - auto obj = nlohmann::json::parse(content); + nlohmann::json obj = nlohmann::json::parse(response->content()); + if (data != nullptr) { + REQUIRE(data == obj); + } REQUIRE(obj.is_object()); header_present(response, "Content-Type", "application/json"); header_present(response, "Content-Length", std::to_string(content.length())); @@ -77,11 +80,9 @@ void CommonResponse::service_unavailable(std::shared_ptr response) }; void CommonResponse::ok(std::shared_ptr response, const nlohmann::json &data) { - std::string data_dump(data.dump()); REQUIRE(response->code() == "200"); REQUIRE(response->message() == "OK"); - REQUIRE(response->content() == data_dump); - is_json_response(response); + is_json_response(response, data); }; void CommonResponse::no_content(std::shared_ptr response) { diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index 1fbc6a8..6c1fb98 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -3,6 +3,17 @@ #include #include +void TestSession::theTorrentExists() { + torrents_.emplace_back(); + libtorrent::sha1_hash sha1_hash; + std::stringstream ss; + ss << torrent_id; + ss >> sha1_hash; + torrents_.back().hash_ = sha1_hash; + torrents_.back().valid = true; + REQUIRE(get_torrents().size() == 1); +} + void TestSessionSettings::set_int(int type, int value) { if (type == download_rate_limit) { download_rate_limit_ = value; diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index b2adedf..3824f0d 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -3,23 +3,31 @@ #include #include + SCENARIO("We are running a GET /session/torrents/id resource") { auto torrent_session = TestSession(); auto response = std::make_shared(); auto request = std::make_shared(); - GIVEN("the server is not working properly") { - WHEN("we recive a request") { + 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); - THEN("the server should reply with service unavailable") { - CommonResponse::service_unavailable(response); - } + CommonResponse::service_unavailable(response); } } - GIVEN("the server is working properly") { - torrent_session.valid = true; - THEN("the server should reply with service unavailable") { - tr::session::torrents::id::get(torrent_session, response, request); - CommonResponse::ok(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, + {// {"paused", true}, + {"info_hash", torrent_id}}); + } + } } } } \ No newline at end of file diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 91931f4..dc39252 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -4,16 +4,6 @@ #include #include -using namespace std; - -const nlohmann::json torrent = { - {"hash", "12a12a12a12a12a12a12"}, - {"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(); @@ -36,24 +26,16 @@ SCENARIO("We are running a GET /session/torrents resource") { } } GIVEN("we have at least one torrent") { - auto t_torrent = TestTorrent(); - libtorrent::sha1_hash sha; - sha.assign("12a12a12a12a12a12a12"); - t_torrent.hash_ = sha; - torrent_session.get_torrents().emplace_back(t_torrent); + torrent_session.theTorrentExists(); THEN("the server should reply with an array of torrents") { tr::session::torrents::get(torrent_session, response, request); - CommonResponse::ok(response, {{"torrents", {torrent}}}); + CommonResponse::ok(response, {{"torrents", {torrent_json}}}); }; } } } } -const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8"; -const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no"; -const auto torrent_url = "http://sout.no/torrent.torrent"; - SCENARIO("We are running a POST /session/torrents resource") { auto torrent_session = TestSession(); auto settings = Config(); @@ -187,17 +169,6 @@ SCENARIO("We are running a POST /session/torrents resource") { } } -void theTorrentExists(TestSession &torrent_session) { - torrent_session.torrents_.emplace_back(); - libtorrent::sha1_hash sha1_hash; - std::stringstream ss; - ss << torrent_id; - ss >> sha1_hash; - torrent_session.torrents_[0].hash_ = sha1_hash; - torrent_session.torrents_[0].valid = true; - REQUIRE(torrent_session.get_torrents().size() == 1); -}; - SCENARIO("We are running a DELETE /session/torrents resource") { auto torrent_session = TestSession(); auto response = std::make_shared(); @@ -236,7 +207,7 @@ SCENARIO("We are running a DELETE /session/torrents resource") { } GIVEN("fields are correct") { GIVEN("the torrent exists") { - theTorrentExists(torrent_session); + 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);