mirror of
https://github.com/falk-werner/webfused
synced 2026-03-02 04:09:19 +00:00
refactor: generalize auth_settings
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
#include "mock_auth_settings.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using webfused_test::IAuthSettings;
|
||||
|
||||
static IAuthSettings * wfd_mock_auth_settings = nullptr;
|
||||
|
||||
extern char const *
|
||||
__real_wfd_auth_settings_get_provider(
|
||||
struct wfd_auth_settings * settings);
|
||||
|
||||
extern char const *
|
||||
__real_wfd_auth_settings_get(
|
||||
struct wfd_auth_settings * settings,
|
||||
char const * key);
|
||||
|
||||
char const *
|
||||
__wrap_wfd_auth_settings_get_provider(
|
||||
struct wfd_auth_settings * settings)
|
||||
{
|
||||
if (nullptr == wfd_mock_auth_settings)
|
||||
{
|
||||
return __real_wfd_auth_settings_get_provider(settings);
|
||||
}
|
||||
else
|
||||
{
|
||||
return wfd_mock_auth_settings->getProvider();
|
||||
}
|
||||
}
|
||||
|
||||
char const *
|
||||
__wrap_wfd_auth_settings_get(
|
||||
struct wfd_auth_settings * settings,
|
||||
char const * key)
|
||||
{
|
||||
if (nullptr == wfd_mock_auth_settings)
|
||||
{
|
||||
return __real_wfd_auth_settings_get(settings, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return wfd_mock_auth_settings->get(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
namespace webfused_test
|
||||
{
|
||||
|
||||
MockAuthSettings::MockAuthSettings()
|
||||
{
|
||||
wfd_mock_auth_settings = this;
|
||||
}
|
||||
|
||||
MockAuthSettings::~MockAuthSettings()
|
||||
{
|
||||
wfd_mock_auth_settings = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#ifndef WFD_MOCK_AUTH_SETTINGS_HPP
|
||||
#define WFD_MOCK_AUTH_SETTINGS_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "webfused/auth/settings.h"
|
||||
|
||||
namespace webfused_test
|
||||
{
|
||||
|
||||
class IAuthSettings
|
||||
{
|
||||
public:
|
||||
virtual ~IAuthSettings() = default;
|
||||
virtual char const * getProvider() = 0;
|
||||
virtual char const * get(char const * key) = 0;
|
||||
};
|
||||
|
||||
class MockAuthSettings: public IAuthSettings
|
||||
{
|
||||
public:
|
||||
MockAuthSettings();
|
||||
~MockAuthSettings() override;
|
||||
MOCK_METHOD0(getProvider, char const * ());
|
||||
MOCK_METHOD1(get, char const * (char const * key));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -52,10 +52,11 @@ wfd_MockConfigBuilder_set_server_document_root(
|
||||
static bool
|
||||
wfd_MockConfigBuilder_add_auth_provider(
|
||||
void * data,
|
||||
struct wfd_auth_settings * settings)
|
||||
char const * provider,
|
||||
struct wfd_settings * settings)
|
||||
{
|
||||
auto * builder = reinterpret_cast<IConfigBuilder*>(data);
|
||||
return builder->addAuthProvider(settings);
|
||||
return builder->addAuthProvider(provider, settings);
|
||||
}
|
||||
|
||||
static bool
|
||||
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
virtual void setServerKey(char const * key_path) = 0;
|
||||
virtual void setServerCert(char const * cert_path) = 0;
|
||||
virtual void setServerDocumentRoot(char const * document_root) = 0;
|
||||
virtual bool addAuthProvider(wfd_auth_settings * settings) = 0;
|
||||
virtual bool addAuthProvider(char const * provider, wfd_settings * settings) = 0;
|
||||
virtual bool addFilesystem(char const * name, char const * mountpoint) = 0;
|
||||
};
|
||||
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
MOCK_METHOD1(setServerKey, void (char const * key_path));
|
||||
MOCK_METHOD1(setServerCert, void (char const * cert_path));
|
||||
MOCK_METHOD1(setServerDocumentRoot, void (char const * document_root));
|
||||
MOCK_METHOD1(addAuthProvider, bool (wfd_auth_settings * settings));
|
||||
MOCK_METHOD2(addAuthProvider, bool (char const * provider, wfd_settings * settings));
|
||||
MOCK_METHOD2(addFilesystem, bool (char const * name, char const * mountpoint));
|
||||
|
||||
struct wfd_config_builder getBuilder();
|
||||
|
||||
45
test/mock_settings.cc
Normal file
45
test/mock_settings.cc
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "mock_settings.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using webfused_test::ISettings;
|
||||
|
||||
static ISettings * wfd_mock_settings = nullptr;
|
||||
|
||||
extern char const *
|
||||
__real_wfd_settings_get(
|
||||
struct wfd_settings * settings,
|
||||
char const * key);
|
||||
|
||||
char const *
|
||||
__wrap_wfd_settings_get(
|
||||
struct wfd_settings * settings,
|
||||
char const * key)
|
||||
{
|
||||
if (nullptr == wfd_mock_settings)
|
||||
{
|
||||
return __real_wfd_settings_get(settings, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return wfd_mock_settings->get(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
namespace webfused_test
|
||||
{
|
||||
|
||||
MockSettings::MockSettings()
|
||||
{
|
||||
wfd_mock_settings = this;
|
||||
}
|
||||
|
||||
MockSettings::~MockSettings()
|
||||
{
|
||||
wfd_mock_settings = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
29
test/mock_settings.hpp
Normal file
29
test/mock_settings.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WFD_MOCK_AUTH_SETTINGS_HPP
|
||||
#define WFD_MOCK_AUTH_SETTINGS_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "webfused/config/settings.h"
|
||||
|
||||
namespace webfused_test
|
||||
{
|
||||
|
||||
class ISettings
|
||||
{
|
||||
public:
|
||||
virtual ~ISettings() = default;
|
||||
virtual char const * get(char const * key) = 0;
|
||||
};
|
||||
|
||||
class MockSettings: public ISettings
|
||||
{
|
||||
public:
|
||||
MockSettings();
|
||||
~MockSettings() override;
|
||||
MOCK_METHOD1(get, char const * (char const * key));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/auth/factory.h"
|
||||
#include "webfused/auth/authenticator.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include "webfused/log/log.h"
|
||||
#include "mock_logger.hpp"
|
||||
|
||||
@@ -15,12 +14,7 @@ TEST(auth_factory, fail_unknown_provider)
|
||||
EXPECT_CALL(logger, log(WFD_LOGLEVEL_ERROR, _, _)).Times(1);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
wfd_auth_settings * auth_settings = wfd_auth_settings_create(
|
||||
"unknown", NULL);
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool result = wfd_authenticator_create(auth_settings, &authenticator);
|
||||
bool result = wfd_authenticator_create("unknown", NULL, &authenticator);
|
||||
ASSERT_FALSE(result);
|
||||
|
||||
wfd_auth_settings_dispose(auth_settings);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/auth/settings.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include <libconfig.h>
|
||||
|
||||
TEST(auth_settings, auth_settings)
|
||||
{
|
||||
char const settings_text[] =
|
||||
"settings:\n"
|
||||
"{\n"
|
||||
" string_value = \"some.string\"\n"
|
||||
" int_value = 42\n"
|
||||
"}\n"
|
||||
;
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_string(&config, settings_text);
|
||||
ASSERT_TRUE(CONFIG_TRUE == rc);
|
||||
|
||||
config_setting_t * settings = config_lookup(&config, "settings");
|
||||
ASSERT_NE(nullptr, settings);
|
||||
|
||||
wfd_auth_settings * auth_settings = wfd_auth_settings_create("test_provider", settings);
|
||||
ASSERT_STREQ("test_provider", wfd_auth_settings_get_provider(auth_settings));
|
||||
ASSERT_STREQ("some.string", wfd_auth_settings_get(auth_settings, "string_value"));
|
||||
ASSERT_EQ(nullptr, wfd_auth_settings_get(auth_settings, "int_value"));
|
||||
ASSERT_EQ(nullptr, wfd_auth_settings_get(auth_settings, "invalid_value"));
|
||||
|
||||
wfd_auth_settings_dispose(auth_settings);
|
||||
config_destroy(&config);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/config/config.h"
|
||||
#include "mock_auth_settings.hpp"
|
||||
#include "mock_settings.hpp"
|
||||
|
||||
#include "webfused/log/logger.h"
|
||||
#include "webfused/log/log.h"
|
||||
@@ -8,7 +8,7 @@
|
||||
using ::webfused_test::MockLogger;
|
||||
using ::testing::_;
|
||||
|
||||
using ::webfused_test::MockAuthSettings;
|
||||
using ::webfused_test::MockSettings;
|
||||
using ::testing::Return;
|
||||
using ::testing::StrEq;
|
||||
|
||||
@@ -37,11 +37,10 @@ TEST(config, auth_config)
|
||||
|
||||
wfd_config_builder builder = wfd_config_get_builder(config);
|
||||
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
|
||||
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, "file", nullptr);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
@@ -54,14 +53,13 @@ TEST(config, auth_config_failed_to_add_second_provider)
|
||||
|
||||
wfd_config_builder builder = wfd_config_get_builder(config);
|
||||
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
|
||||
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, "file", nullptr);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
success = wfd_config_builder_add_auth_provider(builder, "file", nullptr);
|
||||
ASSERT_FALSE(success);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
@@ -74,10 +72,7 @@ TEST(config, auth_config_failed_to_add_unknown_provider)
|
||||
|
||||
wfd_config_builder builder = wfd_config_get_builder(config);
|
||||
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("unknown"));
|
||||
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, nullptr);
|
||||
bool success = wfd_config_builder_add_auth_provider(builder, "unknown", nullptr);
|
||||
ASSERT_FALSE(success);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
|
||||
@@ -19,7 +19,7 @@ TEST(config, is_loadable)
|
||||
StrictMock<MockConfigBuilder> builder;
|
||||
EXPECT_CALL(builder, setServerVhostname(StrEq("localhost"))).Times(1);
|
||||
EXPECT_CALL(builder, setServerPort(8080)).Times(1);
|
||||
EXPECT_CALL(builder, addAuthProvider(_)).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(builder, addFilesystem(_,_)).Times(1).WillOnce(Return(true));
|
||||
|
||||
bool result = wfd_config_load_file(builder.getBuilder(), "webfused.conf");
|
||||
@@ -262,7 +262,7 @@ TEST(config, authentication)
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
StrictMock<MockConfigBuilder> builder;
|
||||
EXPECT_CALL(builder, addAuthProvider(_)).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(true));
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
@@ -283,7 +283,7 @@ TEST(config, failed_create_authenticator)
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
StrictMock<MockConfigBuilder> builder;
|
||||
EXPECT_CALL(builder, addAuthProvider(_)).Times(1).WillOnce(Return(false));
|
||||
EXPECT_CALL(builder, addAuthProvider(_, _)).Times(1).WillOnce(Return(false));
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
#include "webfused/auth/file_authenticator.h"
|
||||
#include "webfused/auth/authenticator.h"
|
||||
#include "webfused/config/auth_settings.h"
|
||||
#include "webfused/config/settings.h"
|
||||
#include "webfused/auth/factory.h"
|
||||
|
||||
#include "mock_credentials.hpp"
|
||||
#include "mock_auth_settings.hpp"
|
||||
#include "mock_settings.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <libconfig.h>
|
||||
|
||||
using ::webfused_test::MockAuthSettings;
|
||||
using ::webfused_test::MockSettings;
|
||||
using ::webfused_test::MockCredentials;
|
||||
using ::testing::Return;
|
||||
using ::testing::StrEq;
|
||||
|
||||
TEST(file_authenticator, create)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
@@ -28,7 +28,7 @@ TEST(file_authenticator, create)
|
||||
|
||||
TEST(file_authenticator, create_fail_missing_file)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return(nullptr));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
@@ -38,12 +38,11 @@ TEST(file_authenticator, create_fail_missing_file)
|
||||
|
||||
TEST(file_authenticator, create_via_factory)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_authenticator_create("file", nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
wfd_authenticator_dispose(authenticator);
|
||||
@@ -51,12 +50,11 @@ TEST(file_authenticator, create_via_factory)
|
||||
|
||||
TEST(file_authenticator, authenticate)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@@ -71,12 +69,11 @@ TEST(file_authenticator, authenticate)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_wrong_passwd)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@@ -91,12 +88,11 @@ TEST(file_authenticator, authenticate_fail_wrong_passwd)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_no_passwd_file)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("unknown_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@@ -111,12 +107,11 @@ TEST(file_authenticator, authenticate_fail_no_passwd_file)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_missing_username)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@@ -131,12 +126,11 @@ TEST(file_authenticator, authenticate_fail_missing_username)
|
||||
|
||||
TEST(file_authenticator, authenticate_fail_missing_password)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
EXPECT_CALL(settings, getProvider()).Times(1).WillOnce(Return("file"));
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
bool success = wfd_authenticator_create(nullptr, &authenticator);
|
||||
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
|
||||
ASSERT_TRUE(success);
|
||||
|
||||
MockCredentials creds;
|
||||
@@ -151,7 +145,7 @@ TEST(file_authenticator, authenticate_fail_missing_password)
|
||||
|
||||
TEST(file_authenticator, get_type)
|
||||
{
|
||||
MockAuthSettings settings;
|
||||
MockSettings settings;
|
||||
EXPECT_CALL(settings, get(StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
|
||||
|
||||
wfd_authenticator authenticator;
|
||||
|
||||
31
test/test_settings.cc
Normal file
31
test/test_settings.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfused/config/settings_intern.h"
|
||||
#include "webfused/config/settings.h"
|
||||
#include <libconfig.h>
|
||||
|
||||
TEST(settings, settings)
|
||||
{
|
||||
char const settings_text[] =
|
||||
"settings:\n"
|
||||
"{\n"
|
||||
" string_value = \"some.string\"\n"
|
||||
" int_value = 42\n"
|
||||
"}\n"
|
||||
;
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_string(&config, settings_text);
|
||||
ASSERT_TRUE(CONFIG_TRUE == rc);
|
||||
|
||||
config_setting_t * setting = config_lookup(&config, "settings");
|
||||
ASSERT_NE(nullptr, setting);
|
||||
|
||||
wfd_settings settings;
|
||||
wfd_settings_init(&settings, setting);
|
||||
ASSERT_STREQ("some.string", wfd_settings_get(&settings, "string_value"));
|
||||
ASSERT_EQ(nullptr, wfd_settings_get(&settings, "int_value"));
|
||||
ASSERT_EQ(nullptr, wfd_settings_get(&settings, "invalid_value"));
|
||||
|
||||
wfd_settings_cleanup(&settings);
|
||||
config_destroy(&config);
|
||||
}
|
||||
Reference in New Issue
Block a user