diff --git a/apache/client/include/lauth/api_client.hpp b/apache/client/include/lauth/api_client.hpp index d217c2eb..8892bc44 100644 --- a/apache/client/include/lauth/api_client.hpp +++ b/apache/client/include/lauth/api_client.hpp @@ -6,7 +6,8 @@ namespace mlibrary::lauth { class ApiClient { public: - bool isAllowed(Request req); + virtual bool isAllowed(Request req); + virtual ~ApiClient() = default; }; } diff --git a/apache/client/include/lauth/authorizer.hpp b/apache/client/include/lauth/authorizer.hpp index 68072705..7b610397 100644 --- a/apache/client/include/lauth/authorizer.hpp +++ b/apache/client/include/lauth/authorizer.hpp @@ -13,10 +13,13 @@ namespace mlibrary::lauth { Authorizer() : client(std::make_unique()) {}; Authorizer(std::unique_ptr&& client) : client(std::move(client)) {}; Authorizer(const Authorizer&) = delete; + Authorizer& operator=(const Authorizer&) = delete; Authorizer(Authorizer&&) = delete; + Authorizer& operator=(const Authorizer&&) = delete; + virtual ~Authorizer() = default; bool isPasswordOnly(std::string url); - bool isAllowed(Request req); + virtual bool isAllowed(Request req); protected: std::unique_ptr client; diff --git a/apache/client/src/lauth/authorizer.cpp b/apache/client/src/lauth/authorizer.cpp index 01538fb8..9c68b62f 100644 --- a/apache/client/src/lauth/authorizer.cpp +++ b/apache/client/src/lauth/authorizer.cpp @@ -8,7 +8,6 @@ namespace mlibrary::lauth { } bool Authorizer::isAllowed(Request req) { - // return (req.user == "lauth-allowed"); return client->isAllowed(req); } } diff --git a/apache/client/test/lauth/authorizer_test.cpp b/apache/client/test/lauth/authorizer_test.cpp index 45c49e1a..1e4c7736 100644 --- a/apache/client/test/lauth/authorizer_test.cpp +++ b/apache/client/test/lauth/authorizer_test.cpp @@ -1,14 +1,48 @@ -#include #include "lauth/authorizer.hpp" + +#include + #include "lauth/request.hpp" #include "lauth/api_client.hpp" #include +#include + +#include "mocks.hpp" + +using ::testing::Return; +using ::testing::_; using mlibrary::lauth::ApiClient; using mlibrary::lauth::Authorizer; -TEST(Authorizer, inject_api_client) { +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)); + Authorizer authorizer(std::move(client)); + + Request req { + .user = "lauth-allowed" + }; + auto allowed = authorizer.isAllowed(req); + + EXPECT_THAT(allowed, true); +} + +TEST(AuthorizerTest, DeniesAccessWhenApiSaysUnauthorized) { + auto client = std::make_unique(); + EXPECT_CALL(*client, isAllowed(_)).WillOnce(Return(false)); + Authorizer authorizer(std::move(client)); + + Request req { + .user = "lauth-denied" + }; + auto allowed = authorizer.isAllowed(req); + + EXPECT_THAT(allowed, false); +} diff --git a/apache/client/test/lauth/mocks.hpp b/apache/client/test/lauth/mocks.hpp new file mode 100644 index 00000000..b9963373 --- /dev/null +++ b/apache/client/test/lauth/mocks.hpp @@ -0,0 +1,13 @@ +#ifndef _LAUTH_TEST_MOCKS_HPP_ +#define _LAUTH_TEST_MOCKS_HPP_ + +#include + +using namespace mlibrary::lauth; + +class MockApiClient : public ApiClient { + public: + MOCK_METHOD(bool, isAllowed, (Request), (override)); +}; + +#endif