-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
5 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |