From 43359b33c5e337b5a74aa5429fb7428cc611f0d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 1 May 2017 23:47:30 +0200 Subject: [PATCH] test torrent get method --- toREST/tests/include/SessionContext.hpp | 3 ++- toREST/tests/include/TorrentContext.hpp | 6 +++-- toREST/tests/stubs/SessionContext.cpp | 4 ++-- toREST/tests/stubs/TorrentContext.cpp | 4 ++-- toREST/tests/torrents_test.cpp | 29 ++++++++++++++++++++----- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index b4fa02e..1cfe8d7 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -28,6 +28,7 @@ class TestSession { public: bool valid = false; bool paused = false; + std::vector torrents; TestSessionSettings settings_; bool is_valid(); TestSessionSettings get_settings(); @@ -36,7 +37,7 @@ public: void resume(); int listen_port(); bool is_dht_running() const; - std::vector get_torrents() const; + std::vector& get_torrents(); void apply_settings(TestSessionSettings settings); }; diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 423a3fa..ddc8d56 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -4,7 +4,7 @@ class InfoHash { public: - std::string to_string() { return "banana"; } + std::string to_string() { return "12a"; } }; class TorrentStatus { @@ -13,7 +13,7 @@ public: bool is_seeding = false; int state = 0; int priority = 0; - std::string name = "N/A"; + std::string name = "Arch"; }; class TestTorrent { @@ -24,6 +24,8 @@ public: int query_name = 1; int query_save_path = 2; bool valid = true; + InfoHash hash; + TorrentStatus status_; }; #endif \ No newline at end of file diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index d51d237..259e62d 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -60,8 +60,8 @@ int TestSession::listen_port() { bool TestSession::is_dht_running() const { return settings_.enable_dht_; } -std::vector TestSession::get_torrents() const { - return std::vector(); +std::vector& TestSession::get_torrents() { + return torrents; } void TestSession::apply_settings(TestSessionSettings settings) { settings_ = settings; diff --git a/toREST/tests/stubs/TorrentContext.cpp b/toREST/tests/stubs/TorrentContext.cpp index cfa93b8..410ba26 100644 --- a/toREST/tests/stubs/TorrentContext.cpp +++ b/toREST/tests/stubs/TorrentContext.cpp @@ -1,11 +1,11 @@ #include InfoHash TestTorrent::info_hash() { - return InfoHash(); + return hash; } TorrentStatus TestTorrent::status(int type) { - return TorrentStatus(); + return status_; } bool TestTorrent::is_valid() { diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 1c0da47..741ab60 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -5,6 +5,15 @@ 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(); @@ -19,12 +28,22 @@ SCENARIO("We are running a GET /session/torrents resource") { } GIVEN("the server is working properly") { torrent_session.valid = true; - AND_WHEN("the session the session has no torrents") { - tr::session::torrents::get(torrent_session, response, request); - THEN("the server should reply with an empty array") { - CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}}); + 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}}}); + } } - } } + } \ No newline at end of file