121 lines
3.6 KiB
C++
121 lines
3.6 KiB
C++
#include <gmock/gmock.h>
|
|
|
|
#include <eedb/db/data/PgAuthToken.hpp>
|
|
|
|
#include <eedb/data/Users.hpp>
|
|
#include <eedb/data/AuthIdentity.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;
|
|
|
|
namespace {
|
|
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{}
|
|
};
|
|
} // namespace
|
|
|
|
class PgAuthTokensTest : public DbTestBase< PgAuthTokensTest > {
|
|
public:
|
|
PgAuthTokensTest() {
|
|
using namespace testing;
|
|
db().native()->start_transaction();
|
|
}
|
|
|
|
void createToken() {
|
|
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;
|
|
|
|
ON_CALL(user, uid()).WillByDefault(testing::Return(auth_info_id));
|
|
|
|
sut = std::make_unique< eedb::PgAuthTokens >(db(), &user);
|
|
}
|
|
|
|
~PgAuthTokensTest() {
|
|
db().native()->rollback_transaction(false);
|
|
}
|
|
|
|
protected:
|
|
UserMock user;
|
|
std::unique_ptr< eedb::PgAuthTokens > sut;
|
|
};
|
|
|
|
template <>
|
|
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgAuthTokensTest >::_test_db = {};
|
|
|
|
using namespace sqlpp;
|
|
|
|
TEST_F(PgAuthTokensTest, tokenFound) {
|
|
createToken();
|
|
EXPECT_TRUE(sut->find("hash"));
|
|
}
|
|
|
|
TEST_F(PgAuthTokensTest, tokenNotFound) {
|
|
createToken();
|
|
EXPECT_FALSE(sut->find("bad hash"));
|
|
}
|
|
|
|
TEST_F(PgAuthTokensTest, readFromCache) {
|
|
createToken();
|
|
EXPECT_TRUE(sut->find("hash"));
|
|
EXPECT_TRUE(sut->find("hash"));
|
|
}
|
|
|
|
TEST_F(PgAuthTokensTest, addToken) {
|
|
createToken();
|
|
sut->addToken("hash2");
|
|
EXPECT_TRUE(sut->find("hash"));
|
|
EXPECT_TRUE(sut->find("hash2"));
|
|
}
|
|
|
|
TEST_F(PgAuthTokensTest, removeToken) {
|
|
createToken();
|
|
EXPECT_TRUE(sut->find("hash"));
|
|
sut->removeToken("hash");
|
|
EXPECT_FALSE(sut->find("hash"));
|
|
}
|