generated from Sithas/conan_template
169 lines
5.1 KiB
C++
169 lines
5.1 KiB
C++
#ifdef WIN32
|
|
#include <sdkddkver.h>
|
|
#include <WinSock2.h>
|
|
#endif
|
|
|
|
#define BOOST_TEST_MODULE AuthLoginExecutors
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
#include <string>
|
|
|
|
#include "./../../src/endpoints_handlers/AuthLoginExecutor.h"
|
|
|
|
#include "../../src/DAO/MemoryAuthDAO.h"
|
|
#include "./../../src/DAO/MySQLUserDAO.h"
|
|
#include "./../../src/db/mysql_connector.h"
|
|
#include "./../../src/exceptions/session_exception.h"
|
|
#include "./../../src/helpers/helpers.h"
|
|
#include "../fixtures/AuthFixture.h"
|
|
|
|
const std::string kUUID = std::to_string(uad::Random());
|
|
|
|
using namespace std;
|
|
using namespace uad;
|
|
using namespace boost;
|
|
using namespace beast;
|
|
using namespace json;
|
|
|
|
using RouteAuthLoginExecutor = AuthLoginExecutor<beast::http::string_body,
|
|
std::allocator<char>,
|
|
beast::http::string_body>;
|
|
using Request = boost::beast::http::request<beast::http::string_body,
|
|
beast::http::basic_fields<std::allocator<char>>>;
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AuthRegistrationExecutor_Cannot_Serialize_JSON, AuthFixture)
|
|
{
|
|
auto& argv = boost::unit_test::framework::master_test_suite().argv;
|
|
|
|
const std::string mysql_credentials = argv[1];
|
|
|
|
mysqlx::Session* mysql_session = new mysqlx::Session(mysql_credentials);
|
|
|
|
uad::SetMySqlSession(mysql_session);
|
|
|
|
auto user_dao = make_shared<MySQLUserDAO>(GetMySqlSession());
|
|
auto auth_dao = make_shared<MemoryAuthDAO>(GetMySqlSession());
|
|
auto executor = RouteAuthLoginExecutor(GetMySqlSession(), user_dao, auth_dao);
|
|
|
|
Request req;
|
|
|
|
req.body() = "{ \"login\": ABS3 }"s;
|
|
req.content_length(req.body().size());
|
|
|
|
BOOST_CHECK_EXCEPTION(executor(std::move(req)), session_exception,
|
|
[](const session_exception& e) -> bool
|
|
{
|
|
return e.code == beast::http::status::bad_request;
|
|
});
|
|
|
|
mysql_session->close();
|
|
delete mysql_session;
|
|
}
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AuthRegistrationExecutor_Invalid_Login_Data, AuthFixture)
|
|
{
|
|
auto& argv = boost::unit_test::framework::master_test_suite().argv;
|
|
|
|
const std::string mysql_credentials = argv[1];
|
|
|
|
mysqlx::Session* mysql_session = new mysqlx::Session(mysql_credentials);
|
|
|
|
uad::SetMySqlSession(mysql_session);
|
|
|
|
auto user_dao = make_shared<MySQLUserDAO>(GetMySqlSession());
|
|
auto auth_dao = make_shared<MemoryAuthDAO>(GetMySqlSession());
|
|
auto executor = RouteAuthLoginExecutor(GetMySqlSession(), user_dao, auth_dao);
|
|
|
|
Request req;
|
|
value req_body;
|
|
|
|
req_body.emplace_object();
|
|
|
|
req_body.as_object().emplace("login"s, "MyLogin123456780"s + kUUID);
|
|
req_body.as_object().emplace("password"s, "Qwerty123456"s);
|
|
|
|
req.body() = serialize(req_body);
|
|
|
|
BOOST_CHECK_EXCEPTION(executor(std::move(req)), session_exception,
|
|
[](const session_exception& e) -> bool
|
|
{
|
|
return e.code == beast::http::status::forbidden;
|
|
});
|
|
|
|
mysql_session->close();
|
|
delete mysql_session;
|
|
}
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AuthRegistrationExecutor_Invalid_Fields, AuthFixture)
|
|
{
|
|
auto& argv = boost::unit_test::framework::master_test_suite().argv;
|
|
|
|
const std::string mysql_credentials = argv[1];
|
|
|
|
mysqlx::Session* mysql_session = new mysqlx::Session(mysql_credentials);
|
|
|
|
uad::SetMySqlSession(mysql_session);
|
|
|
|
auto user_dao = make_shared<MySQLUserDAO>(GetMySqlSession());
|
|
auto auth_dao = make_shared<MemoryAuthDAO>(GetMySqlSession());
|
|
auto executor = RouteAuthLoginExecutor(GetMySqlSession(), user_dao, auth_dao);
|
|
|
|
Request req;
|
|
value req_body;
|
|
|
|
req_body.emplace_object();
|
|
|
|
req_body.as_object().emplace("login"s, ""s);
|
|
req_body.as_object().emplace("password"s, ""s);
|
|
|
|
req.body() = serialize(req_body);
|
|
|
|
BOOST_CHECK_EXCEPTION(executor(std::move(req)), session_exception,
|
|
[](const session_exception& e) -> bool
|
|
{
|
|
return e.code == beast::http::status::unprocessable_entity;
|
|
});
|
|
|
|
mysql_session->close();
|
|
delete mysql_session;
|
|
}
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AuthRegistrationExecutor_Succesful_Login, AuthFixture)
|
|
{
|
|
auto& argv = boost::unit_test::framework::master_test_suite().argv;
|
|
|
|
const std::string mysql_credentials = argv[1];
|
|
|
|
mysqlx::Session* mysql_session = new mysqlx::Session(mysql_credentials);
|
|
|
|
uad::SetMySqlSession(mysql_session);
|
|
|
|
auto user_dao = make_shared<MySQLUserDAO>(GetMySqlSession());
|
|
auto auth_dao = make_shared<MemoryAuthDAO>(GetMySqlSession());
|
|
auto executor = RouteAuthLoginExecutor(GetMySqlSession(), user_dao, auth_dao);
|
|
|
|
Request req;
|
|
value req_body;
|
|
|
|
user_dao->Create({""s, "MyLogin123456780"s + kUUID, HashPassword("Qwerty123456"s)});
|
|
|
|
req_body.emplace_object();
|
|
|
|
req_body.as_object().emplace("login"s, "MyLogin123456780"s + kUUID);
|
|
req_body.as_object().emplace("password"s, "Qwerty123456"s);
|
|
|
|
req.body() = serialize(req_body);
|
|
|
|
auto response = executor(std::move(req));
|
|
|
|
BOOST_CHECK(response.result() == http::status::ok);
|
|
|
|
auto response_body = parse(response.body());
|
|
|
|
BOOST_CHECK(response_body.as_object().count("token"s) == 1);
|
|
|
|
mysql_session->close();
|
|
delete mysql_session;
|
|
}
|