Skip to content

Commit

Permalink
Add tests for login_api
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Jul 29, 2022
1 parent a666963 commit 531c611
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
54 changes: 51 additions & 3 deletions tests/common/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <graphene/api_helper_indexes/api_helper_indexes.hpp>
#include <graphene/es_objects/es_objects.hpp>
#include <graphene/custom_operations/custom_operations_plugin.hpp>
#include <graphene/debug_witness/debug_witness.hpp>

#include <graphene/chain/balance_object.hpp>
#include <graphene/chain/committee_member_object.hpp>
Expand Down Expand Up @@ -306,10 +307,57 @@ std::shared_ptr<boost::program_options::variables_map> database_fixture_base::in
fc::set_option( options, "api-limit-get-full-accounts", (uint32_t)200 );
fc::set_option( options, "api-limit-get-full-accounts-lists", (uint32_t)120 );
}
if( fixture.current_suite_name == "login_api_tests"
&& fixture.current_test_name =="get_config_test" )

if( fixture.current_suite_name == "login_api_tests" )
{
fc::set_option( options, "api-limit-get-full-accounts-subscribe", (uint32_t)120 );
if( fixture.current_test_name =="get_config_test" )
fc::set_option( options, "api-limit-get-full-accounts-subscribe", (uint32_t)120 );
if( fixture.current_test_name =="login_test" )
{
// bytemaster/supersecret, user2/superpassword2
string api_access_config = R"(
{
"permission_map" :
[
[
"bytemaster",
{
"password_hash_b64" : "9e9GF7ooXVb9k4BoSfNIPTelXeGOZ5DrgOYMj94elaY=",
"password_salt_b64" : "INDdM6iCi/8=",
"allowed_apis" : ["database_api", "network_broadcast_api", "history_api", "network_node_api",
"asset_api", "crypto_api", "block_api", "orders_api", "custom_operations_api"
"debug_api"]
}
],
[
"user2",
{
"password_hash_b64" : "myadjRISnFOWn2TTd91zqbY50q0w2j/oJGlcdQkUB0Y=",
"password_salt_b64" : "Zb8JrQDKNIQ=",
"allowed_apis" : ["history_api"]
}
],
[
"*",
{
"password_hash_b64" : "*",
"password_salt_b64" : "*",
"allowed_apis" : ["database_api", "network_broadcast_api", "history_api"]
}
]
]
}
)";

fc::json::save_to_file( fc::json::from_string( api_access_config ),
fixture.data_dir.path() / "api-access.json" );
fc::set_option( options, "api-access",
boost::filesystem::path(fixture.data_dir.path() / "api-access.json") );

fixture.app.register_plugin<graphene::debug_witness_plugin::debug_witness_plugin>(true);
fixture.app.register_plugin<graphene::custom_operations::custom_operations_plugin>(true);
fc::set_option( options, "custom-operations-start-block", uint32_t(1) );
}
}

// add account tracking for ahplugin for special test case with track-account enabled
Expand Down
62 changes: 62 additions & 0 deletions tests/tests/login_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,66 @@ BOOST_AUTO_TEST_CASE( get_config_test )

} FC_CAPTURE_LOG_AND_RETHROW( (0) ) }

BOOST_AUTO_TEST_CASE( login_test )
{ try {
graphene::app::login_api login_api1( app );
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 0u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );

login_api1.login("",""); // */*
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 3u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );
auto db_api1 = login_api1.database();
auto his_api1 = login_api1.history();
auto nb_api1 = login_api1.network_broadcast();

login_api1.login("user2","superpassword2");
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 1u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );
BOOST_CHECK_THROW( login_api1.database(), fc::exception );
auto his_api2 = login_api1.history();
BOOST_CHECK( his_api1 == his_api2 );

login_api1.login("user2","superpassword3"); // wrong password
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 0u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );
BOOST_CHECK_THROW( login_api1.database(), fc::exception );
BOOST_CHECK_THROW( login_api1.history(), fc::exception );

login_api1.login("bytemaster","supersecret4"); // wrong password
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 0u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );
BOOST_CHECK_THROW( login_api1.database(), fc::exception );
BOOST_CHECK_THROW( login_api1.history(), fc::exception );

login_api1.login("bytemaster","supersecret");
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 10u );
auto nn_api3 = login_api1.network_node();
auto db_api3 = login_api1.database();
auto his_api3 = login_api1.history();
auto ord_api3 = login_api1.orders();
auto nb_api3 = login_api1.network_broadcast();
auto as_api3 = login_api1.asset();
auto cr_api3 = login_api1.crypto();
auto blk_api3 = login_api1.block();
auto co_api3 = login_api1.custom_operations();
auto dbg_api3 = login_api1.debug();
BOOST_CHECK( his_api1 == his_api3 );

login_api1.logout();
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 0u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );
BOOST_CHECK_THROW( login_api1.database(), fc::exception );
BOOST_CHECK_THROW( login_api1.history(), fc::exception );

login_api1.login("bytemaster2","randompassword"); // */*
BOOST_CHECK_EQUAL( login_api1.get_available_api_sets().size(), 3u );
BOOST_CHECK_THROW( login_api1.network_node(), fc::exception );
auto db_api4 = login_api1.database();
auto his_api4 = login_api1.history();
auto nb_api4 = login_api1.network_broadcast();
BOOST_CHECK( his_api1 == his_api4 );

} FC_CAPTURE_LOG_AND_RETHROW( (0) ) }

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 531c611

Please sign in to comment.