Skip to content

Commit

Permalink
test: Add and fix tests
Browse files Browse the repository at this point in the history
Some tests added to the AgentRegistration and AgentInfo classes,
and others fixed due to previous changes.
  • Loading branch information
sdvendramini committed Oct 30, 2024
1 parent 4d0febb commit d871cbc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
39 changes: 25 additions & 14 deletions src/agent/agent_info/tests/agent_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,15 @@ TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues)
EXPECT_NE(agentInfo.GetUUID(), "");
}

TEST_F(AgentInfoTest, TestParameterizedConstructor)
{
const std::string name = "new_name";
const std::string key = "new_key";
const std::string uuid = "new_uuid";

const AgentInfo agentInfo(name, key, uuid);
EXPECT_EQ(agentInfo.GetName(), name);
EXPECT_EQ(agentInfo.GetKey(), key);
EXPECT_EQ(agentInfo.GetUUID(), uuid);
}

TEST_F(AgentInfoTest, TestPersistedValues)
{
const AgentInfo agentInfo("test_name", "test_key", "test_uuid");
AgentInfo agentInfo;
agentInfo.SetName("test_name");
agentInfo.SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
agentInfo.SetUUID("test_uuid");
const AgentInfo agentInfoReloaded;
EXPECT_EQ(agentInfoReloaded.GetName(), "test_name");
EXPECT_EQ(agentInfoReloaded.GetKey(), "test_key");
EXPECT_EQ(agentInfoReloaded.GetKey(), "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
EXPECT_EQ(agentInfoReloaded.GetUUID(), "test_uuid");
}

Expand Down Expand Up @@ -72,6 +63,26 @@ TEST_F(AgentInfoTest, TestSetKey)
EXPECT_EQ(agentInfoReloaded.GetKey(), newKey);
}

TEST_F(AgentInfoTest, TestSetBadKey)
{
AgentInfo agentInfo;
const std::string newKey = "4GhT7uFm";

ASSERT_FALSE(agentInfo.SetKey(newKey));
}

TEST_F(AgentInfoTest, TestSetEmptyKey)
{
AgentInfo agentInfo;
const std::string newKey;

agentInfo.SetKey(newKey);
EXPECT_NE(agentInfo.GetKey(), newKey);

const AgentInfo agentInfoReloaded;
EXPECT_NE(agentInfoReloaded.GetKey(), newKey);
}

TEST_F(AgentInfoTest, TestSetUUID)
{
AgentInfo agentInfo;
Expand Down
51 changes: 45 additions & 6 deletions src/agent/tests/agent_registration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,17 @@ class MockHttpClient : public http_client::IHttpClient
class RegisterTest : public ::testing::Test
{
protected:
void SetUp() override
{
agent = std::make_unique<AgentInfo>("agent_name", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_uuid");
registration = std::make_unique<agent_registration::AgentRegistration>(
"user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt);
}
void SetUp() override {}

std::unique_ptr<AgentInfo> agent;
std::unique_ptr<agent_registration::AgentRegistration> registration;
};

TEST_F(RegisterTest, RegistrationTestSuccess)
{
registration = std::make_unique<agent_registration::AgentRegistration>(
"user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt);
agent = std::make_unique<AgentInfo>();

MockHttpClient mockHttpClient;

Expand Down Expand Up @@ -106,6 +104,10 @@ TEST_F(RegisterTest, RegistrationTestSuccess)

TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails)
{
registration = std::make_unique<agent_registration::AgentRegistration>(
"user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt);
agent = std::make_unique<AgentInfo>();

MockHttpClient mockHttpClient;

EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, "user", "password", testing::_))
Expand All @@ -118,6 +120,10 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails)

TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
{
registration = std::make_unique<agent_registration::AgentRegistration>(
"user", "password", "4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj", "agent_name", std::nullopt);
agent = std::make_unique<AgentInfo>();

MockHttpClient mockHttpClient;

EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, "user", "password", testing::_))
Expand All @@ -133,6 +139,39 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
ASSERT_FALSE(res);
}

TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey)
{
registration =
std::make_unique<agent_registration::AgentRegistration>("user", "password", "", "agent_name", std::nullopt);
agent = std::make_unique<AgentInfo>();

MockHttpClient mockHttpClient;

EXPECT_CALL(mockHttpClient,
AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return("token"));

nlohmann::json bodyJson = {{"id", agent->GetUUID()}, {"key", agent->GetKey()}, {"name", agent->GetName()}};

http_client::HttpRequestParams reqParams(
boost::beast::http::verb::post, "localhost", "55000", "/agents", true, "token", "", bodyJson.dump());

boost::beast::http::response<boost::beast::http::dynamic_body> expectedResponse;
expectedResponse.result(boost::beast::http::status::ok);

EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::Eq(reqParams))).WillOnce(testing::Return(expectedResponse));

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
const bool res = registration->Register(mockHttpClient);
ASSERT_TRUE(res);
}

TEST_F(RegisterTest, RegistrationTestFailWithBadKey)
{
ASSERT_THROW(agent_registration::AgentRegistration("user", "password", "badKey", "agent_name", std::nullopt),
std::invalid_argument);
}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit d871cbc

Please sign in to comment.