Added /api/v1/polls/:id and /api/v1/polls/:id/votes.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2019-09-20 00:40:22 +02:00
parent 416675a667
commit 1b514df555
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 172 additions and 4 deletions

View File

@ -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

View File

@ -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";

View File

@ -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";

View File

@ -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<Easy::Tag>;
template struct Easy::return_entity<Easy::Token>;
template struct Easy::return_entity<Easy::PushSubscription>;
template struct Easy::return_entity<Easy::Filter>;
template struct Easy::return_entity<Easy::Poll>;
template<typename T>

View File

@ -244,8 +244,8 @@ namespace Mastodon
notifications_dismiss,
push_subscription,
// polls_id,
// polls_id_votes,
polls_id,
polls_id_votes,
reports,

View File

@ -0,0 +1,76 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <exception>
#include <string>
#include <catch.hpp>
#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() != "");
}
}
}
}

View File

@ -0,0 +1,80 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <exception>
#include <string>
#include <catch.hpp>
#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");
}
}
}
}