eedb/tests/unit/db/test_eedb_data_PgAuthToken.cpp
Bartosz Wieczorek cdf31fc1bf fix build
2018-02-05 07:27:35 +01:00

134 lines
4.7 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.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 t_user;
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 UserMock : public eedb::User {
public:
MOCK_CONST_METHOD0(uid, int());
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{}
};
class PgAuthTokenTest : public DbTestBase< PgAuthTokenTest > {
public:
PgAuthTokenTest() {
using namespace testing;
db().native()->start_transaction();
}
void createExpiredToken() {
auto sql = R"sql(
WITH user_ AS ( INSERT INTO "user" ("full_name","status")
VALUES('none',0)
RETURNING "user"."uid" AS user_id )
, auth_info_ AS (
INSERT INTO "auth_info" ("password_hash","password_method","password_salt","user_uid","email")
SELECT '$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q','bcrypt','OM/Z1c4WBFXvwkxh',user_id,'none@eedb.pl'
FROM user_
RETURNING "auth_info"."id" AS auth_info_id )
, auth_identity_ AS(
INSERT INTO "auth_identity" ("auth_info_id","identity","provider")
SELECT auth_info_id,'username','loginname'
FROM auth_info_ )
INSERT into auth_token ("auth_info_id","value","expires")
SELECT auth_info_id, 'hash', now() - interval '1 hour'
FROM auth_info_
)sql";
db().native()->execute(sql);
auto auth_info_id = db()(select(t_auth_info.id).from(t_auth_info).where(t_auth_info.email == "none@eedb.pl")).front().id;
auto _uid = db()(select(t_auth_token.id).from(t_auth_token).where(t_auth_token.auth_info_id == auth_info_id)).front().id;
sut = std::make_unique< eedb::PgAuthToken >(db(), _uid);
}
void createValidToken() {
auto sql = R"sql(
WITH user_ AS ( INSERT INTO "user" ("full_name","status")
VALUES('none',0)
RETURNING "user"."uid" AS user_id )
, auth_info_ AS (
INSERT INTO "auth_info" ("password_hash","password_method","password_salt","user_uid","email")
SELECT '$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q','bcrypt','OM/Z1c4WBFXvwkxh',user_id,'none@eedb.pl'
FROM user_
RETURNING "auth_info"."id" AS auth_info_id )
, auth_identity_ AS(
INSERT INTO "auth_identity" ("auth_info_id","identity","provider")
SELECT auth_info_id,'username','loginname'
FROM auth_info_ )
INSERT into auth_token ("auth_info_id","value","expires")
SELECT auth_info_id, 'hash', now() + interval '1 hour'
FROM auth_info_
)sql";
db().native()->execute(sql);
auto auth_info_id = db()(select(t_auth_info.id).from(t_auth_info).where(t_auth_info.email == "none@eedb.pl")).front().id;
auto _uid = db()(select(t_auth_token.id).from(t_auth_token).where(t_auth_token.auth_info_id == auth_info_id)).front().id;
sut = std::make_unique< eedb::PgAuthToken >(db(), _uid);
}
~PgAuthTokenTest() {
db().native()->rollback_transaction(false);
}
protected:
testing::NiceMock< UserMock > user;
std::unique_ptr< eedb::PgAuthToken > sut;
};
template <>
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgAuthTokenTest >::_test_db = {};
using namespace sqlpp;
TEST_F(PgAuthTokenTest, isExpired) {
createExpiredToken();
EXPECT_TRUE(sut->expired());
}
TEST_F(PgAuthTokenTest, isNotExpired) {
createValidToken();
EXPECT_FALSE(sut->expired());
}
TEST_F(PgAuthTokenTest, validValue) {
createValidToken();
EXPECT_EQ(sut->token(), "hash");
}
TEST_F(PgAuthTokenTest, updateValue) {
createValidToken();
sut->update("NEW");
EXPECT_EQ(sut->token(), "NEW");
}