rublon-ssh/PAM/ssh/tests/core_handler_tests.cpp
2023-08-16 14:18:48 +02:00

73 lines
2.1 KiB
C++

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <numeric>
#include <set>
#include "http_mock.hpp"
#include <rublon/core_handler.hpp>
using namespace rublon;
class CoreHandlerTestable : public CoreHandler< HttpHandlerMock > {
public:
CoreHandlerTestable() : CoreHandler< HttpHandlerMock >{conf} {}
auto & _http() {
return http;
}
};
class CoreHandlerTests : public testing::Test {
public:
CoreHandlerTests() : http{sut._http()} {
doc.SetObject();
}
CoreHandlerTestable sut;
HttpHandlerMock & http;
Document doc;
};
using namespace testing;
MATCHER(HasValue, "") {
return arg.has_value();
}
bool operator==(const Error & lhs, const Error & rhs) {
return lhs.category() == rhs.category();
}
MATCHER_P(Unexpected, error, "") {
return arg.error() == error;
}
auto ReturnError(SocketError::ErrorClass value) {
return Return(tl::unexpected{Error{SocketError{value}}});
}
TEST_F(CoreHandlerTests, coreShouldSetConnectionErrorOnBrokenConnection) {
EXPECT_CALL(http, request(_, _)).WillOnce(ReturnError(SocketError::Timeout));
EXPECT_THAT(sut.request("", doc), //
AllOf(Not(HasValue()), Unexpected(CoreHandlerError{CoreHandlerError{CoreHandlerError::ConnectionError}})));
}
TEST_F(CoreHandlerTests, coreShouldCheckSignatureAndReturnBadSignatureBeforeAnythingElse) {
EXPECT_CALL(http, request(_, _)).WillOnce(Return(( Response ) http.brokenSignature().brokenBody()));
EXPECT_THAT(sut.request("", doc), //
AllOf(Not(HasValue()), Unexpected(CoreHandlerError{CoreHandlerError::BadSigature})));
}
TEST_F(CoreHandlerTests, coreShouldSetBrokenDataWhenResultIsNotAvailable) {
EXPECT_CALL(http, request(_, _)).WillOnce(Return(( Response ) http.brokenBody()));
EXPECT_THAT(sut.request("", doc), //
AllOf(Not(HasValue()), Unexpected(CoreHandlerError{CoreHandlerError::BrokenData})));
}
TEST_F(CoreHandlerTests, coreSignatureIsBeingChecked) {
EXPECT_CALL(http, request(Eq(conf.parameters.apiServer + "/path"), _)).WillOnce(Return(( Response ) http.statusPending()));
auto val = sut.request("/path", doc);
EXPECT_TRUE(val.value().IsObject());
}