eedb/tests/unit/db/test_eedb_data_PgAuthToken.cpp
2018-02-05 14:36:42 +01:00

92 lines
3.2 KiB
C++

#include <gmock/gmock.h>
#include <eedb/db/data/PgAuthToken.hpp>
#include <eedb/data/AuthIdentity.hpp>
#include <eedb/data/Users.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;
class PgAuthTokenTest : public DbTestBase< PgAuthTokenTest > {
public:
PgAuthTokenTest() {
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;
sutId = db()(sqlpp::postgresql::insert_into(t_auth_token) //
.set( //
t_auth_token.auth_info_id = authInfoId, //
t_auth_token.expires = std::chrono::system_clock::now(), //
t_auth_token.role = static_cast< int >(eedb::AuthTokenRole::Auth), //
t_auth_token.value = "empty") //
.returning(t_auth_token.id))
.front()
.id;
sut = std::make_unique< eedb::PgAuthToken >(db(), sutId);
}
void setExpireDate(std::chrono::system_clock::time_point exp) {
using namespace std::chrono;
db()(update(t_auth_token).set(t_auth_token.expires = time_point_cast< microseconds >(exp)).where(t_auth_token.id == sutId));
}
~PgAuthTokenTest() {
db().native()->rollback_transaction(false);
}
protected:
int64_t authInfoId;
int64_t sutId;
std::unique_ptr< eedb::PgAuthToken > sut;
};
template <>
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgAuthTokenTest >::_test_db = {};
using namespace sqlpp;
using namespace std::chrono;
TEST_F(PgAuthTokenTest, isExpired) {
setExpireDate(system_clock::now() - hours{1});
EXPECT_TRUE(sut->expired());
}
TEST_F(PgAuthTokenTest, isNotExpired) {
setExpireDate(system_clock::now() + hours{1});
EXPECT_FALSE(sut->expired());
}
TEST_F(PgAuthTokenTest, validValue) {
EXPECT_EQ(sut->token(), "empty");
}
TEST_F(PgAuthTokenTest, updateValue) {
sut->update("NEW");
EXPECT_EQ(sut->token(), "NEW");
}