1
0
mirror of https://github.com/falk-werner/webfused synced 2024-10-27 20:44:08 +00:00
falk-werner_webfused/test/auth/file_authenticator.cc

160 lines
5.5 KiB
C++
Raw Normal View History

#include "webfused/auth/file_authenticator.h"
#include "webfused/auth/authenticator.h"
2020-03-18 09:17:17 +00:00
#include "webfused/config/settings.h"
#include "webfused/auth/factory.h"
2020-03-20 12:48:58 +00:00
#include "mock/credentials.hpp"
#include "mock/settings.hpp"
#include <gtest/gtest.h>
#include <libconfig.h>
2020-03-18 09:17:17 +00:00
using ::webfused_test::MockSettings;
using ::webfused_test::MockCredentials;
using ::testing::Return;
using ::testing::StrEq;
2020-03-20 13:16:32 +00:00
using ::testing::_;
TEST(file_authenticator, create)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
2020-03-17 15:49:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
wfd_authenticator_dispose(authenticator);
}
TEST(file_authenticator, create_fail_missing_file)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return(nullptr));
wfd_authenticator authenticator;
2020-03-17 15:49:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_FALSE(success);
}
TEST(file_authenticator, create_via_factory)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
2020-03-18 09:17:17 +00:00
bool success = wfd_authenticator_create("file", nullptr, &authenticator);
ASSERT_TRUE(success);
wfd_authenticator_dispose(authenticator);
}
TEST(file_authenticator, authenticate)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
2020-03-18 09:17:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
2020-03-20 13:26:41 +00:00
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secret"));
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_TRUE(is_authenticated);
wfd_authenticator_dispose(authenticator);
}
TEST(file_authenticator, authenticate_fail_wrong_passwd)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
2020-03-18 09:17:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
2020-03-20 13:26:41 +00:00
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("unkown"));
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_authenticator_dispose(authenticator);
}
TEST(file_authenticator, authenticate_fail_no_passwd_file)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("unknown_passwd.json"));
wfd_authenticator authenticator;
2020-03-18 09:17:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
2020-03-20 13:26:41 +00:00
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("secred"));
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_authenticator_dispose(authenticator);
}
TEST(file_authenticator, authenticate_fail_missing_username)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
2020-03-18 09:17:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
2020-03-20 13:26:41 +00:00
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return(nullptr));
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return("unkown"));
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_authenticator_dispose(authenticator);
}
TEST(file_authenticator, authenticate_fail_missing_password)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("test_passwd.json"));
wfd_authenticator authenticator;
2020-03-18 09:17:17 +00:00
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
MockCredentials creds;
2020-03-20 13:26:41 +00:00
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("username"))).Times(1).WillOnce(Return("bob"));
EXPECT_CALL(creds, wf_credentials_get(_,StrEq("password"))).Times(1).WillOnce(Return(nullptr));
bool is_authenticated = wfd_authenticator_authenticate(authenticator, nullptr);
ASSERT_FALSE(is_authenticated);
wfd_authenticator_dispose(authenticator);
2020-03-17 15:49:17 +00:00
}
TEST(file_authenticator, get_type)
{
2020-03-18 09:17:17 +00:00
MockSettings settings;
2020-03-20 13:16:32 +00:00
EXPECT_CALL(settings, wfd_settings_get_string(_,StrEq("file"))).Times(1).WillOnce(Return("/any/path"));
2020-03-17 15:49:17 +00:00
wfd_authenticator authenticator;
bool success = wfd_file_authenticator_create(nullptr, &authenticator);
ASSERT_TRUE(success);
ASSERT_STREQ("username", wfd_authenticator_get_type(authenticator));
wfd_authenticator_dispose(authenticator);
}