From 09793c55113de3253d221af04a291f20934e117f Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 14 Apr 2019 21:57:22 +0200 Subject: [PATCH] Added tests for API::v1::accounts_verify_credentials. --- .drone.yml | 6 + tests/test_api_v1_accounts_id.cpp | 123 +++++++++--------- ...est_api_v1_accounts_verify_credentials.cpp | 107 +++++++++++++++ tests/test_api_v1_instance.cpp | 118 ++++++++--------- 4 files changed, 236 insertions(+), 118 deletions(-) create mode 100644 tests/test_api_v1_accounts_verify_credentials.cpp diff --git a/.drone.yml b/.drone.yml index d339fc0..3cad77b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,6 +8,8 @@ pipeline: - LANG=C.utf8 - CXX=g++-6 - CXXFLAGS=-pipe -O2 + secrets: + - mastodon_cpp_access_token commands: - rm /etc/apt/apt.conf.d/docker-clean - echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release @@ -33,6 +35,8 @@ pipeline: - LANG=C.utf8 - CXX=g++-7 - CXXFLAGS=-pipe -O2 + secrets: + - mastodon_cpp_access_token commands: - rm /etc/apt/apt.conf.d/docker-clean - apt-get update -q @@ -64,6 +68,8 @@ pipeline: - LANG=C.utf8 - CXX=g++-8 - CXXFLAGS=-pipe -O2 + secrets: + - mastodon_cpp_access_token commands: - rm /etc/apt/apt.conf.d/docker-clean - apt-get update -q diff --git a/tests/test_api_v1_accounts_id.cpp b/tests/test_api_v1_accounts_id.cpp index 051307f..ca38625 100644 --- a/tests/test_api_v1_accounts_id.cpp +++ b/tests/test_api_v1_accounts_id.cpp @@ -26,77 +26,80 @@ using namespace Mastodon; SCENARIO ("/api/v1/accounts/:id can be called successfully", "[api][mastodon][pleroma][glitch-soc]") { - GIVEN ("Mastodon::API") + GIVEN ("id and return_call") { - Mastodon::API masto("likeable.space", ""); + const string id = "9hnrrVPriLiLVAhfVo"; return_call ret; bool exception = false; - bool username_found = false; - WHEN ("/api/v1/accounts/9hnrrVPriLiLVAhfVo is called") + GIVEN ("Mastodon::API") { - try + Mastodon::API masto("likeable.space", ""); + bool username_found = false; + + WHEN ("/api/v1/accounts/" + id + " is called") { - ret = masto.get(API::v1::accounts_id, - {{ "id", { "9hnrrVPriLiLVAhfVo" }}}); - username_found = ret.answer.find( - "\"username\":\"testaccount\"") != std::string::npos; - } - catch (const std::exception &e) - { - exception = true; - } - THEN("No exception is thrown") - { - REQUIRE_FALSE(exception); - } - THEN ("No errors are returned") - { - REQUIRE(ret.error_code == 0); - REQUIRE(ret.http_error_code == 200); - } - THEN ("The answer makes sense") - { - REQUIRE(username_found); + try + { + ret = masto.get(API::v1::accounts_id, + {{ "id", { id }}}); + username_found = ret.answer.find( + "\"username\":\"testaccount\"") != std::string::npos; + } + catch (const std::exception &e) + { + exception = true; + } + THEN("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + THEN ("No errors are returned") + { + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + } + THEN ("The answer makes sense") + { + REQUIRE(username_found); + } } } - } - GIVEN ("Mastodon::Easy::API") - { - Mastodon::Easy::API masto("likeable.space", ""); - return_call ret; - Easy::Account account; - bool exception = false; - - WHEN ("/api/v1/accounts/9hnrrVPriLiLVAhfVo is called") + GIVEN ("Mastodon::Easy::API") { - try + Mastodon::Easy::API masto("likeable.space", ""); + Easy::Account account; + + WHEN ("/api/v1/accounts/" + id + " is called") { - ret = masto.get(API::v1::accounts_id, - {{ "id", { "9hnrrVPriLiLVAhfVo" }}}); - account = Easy::Account(ret.answer); - } - catch (const std::exception &e) - { - exception = true; - } - THEN("No exception is thrown") - { - REQUIRE_FALSE(exception); - } - THEN ("No errors are returned") - { - REQUIRE(ret.error_code == 0); - REQUIRE(ret.http_error_code == 200); - } - THEN ("Answer is valid") - { - REQUIRE(account.valid()); - } - THEN ("The answer makes sense") - { - REQUIRE(account.username() == "testaccount"); + try + { + ret = masto.get(API::v1::accounts_id, + {{ "id", { id }}}); + account = Easy::Account(ret.answer); + } + catch (const std::exception &e) + { + exception = true; + } + THEN("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + THEN ("No errors are returned") + { + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + } + THEN ("Answer is valid") + { + REQUIRE(account.valid()); + } + THEN ("The answer makes sense") + { + REQUIRE(account.username() == "testaccount"); + } } } } diff --git a/tests/test_api_v1_accounts_verify_credentials.cpp b/tests/test_api_v1_accounts_verify_credentials.cpp new file mode 100644 index 0000000..6513b1c --- /dev/null +++ b/tests/test_api_v1_accounts_verify_credentials.cpp @@ -0,0 +1,107 @@ +/* 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 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include "mastodon-cpp.hpp" +#include "easy/easy.hpp" +#include "easy/entities/account.hpp" + +using namespace Mastodon; + +SCENARIO ("/api/v1/accounts/verify_credentials can be called successfully", + "[api][mastodon][pleroma][glitch-soc]") +{ + GIVEN ("access token return_call") + { + const char *access_token = std::getenv("MASTODON_CPP_ACCESS_TOKEN"); + return_call ret; + bool exception = false; + bool username_found = false; + + REQUIRE (access_token != nullptr); + + GIVEN ("Mastodon::API") + { + Mastodon::API masto("likeable.space", access_token); + + WHEN ("/api/v1/accounts/verify_credentials is called") + { + try + { + ret = masto.get(API::v1::accounts_verify_credentials); + username_found = ret.answer.find( + "\"username\":\"testaccount\"") != std::string::npos; + } + catch (const std::exception &e) + { + exception = true; + } + THEN("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + THEN ("No errors are returned") + { + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + } + THEN ("The answer makes sense") + { + REQUIRE(username_found); + } + } + } + + GIVEN ("Mastodon::Easy::API") + { + Mastodon::Easy::API masto("likeable.space", access_token); + Easy::Account account; + + WHEN ("/api/v1/accounts/verify_credentials is called") + { + try + { + ret = masto.get(API::v1::accounts_verify_credentials); + account = Easy::Account(ret.answer); + } + catch (const std::exception &e) + { + exception = true; + } + THEN("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + THEN ("No errors are returned") + { + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + } + THEN ("Answer is valid") + { + REQUIRE(account.valid()); + } + THEN ("The answer makes sense") + { + REQUIRE(account.username() == "testaccount"); + } + } + } + } +} diff --git a/tests/test_api_v1_instance.cpp b/tests/test_api_v1_instance.cpp index 944a7fe..02b3eae 100644 --- a/tests/test_api_v1_instance.cpp +++ b/tests/test_api_v1_instance.cpp @@ -26,75 +26,77 @@ using namespace Mastodon; SCENARIO ("/api/v1/instance can be called successfully", "[api][mastodon][pleroma][glitch-soc]") { - GIVEN ("Mastodon::API") + GIVEN ("return_call") { - Mastodon::API masto("likeable.space", ""); return_call ret; bool exception = false; - bool uri_found = false; - WHEN ("/api/v1/instance is called") + GIVEN ("Mastodon::API") { - try + Mastodon::API masto("likeable.space", ""); + bool uri_found = false; + + WHEN ("/api/v1/instance is called") { - ret = masto.get(API::v1::instance); - uri_found = ret.answer.find( - "\"uri\":\"https://likeable.space\"") != std::string::npos; - } - catch (const std::exception &e) - { - exception = true; - } - THEN("No exception is thrown") - { - REQUIRE_FALSE(exception); - } - THEN ("No errors are returned") - { - REQUIRE(ret.error_code == 0); - REQUIRE(ret.http_error_code == 200); - } - THEN ("The answer makes sense") - { - REQUIRE(uri_found); + try + { + ret = masto.get(API::v1::instance); + uri_found = ret.answer.find( + "\"uri\":\"https://likeable.space\"") != std::string::npos; + } + catch (const std::exception &e) + { + exception = true; + } + THEN("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + THEN ("No errors are returned") + { + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + } + THEN ("The answer makes sense") + { + REQUIRE(uri_found); + } } } - } - GIVEN ("Mastodon::Easy::API") - { - Mastodon::Easy::API masto("likeable.space", ""); - return_call ret; - Easy::Instance instance; - bool exception = false; - - WHEN ("/api/v1/instance is called") + GIVEN ("Mastodon::Easy::API") { - try + Mastodon::Easy::API masto("likeable.space", ""); + Easy::Instance instance; + + WHEN ("/api/v1/instance is called") { - ret = masto.get(API::v1::instance); - instance = Easy::Instance(ret.answer); - } - catch (const std::exception &e) - { - exception = true; - } - THEN("No exception is thrown") - { - REQUIRE_FALSE(exception); - } - THEN ("No errors are returned") - { - REQUIRE(ret.error_code == 0); - REQUIRE(ret.http_error_code == 200); - } - THEN ("Answer is valid") - { - REQUIRE(instance.valid()); - } - THEN ("The answer makes sense") - { - REQUIRE(instance.uri() == "https://likeable.space"); + try + { + ret = masto.get(API::v1::instance); + instance = Easy::Instance(ret.answer); + } + catch (const std::exception &e) + { + exception = true; + } + THEN("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + THEN ("No errors are returned") + { + REQUIRE(ret.error_code == 0); + REQUIRE(ret.http_error_code == 200); + } + THEN ("Answer is valid") + { + REQUIRE(instance.valid()); + } + THEN ("The answer makes sense") + { + REQUIRE(instance.uri() == "https://likeable.space"); + } } } }