Skip to content

Commit

Permalink
Delegate isAllowed to the ApiClient
Browse files Browse the repository at this point in the history
- Add allowed/denied unit tests at the Authorizer
- Ensure that the authorizer can have an injected ApiClient
- Ensure that ApiClient::isAllowed is virtual to enable mocking
- Add ApiClient mock class and mock isAllowed method
- Replace literal implementation of Authorizer::isAllowed with
  delegation to the injected ApiClient
- Prepare Authorizer somewhat for injection/mocking with virtual
  declarations and adding destructor
  • Loading branch information
botimer committed Nov 3, 2023
1 parent ff368a5 commit 9f46c05
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
3 changes: 2 additions & 1 deletion apache/client/include/lauth/api_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace mlibrary::lauth {
class ApiClient {
public:
bool isAllowed(Request req);
virtual bool isAllowed(Request req);
virtual ~ApiClient() = default;
};
}

Expand Down
5 changes: 4 additions & 1 deletion apache/client/include/lauth/authorizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ namespace mlibrary::lauth {
Authorizer() : client(std::make_unique<ApiClient>()) {};
Authorizer(std::unique_ptr<ApiClient>&& 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<ApiClient> client;
Expand Down
1 change: 0 additions & 1 deletion apache/client/src/lauth/authorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace mlibrary::lauth {
}

bool Authorizer::isAllowed(Request req) {
// return (req.user == "lauth-allowed");
return client->isAllowed(req);
}
}
38 changes: 36 additions & 2 deletions apache/client/test/lauth/authorizer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
#include <memory>
#include "lauth/authorizer.hpp"

#include <memory>

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

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#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<ApiClient>();
Authorizer authorizer(std::move(client));
}

TEST(AuthorizerTest, AllowsAccessWhenApiSaysAuthorized) {
auto client = std::make_unique<MockApiClient>();
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<MockApiClient>();
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);
}
13 changes: 13 additions & 0 deletions apache/client/test/lauth/mocks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _LAUTH_TEST_MOCKS_HPP_
#define _LAUTH_TEST_MOCKS_HPP_

#include <lauth/api_client.hpp>

using namespace mlibrary::lauth;

class MockApiClient : public ApiClient {
public:
MOCK_METHOD(bool, isAllowed, (Request), (override));
};

#endif

0 comments on commit 9f46c05

Please sign in to comment.