From 7615dabdf880a8bbd4790679d52bb90eebfa17b6 Mon Sep 17 00:00:00 2001 From: Santiago Vendramini Date: Sat, 17 Aug 2024 04:09:43 -0300 Subject: [PATCH 1/3] feat: key parameter is added to registration and authentication. The ip parameter is removed from the registration process as it will no longer be necessary. Also the parameter key is added to the registration process. Then this key is also sent together with the uuid during authentication. Unit tests are fixed. --- src/agent/agent_info/include/agent_info.hpp | 8 +++---- .../include/agent_info_persistance.hpp | 4 ++-- src/agent/agent_info/src/agent_info.cpp | 18 +++++++-------- .../agent_info/src/agent_info_persistance.cpp | 12 +++++----- .../tests/agent_info_persistance_test.cpp | 12 +++++----- .../agent_info/tests/agent_info_test.cpp | 22 +++++++++---------- .../communicator/include/communicator.hpp | 2 ++ .../communicator/include/http_client.hpp | 6 +++-- src/agent/communicator/src/communicator.cpp | 4 +++- src/agent/communicator/src/http_client.cpp | 12 +++++----- .../communicator/tests/communicator_test.cpp | 2 +- src/agent/src/agent.cpp | 1 + src/agent/src/main.cpp | 10 +++------ src/agent/src/register.cpp | 8 +++---- 14 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index d2344820d8..548a7c5f6f 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -6,18 +6,18 @@ class AgentInfo { public: AgentInfo(); - AgentInfo(const std::string& name, const std::string& ip, const std::string& uuid); + AgentInfo(const std::string& name, const std::string& key, const std::string& uuid); std::string GetName() const; - std::string GetIP() const; + std::string GetKey() const; std::string GetUUID() const; void SetName(const std::string& name); - void SetIP(const std::string& ip); + void SetKey(const std::string& key); void SetUUID(const std::string& uuid); private: std::string m_name; - std::string m_ip; + std::string m_key; std::string m_uuid; }; diff --git a/src/agent/agent_info/include/agent_info_persistance.hpp b/src/agent/agent_info/include/agent_info_persistance.hpp index 47cc7d0b51..66267f73d5 100644 --- a/src/agent/agent_info/include/agent_info_persistance.hpp +++ b/src/agent/agent_info/include/agent_info_persistance.hpp @@ -22,11 +22,11 @@ class AgentInfoPersistance AgentInfoPersistance& operator=(AgentInfoPersistance&&) = delete; std::string GetName() const; - std::string GetIP() const; + std::string GetKey() const; std::string GetUUID() const; void SetName(const std::string& name); - void SetIP(const std::string& ip); + void SetKey(const std::string& key); void SetUUID(const std::string& uuid); void ResetToDefault(); diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index 74911e3e59..224a68fb90 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -9,7 +9,7 @@ AgentInfo::AgentInfo() { AgentInfoPersistance agentInfoPersistance; m_name = agentInfoPersistance.GetName(); - m_ip = agentInfoPersistance.GetIP(); + m_key = agentInfoPersistance.GetKey(); m_uuid = agentInfoPersistance.GetUUID(); if (m_uuid.empty()) @@ -20,14 +20,14 @@ AgentInfo::AgentInfo() } } -AgentInfo::AgentInfo(const std::string& name, const std::string& ip, const std::string& uuid) +AgentInfo::AgentInfo(const std::string& name, const std::string& key, const std::string& uuid) : m_name(name) - , m_ip(ip) + , m_key(key) , m_uuid(uuid) { AgentInfoPersistance agentInfoPersistance; agentInfoPersistance.SetName(m_name); - agentInfoPersistance.SetIP(m_ip); + agentInfoPersistance.SetKey(m_key); agentInfoPersistance.SetUUID(m_uuid); } @@ -35,9 +35,9 @@ std::string AgentInfo::GetName() const { return m_name; } -std::string AgentInfo::GetIP() const +std::string AgentInfo::GetKey() const { - return m_ip; + return m_key; } std::string AgentInfo::GetUUID() const { @@ -51,11 +51,11 @@ void AgentInfo::SetName(const std::string& name) m_name = name; } -void AgentInfo::SetIP(const std::string& ip) +void AgentInfo::SetKey(const std::string& key) { AgentInfoPersistance agentInfoPersistance; - agentInfoPersistance.SetIP(ip); - m_ip = ip; + agentInfoPersistance.SetKey(key); + m_key = key; } void AgentInfo::SetUUID(const std::string& uuid) diff --git a/src/agent/agent_info/src/agent_info_persistance.cpp b/src/agent/agent_info/src/agent_info_persistance.cpp index 422efdf112..2502595ffe 100644 --- a/src/agent/agent_info/src/agent_info_persistance.cpp +++ b/src/agent/agent_info/src/agent_info_persistance.cpp @@ -66,7 +66,7 @@ void AgentInfoPersistance::CreateAgentInfoTable() { m_db->exec("CREATE TABLE IF NOT EXISTS agent_info (" "name TEXT, " - "ip TEXT, " + "key TEXT, " "uuid TEXT" ");"); } @@ -86,7 +86,7 @@ void AgentInfoPersistance::InsertDefaultAgentInfo() if (count == 0) { - SQLite::Statement insert(*m_db, "INSERT INTO agent_info (name, ip, uuid) VALUES (?, ?, ?);"); + SQLite::Statement insert(*m_db, "INSERT INTO agent_info (name, key, uuid) VALUES (?, ?, ?);"); insert.exec(); } } @@ -133,9 +133,9 @@ std::string AgentInfoPersistance::GetName() const return GetAgentInfoValue("name"); } -std::string AgentInfoPersistance::GetIP() const +std::string AgentInfoPersistance::GetKey() const { - return GetAgentInfoValue("ip"); + return GetAgentInfoValue("key"); } std::string AgentInfoPersistance::GetUUID() const @@ -148,9 +148,9 @@ void AgentInfoPersistance::SetName(const std::string& name) SetAgentInfoValue("name", name); } -void AgentInfoPersistance::SetIP(const std::string& ip) +void AgentInfoPersistance::SetKey(const std::string& key) { - SetAgentInfoValue("ip", ip); + SetAgentInfoValue("key", key); } void AgentInfoPersistance::SetUUID(const std::string& uuid) diff --git a/src/agent/agent_info/tests/agent_info_persistance_test.cpp b/src/agent/agent_info/tests/agent_info_persistance_test.cpp index 59c2da2a90..2356f4674c 100644 --- a/src/agent/agent_info/tests/agent_info_persistance_test.cpp +++ b/src/agent/agent_info/tests/agent_info_persistance_test.cpp @@ -25,7 +25,7 @@ TEST_F(AgentInfoPersistanceTest, TestConstruction) TEST_F(AgentInfoPersistanceTest, TestDefaultValues) { EXPECT_EQ(persistance->GetName(), ""); - EXPECT_EQ(persistance->GetIP(), ""); + EXPECT_EQ(persistance->GetKey(), ""); EXPECT_EQ(persistance->GetUUID(), ""); } @@ -36,11 +36,11 @@ TEST_F(AgentInfoPersistanceTest, TestSetName) EXPECT_EQ(persistance->GetName(), newName); } -TEST_F(AgentInfoPersistanceTest, TestSetIP) +TEST_F(AgentInfoPersistanceTest, TestSetKey) { - const std::string newIP = "192.168.1.1"; - persistance->SetIP(newIP); - EXPECT_EQ(persistance->GetIP(), newIP); + const std::string newKey = "new_key"; + persistance->SetKey(newKey); + EXPECT_EQ(persistance->GetKey(), newKey); } TEST_F(AgentInfoPersistanceTest, TestSetUUID) @@ -58,7 +58,7 @@ TEST_F(AgentInfoPersistanceTest, TestResetToDefault) persistance->ResetToDefault(); EXPECT_EQ(persistance->GetName(), ""); - EXPECT_EQ(persistance->GetIP(), ""); + EXPECT_EQ(persistance->GetKey(), ""); EXPECT_EQ(persistance->GetUUID(), ""); } diff --git a/src/agent/agent_info/tests/agent_info_test.cpp b/src/agent/agent_info/tests/agent_info_test.cpp index 0f9d46129e..85f7f5f6dc 100644 --- a/src/agent/agent_info/tests/agent_info_test.cpp +++ b/src/agent/agent_info/tests/agent_info_test.cpp @@ -23,28 +23,28 @@ TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues) { const AgentInfo agentInfo; EXPECT_EQ(agentInfo.GetName(), ""); - EXPECT_EQ(agentInfo.GetIP(), ""); + EXPECT_EQ(agentInfo.GetKey(), ""); EXPECT_NE(agentInfo.GetUUID(), ""); } TEST_F(AgentInfoTest, TestParameterizedConstructor) { const std::string name = "new_name"; - const std::string ip = "192.168.1.1"; + const std::string key = "new_key"; const std::string uuid = "new_uuid"; - const AgentInfo agentInfo(name, ip, uuid); + const AgentInfo agentInfo(name, key, uuid); EXPECT_EQ(agentInfo.GetName(), name); - EXPECT_EQ(agentInfo.GetIP(), ip); + EXPECT_EQ(agentInfo.GetKey(), key); EXPECT_EQ(agentInfo.GetUUID(), uuid); } TEST_F(AgentInfoTest, TestPersistedValues) { - const AgentInfo agentInfo("test_name", "test_ip", "test_uuid"); + const AgentInfo agentInfo("test_name", "test_key", "test_uuid"); const AgentInfo agentInfoReloaded; EXPECT_EQ(agentInfoReloaded.GetName(), "test_name"); - EXPECT_EQ(agentInfoReloaded.GetIP(), "test_ip"); + EXPECT_EQ(agentInfoReloaded.GetKey(), "test_key"); EXPECT_EQ(agentInfoReloaded.GetUUID(), "test_uuid"); } @@ -60,16 +60,16 @@ TEST_F(AgentInfoTest, TestSetName) EXPECT_EQ(agentInfoReloaded.GetName(), newName); } -TEST_F(AgentInfoTest, TestSetIP) +TEST_F(AgentInfoTest, TestSetKey) { AgentInfo agentInfo; - const std::string newIP = "192.168.1.1"; + const std::string newKey = "new_key"; - agentInfo.SetIP(newIP); - EXPECT_EQ(agentInfo.GetIP(), newIP); + agentInfo.SetKey(newKey); + EXPECT_EQ(agentInfo.GetKey(), newKey); const AgentInfo agentInfoReloaded; - EXPECT_EQ(agentInfoReloaded.GetIP(), newIP); + EXPECT_EQ(agentInfoReloaded.GetKey(), newKey); } TEST_F(AgentInfoTest, TestSetUUID) diff --git a/src/agent/communicator/include/communicator.hpp b/src/agent/communicator/include/communicator.hpp index 5d1f028921..2ab91c27c6 100644 --- a/src/agent/communicator/include/communicator.hpp +++ b/src/agent/communicator/include/communicator.hpp @@ -17,6 +17,7 @@ namespace communicator { public: Communicator(const std::string& uuid, + const std::string& key, const std::function GetStringConfigValue); boost::asio::awaitable WaitForTokenExpirationAndAuthenticate(); @@ -37,6 +38,7 @@ namespace communicator std::string m_managerIp; std::string m_port; std::string m_uuid; + std::string m_key; std::string m_token; long long m_tokenExpTimeInSeconds = 0; std::unique_ptr m_tokenExpTimer; diff --git a/src/agent/communicator/include/http_client.hpp b/src/agent/communicator/include/http_client.hpp index 0cc45e1b6b..f2f0d9f099 100644 --- a/src/agent/communicator/include/http_client.hpp +++ b/src/agent/communicator/include/http_client.hpp @@ -54,8 +54,10 @@ namespace http_client boost::beast::http::response PerformHttpRequest(const HttpRequestParams& params); - std::optional - AuthenticateWithUuid(const std::string& host, const std::string& port, const std::string& uuid); + std::optional AuthenticateWithUuidAndKey(const std::string& host, + const std::string& port, + const std::string& uuid, + const std::string& key); std::optional AuthenticateWithUserPassword(const std::string& host, const std::string& port, const std::string& user, diff --git a/src/agent/communicator/src/communicator.cpp b/src/agent/communicator/src/communicator.cpp index 23c61d7c91..638d6ca3a9 100644 --- a/src/agent/communicator/src/communicator.cpp +++ b/src/agent/communicator/src/communicator.cpp @@ -18,8 +18,10 @@ namespace communicator constexpr int TokenPreExpirySecs = 2; Communicator::Communicator(const std::string& uuid, + const std::string& key, const std::function GetStringConfigValue) : m_uuid(uuid) + , m_key(key) { if (GetStringConfigValue != nullptr) { @@ -30,7 +32,7 @@ namespace communicator boost::beast::http::status Communicator::SendAuthenticationRequest() { - const auto token = http_client::AuthenticateWithUuid(m_managerIp, m_port, m_uuid); + const auto token = http_client::AuthenticateWithUuidAndKey(m_managerIp, m_port, m_uuid, m_key); if (token.has_value()) { diff --git a/src/agent/communicator/src/http_client.cpp b/src/agent/communicator/src/http_client.cpp index 9ffba759db..5188607473 100644 --- a/src/agent/communicator/src/http_client.cpp +++ b/src/agent/communicator/src/http_client.cpp @@ -182,12 +182,14 @@ namespace http_client return res; } - std::optional - AuthenticateWithUuid(const std::string& host, const std::string& port, const std::string& uuid) + std::optional AuthenticateWithUuidAndKey(const std::string& host, + const std::string& port, + const std::string& uuid, + const std::string& key) { - const std::string uuidKeyValue = "{\"uuid\":\"" + uuid + "\"}"; - const auto reqParams = http_client::HttpRequestParams( - boost::beast::http::verb::post, host, port, "/authentication", "", "", uuidKeyValue); + const std::string body = "{\"uuid\":\"" + uuid + "\", \"key\":\"" + key + "\"}"; + const auto reqParams = + http_client::HttpRequestParams(boost::beast::http::verb::post, host, port, "/authentication", "", "", body); return GetTokenFromResponse(reqParams); } diff --git a/src/agent/communicator/tests/communicator_test.cpp b/src/agent/communicator/tests/communicator_test.cpp index 301e1e15fc..76e0bdf8b5 100644 --- a/src/agent/communicator/tests/communicator_test.cpp +++ b/src/agent/communicator/tests/communicator_test.cpp @@ -4,7 +4,7 @@ TEST(CommunicatorTest, CommunicatorConstructor) { - EXPECT_NO_THROW(communicator::Communicator communicator("uuid", nullptr)); + EXPECT_NO_THROW(communicator::Communicator communicator("uuid", "key", nullptr)); } int main(int argc, char** argv) diff --git a/src/agent/src/agent.cpp b/src/agent/src/agent.cpp index 1e0ede2169..4fdad374a6 100644 --- a/src/agent/src/agent.cpp +++ b/src/agent/src/agent.cpp @@ -4,6 +4,7 @@ Agent::Agent() : m_communicator(m_agentInfo.GetUUID(), + m_agentInfo.GetKey(), [this](std::string table, std::string key) -> std::string { return m_configurationParser.GetConfig(table, key); }) { diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index f928fdc7e0..4f27fc1897 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -14,23 +14,19 @@ int main(int argc, char* argv[]) { std::cout << "Starting registration process" << std::endl; - if (cmdParser.OptionExists("--user") && cmdParser.OptionExists("--password")) + if (cmdParser.OptionExists("--user") && cmdParser.OptionExists("--password") && cmdParser.OptionExists("--key")) { const auto user = cmdParser.getOptionValue("--user"); const auto password = cmdParser.getOptionValue("--password"); AgentInfo agentInfo; + agentInfo.SetKey(cmdParser.getOptionValue("--key")); if (cmdParser.OptionExists("--name")) { agentInfo.SetName(cmdParser.getOptionValue("--name")); } - if (cmdParser.OptionExists("--ip")) - { - agentInfo.SetIP(cmdParser.getOptionValue("--ip")); - } - const registration::UserCredentials userCredentials {user, password}; if (registration::RegisterAgent(userCredentials)) @@ -44,7 +40,7 @@ int main(int argc, char* argv[]) } else { - std::cout << "--user and --password args are mandatory" << std::endl; + std::cout << "--user, --password and --key args are mandatory" << std::endl; } std::cout << "Exiting ..." << std::endl; diff --git a/src/agent/src/register.cpp b/src/agent/src/register.cpp index a4c24d7afa..1cb2fd2f11 100644 --- a/src/agent/src/register.cpp +++ b/src/agent/src/register.cpp @@ -22,7 +22,7 @@ namespace const std::string& token, const std::string& uuid, const std::optional& name, - const std::optional& ip) + const std::optional& key) { json bodyJson = {{"uuid", uuid}}; @@ -31,9 +31,9 @@ namespace bodyJson["name"] = name.value(); } - if (ip.has_value()) + if (key.has_value()) { - bodyJson["ip"] = ip.value(); + bodyJson["key"] = key.value(); } const auto reqParams = @@ -63,7 +63,7 @@ namespace registration const AgentInfo agentInfo {}; if (const auto registrationResultCode = SendRegistrationRequest( - managerIp, managerPort, token.value(), agentInfo.GetUUID(), agentInfo.GetName(), agentInfo.GetIP()); + managerIp, managerPort, token.value(), agentInfo.GetUUID(), agentInfo.GetName(), agentInfo.GetKey()); registrationResultCode != http::status::ok) { std::cout << "Registration error: " << registrationResultCode << std::endl; From eb90aa1dee3b03a8d75f710e42d7a878cad26ec8 Mon Sep 17 00:00:00 2001 From: Santiago Vendramini Date: Sun, 18 Aug 2024 05:07:12 -0300 Subject: [PATCH 2/3] feat: register UT is fixed Some functions were defined as std::function in order to be able to mock them correctly in gtest without having to define abstract classes or interfaces. This way it was possible to mock everything needed to run the log test correctly. --- .../communicator/include/http_client.hpp | 11 ++-- src/agent/include/register.hpp | 18 +++++- src/agent/src/main.cpp | 4 +- src/agent/src/register.cpp | 29 +++------ src/agent/tests/CMakeLists.txt | 2 +- src/agent/tests/register_test.cpp | 62 ++++++++++++++++++- 6 files changed, 97 insertions(+), 29 deletions(-) diff --git a/src/agent/communicator/include/http_client.hpp b/src/agent/communicator/include/http_client.hpp index f2f0d9f099..bc8e5255bd 100644 --- a/src/agent/communicator/include/http_client.hpp +++ b/src/agent/communicator/include/http_client.hpp @@ -58,8 +58,11 @@ namespace http_client const std::string& port, const std::string& uuid, const std::string& key); - std::optional AuthenticateWithUserPassword(const std::string& host, - const std::string& port, - const std::string& user, - const std::string& password); + + using AuthenticateFunctionType = std::optional(const std::string& host, + const std::string& port, + const std::string& user, + const std::string& password); + + AuthenticateFunctionType AuthenticateWithUserPassword; } // namespace http_client diff --git a/src/agent/include/register.hpp b/src/agent/include/register.hpp index f50907d3c0..9bae18ab71 100644 --- a/src/agent/include/register.hpp +++ b/src/agent/include/register.hpp @@ -1,5 +1,9 @@ #pragma once +#include + +#include +#include #include #include @@ -11,5 +15,17 @@ namespace registration std::string password; }; - bool RegisterAgent(const UserCredentials& userCredentials); + using RegisterFunctionType = boost::beast::http::status(const std::string& host, + const std::string& port, + const std::string& token, + const std::string& uuid, + const std::string& key, + const std::optional& name); + + RegisterFunctionType SendRegistrationRequest; + + bool RegisterAgent(const UserCredentials& userCredentials, + std::function AuthenticateFunction, + std::function RegisterFunction); + } // namespace registration diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index 4f27fc1897..e2c298fa94 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -29,7 +30,8 @@ int main(int argc, char* argv[]) const registration::UserCredentials userCredentials {user, password}; - if (registration::RegisterAgent(userCredentials)) + if (registration::RegisterAgent( + userCredentials, http_client::AuthenticateWithUserPassword, registration::SendRegistrationRequest)) { std::cout << "Agent registered." << std::endl; } diff --git a/src/agent/src/register.cpp b/src/agent/src/register.cpp index 1cb2fd2f11..6bca1c7e46 100644 --- a/src/agent/src/register.cpp +++ b/src/agent/src/register.cpp @@ -2,10 +2,8 @@ #include #include -#include #include -#include #include #include #include @@ -15,44 +13,37 @@ namespace http = beast::http; using tcp = boost::asio::ip::tcp; using json = nlohmann::json; -namespace +namespace registration { http::status SendRegistrationRequest(const std::string& host, const std::string& port, const std::string& token, const std::string& uuid, - const std::optional& name, - const std::optional& key) + const std::string& key, + const std::optional& name) { - json bodyJson = {{"uuid", uuid}}; + json bodyJson = {{"uuid", uuid}, {"key", key}}; if (name.has_value()) { bodyJson["name"] = name.value(); } - if (key.has_value()) - { - bodyJson["key"] = key.value(); - } - const auto reqParams = http_client::HttpRequestParams(http::verb::post, host, port, "/agents", token, "", bodyJson.dump()); const http::response res = http_client::PerformHttpRequest(reqParams); return res.result(); } -} // namespace -namespace registration -{ - bool RegisterAgent(const UserCredentials& userCredentials) + bool RegisterAgent(const UserCredentials& userCredentials, + std::function AuthenticateFunction, + std::function RegistrationFunction) { const configuration::ConfigurationParser configurationParser; const auto managerIp = configurationParser.GetConfig("agent", "manager_ip"); const auto managerPort = configurationParser.GetConfig("agent", "server_mgmt_api_port"); - const auto token = http_client::AuthenticateWithUserPassword( - managerIp, managerPort, userCredentials.user, userCredentials.password); + const auto token = AuthenticateFunction(managerIp, managerPort, userCredentials.user, userCredentials.password); if (!token.has_value()) { @@ -62,8 +53,8 @@ namespace registration const AgentInfo agentInfo {}; - if (const auto registrationResultCode = SendRegistrationRequest( - managerIp, managerPort, token.value(), agentInfo.GetUUID(), agentInfo.GetName(), agentInfo.GetKey()); + if (const auto registrationResultCode = RegistrationFunction( + managerIp, managerPort, token.value(), agentInfo.GetUUID(), agentInfo.GetKey(), agentInfo.GetName()); registrationResultCode != http::status::ok) { std::cout << "Registration error: " << registrationResultCode << std::endl; diff --git a/src/agent/tests/CMakeLists.txt b/src/agent/tests/CMakeLists.txt index 89c01feb5f..1850fddeda 100644 --- a/src/agent/tests/CMakeLists.txt +++ b/src/agent/tests/CMakeLists.txt @@ -9,7 +9,7 @@ target_link_libraries(task_manager_test PRIVATE Agent GTest::gtest) add_test(NAME TaskManagerTest COMMAND task_manager_test) add_executable(register_test register_test.cpp) -target_link_libraries(register_test PRIVATE Agent GTest::gtest) +target_link_libraries(register_test PRIVATE Agent GTest::gmock GTest::gtest) add_test(NAME RegisterTest COMMAND register_test) add_executable(signal_handler_test signal_handler_test.cpp) diff --git a/src/agent/tests/register_test.cpp b/src/agent/tests/register_test.cpp index 19bf67ed21..e96aa2984d 100644 --- a/src/agent/tests/register_test.cpp +++ b/src/agent/tests/register_test.cpp @@ -1,14 +1,70 @@ #include +#include + +#include #include -TEST(RegistrationTest, RegistrationTest) +namespace beast = boost::beast; +namespace http = beast::http; +using ::testing::_; +using ::testing::MockFunction; +using ::testing::Return; + +class RegisterTest : public ::testing::Test { - const registration::UserCredentials userCredentials {"user", "123456"}; - const bool res = registration::RegisterAgent(userCredentials); +protected: + std::unique_ptr agent; + + MockFunction MockAuthenticateFunction; + MockFunction MockRegistrationFunction; + + void SetUp() override + { + agent = std::make_unique("agent_name", "agent_key", "fbdea7a6-0e0d-4d2c-8af3-9330ea8799a3"); + } +}; + +TEST_F(RegisterTest, RegistrationTest) +{ + const registration::UserCredentials userCredentials {"user", "password"}; + + const std::optional name = agent->GetName(); + + EXPECT_CALL(MockAuthenticateFunction, Call("localhost", "55000", "user", "password")).WillOnce(Return("token")); + EXPECT_CALL(MockRegistrationFunction, Call("localhost", "55000", "token", agent->GetUUID(), agent->GetKey(), name)) + .WillOnce(Return(http::status::ok)); + const bool res = registration::RegisterAgent( + userCredentials, MockAuthenticateFunction.AsStdFunction(), MockRegistrationFunction.AsStdFunction()); ASSERT_TRUE(res); } +TEST_F(RegisterTest, RegistrationTestFail) +{ + const registration::UserCredentials userCredentials {"user", "password"}; + + const std::optional name = agent->GetName(); + + EXPECT_CALL(MockAuthenticateFunction, Call("localhost", "55000", "user", "password")).WillOnce(Return("token")); + EXPECT_CALL(MockRegistrationFunction, Call("localhost", "55000", "token", agent->GetUUID(), agent->GetKey(), name)) + .WillOnce(Return(http::status::bad_gateway)); + const bool res = registration::RegisterAgent( + userCredentials, MockAuthenticateFunction.AsStdFunction(), MockRegistrationFunction.AsStdFunction()); + ASSERT_FALSE(res); +} + +TEST_F(RegisterTest, AuthenticationFailDuringRegistrationTest) +{ + const registration::UserCredentials userCredentials {"user", "password"}; + + EXPECT_CALL(MockAuthenticateFunction, Call("localhost", "55000", "user", "password")) + .WillOnce(Return(std::nullopt)); + + const bool res = registration::RegisterAgent( + userCredentials, MockAuthenticateFunction.AsStdFunction(), MockRegistrationFunction.AsStdFunction()); + ASSERT_FALSE(res); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); From 99f7a47b3311ce491c96c5b936453120f510b8af Mon Sep 17 00:00:00 2001 From: Santiago Vendramini Date: Sun, 18 Aug 2024 05:11:08 -0300 Subject: [PATCH 3/3] feat: continue-on-error flag in github actions is set to false --- .github/actions/compile/action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/actions/compile/action.yml b/.github/actions/compile/action.yml index 07ae5ea1c0..097809899e 100644 --- a/.github/actions/compile/action.yml +++ b/.github/actions/compile/action.yml @@ -30,9 +30,9 @@ runs: shell: bash - name: Run Tests - continue-on-error: true + continue-on-error: false run: | set +e cd build - ctest || echo "Tests failed, but continuing" + ctest shell: bash