Skip to content

Commit

Permalink
fix: register test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Sep 26, 2024
1 parent 0e4df93 commit f294c8e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/agent/tests/register_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ class RegisterTest : public ::testing::Test
void SetUp() override
{
agent = std::make_unique<AgentInfo>("agent_name", "agent_key", "agent_uuid");
registration =
std::make_unique<agent_registration::AgentRegistration>("user", "password", "agent_key", "agent_name");
}

MockHttpClient mockHttpClient;
std::unique_ptr<AgentInfo> agent;
registration::UserCredentials userCredentials {"user", "password"};
std::unique_ptr<agent_registration::AgentRegistration> registration;
};

TEST_F(RegisterTest, RegistrationTestSuccess)
{
EXPECT_CALL(mockHttpClient,
AuthenticateWithUserPassword(testing::_, testing::_, userCredentials.user, userCredentials.password))
MockHttpClient mockHttpClient;

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

nlohmann::json bodyJson = {{"id", agent->GetUUID()}, {"key", agent->GetKey()}};
Expand All @@ -76,32 +78,38 @@ TEST_F(RegisterTest, RegistrationTestSuccess)
}

http_client::HttpRequestParams reqParams(
boost::beast::http::verb::post, "localhost", "55000", "/agents", "token", "", bodyJson.dump());
boost::beast::http::verb::post, "localhost", "55000", "/api/v1/agents", "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::RegisterAgent(userCredentials, mockHttpClient);
const bool res = registration->SendRegistration(mockHttpClient);
ASSERT_TRUE(res);
}

TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails)
{
EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_))
MockHttpClient mockHttpClient;

std::string st1 = "user";
std::string st2 = "password";

EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, st1, st2))
.WillOnce(testing::Return(std::nullopt));

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
const bool res = registration::RegisterAgent(userCredentials, mockHttpClient);
const bool res = registration->SendRegistration(mockHttpClient);
ASSERT_FALSE(res);
}

TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
{
EXPECT_CALL(mockHttpClient,
AuthenticateWithUserPassword(testing::_, testing::_, userCredentials.user, userCredentials.password))
MockHttpClient mockHttpClient;

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

boost::beast::http::response<boost::beast::http::dynamic_body> expectedResponse;
Expand All @@ -110,7 +118,7 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk)
EXPECT_CALL(mockHttpClient, PerformHttpRequest(testing::_)).WillOnce(testing::Return(expectedResponse));

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
const bool res = registration::RegisterAgent(userCredentials, mockHttpClient);
const bool res = registration->SendRegistration(mockHttpClient);
ASSERT_FALSE(res);
}

Expand Down

0 comments on commit f294c8e

Please sign in to comment.