From 1b514df5550265b0e5bcdd5b2f502b1ead4ffa61 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 20 Sep 2019 00:40:22 +0200 Subject: [PATCH] Added /api/v1/polls/:id and /api/v1/polls/:id/votes. --- README.adoc | 4 +- src/api/get.cpp | 5 ++ src/api/post.cpp | 5 ++ src/easy/return_types_easy.cpp | 2 + src/mastodon-cpp.hpp | 4 +- tests/api/test_api_v1_polls_id.cpp | 76 ++++++++++++++++++++++ tests/api/test_api_v1_polls_id_votes.cpp | 80 ++++++++++++++++++++++++ 7 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 tests/api/test_api_v1_polls_id.cpp create mode 100644 tests/api/test_api_v1_polls_id_votes.cpp diff --git a/README.adoc b/README.adoc index 29ca70e..ebb6cb7 100644 --- a/README.adoc +++ b/README.adoc @@ -343,8 +343,8 @@ strings and you can use unsupported fields in an `Entity` by converting it to ** [x] PUT /api/v1/push/subscription ** [x] DELETE /api/v1/push/subscription * Polls - ** [ ] GET /api/v1/polls/:id - ** [ ] POST /api/v1/polls/:id/votes + ** [x] GET /api/v1/polls/:id + ** [x] POST /api/v1/polls/:id/votes * Reports ** [x] GET /api/v1/reports ^(Deprecated)^ ** [x] POST /api/v1/reports diff --git a/src/api/get.cpp b/src/api/get.cpp index 92dfc0f..38c74eb 100644 --- a/src/api/get.cpp +++ b/src/api/get.cpp @@ -231,6 +231,11 @@ const return_call API::get(const Mastodon::API::v1 &call, strcall = "/api/v1/suggestions"; break; } + case v1::polls_id: + { + strcall = "/api/v1/polls/" + strid; + break; + } default: { ttdebug << "ERROR: Invalid argument.\n"; diff --git a/src/api/post.cpp b/src/api/post.cpp index fed3d55..576f708 100644 --- a/src/api/post.cpp +++ b/src/api/post.cpp @@ -195,6 +195,11 @@ return_call API::post(const Mastodon::API::v1 &call, strcall = "/api/v1/filters"; break; } + case v1::polls_id_votes: + { + strcall = "/api/v1/polls/" + strid + "/votes"; + break; + } default: { ttdebug << "ERROR: Invalid argument.\n"; diff --git a/src/easy/return_types_easy.cpp b/src/easy/return_types_easy.cpp index 8204e36..e9f8af9 100644 --- a/src/easy/return_types_easy.cpp +++ b/src/easy/return_types_easy.cpp @@ -33,6 +33,7 @@ #include "easy/entities/token.hpp" #include "easy/entities/pushsubscription.hpp" #include "easy/entities/filter.hpp" +#include "easy/entities/poll.hpp" using namespace Mastodon; @@ -92,6 +93,7 @@ template struct Easy::return_entity; template struct Easy::return_entity; template struct Easy::return_entity; template struct Easy::return_entity; +template struct Easy::return_entity; template diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index b70f846..0a7bb51 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -244,8 +244,8 @@ namespace Mastodon notifications_dismiss, push_subscription, - // polls_id, - // polls_id_votes, + polls_id, + polls_id_votes, reports, diff --git a/tests/api/test_api_v1_polls_id.cpp b/tests/api/test_api_v1_polls_id.cpp new file mode 100644 index 0000000..908cb98 --- /dev/null +++ b/tests/api/test_api_v1_polls_id.cpp @@ -0,0 +1,76 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include "mastodon-cpp.hpp" +#include "easy/easy.hpp" +#include "easy/entities/poll.hpp" +#include "../environment_variables.hpp" + +using namespace Mastodon; + +SCENARIO ("/api/v1/polls/:id can be called successfully", + "[api][auth][mastodon][pleroma][glitch-soc]") +{ + REQUIRE (access_token != nullptr); + + GIVEN ("instance = " + instance) + { + Mastodon::Easy::API masto(instance, access_token); + return_call ret; + Easy::Poll poll; + bool exception = false; + + WHEN ("GET /api/v1/polls/:id is called") + { + try + { + ret = masto.get(API::v1::polls_id, + {{ "id", { "4823515" } }}); + if (ret.answer == "[]") + { + WARN("Poll not found."); + } + else + { + poll.from_string(ret.answer); + } + } + catch (const std::exception &e) + { + exception = true; + WARN(e.what()); + } + + THEN("No exception is thrown") + AND_THEN ("No errors are returned") + AND_THEN ("Answer is valid") + AND_THEN ("The answer makes sense") + { + REQUIRE_FALSE(exception); + + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + REQUIRE(poll.valid()); + + REQUIRE(poll.id() != ""); + } + } + } +} + diff --git a/tests/api/test_api_v1_polls_id_votes.cpp b/tests/api/test_api_v1_polls_id_votes.cpp new file mode 100644 index 0000000..12b3de1 --- /dev/null +++ b/tests/api/test_api_v1_polls_id_votes.cpp @@ -0,0 +1,80 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include "mastodon-cpp.hpp" +#include "easy/easy.hpp" +#include "easy/entities/poll.hpp" +#include "../environment_variables.hpp" + +using namespace Mastodon; + +SCENARIO ("/api/v1/polls/:id/votes can be called successfully", + "[api][auth][mastodon][pleroma][glitch-soc]") +{ + REQUIRE (access_token != nullptr); + + GIVEN ("instance = " + instance) + { + Mastodon::Easy::API masto(instance, access_token); + return_call ret; + Easy::Poll poll; + bool exception = false; + + WHEN ("POST /api/v1/polls/:id/votes is called") + { + try + { + ret = masto.post(API::v1::polls_id_votes, + { + { "id", { "4823515" } }, + // TODO: Check if this is the right format. + { "choices", { "false", "false", "true", + "false", "false" } } + }); + if (ret.answer == "[]") + { + WARN("Poll not found."); + } + else + { + poll.from_string(ret.answer); + } + } + catch (const std::exception &e) + { + exception = true; + WARN(e.what()); + } + + THEN("No exception is thrown") + AND_THEN ("An error is returned") + AND_THEN ("Answer is invalid") + { + REQUIRE_FALSE(exception); + + REQUIRE(ret.error_code == 12); + REQUIRE(ret.http_error_code != 200); // Pleroma returns 422. + REQUIRE_FALSE(poll.valid()); + REQUIRE(poll.id() == ""); + REQUIRE(poll.error() == "Poll's author can't vote"); + } + } + } +} +