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

104 lines
3.3 KiB
C++

#include <gmock/gmock.h>
#include <eedb/db/data/PgAuthIdentity.hpp>
#include <eedb/data/AuthIdentityConst.hpp>
#include <eedb/data/User.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;
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 PgAuthIdentitiesTest : public DbTestBase< PgAuthIdentitiesTest > {
public:
PgAuthIdentitiesTest() {
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::PgAuthIdentities >(db(), &user);
}
void createIdentity(const eedb::AuthIdentityConst id) {
db()(insert_into(t_auth_identity) //
.set(t_auth_identity.auth_info_id = authInfoId,
t_auth_identity.provider = std::string{id.provider()},
t_auth_identity.identity = std::string{id.identity()}));
}
~PgAuthIdentitiesTest() {
db().native()->rollback_transaction(false);
}
protected:
UserMock user;
int64_t authInfoId{0};
std::unique_ptr< eedb::PgAuthIdentities > sut;
};
template <>
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgAuthIdentitiesTest >::_test_db = {};
using namespace sqlpp;
using namespace eedb;
TEST_F(PgAuthIdentitiesTest, searchForFalseIdentity) {
EXPECT_FALSE(sut->byProvider("nonexisting"));
}
TEST_F(PgAuthIdentitiesTest, findIdentity) {
createIdentity({"identity", "provider"});
EXPECT_TRUE(sut->byProvider("provider"));
}
TEST_F(PgAuthIdentitiesTest, addProvider) {
sut->addIdentity(std::make_unique< AuthIdentityConst >("identity2", "provider"));
EXPECT_TRUE(sut->byProvider("provider"));
}
TEST_F(PgAuthIdentitiesTest, removeIdentity) {
createIdentity({"identity", "provider"});
sut->removeProvider("provider");
EXPECT_FALSE(sut->byProvider("provider"));
}