eedb/tests/unit/db/test_eedb_data_PgAuthTokens.cpp
2018-02-08 08:26:55 +01:00

116 lines
3.8 KiB
C++

#include <gmock/gmock.h>
#include <eedb/db/data/PgAuthToken.hpp>
#include <eedb/data/AuthIdentity.hpp>
#include <eedb/data/Users.hpp>
#include <eedb/data/User.hpp>
#include <eedb/db/model/auth_identity.h>
#include <eedb/db/model/auth_info.h>
#include <eedb/db/model/auth_token.h>
#include <eedb/db/model/user_action.h>
#include <eedb/db/model/user_audit.h>
#include <sqlpp11/sqlpp11.h>
#include <utils/DbTestBase.hpp>
constexpr eedb::user_audit t_user_audit;
constexpr eedb::user_action t_user_action;
constexpr eedb::auth_identity t_auth_identity;
constexpr eedb::auth_info t_auth_info;
constexpr eedb::auth_token t_auth_token;
namespace {
class UserMock : public eedb::User {
public:
MOCK_CONST_METHOD0(uid, int());
const eedb::UserName & name() const {}
const eedb::UserConfig & config() const override {}
eedb::AuthTokens & authTokens() const override {}
void logout() override {}
eedb::AuthIdentities & authIdentities() const {}
int64_t _uid;
// User interface
public:
eedb::AuthInfo & authInfo() const override {}
};
} // namespace
class PgAuthTokensTest : public DbTestBase< PgAuthTokensTest > {
public:
PgAuthTokensTest() {
using namespace testing;
db().native()->start_transaction();
authInfoId = db()(sqlpp::postgresql::insert_into(t_auth_info)
.set( //
t_auth_info.password_hash = "$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q",
t_auth_info.password_method = "bcrypt",
t_auth_info.password_salt = "OM/Z1c4WBFXvwkxh",
t_auth_info.email = "none@eedb.pl", //
t_auth_info.status = 0)
.returning(t_auth_info.id))
.front()
.id;
EXPECT_CALL(user, uid()).WillRepeatedly(testing::Return(authInfoId));
sut = std::make_unique< eedb::PgAuthTokens >(db(), &user);
}
void createToken(std::string hash, eedb::AuthTokenRole role) {
db()(sqlpp::postgresql::insert_into(t_auth_token) //
.set( //
t_auth_token.value = hash, //
t_auth_token.auth_info_id = authInfoId, //
t_auth_token.role = static_cast< int16_t >(role), //
t_auth_token.expires = std::chrono::system_clock::now() + std::chrono::hours{1}));
}
~PgAuthTokensTest() {
db().native()->rollback_transaction(false);
}
protected:
UserMock user;
int64_t authInfoId{0};
std::unique_ptr< eedb::PgAuthTokens > sut;
};
template <>
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgAuthTokensTest >::_test_db = {};
using namespace sqlpp;
using namespace eedb;
TEST_F(PgAuthTokensTest, authTokenFound) {
createToken("token", AuthTokenRole::Auth);
EXPECT_TRUE(sut->find("token"));
}
TEST_F(PgAuthTokensTest, emailTokenFound) {
createToken("emailtoken", AuthTokenRole::EmailToken);
EXPECT_TRUE(sut->find(AuthTokenRole::EmailToken));
}
TEST_F(PgAuthTokensTest, tokenNotFound) {
EXPECT_FALSE(sut->find("token"));
}
TEST_F(PgAuthTokensTest, addMultipleAuthTokens) {
sut->addToken("authtoken1", eedb::AuthTokenRole::Auth);
sut->addToken("authtoken2", eedb::AuthTokenRole::Auth);
EXPECT_TRUE(sut->find("authtoken1"));
EXPECT_TRUE(sut->find("authtoken2"));
}
TEST_F(PgAuthTokensTest, removeToken) {
sut->addToken("authtoken1", eedb::AuthTokenRole::Auth);
sut->addToken("authtoken2", eedb::AuthTokenRole::Auth);
sut->removeToken("authtoken1");
EXPECT_FALSE(sut->find("authtoken1"));
}