#include #include #include "../src/rublon.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, CoreHandlerError > ), request, ( std::string_view, const Document & ), (const)); CoreHandlerMock & statusPending() { gen.status = "pending"; return *this; } CoreHandlerMock & brokenBody() { gen.generateBrokenData = true; return *this; } operator tl::expected< Document, CoreHandlerError >() { auto body = gen.generateBody(); rublon::Document doc; doc.Parse(body.c_str()); return doc; } CoreResponseGenerator gen; }; class PamInfoMock { public: PamInfoMock(pam_handle_t *) {} MOCK_METHOD(rublon::NonOwningPtr< const char >, ip, (), (const)); MOCK_METHOD(rublon::NonOwningPtr< const char >, username, (), (const)); }; template < typename Pam > class MethodFactoryMock { public: MethodFactoryMock(const Pam &) {} template < typename Array_t > tl::expected< Method, PamAction > create(const Array_t & methods) const {} }; class InitTestable : public Init< MethodFactoryMock, PamInfoMock > { public: InitTestable(const rublon::Configuration & conf) : Init{nullptr, conf} {} PamInfoMock & pam() { return _pamInfo; } }; 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{}, sut{conf}, pam{sut.pam()} {} CoreHandlerMock coreHandler; InitTestable sut{conf}; PamInfoMock & pam; }; using CoreReturn = tl::expected< Document, CoreHandlerError >; TEST_F(RublonHttpInitTest, initializationSendsRequestOnGoodPath) { expectDefaultPamInfo(); EXPECT_CALL(coreHandler, request("/api/transaction/init", _)) .WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::BadSigature}})); sut.handle(coreHandler); } MATCHER_P(HoldsPamAction, action, "") { return not arg.has_value() && arg.error() == action; } TEST_F(RublonHttpInitTest, rublon_Accept_pamLoginWhenThereIsNoConnection) { expectDefaultPamInfo(); EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::ConnectionError}})); EXPECT_THAT(sut.handle(coreHandler), HoldsPamAction(PamAction::decline)); } TEST_F(RublonHttpInitTest, rublon_Decline_pamLoginWhenServerHasBadSignature) { expectDefaultPamInfo(); EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::BadSigature}})); EXPECT_THAT(sut.handle(coreHandler), HoldsPamAction(PamAction::decline)); } TEST_F(RublonHttpInitTest, rublon_Decline_pamLoginWhenServerReturnsBrokenData) { expectDefaultPamInfo(); EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::BrokenData}})); EXPECT_THAT(sut.handle(coreHandler), HoldsPamAction(PamAction::decline)); } TEST_F(RublonHttpInitTest, rublon_Decline_pamLoginWhenServerReturnsCoreException) { expectDefaultPamInfo(); EXPECT_CALL(coreHandler, request(_, _)).WillOnce(Return(tl::unexpected{CoreHandlerError{CoreHandlerError::CoreException}})); EXPECT_THAT(sut.handle(coreHandler), HoldsPamAction(PamAction::decline)); }