Skip to content

Commit

Permalink
half way there
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Nov 9, 2023
1 parent 3972d00 commit 57fdc39
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions apache/client/include/lauth/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "lauth/request.hpp"

#include <string>

namespace mlibrary::lauth {
class HttpClient {
public:
Expand Down
7 changes: 6 additions & 1 deletion apache/client/src/lauth/api_client.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "lauth/api_client.hpp"

#include <sstream>
#include <string>

namespace mlibrary::lauth {
bool ApiClient::isAllowed(Request req) {
std::stringstream url;
url << "/users/" << req.user << "/is_allowed";
std::string result = client->get(url.str());
return client->isAllowed(req);
}
}

40 changes: 35 additions & 5 deletions apache/client/test/lauth/api_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,37 @@ using ::testing::Return;

using namespace mlibrary::lauth;

TEST(ApiClientTest, allowed_by_mock) {
TEST(ApiClient, allowed_by_mock_http_client) {
auto client = std::make_unique<MockHttpClient>();
EXPECT_CALL(*client, get("/users/authorized/is_allowed")).WillOnce(Return("yes"));
EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(true));
ApiClient api_client(std::move(client));

Request req {
.user = "authorized",
};

auto allowed = api_client.isAllowed(req);

EXPECT_THAT(allowed, true);
}

TEST(ApiClient, denied_by_mock_http_client) {
auto client = std::make_unique<MockHttpClient>();
EXPECT_CALL(*client, get("/users/unauthorized/is_allowed")).WillOnce(Return("no"));
EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(false));
ApiClient api_client(std::move(client));

Request req {
.user = "unauthorized",
};

auto allowed = api_client.isAllowed(req);

EXPECT_THAT(allowed, false);
}

TEST(ApiClient, allowed_by_mock) {
auto client = std::make_unique<MockHttpClient>();
EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(true));
ApiClient api_client(std::move(client));
Expand All @@ -22,7 +52,7 @@ TEST(ApiClientTest, allowed_by_mock) {
EXPECT_THAT(allowed, true);
}

TEST(ApiClientTest, denied_by_mock) {
TEST(ApiClient, denied_by_mock) {
auto client = std::make_unique<MockHttpClient>();
EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(false));
ApiClient api_client(std::move(client));
Expand All @@ -34,7 +64,7 @@ TEST(ApiClientTest, denied_by_mock) {
EXPECT_THAT(allowed, false);
}

TEST(ApiClientTest, a_request_with_no_user_is_denied) {
TEST(ApiClient, a_request_with_no_user_is_denied) {
ApiClient client;
Request request;

Expand All @@ -43,7 +73,7 @@ TEST(ApiClientTest, a_request_with_no_user_is_denied) {
}


TEST(ApiClientTest, a_request_with_authorized_user_is_allowed) {
TEST(ApiClient, a_request_with_authorized_user_is_allowed) {
ApiClient client;
Request request;

Expand All @@ -52,7 +82,7 @@ TEST(ApiClientTest, a_request_with_authorized_user_is_allowed) {
EXPECT_THAT(result, true);
}

TEST(ApiClientTest, a_request_with_unauthorized_user_is_denied) {
TEST(ApiClient, a_request_with_unauthorized_user_is_denied) {
ApiClient client;
Request request;

Expand Down
8 changes: 8 additions & 0 deletions apache/client/test/mock_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ int main(int argc, char **argv) {
res.set_content("pong", "text/plain");
});

server.Get("/users/authorized/is_allowed", [](const Request &req, Response &res) {
res.set_content("yes", "text/plain");
});

server.Get("/users/unauthorized/is_allowed", [](const Request &req, Response &res) {
res.set_content("no", "text/plain");
});

server.Get("/stop", [&](const Request &req, Response &res) {
res.set_content("Shutting down server...", "text/plain");
server.stop();
Expand Down

0 comments on commit 57fdc39

Please sign in to comment.