Browse Source

Test pause patch on torrent

master
Jørgen Lien Sellæg 9 years ago
parent
commit
5f1bc697ce
  1. 39
      toREST/include/torrent.hpp
  2. 1
      toREST/include/torrents.hpp
  3. 1
      toREST/tests/include/ConfigContext.hpp
  4. 1
      toREST/tests/include/ServerContext.hpp
  5. 6
      toREST/tests/include/SessionContext.hpp
  6. 14
      toREST/tests/include/TorrentContext.hpp
  7. 40
      toREST/tests/stubs/SessionContext.cpp
  8. 12
      toREST/tests/stubs/TorrentContext.cpp
  9. 1
      toREST/tests/test.cpp
  10. 25
      toREST/tests/torrent_test.cpp

39
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);
}
}
}

1
toREST/include/torrents.hpp

@ -1,5 +1,6 @@
#ifndef _TR_TORRENTS_HPP_
#define _TR_TORRENTS_HPP_
#include <boost/filesystem.hpp>
#include <http.hpp>
#include <libtorrent/magnet_uri.hpp>

1
toREST/tests/include/ConfigContext.hpp

@ -1,3 +1,4 @@
#define _TR_TESTING_
#ifndef _TR_TEST_CONFIG_CONTEXT_HPP_
#define _TR_TEST_CONFIG_CONTEXT_HPP_

1
toREST/tests/include/ServerContext.hpp

@ -1,3 +1,4 @@
#define _TR_TESTING_
#ifndef _TR_TEST_SERVER_CONTEXT_HPP_
#define _TR_TEST_SERVER_CONTEXT_HPP_

6
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<TestTorrent> torrents_;
@ -38,11 +41,12 @@ public:
int listen_port();
bool is_dht_running() const;
std::vector<TestTorrent> &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 &params);
void theTorrentExists();
TestTorrent invalid_;
};

14
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 <libtorrent/magnet_uri.hpp>
#include <string>
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 &params);
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},

40
toREST/tests/stubs/SessionContext.cpp

@ -1,16 +1,17 @@
#include <Catch/catch.hpp>
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <Catch/catch.hpp>
#include <vector>
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<TestTorrent> &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();
}));
}
}

12
toREST/tests/stubs/TorrentContext.cpp

@ -1,6 +1,6 @@
#include <TorrentContext.hpp>
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 &params) {
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 &params) {
libtorrent::sha1_hash TestTorrent::info_hash() const {
return hash_;
}
void TestTorrent::pause(int flags) {
status_.paused = true;
}
void TestTorrent::resume() {
status_.paused = false;
}

1
toREST/tests/test.cpp

@ -1,2 +1,3 @@
#define _TR_TESTING_
#define CATCH_CONFIG_MAIN
#include <Catch/catch.hpp>

25
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);
}
}
}

Loading…
Cancel
Save