From ada05fe896703762e8d26eb88c4de43e95a7936d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 15 May 2017 17:15:14 +0200 Subject: [PATCH] now correctly handles invalid torrents --- toREST/include/torrent.hpp | 7 +++++++ toREST/tests/torrent_test.cpp | 23 ++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 1019097..4a7f5ef 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -67,6 +67,13 @@ void patch(torrent_session &session, response resp, request req) { if (!session.is_valid()) { 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)); + + if (!torrent.is_valid()) { + return respond(http::bad_request); + } } } } diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index c1ad580..ddeb297 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -23,11 +23,11 @@ SCENARIO("We are running a GET /session/torrents/id resource") { 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}); + CommonResponse::ok(response, {torrent_json}); } - WHEN("The torrent isn't valid"){ + WHEN("The torrent isn't valid") { torrent_session.torrents_.back().valid = false; - THEN("the server should reply with bad request"){ + THEN("the server should reply with bad request") { tr::session::torrents::id::get(torrent_session, response, request); CommonResponse::bad_request(response); } @@ -48,4 +48,21 @@ 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") { + 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); + } + } + } + } + } }