Skip to content

Commit

Permalink
HttpClient is injected into ApiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Nov 6, 2023
1 parent 6c07589 commit 9902621
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 17 deletions.
15 changes: 14 additions & 1 deletion apache/client/include/lauth/api_client.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#ifndef __LAUTH_API_CLIENT_HPP__
#define __LAUTH_API_CLIENT_HPP__

#include <memory>

#include "lauth/http_client.hpp"
#include "lauth/request.hpp"

namespace mlibrary::lauth {
class ApiClient {
public:
virtual bool isAllowed(Request req);
ApiClient() : client(std::make_unique<HttpClient>()) {};
ApiClient(std::unique_ptr<HttpClient>&& client) : client(std::move(client)) {};
ApiClient(const ApiClient&) = delete;
ApiClient& operator=(const ApiClient&) = delete;
ApiClient(ApiClient&&) = delete;
ApiClient& operator=(const ApiClient&&) = delete;
virtual ~ApiClient() = default;

virtual bool isAllowed(Request req);

protected:
std::unique_ptr<HttpClient> client;
};
}

Expand Down
15 changes: 15 additions & 0 deletions apache/client/include/lauth/http_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __LAUTH_HTTP_CLIENT_HPP__
#define __LAUTH_HTTP_CLIENT_HPP__

#include "lauth/request.hpp"

namespace mlibrary::lauth {
class HttpClient {
public:
virtual ~HttpClient() = default;

virtual bool isAllowed(Request req);
};
}

#endif // __LAUTH_HTTP_CLIENT_HPP__
3 changes: 3 additions & 0 deletions apache/client/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ lauth_includes = include_directories('include')
lauth_sources = files([
'src/lauth/api_client.cpp',
'src/lauth/authorizer.cpp',
'src/lauth/http_client.cpp',
])
lauth_tests = files([
'test/lauth/api_client_test.cpp',
'test/lauth/authorizer_test.cpp',
'test/lauth/http_client_test.cpp',
'test/lauth/request_test.cpp',
])

Expand All @@ -38,6 +40,7 @@ liblauth = shared_library(
install_headers(
'include/lauth/api_client.hpp',
'include/lauth/authorizer.hpp',
'include/lauth/http_client.hpp',
'include/lauth/request.hpp',
subdir: 'lauth')

Expand Down
5 changes: 1 addition & 4 deletions apache/client/src/lauth/api_client.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#include "lauth/api_client.hpp"

// #include <string>

namespace mlibrary::lauth {
bool ApiClient::isAllowed(Request req) {
return req.user.size() > 0;
// bool result = http_client(req.uri, req.user);
return client->isAllowed(req);
}
}

2 changes: 0 additions & 2 deletions apache/client/src/lauth/authorizer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "lauth/authorizer.hpp"

#include <string>

namespace mlibrary::lauth {
bool Authorizer::isPasswordOnly(std::string url) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions apache/client/src/lauth/http_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "lauth/http_client.hpp"

namespace mlibrary::lauth {
bool HttpClient::isAllowed(Request req) {
return (req.user == "authorized");
}
}

42 changes: 34 additions & 8 deletions apache/client/test/lauth/api_client_test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "mocks.hpp"

using testing::_;
using ::testing::Return;
using ::testing::_;

#include "lauth/api_client.hpp"
#include "lauth/request.hpp"

using namespace mlibrary::lauth;

TEST(ApiClientTest, allowed_by_mock) {
auto client = std::make_unique<MockHttpClient>();
EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(true));
ApiClient api_client(std::move(client));

Request req {};

auto allowed = api_client.isAllowed(req);

EXPECT_THAT(allowed, true);
}

TEST(ApiClientTest, denied_by_mock) {
auto client = std::make_unique<MockHttpClient>();
EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(false));
ApiClient api_client(std::move(client));

Request req {};

auto allowed = api_client.isAllowed(req);

EXPECT_THAT(allowed, false);
}

TEST(ApiClient, a_request_with_no_user_is_denied) {
ApiClient client;
Request request;
Expand All @@ -26,11 +52,11 @@ TEST(ApiClient, a_request_with_authorized_user_is_allowed) {
EXPECT_THAT(result, true);
}

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

// request.user = "unauthorized";
// bool result = client.isAllowed(request);
// EXPECT_THAT(result, false);
// }
request.user = "unauthorized";
bool result = client.isAllowed(request);
EXPECT_THAT(result, false);
}
18 changes: 18 additions & 0 deletions apache/client/test/lauth/http_client_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using testing::_;

#include "lauth/http_client.hpp"
#include "lauth/request.hpp"

using namespace mlibrary::lauth;

TEST(HttpClient, a_request_with_authorized_user_is_allowed) {
HttpClient client;
Request request;

request.user = "authorized";
bool result = client.isAllowed(request);
EXPECT_THAT(result, true);
}
8 changes: 7 additions & 1 deletion apache/client/test/lauth/mocks.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef _LAUTH_TEST_MOCKS_HPP_
#define _LAUTH_TEST_MOCKS_HPP_

#include <lauth/api_client.hpp>
#include "lauth/api_client.hpp"
#include "lauth/http_client.hpp"

using namespace mlibrary::lauth;

Expand All @@ -10,4 +11,9 @@ class MockApiClient : public ApiClient {
MOCK_METHOD(bool, isAllowed, (Request), (override));
};

class MockHttpClient : public HttpClient {
public:
MOCK_METHOD(bool, isAllowed, (Request), (override));
};

#endif
2 changes: 1 addition & 1 deletion test/restrictions/umich_login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
end

context "when logged in as an authorized user" do
xcontext "when logged in as an authorized user" do
it "is allowed" do
response = website.get("/user/") do |req|
req.headers["Authorization"] = basic_auth_good_user
Expand Down

0 comments on commit 9902621

Please sign in to comment.