129 lines
4.0 KiB
C++
129 lines
4.0 KiB
C++
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <rublon/init.hpp>
|
|
|
|
#include "core_response_generator.hpp"
|
|
|
|
using namespace rublon;
|
|
using namespace testing;
|
|
|
|
namespace {
|
|
Configuration conf;
|
|
}
|
|
|
|
class CoreHandlerMock : public CoreHandlerInterface< CoreHandlerMock > {
|
|
public:
|
|
CoreHandlerMock() {}
|
|
|
|
MOCK_METHOD(( tl::expected< Document, Error > ), request, ( std::string_view, const Document & ), (const));
|
|
|
|
CoreHandlerMock & statusPending() {
|
|
gen.status = "pending";
|
|
return *this;
|
|
}
|
|
|
|
CoreHandlerMock & brokenBody() {
|
|
gen.generateBrokenData = true;
|
|
return *this;
|
|
}
|
|
|
|
CoreHandlerMock & methods(std::initializer_list< std::string > methods) {
|
|
gen.methods(methods);
|
|
return *this;
|
|
}
|
|
|
|
CoreHandlerMock & tid(std::string tid) {
|
|
gen.tid(tid);
|
|
return *this;
|
|
}
|
|
|
|
operator tl::expected< Document, Error >() {
|
|
auto body = gen.generateBody();
|
|
|
|
rublon::Document doc;
|
|
doc.Parse(body.c_str());
|
|
|
|
return doc;
|
|
}
|
|
|
|
auto create() {
|
|
return static_cast< tl::expected< Document, Error > >(*this);
|
|
}
|
|
|
|
CoreResponseGenerator gen;
|
|
};
|
|
|
|
class PamInfoMock {
|
|
public:
|
|
MOCK_METHOD(rublon::NonOwningPtr< const char >, ip, (), (const));
|
|
MOCK_METHOD(rublon::NonOwningPtr< const char >, username, (), (const));
|
|
};
|
|
|
|
class MethodFactoryMock {
|
|
public:
|
|
using MethodFactoryCreate_t = tl::expected< Method, Error >;
|
|
|
|
template < typename... Args >
|
|
MethodFactoryMock(Args &&...) {}
|
|
|
|
MOCK_METHOD(MethodFactoryCreate_t, create, (const PamInfoMock &, std::string tid), (const));
|
|
};
|
|
|
|
class RublonHttpInitTest : public testing::Test {
|
|
public:
|
|
void expectDefaultPamInfo() {
|
|
EXPECT_CALL(pam, ip()).WillOnce(Return("192.168.0.1"));
|
|
EXPECT_CALL(pam, username()).WillOnce(Return("bwi"));
|
|
}
|
|
|
|
RublonHttpInitTest() : coreHandler{}, pam{}, sut{conf} {
|
|
expectDefaultPamInfo();
|
|
}
|
|
|
|
CoreHandlerMock coreHandler;
|
|
PamInfoMock pam;
|
|
Init< MethodFactoryMock > sut;
|
|
// MethodFactoryMock &methodFactoryMock;
|
|
};
|
|
|
|
using CoreReturn = tl::expected< Document, CoreHandlerError >;
|
|
|
|
TEST_F(RublonHttpInitTest, initializationSendsRequestOnGoodPath) {
|
|
EXPECT_CALL(coreHandler, request("/api/transaction/init", _))
|
|
.WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::BadSigature}}));
|
|
sut.handle(coreHandler, pam);
|
|
}
|
|
/// TODO fix
|
|
|
|
//MATCHER_P(HoldsPamAction, action, "") {
|
|
// return not arg.has_value() && arg.error() == action;
|
|
//}
|
|
|
|
//TEST_F(RublonHttpInitTest, rublon_Accept_pamLoginWhenThereIsNoConnection) {
|
|
// EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::ConnectionError}}));
|
|
// EXPECT_THAT(sut.handle(coreHandler, pam), HoldsPamAction(PamAction::decline));
|
|
//}
|
|
|
|
//TEST_F(RublonHttpInitTest, rublon_Decline_pamLoginWhenServerHasBadSignature) {
|
|
// EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::BadSigature}}));
|
|
// EXPECT_THAT(sut.handle(coreHandler, pam), HoldsPamAction(PamAction::decline));
|
|
//}
|
|
|
|
//TEST_F(RublonHttpInitTest, rublon_Decline_pamLoginWhenServerReturnsBrokenData) {
|
|
// EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::BrokenData}}));
|
|
// EXPECT_THAT(sut.handle(coreHandler, pam), HoldsPamAction(PamAction::decline));
|
|
//}
|
|
|
|
//TEST_F(RublonHttpInitTest, rublon_Decline_pamLoginWhenServerReturnsCoreException) {
|
|
// EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::CoreException}}));
|
|
// EXPECT_THAT(sut.handle(coreHandler, pam), HoldsPamAction(PamAction::decline));
|
|
//}
|
|
|
|
//TEST_F(RublonHttpInitTest, AllNeededInformationNeedsToBePassedToMethodFactory) {
|
|
// EXPECT_CALL(coreHandler, request(_, _))
|
|
// .WillOnce(Return(coreHandler.statusPending().methods({"sms", "otp"}).tid("transaction ID").create()));
|
|
// // EXPECT_CALL(methodFactoryMock, create_mocked(_,"transaction ID") );
|
|
// sut.handle(coreHandler, pam);
|
|
//}
|