From 6d2a952d94a5fbf9d1c155a8bc497cbb5c477c3e Mon Sep 17 00:00:00 2001 From: Greg Kostin Date: Fri, 3 Nov 2023 15:26:49 -0400 Subject: [PATCH] HttpClient is injected into ApiClient --- apache/client/include/lauth/api_client.hpp | 15 ++++++- apache/client/include/lauth/http_client.hpp | 15 +++++++ apache/client/meson.build | 3 ++ apache/client/src/lauth/api_client.cpp | 5 +-- apache/client/src/lauth/authorizer.cpp | 2 - apache/client/src/lauth/http_client.cpp | 8 ++++ apache/client/test/lauth/api_client_test.cpp | 42 +++++++++++++++---- apache/client/test/lauth/authorizer_test.cpp | 5 --- apache/client/test/lauth/http_client_test.cpp | 18 ++++++++ apache/client/test/lauth/mocks.hpp | 8 +++- 10 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 apache/client/include/lauth/http_client.hpp create mode 100644 apache/client/src/lauth/http_client.cpp create mode 100644 apache/client/test/lauth/http_client_test.cpp diff --git a/apache/client/include/lauth/api_client.hpp b/apache/client/include/lauth/api_client.hpp index 8892bc44..ef5a5f8f 100644 --- a/apache/client/include/lauth/api_client.hpp +++ b/apache/client/include/lauth/api_client.hpp @@ -1,13 +1,26 @@ #ifndef __LAUTH_API_CLIENT_HPP__ #define __LAUTH_API_CLIENT_HPP__ +#include + +#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()) {}; + ApiClient(std::unique_ptr&& 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 client; }; } diff --git a/apache/client/include/lauth/http_client.hpp b/apache/client/include/lauth/http_client.hpp new file mode 100644 index 00000000..64149791 --- /dev/null +++ b/apache/client/include/lauth/http_client.hpp @@ -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__ diff --git a/apache/client/meson.build b/apache/client/meson.build index 3f594be3..e2d98739 100644 --- a/apache/client/meson.build +++ b/apache/client/meson.build @@ -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', ]) @@ -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') diff --git a/apache/client/src/lauth/api_client.cpp b/apache/client/src/lauth/api_client.cpp index 52f705cd..d6e1921f 100644 --- a/apache/client/src/lauth/api_client.cpp +++ b/apache/client/src/lauth/api_client.cpp @@ -1,11 +1,8 @@ #include "lauth/api_client.hpp" -// #include - 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); } } diff --git a/apache/client/src/lauth/authorizer.cpp b/apache/client/src/lauth/authorizer.cpp index 9c68b62f..1a8e32b9 100644 --- a/apache/client/src/lauth/authorizer.cpp +++ b/apache/client/src/lauth/authorizer.cpp @@ -1,7 +1,5 @@ #include "lauth/authorizer.hpp" -#include - namespace mlibrary::lauth { bool Authorizer::isPasswordOnly(std::string url) { return false; diff --git a/apache/client/src/lauth/http_client.cpp b/apache/client/src/lauth/http_client.cpp new file mode 100644 index 00000000..66552431 --- /dev/null +++ b/apache/client/src/lauth/http_client.cpp @@ -0,0 +1,8 @@ +#include "lauth/http_client.hpp" + +namespace mlibrary::lauth { + bool HttpClient::isAllowed(Request req) { + return (req.user == "authorized"); + } +} + diff --git a/apache/client/test/lauth/api_client_test.cpp b/apache/client/test/lauth/api_client_test.cpp index e9bbad0d..b7c64f9a 100644 --- a/apache/client/test/lauth/api_client_test.cpp +++ b/apache/client/test/lauth/api_client_test.cpp @@ -1,13 +1,39 @@ #include #include +#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(); + 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(); + 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; @@ -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); +} diff --git a/apache/client/test/lauth/authorizer_test.cpp b/apache/client/test/lauth/authorizer_test.cpp index 1e4c7736..b8e4d4ee 100644 --- a/apache/client/test/lauth/authorizer_test.cpp +++ b/apache/client/test/lauth/authorizer_test.cpp @@ -16,11 +16,6 @@ using ::testing::_; using mlibrary::lauth::ApiClient; using mlibrary::lauth::Authorizer; -TEST(AuthorizerTest, inject_api_client) { - auto client = std::make_unique(); - Authorizer authorizer(std::move(client)); -} - TEST(AuthorizerTest, AllowsAccessWhenApiSaysAuthorized) { auto client = std::make_unique(); EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(true)); diff --git a/apache/client/test/lauth/http_client_test.cpp b/apache/client/test/lauth/http_client_test.cpp new file mode 100644 index 00000000..0b8c2dc9 --- /dev/null +++ b/apache/client/test/lauth/http_client_test.cpp @@ -0,0 +1,18 @@ +#include +#include + +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); +} diff --git a/apache/client/test/lauth/mocks.hpp b/apache/client/test/lauth/mocks.hpp index b9963373..da6083f4 100644 --- a/apache/client/test/lauth/mocks.hpp +++ b/apache/client/test/lauth/mocks.hpp @@ -1,7 +1,8 @@ #ifndef _LAUTH_TEST_MOCKS_HPP_ #define _LAUTH_TEST_MOCKS_HPP_ -#include +#include "lauth/api_client.hpp" +#include "lauth/http_client.hpp" using namespace mlibrary::lauth; @@ -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