mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-20 11:45:53 +00:00
Api v1 bool support (#877)
* Allow not bool for signer_lists * Allow transactions to be not bool for v1 * Add tests for JsonBool
This commit is contained in:
81
unittests/rpc/JsonBoolTests.cpp
Normal file
81
unittests/rpc/JsonBoolTests.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
#include <rpc/common/JsonBool.h>
|
||||
|
||||
#include <boost/json/parse.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace rpc;
|
||||
namespace json = boost::json;
|
||||
using namespace testing;
|
||||
|
||||
struct JsonBoolTestsCaseBundle
|
||||
{
|
||||
std::string testName;
|
||||
std::string json;
|
||||
bool expectedBool;
|
||||
};
|
||||
|
||||
class JsonBoolTests : public TestWithParam<JsonBoolTestsCaseBundle>
|
||||
{
|
||||
public:
|
||||
struct NameGenerator
|
||||
{
|
||||
template <class ParamType>
|
||||
std::string
|
||||
operator()(const testing::TestParamInfo<ParamType>& info) const
|
||||
{
|
||||
auto bundle = static_cast<JsonBoolTestsCaseBundle>(info.param);
|
||||
return bundle.testName;
|
||||
}
|
||||
};
|
||||
|
||||
static auto
|
||||
generateTestValuesForParametersTest()
|
||||
{
|
||||
return std::vector<JsonBoolTestsCaseBundle>{
|
||||
{"NullValue", R"({ "test_bool": null })", false},
|
||||
{"BoolTrueValue", R"({ "test_bool": true })", true},
|
||||
{"BoolFalseValue", R"({ "test_bool": false })", false},
|
||||
{"IntTrueValue", R"({ "test_bool": 1 })", true},
|
||||
{"IntFalseValue", R"({ "test_bool": 0 })", false},
|
||||
{"DoubleTrueValue", R"({ "test_bool": 0.1 })", true},
|
||||
{"DoubleFalseValue", R"({ "test_bool": 0.0 })", false},
|
||||
{"StringTrueValue", R"({ "test_bool": "true" })", true},
|
||||
{"StringFalseValue", R"({ "test_bool": "false" })", true},
|
||||
{"ArrayTrueValue", R"({ "test_bool": [0] })", true},
|
||||
{"ArrayFalseValue", R"({ "test_bool": [] })", false},
|
||||
{"ObjectTrueValue", R"({ "test_bool": { "key": null } })", true},
|
||||
{"ObjectFalseValue", R"({ "test_bool": {} })", false}};
|
||||
}
|
||||
};
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
JsonBoolCheckGroup,
|
||||
JsonBoolTests,
|
||||
ValuesIn(JsonBoolTests::generateTestValuesForParametersTest()),
|
||||
JsonBoolTests::NameGenerator{});
|
||||
|
||||
TEST_P(JsonBoolTests, Parse)
|
||||
{
|
||||
auto const testBundle = GetParam();
|
||||
const auto jv = json::parse(testBundle.json).as_object();
|
||||
ASSERT_TRUE(jv.contains("test_bool"));
|
||||
EXPECT_EQ(testBundle.expectedBool, value_to<JsonBool>(jv.at("test_bool")).value);
|
||||
}
|
||||
@@ -107,7 +107,7 @@ TEST_P(AccountInfoParameterTest, InvalidParams)
|
||||
runSpawn([&, this](auto yield) {
|
||||
auto const handler = AnyHandler{AccountInfoHandler{mockBackendPtr}};
|
||||
auto const req = json::parse(testBundle.testJson);
|
||||
auto const output = handler.process(req, Context{yield});
|
||||
auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 2});
|
||||
ASSERT_FALSE(output);
|
||||
|
||||
auto const err = rpc::makeError(output.error());
|
||||
@@ -116,6 +116,25 @@ TEST_P(AccountInfoParameterTest, InvalidParams)
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(AccountInfoParameterTest, ApiV1SignerListIsNotBool)
|
||||
{
|
||||
static constexpr auto reqJson = R"(
|
||||
{"ident":"rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun", "signer_lists":1}
|
||||
)";
|
||||
auto* rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
|
||||
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence);
|
||||
runSpawn([&, this](auto yield) {
|
||||
auto const handler = AnyHandler{AccountInfoHandler{mockBackendPtr}};
|
||||
auto const req = json::parse(reqJson);
|
||||
auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 1});
|
||||
ASSERT_FALSE(output);
|
||||
|
||||
auto const err = rpc::makeError(output.error());
|
||||
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
|
||||
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(RPCAccountInfoHandlerTest, LedgerNonExistViaIntSequence)
|
||||
{
|
||||
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
|
||||
|
||||
@@ -156,7 +156,7 @@ TEST_P(NoRippleCheckParameterTest, InvalidParams)
|
||||
runSpawn([&, this](auto yield) {
|
||||
auto const handler = AnyHandler{NoRippleCheckHandler{mockBackendPtr}};
|
||||
auto const req = json::parse(testBundle.testJson);
|
||||
auto const output = handler.process(req, Context{yield});
|
||||
auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 2});
|
||||
ASSERT_FALSE(output);
|
||||
|
||||
auto const err = rpc::makeError(output.error());
|
||||
@@ -165,6 +165,29 @@ TEST_P(NoRippleCheckParameterTest, InvalidParams)
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(NoRippleCheckParameterTest, V1ApiTransactionsIsNotBool)
|
||||
{
|
||||
static constexpr auto reqJson = R"(
|
||||
{
|
||||
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
||||
"role": "gateway",
|
||||
"transactions": "gg"
|
||||
}
|
||||
)";
|
||||
auto rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
|
||||
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence);
|
||||
runSpawn([&, this](auto yield) {
|
||||
auto const handler = AnyHandler{NoRippleCheckHandler{mockBackendPtr}};
|
||||
auto const req = json::parse(reqJson);
|
||||
auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 1});
|
||||
ASSERT_FALSE(output);
|
||||
|
||||
auto const err = rpc::makeError(output.error());
|
||||
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
|
||||
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(RPCNoRippleCheckTest, LedgerNotExistViaHash)
|
||||
{
|
||||
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
|
||||
|
||||
Reference in New Issue
Block a user